Completed
Pull Request — master (#1)
by Dmitry
03:17
created

RoboKassaMerchant   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 2
cbo 6
dl 0
loc 54
ccs 0
cts 23
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createGateway() 0 8 1
A requestPurchase() 0 15 1
A completePurchase() 0 13 1
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
            'secretKey2' => $this->credentials->getKey2(),
36
        ]);
37
    }
38
39
    /**
40
     * @param InvoiceInterface $invoice
41
     * @return RedirectPurchaseResponse
42
     */
43
    public function requestPurchase(InvoiceInterface $invoice)
44
    {
45
        /**
46
         * @var \Omnipay\RoboKassa\Message\PurchaseResponse $response
47
         */
48
        $response = $this->gateway->purchase([
49
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
50
            'transaction_id' => $invoice->getId(),
51
            'description' => $invoice->getDescription(),
52
            'currency' => $invoice->getCurrency()->getCode(),
53
            'client' => $invoice->getClient(),
54
        ])->send();
55
56
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
57
    }
58
59
    /**
60
     * @param array $data
61
     * @return CompletePurchaseResponse
62
     */
63
    public function completePurchase($data)
64
    {
65
        /** @var \Omnipay\RoboKassa\Message\CompletePurchaseResponse $response */
66
        $response = $this->gateway->completePurchase($data)->send();
67
68
        return (new CompletePurchaseResponse())
69
            ->setIsSuccessful($response->isSuccessful())
70
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
71
            ->setTransactionReference($response->getTransactionReference())
72
            ->setTransactionId($response->getTransactionId())
73
            ->setPayer($response->getPayer())
74
            ->setTime((new \DateTime())->setTimezone(new \DateTimeZone('UTC')));
75
    }
76
}
77