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

WebmoneyMerchant::requestPurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\php\merchant\merchants\webmoney;
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 Money\Currency;
10
use Money\Money;
11
use Omnipay\WebMoney\Gateway;
12
13
/**
14
 * Class WebmoneyMerchant
15
 *
16
 * @author Dmytro Naumenko <[email protected]>
17
 */
18
class WebmoneyMerchant extends AbstractMerchant
19
{
20
    /**
21
     * @var Gateway
22
     */
23
    protected $gateway;
24
25
    protected function createGateway()
26
    {
27
        return $this->gatewayFactory->build('WebMoney', [
28
            'merchantPurse' => $this->credentials->getPurse(),
29
            'secretKey'  => $this->credentials->getKey1(),
30
            'testMode' => $this->credentials->isTestMode()
31
        ]);
32
    }
33
34
    /**
35
     * @param InvoiceInterface $invoice
36
     * @return RedirectPurchaseResponse
37
     */
38
    public function requestPurchase(InvoiceInterface $invoice)
39
    {
40
        /**
41
         * @var \Omnipay\WebMoney\Message\PurchaseResponse $response
42
         */
43
        $response = $this->gateway->purchase([
44
            'transactionId' => $invoice->getId(),
45
            'description' => $invoice->getDescription(),
46
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
47
            'currency' => $invoice->getCurrency()->getCode(),
48
            'returnUrl' => $invoice->getReturnUrl(),
49
            'returnMethod' => $invoice->getReturnMethod(),
50
            'notifyUrl' => $invoice->getNotifyUrl(),
51
            'notifyMethod' => $invoice->getNotifyMethod(),
52
            'cancelUrl' => $invoice->getCancelUrl(),
53
            'cancelMethod' => $invoice->getCancelMethod(),
54
        ])->send();
55
56
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
57
    }
58
59
    /**
60
     * @param array $data
61
     * @return CompletePurchaseResponse
62
     */
63 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...
64
    {
65
        /** @var \Omnipay\WebMoney\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
            ->setFee(new Money(0, new Currency($response->getCurrency())))
72
            ->setTransactionReference($response->getTransactionReference())
73
            ->setTransactionId($response->getTransactionId())
74
            ->setPayer($response->getData()['LMI_PAYER_PURSE'])
75
            ->setTime(new \DateTime($response->getData()['LMI_SYS_TRANS_DATE']));
76
    }
77
}
78