Completed
Push — master ( eafb02...74552d )
by Dmitry
09:07
created

EpaymentsMerchant   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 9
dl 0
loc 59
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createGateway() 0 7 1
A requestPurchase() 0 16 1
A completePurchase() 0 15 1
A fetchOrderDetails() 0 4 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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\merchant\merchants\epayments;
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 Omnipay\ePayments\Message\DetailsResponse;
18
use Omnipay\ePayments\Message\PurchaseResponse;
19
20
/**
21
 * Class EpaymentsMerchant.
22
 *
23
 * @author Dmytro Naumenko <[email protected]>
24
 *
25
 * @property \Omnipay\ePayments\Gateway $gateway
26
 */
27
class EpaymentsMerchant extends AbstractMerchant
28
{
29
    /**
30
     * @return \Omnipay\Common\GatewayInterface
31
     */
32
    protected function createGateway()
33
    {
34
        return $this->gatewayFactory->build('ePayments', [
35
            'partnerId' => $this->credentials->getPurse(),
36
            'secret'  => $this->credentials->getKey1(),
37
        ]);
38
    }
39
40
    /**
41
     * @param InvoiceInterface $invoice
42
     * @return RedirectPurchaseResponse
43
     */
44
    public function requestPurchase(InvoiceInterface $invoice)
45
    {
46
        /**
47
         * @var PurchaseResponse $response
48
         */
49
        $response = $this->gateway->purchase([
50
            'orderId' => $invoice->getId(),
51
            'details' => $invoice->getDescription(),
52
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
53
            'currency' => strtolower($invoice->getCurrency()->getCode()),
54
            'returnUrl' => $invoice->getReturnUrl(),
55
            'cancelUrl' => $invoice->getCancelUrl(),
56
        ])->send();
57
58
        return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
59
    }
60
61
    /**
62
     * @param array $data
63
     * @return CompletePurchaseResponse
64
     */
65
    public function completePurchase($data)
66
    {
67
        /** @var \Omnipay\ePayments\Message\CompletePurchaseResponse $notification */
68
        $notification = $this->gateway->completePurchase($data)->send();
69
70
        $response = $this->fetchOrderDetails($notification->getOrderId());
71
72
        return (new CompletePurchaseResponse())
73
            ->setIsSuccessful($response->isSuccessful())
74
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
75
            ->setTransactionReference($response->getTransactionReference())
76
            ->setTransactionId($response->getTransactionId())
77
            ->setPayer('')
78
            ->setTime($response->getPaymentDate());
79
    }
80
81
    protected function fetchOrderDetails(string $orderId): DetailsResponse
82
    {
83
        return $this->gateway->details(['orderId' => $orderId])->send();
84
    }
85
}
86