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

YandexP2pMerchant::requestPurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\php\merchant\merchants\yandex;
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\YandexMoney\P2pGateway;
10
11
/**
12
 * Class YandexP2pMerchant
13
 *
14
 * @author Dmytro Naumenko <[email protected]>
15
 */
16
class YandexP2pMerchant extends AbstractMerchant
17
{
18
    /**
19
     * @var P2pGateway
20
     */
21
    protected $gateway;
22
23
    protected function createGateway()
24
    {
25
        return $this->gatewayFactory->build('YandexMoney_P2p', [
26
            'account' => $this->credentials->getPurse(),
27
            'password' => $this->credentials->getKey1(),
28
            'testMode' => $this->credentials->isTestMode()
29
        ]);
30
    }
31
32
    /**
33
     * @param InvoiceInterface $invoice
34
     * @return RedirectPurchaseResponse
35
     */
36
    public function requestPurchase(InvoiceInterface $invoice)
37
    {
38
        /**
39
         * @var \Omnipay\YandexMoney\Message\p2p\PurchaseResponse $response
40
         */
41
        $response = $this->gateway->purchase([
42
            'transactionId' => $invoice->getId(),
43
            'description' => $invoice->getDescription(),
44
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
45
            'currency' => $invoice->getCurrency()->getCode(),
46
            'returnUrl' => $invoice->getReturnUrl(),
47
            'notifyUrl' => $invoice->getNotifyUrl(),
48
            'cancelUrl' => $invoice->getCancelUrl(),
49
            'method' => 'PC', // https://money.yandex.ru/doc.xml?id=526991
50
        ])->send();
51
52
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
53
    }
54
55
    /**
56
     * @param array $data
57
     * @return CompletePurchaseResponse
58
     */
59 View Code Duplication
    public function completePurchase($data)
0 ignored issues
show
Duplication introduced by
This method 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...
60
    {
61
        /** @var \Omnipay\YandexMoney\Message\p2p\CompletePurchaseResponse $response */
62
        $response = $this->gateway->completePurchase($data)->send();
63
64
        return (new CompletePurchaseResponse())
65
            ->setIsSuccessful($response->isSuccessful())
66
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
67
            ->setTransactionReference($response->getTransactionReference())
68
            ->setTransactionId($response->getTransactionId())
69
            ->setPayer($response->getData()['sender'] ?? $response->getData()['email'] ?? '')
70
            ->setTime(
71
                (new \DateTime($response->getTime(), new \DateTimeZone('Europe/Moscow')))
72
                    ->setTimezone(new \DateTimeZone('UTC'))
73
            );
74
    }
75
}
76