Completed
Pull Request — master (#1)
by Dmitry
02:28
created

WebmoneyMerchant::completePurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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