Completed
Push — master ( 189007...259fd8 )
by Dmitry
08:51
created

FreeKassaMerchant::completePurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
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\freekassa;
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\FreeKassa\Gateway;
10
11
/**
12
 * Class FreeKassaMerchant
13
 *
14
 * @author Dmytro Naumenko <[email protected]>
15
 */
16 View Code Duplication
class FreeKassaMerchant extends AbstractMerchant
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @var Gateway
20
     */
21
    protected $gateway;
22
23
    protected function createGateway()
24
    {
25
        return $this->gatewayFactory->build('FreeKassa', [
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\FreeKassa\Message\PurchaseResponse $response
40
         */
41
        $response = $this->gateway->purchase([
42
            'transaction_id' => $invoice->getId(),
43
            'currency' => $invoice->getCurrency()->getCode(),
44
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
45
            'client' => $invoice->getClient(),
46
        ])->send();
47
48
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
49
    }
50
51
    /**
52
     * @param array $data
53
     * @return CompletePurchaseResponse
54
     */
55
    public function completePurchase($data)
56
    {
57
        /** @var \Omnipay\FreeKassa\Message\CompletePurchaseResponse $response */
58
        $response = $this->gateway->completePurchase($data)->send();
59
60
        return (new CompletePurchaseResponse())
61
            ->setIsSuccessful($response->isSuccessful())
62
            // TODO: !(>_<)! FreeKassa does not indicate currency.
63
            ->setAmount($this->moneyParser->parse($response->getAmount(), 'RUB'))
64
            ->setTransactionReference($response->getTransactionReference())
65
            ->setTransactionId($response->getTransactionId())
66
            ->setPayer($response->getPayer())
67
            ->setTime((new \DateTime($response->getTime()))->setTimezone(new \DateTimeZone('UTC')));
68
    }
69
}
70