Completed
Pull Request — master (#1)
by Dmitry
14:10
created

RoboKassaMerchant::createGateway()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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