Completed
Push — master ( e1aeba...525b5a )
by Dmitry
01:51
created

TwoCheckoutPlusMerchant::completePurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
/**
3
 * Generalization over Omnipay and Payum
4
 *
5
 * @link      https://github.com/hiqdev/php-merchant
6
 * @package   php-merchant
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\merchant\merchants\twocheckoutplus;
12
13
use hiqdev\php\merchant\InvoiceInterface;
14
use hiqdev\php\merchant\merchants\AbstractMerchant;
15
use hiqdev\php\merchant\response\CompletePurchaseResponse;
16
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
17
use Money\Currency;
18
use Money\Money;
19
use Omnipay\TwoCheckoutPlus\Gateway;
20
21
/**
22
 * Class TwoCheckoutPlusMerchant.
23
 *
24
 * @author Dmytro Naumenko <[email protected]>
25
 */
26
class TwoCheckoutPlusMerchant extends AbstractMerchant
27
{
28
    /**
29
     * @var Gateway
30
     */
31
    protected $gateway;
32
33
    protected function createGateway()
34
    {
35
        return $this->gatewayFactory->build('TwoCheckoutPlusMerchant', [
36
            'account_number' => $this->credentials->getPurse(),
37
            'secret_word'  => $this->credentials->getKey1(),
38
            'testMode' => $this->credentials->isTestMode(),
39
            'demoMode' => $this->credentials->isTestMode(),
40
        ]);
41
    }
42
43
    /**
44
     * @param InvoiceInterface $invoice
45
     * @return RedirectPurchaseResponse
46
     */
47
    public function requestPurchase(InvoiceInterface $invoice)
48
    {
49
        $gateway = clone $this->gateway;
50
        $gateway->setCart([
51
            [
52
                'product' => 'product',
53
                'description' => $invoice->getDescription(),
54
                'price' => $this->moneyFormatter->format($invoice->getAmount()),
55
                'quantity' => 1,
56
            ]
57
        ]);
58
        /**
59
         * @var \Omnipay\TwoCheckoutPlus\Message\PurchaseResponse
60
         */
61
        $response = $this->gateway->purchase([
62
            'transactionId' => $invoice->getId(),
63
            'currency' => $invoice->getCurrency()->getCode(),
64
            'returnUrl' => $invoice->getReturnUrl(),
65
        ])->send();
66
67
        return (new RedirectPurchaseResponse($response->getRedirectUrl(), []))->setMethod('GET');
68
    }
69
70
    /**
71
     * @param array $data
72
     * @return CompletePurchaseResponse
73
     */
74 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...
75
    {
76
        /** @var \Omnipay\WebMoney\Message\CompletePurchaseResponse $response */
77
        $response = $this->gateway->completePurchase($data)->send();
78
79
        return (new CompletePurchaseResponse())
80
            ->setIsSuccessful($response->isSuccessful())
0 ignored issues
show
Documentation introduced by
$response->isSuccessful() is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
82
            ->setFee(new Money(0, new Currency($response->getCurrency())))
83
            ->setTransactionReference($response->getTransactionReference())
84
            ->setTransactionId($response->getTransactionId())
85
            ->setPayer($response->getData()['LMI_PAYER_PURSE'])
86
            ->setTime(new \DateTime($response->getData()['LMI_SYS_TRANS_DATE']));
87
    }
88
}
89