Completed
Pull Request — master (#1)
by Dmitry
11:43 queued 10:09
created

RoboKassaMerchant::requestPurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\php\merchant\merchants\robokassa;
4
5
use hiqdev\php\merchant\InvoiceInterface;
6
use hiqdev\php\merchant\merchants\AbstractMerchant;
7
use hiqdev\php\merchant\response\CompletePurchaseResponse;
8
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
9
use Omnipay\RoboKassa\Gateway;
10
11
/**
12
 * Class RoboKassaMerchant
13
 *
14
 * @author Dmytro Naumenko <[email protected]>
15
 */
16
class RoboKassaMerchant extends AbstractMerchant
17
{
18
    /**
19
     * @var Gateway
20
     */
21
    protected $gateway;
22
23
    protected function createGateway()
24
    {
25
        return $this->gatewayFactory->build('RoboKassa', [
26
            'purse' => $this->credentials->getPurse(),
27
            'secretKey' => $this->credentials->getKey1(),
28
            'secretKey2' => $this->credentials->getKey2(),
29
        ]);
30
    }
31
32
    /**
33
     * @param InvoiceInterface $invoice
34
     * @return RedirectPurchaseResponse
35
     */
36
    public function requestPurchase(InvoiceInterface $invoice)
37
    {
38
        /**
39
         * @var \Omnipay\RoboKassa\Message\PurchaseResponse $response
40
         */
41
        $response = $this->gateway->purchase([
42
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
43
            'transaction_id' => $invoice->getId(),
44
            'description' => $invoice->getDescription(),
45
            'currency' => $invoice->getCurrency()->getCode(),
46
            'client' => $invoice->getClient(),
47
        ])->send();
48
49
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
50
    }
51
52
    /**
53
     * @param array $data
54
     * @return CompletePurchaseResponse
55
     */
56
    public function completePurchase($data)
57
    {
58
        /** @var \Omnipay\RoboKassa\Message\CompletePurchaseResponse $response */
59
        $response = $this->gateway->completePurchase($data)->send();
60
61
        return (new CompletePurchaseResponse())
62
            ->setIsSuccessful($response->isSuccessful())
63
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
64
            ->setTransactionReference($response->getTransactionReference())
65
            ->setTransactionId($response->getTransactionId())
66
            ->setPayer($response->getPayer())
67
            ->setTime((new \DateTime())->setTimezone(new \DateTimeZone('UTC')));
68
    }
69
}
70