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 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 $invoiceAmount The display invoice total amount. |
30
|
|
|
* @param string $currencyIso The display invoice currency ISO. |
31
|
|
|
* @param bool $isApRegistered Is site domain is registered with ApplePay and MyFatoorah or not. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getCheckoutGateways($invoiceAmount, $currencyIso, $isApRegistered) |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
if (!empty(self::$checkoutGateways)) { |
39
|
|
|
return self::$checkoutGateways; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$gateways = $this->initiatePayment($invoiceAmount, $currencyIso); |
43
|
|
|
|
44
|
|
|
$mfListObj = new MyFatoorahList($this->config); |
45
|
|
|
$allRates = $mfListObj->getCurrencyRates(); |
46
|
|
|
$currencyRate = MyFatoorahList::getOneCurrencyRate($currencyIso, $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
|
|
|
'GatewayTransCurrency' => self::getTranslatedCurrency($gateway->PaymentCurrencyIso), |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
self::$checkoutGateways = $this->addGatewayToCheckout($gateway, self::$checkoutGateways, $isApRegistered); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($isApRegistered) { |
62
|
|
|
//add only one ap gateway |
63
|
|
|
self::$checkoutGateways['ap'] = $this->getOneEmbeddedGateway(self::$checkoutGateways['ap'], $currencyIso, $allRates); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return self::$checkoutGateways; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Calculate the amount value that will be paid in each payment method |
73
|
|
|
* |
74
|
|
|
* @param object $paymentMethod The payment method object obtained from the initiate payment endpoint |
75
|
|
|
* @param array $allRates The MyFatoorah currency rate array of all gateways. |
76
|
|
|
* @param double $currencyRate The currency rate of the invoice. |
77
|
|
|
* |
78
|
|
|
* @return double |
79
|
|
|
*/ |
80
|
|
|
private function getPaymentTotalAmount($paymentMethod, $allRates, $currencyRate) |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
$dbTrucVal = ((int) ($paymentMethod->TotalAmount * 1000)) / 1000; |
84
|
|
|
if ($paymentMethod->PaymentCurrencyIso == $paymentMethod->CurrencyIso) { |
85
|
|
|
return $this->roundUp($dbTrucVal, 2); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//convert to portal base currency |
89
|
|
|
$dueVal = ($currencyRate == 1) ? $dbTrucVal : round($paymentMethod->TotalAmount / $currencyRate, 3); |
90
|
|
|
$baseTotalAmount = $this->roundUp($dueVal, 2); |
91
|
|
|
|
92
|
|
|
//gateway currency is not the portal currency |
93
|
|
|
$paymentCurrencyRate = MyFatoorahList::getOneCurrencyRate($paymentMethod->PaymentCurrencyIso, $allRates); |
94
|
|
|
if ($paymentCurrencyRate != 1) { |
95
|
|
|
$paymentTotalAmount = $baseTotalAmount * $paymentCurrencyRate; |
96
|
|
|
return $this->roundUp($paymentTotalAmount, 2); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $baseTotalAmount; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the next highest float value by rounding up to a certain decimal |
106
|
|
|
* |
107
|
|
|
* @param mixed $number |
108
|
|
|
* @param mixed $decimalPlaces |
109
|
|
|
* |
110
|
|
|
* @return float |
111
|
|
|
*/ |
112
|
|
|
private function roundUp($number, $decimalPlaces) |
113
|
|
|
{ |
114
|
|
|
$multi = pow(10, $decimalPlaces); |
115
|
|
|
$nrAsStr = (string) ($number * $multi); |
116
|
|
|
return ceil((float) $nrAsStr) / $multi; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns One Apple pay array in case multiple are enabled in the account |
123
|
|
|
* |
124
|
|
|
* @param array $gateways The all available AP/GP gateways |
125
|
|
|
* @param string $displayCurrency The currency of the invoice total amount. |
126
|
|
|
* @param array $allRates The MyFatoorah currency rate array of all gateways. |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
private function getOneEmbeddedGateway($gateways, $displayCurrency, $allRates) |
131
|
|
|
{ |
132
|
|
|
|
133
|
|
|
$displayCurrencyIndex = array_search($displayCurrency, array_column($gateways, 'PaymentCurrencyIso')); |
134
|
|
|
if ($displayCurrencyIndex) { |
135
|
|
|
return $gateways[$displayCurrencyIndex]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
//get defult mf account currency |
139
|
|
|
$defCurKey = array_search('1', array_column($allRates, 'Value')); |
140
|
|
|
$defaultCurrency = $allRates[$defCurKey]->Text; |
141
|
|
|
|
142
|
|
|
$defaultCurrencyIndex = array_search($defaultCurrency, array_column($gateways, 'PaymentCurrencyIso')); |
143
|
|
|
if ($defaultCurrencyIndex) { |
144
|
|
|
return $gateways[$defaultCurrencyIndex]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (isset($gateways[0])) { |
148
|
|
|
return $gateways[0]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return []; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the translation of the currency ISO code |
158
|
|
|
* |
159
|
|
|
* @param string $currency currency ISO code |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public static function getTranslatedCurrency($currency) |
164
|
|
|
{ |
165
|
|
|
|
166
|
|
|
$currencies = [ |
167
|
|
|
'KWD' => ['en' => 'KD', 'ar' => 'د.ك'], |
168
|
|
|
'SAR' => ['en' => 'SR', 'ar' => 'ريال'], |
169
|
|
|
'BHD' => ['en' => 'BD', 'ar' => 'د.ب'], |
170
|
|
|
'EGP' => ['en' => 'LE', 'ar' => 'ج.م'], |
171
|
|
|
'QAR' => ['en' => 'QR', 'ar' => 'ر.ق'], |
172
|
|
|
'OMR' => ['en' => 'OR', 'ar' => 'ر.ع'], |
173
|
|
|
'JOD' => ['en' => 'JD', 'ar' => 'د.أ'], |
174
|
|
|
'AED' => ['en' => 'AED', 'ar' => 'د'], |
175
|
|
|
'USD' => ['en' => 'USD', 'ar' => 'دولار'], |
176
|
|
|
'EUR' => ['en' => 'EUR', 'ar' => 'يورو'] |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
return $currencies[$currency] ?? ['en' => '', 'ar' => '']; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
183
|
|
|
} |
184
|
|
|
|