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
|
|
|
/** |
18
|
|
|
* The checkoutGateways array is used to display the payment in the checkout page. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected static $checkoutGateways; |
23
|
|
|
|
24
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* List available Payment Methods |
28
|
|
|
* |
29
|
|
|
* @param double|int $invoiceValue Total invoice amount. |
30
|
|
|
* @param string $displayCurrencyIso Total invoice currency. |
31
|
|
|
* @param bool $isAppleRegistered Is site domain is registered with applePay and MyFatoorah or not. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getCheckoutGateways($invoiceValue, $displayCurrencyIso, $isAppleRegistered) |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
if (!empty(self::$checkoutGateways)) { |
39
|
|
|
return self::$checkoutGateways; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$gateways = $this->initiatePayment($invoiceValue, $displayCurrencyIso); |
43
|
|
|
|
44
|
|
|
$mfListObj = new MyFatoorahList($this->config); |
45
|
|
|
$allRates = $mfListObj->getCurrencyRates(); |
46
|
|
|
$currencyRate = $mfListObj->getCurrencyRate($displayCurrencyIso, $allRates); |
47
|
|
|
|
48
|
|
|
self::$checkoutGateways = ['all' => [], 'cards' => [], 'form' => [], 'ap' => [], 'gp' => []]; |
49
|
|
|
foreach ($gateways as $gateway) { |
50
|
|
|
$gateway->PaymentTotalAmount = $this->getPaymentTotalAmount($gateway, $allRates, $currencyRate); |
51
|
|
|
|
52
|
|
|
$gateway->GatewayData = [ |
53
|
|
|
'GatewayTotalAmount' => number_format($gateway->PaymentTotalAmount, 2), |
54
|
|
|
'GatewayCurrency' => $gateway->PaymentCurrencyIso |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
self::$checkoutGateways = $this->addGatewayToCheckoutGateways($gateway, self::$checkoutGateways, $isAppleRegistered); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($isAppleRegistered) { |
61
|
|
|
//add only one ap gateway |
62
|
|
|
self::$checkoutGateways['ap'] = $this->getOneApplePayGateway(self::$checkoutGateways['ap'], $displayCurrencyIso, $allRates); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return self::$checkoutGateways; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Calculate the amount value that will be paid in each payment method |
72
|
|
|
* |
73
|
|
|
* @param object $paymentMethod The payment method object obtained from the initiate payment endpoint |
74
|
|
|
* @param array $allRates The MyFatoorah currency rate array of all gateways. |
75
|
|
|
* @param double $currencyRate The currency rate of the invoice. |
76
|
|
|
* |
77
|
|
|
* @return double |
78
|
|
|
*/ |
79
|
|
|
private function getPaymentTotalAmount($paymentMethod, $allRates, $currencyRate) |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
if ($paymentMethod->PaymentCurrencyIso == $paymentMethod->CurrencyIso) { |
83
|
|
|
return $paymentMethod->TotalAmount; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//convert to portal base currency |
87
|
|
|
$baseTotalAmount = ceil(((int) ($paymentMethod->TotalAmount * 100)) / $currencyRate) / 100; |
88
|
|
|
|
89
|
|
|
//gateway currency is not the portal currency |
90
|
|
|
$mfListObj = new MyFatoorahList($this->config); |
91
|
|
|
$paymentCurrencyRate = $mfListObj->getCurrencyRate($paymentMethod->PaymentCurrencyIso, $allRates); |
92
|
|
|
if ($paymentCurrencyRate != 1) { |
93
|
|
|
return ceil($baseTotalAmount * $paymentCurrencyRate * 100) / 100; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $baseTotalAmount; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns One Apple pay array in case multiple are enabled in the account |
103
|
|
|
* |
104
|
|
|
* @param array $apGateways The all available AP gateways |
105
|
|
|
* @param string $displayCurrency The currency of the invoice total amount. |
106
|
|
|
* @param array $allRates The MyFatoorah currency rate array of all gateways. |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function getOneApplePayGateway($apGateways, $displayCurrency, $allRates) |
111
|
|
|
{ |
112
|
|
|
|
113
|
|
|
$displayCurrencyIndex = array_search($displayCurrency, array_column($apGateways, 'PaymentCurrencyIso')); |
114
|
|
|
if ($displayCurrencyIndex) { |
115
|
|
|
return $apGateways[$displayCurrencyIndex]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//get defult mf account currency |
119
|
|
|
$defCurKey = array_search('1', array_column($allRates, 'Value')); |
120
|
|
|
$defaultCurrency = $allRates[$defCurKey]->Text; |
121
|
|
|
|
122
|
|
|
$defaultCurrencyIndex = array_search($defaultCurrency, array_column($apGateways, 'PaymentCurrencyIso')); |
123
|
|
|
if ($defaultCurrencyIndex) { |
124
|
|
|
return $apGateways[$defaultCurrencyIndex]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (isset($apGateways[0])) { |
128
|
|
|
return $apGateways[0]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
135
|
|
|
} |
136
|
|
|
|