PaymentProviderPaywayKh   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A statusToken() 0 6 1
A base64OrderItems() 0 12 2
A paymentProviderPayload() 0 12 1
A instantiateValidator() 0 3 1
A initToken() 0 12 1
1
<?php declare(strict_types=1);
2
3
namespace Getloy\PaymentProviders;
4
5
use Getloy\PaymentProviders;
6
use Getloy\PaymentProviders\Configuration\PaymentProviderConfigPaywayKh;
7
use Getloy\PaymentProviders\Configuration\PaymentProviderConfig;
8
use Getloy\TransactionDetails\OrderDetails;
9
use Getloy\TransactionDetails\OrderItems;
10
use Getloy\TransactionDetails\PayeeDetails;
11
12
class PaymentProviderPaywayKh extends PaymentProvider
13
{
14
    protected $paymentMethod = PaymentProviders::PAYWAY_KH;
15
    
16
    /**
17
     * Instantiate a payment provider config for the payment method.
18
     *
19
     * @param array $config Configuration for the payment method.
20
     * @return PaymentProviderConfig The configuration.
21
     */
22
    protected function instantiateValidator(array $config): PaymentProviderConfig
23
    {
24
        return new PaymentProviderConfigPaywayKh($config);
25
    }
26
27
    /**
28
     * Generate the payment provider-specific part of the widget payload.
29
     *
30
     * @param string $transactionId
31
     * @param OrderDetails $order
32
     * @param PayeeDetails $payee
33
     * @param string $callbackUrl
34
     * @param string $paymentMethodVariant Payment method variant name (optional).
35
     * @return array
36
     */
37
    public function paymentProviderPayload(
38
        string $transactionId,
39
        OrderDetails $order,
40
        PayeeDetails $payee,
41
        string $callbackUrl,
42
        string $paymentMethodVariant = null
43
    ): array {
44
45
        return [
46
            'init_token' => $this->initToken($transactionId, $order),
47
            'status_token' => $this->statusToken($transactionId),
48
            'payment_method' => $this->config->get('allowedMethods'),
49
        ];
50
    }
51
52
    /**
53
     * Gnerate status token for payment provider payload.
54
     *
55
     * @param string $transactionId
56
     * @return string
57
     */
58
    protected function statusToken(string $transactionId): string
59
    {
60
        return hash_hmac(
61
            'sha512',
62
            $this->config->get('merchantId') . $transactionId,
63
            $this->config->get('merchantKey')
64
        );
65
    }
66
67
    protected function initToken(string $transactionId, OrderDetails $order): string
68
    {
69
        return hash_hmac(
70
            'sha512',
71
            sprintf(
72
                '%s%s%.2f%s',
73
                $this->config->get('merchantId'),
74
                $transactionId,
75
                $order->totalAmount(),
76
                $this->base64OrderItems($order->orderItems())
77
            ),
78
            $this->config->get('merchantKey')
79
        );
80
    }
81
82
    /**
83
     * Transform an array of order items to a base64 encoded string.
84
     *
85
     * @param OrderItems $orderItems
86
     * @return string Order items as base64 encoded string
87
     */
88
    protected function base64OrderItems(OrderItems $orderItems): string
89
    {
90
        $items = [];
91
        foreach ($orderItems->payloadConfig() as $item) {
92
            $items[] = [
93
                'name' => $item['description'],
94
                'quantity' => $item['quantity'],
95
                'price' => $item['unit_price'],
96
            ];
97
        }
98
99
        return base64_encode(mb_convert_encoding(json_encode($items), 'UTF-8'));
100
    }
101
}
102