PaymentProviderIpay88Kh   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A paymentProviderPayload() 0 17 3
A transactionSignature() 0 10 1
A instantiateValidator() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Getloy\PaymentProviders;
4
5
use Getloy\PaymentProviders;
6
use Getloy\PaymentProviders\Configuration\PaymentProviderConfigIpay88Kh;
7
use Getloy\PaymentProviders\Configuration\PaymentProviderConfig;
8
use Getloy\TransactionDetails\OrderDetails;
9
use Getloy\TransactionDetails\OrderItems;
10
use Getloy\TransactionDetails\PayeeDetails;
11
12
class PaymentProviderIpay88Kh extends PaymentProvider
13
{
14
    const VARIANT_CREDITCARD = 'cc';
15
    const VARIANT_UNIONPAY = 'upay';
16
    const VARIANT_PIPAY = 'pipay';
17
    const VARIANT_WING = 'wing';
18
    const VARIANT_METFONE = 'metfone';
19
    const VARIANT_ALIPAY_BARCODE = 'alipayBarcode';
20
    const VARIANT_ALIPAY_QR = 'alipayQr';
21
    const VARIANT_ACLEDA_XPAY = 'acledaXpay';
22
23
    protected $paymentMethod = PaymentProviders::PIPAY_KH;
24
    protected $variantCodes = [
25
        'cc' => 1,
26
        'upay' => 15,
27
        'pipay' => 11,
28
        'wing' => 123,
29
        'metfone' => 9,
30
        'alipaybc' => 234,
31
        'alipayqr' => 233,
32
        'acledaxpay' => 3,
33
    ];
34
    
35
    /**
36
     * Instantiate a payment provider config for the payment method.
37
     *
38
     * @param array $config Configuration for the payment method.
39
     * @return PaymentProviderConfig The configuration.
40
     */
41
    protected function instantiateValidator(array $config): PaymentProviderConfig
42
    {
43
        return new PaymentProviderConfigIpay88Kh($config);
44
    }
45
46
    /**
47
     * Generate the payment provider-specific part of the widget payload.
48
     *
49
     * @param string $transactionId
50
     * @param OrderDetails $order
51
     * @param PayeeDetails $payee
52
     * @param string $callbackUrl
53
     * @param string $paymentMethodVariant Payment method variant name (optional).
54
     * @return array
55
     */
56
    public function paymentProviderPayload(
57
        string $transactionId,
58
        OrderDetails $order,
59
        PayeeDetails $payee,
60
        string $callbackUrl,
61
        string $paymentMethodVariant = null
62
    ): array {
63
64
        $payload = [
65
            'merchant_code' => $this->config->get('merchantCode'),
66
            'signature' => $this->transactionSignature($transactionId, $order),
67
        ];
68
69
        if ($paymentMethodVariant && array_key_exists($paymentMethodVariant, $this->variantCodes)) {
70
            $payload['payment_method_id'] = $this->variantCodes[$paymentMethodVariant];
71
        }
72
        return $payload;
73
    }
74
75
    /**
76
     * Generate transaction signature for payment provider payload.
77
     *
78
     * @param string $transactionId
79
     * @param OrderDetails $order
80
     * @return string
81
     */
82
    public function transactionSignature(string $transactionId, OrderDetails $order): string
83
    {
84
        return base64_encode(
85
            sha1(
86
                $this->config->get('merchantKey') .
87
                $this->config->get('merchantCode') .
88
                $transactionId .
89
                sprintf('%03d', $order->totalAmount() * 100) .
90
                $order->currency(),
91
                true
92
            )
93
        );
94
    }
95
}
96