Passed
Push — main ( 4a5f0c...02f3c9 )
by MyFatoorah
06:23 queued 02:44
created

MyFatoorahPaymentEmbedded::getOneApplePayGateway()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 22
rs 9.9
1
<?php
2
3
namespace MyFatoorah\Library\API\Payment;
4
5
use MyFatoorah\Library\API\MyFatoorahList;
6
7
/**
8
 *  MyFatoorahPaymentForm handles the form process of MyFatoorah API endpoints
9
 *
10
 * @author    MyFatoorah <[email protected]>
11
 * @copyright 2021 MyFatoorah, All rights reserved
12
 * @license   GNU General Public License v3.0
13
 */
14
class MyFatoorahPaymentEmbedded extends MyFatoorahPayment
15
{
16
    /**
17
     * The checkoutGateways array is used to display the payment in the checkout page.
18
     *
19
     * @var array
20
     */
21
    protected static $checkoutGateways;
22
23
    //-----------------------------------------------------------------------------------------------------------------------------------------
24
25
    /**
26
     * List available Payment Methods
27
     *
28
     * @param double|int $invoiceValue       Total invoice amount.
29
     * @param string     $displayCurrencyIso Total invoice currency.
30
     * @param bool       $isAppleRegistered  Is site domain is registered with applePay and MyFatoorah or not.
31
     *
32
     * @return array
33
     */
34
    public function getCheckoutGateways($invoiceValue, $displayCurrencyIso, $isAppleRegistered)
35
    {
36
37
        if (!empty(self::$checkoutGateways)) {
38
            return self::$checkoutGateways;
39
        }
40
41
        $gateways = $this->initiatePayment($invoiceValue, $displayCurrencyIso);
42
43
        $mfListObj = new MyFatoorahList($this->config);
44
        $allRates  = $mfListObj->getCurrencyRates();
45
46
        self::$checkoutGateways = ['all' => [], 'cards' => [], 'form' => [], 'ap' => []];
47
        foreach ($gateways as $gateway) {
48
            $gateway->GatewayData   = $this->calcGatewayData($gateway->TotalAmount, $gateway->CurrencyIso, $gateway->PaymentCurrencyIso, $allRates);
49
            self::$checkoutGateways = $this->addGatewayToCheckoutGateways($gateway, self::$checkoutGateways, $isAppleRegistered);
50
        }
51
52
        if ($isAppleRegistered) {
53
            //add only one ap gateway
54
            self::$checkoutGateways['ap'] = $this->getOneApplePayGateway(self::$checkoutGateways['ap'], $displayCurrencyIso, $allRates);
55
        }
56
        return self::$checkoutGateways;
57
    }
58
59
    //-----------------------------------------------------------------------------------------------------------------------------------------
60
61
    /**
62
     * Calculate the amount value that will be paid in each gateway
63
     *
64
     * @param double|int $totalAmount        The total amount of the invoice.
65
     * @param string     $currency           The currency of the invoice total amount.
66
     * @param string     $paymentCurrencyIso The currency of the gateway.
67
     * @param array      $allRates           The MyFatoorah currency rate array of all gateways.
68
     *
69
     * @return array
70
     */
71
    protected function calcGatewayData($totalAmount, $currency, $paymentCurrencyIso, $allRates)
72
    {
73
74
        foreach ($allRates as $data) {
75
            if ($data->Text == $currency) {
76
                $baseCurrencyRate = $data->Value;
77
            }
78
            if ($data->Text == $paymentCurrencyIso) {
79
                $gatewayCurrencyRate = $data->Value;
80
            }
81
        }
82
83
        if (isset($baseCurrencyRate) && isset($gatewayCurrencyRate)) {
84
            $baseAmount = ceil(((int) ($totalAmount * 1000)) / $baseCurrencyRate / 10) / 100;
85
86
            $number = ceil(($baseAmount * $gatewayCurrencyRate * 100)) / 100;
87
            return [
88
                'GatewayTotalAmount' => number_format($number, 2, '.', ''),
89
                'GatewayCurrency'    => $paymentCurrencyIso
90
            ];
91
        } else {
92
            return [
93
                'GatewayTotalAmount' => $totalAmount,
94
                'GatewayCurrency'    => $currency
95
            ];
96
        }
97
98
        //        }
99
    }
100
101
    //-----------------------------------------------------------------------------------------------------------------------------------------
102
103
    /**
104
     * Returns One Apple pay array in case multiple are enabled in the account
105
     *
106
     * @param array  $apGateways      The all available AP gateways
107
     * @param string $displayCurrency The currency of the invoice total amount.
108
     * @param array  $allRates        The MyFatoorah currency rate array of all gateways.
109
     *
110
     * @return array
111
     */
112
    protected function getOneApplePayGateway($apGateways, $displayCurrency, $allRates)
113
    {
114
115
        $displayCurrencyIndex = array_search($displayCurrency, array_column($apGateways, 'PaymentCurrencyIso'));
116
        if ($displayCurrencyIndex) {
117
            return $apGateways[$displayCurrencyIndex];
118
        }
119
120
        //get defult mf account currency
121
        $defCurKey       = array_search('1', array_column($allRates, 'Value'));
122
        $defaultCurrency = $allRates[$defCurKey]->Text;
123
124
        $defaultCurrencyIndex = array_search($defaultCurrency, array_column($apGateways, 'PaymentCurrencyIso'));
125
        if ($defaultCurrencyIndex) {
126
            return $apGateways[$defaultCurrencyIndex];
127
        }
128
129
        if (isset($apGateways[0])) {
130
            return $apGateways[0];
131
        }
132
133
        return [];
134
    }
135
136
    //-----------------------------------------------------------------------------------------------------------------------------------------
137
}
138