1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyFatoorah\Library\API\Payment; |
4
|
|
|
|
5
|
|
|
use MyFatoorah\Library\MyFatoorah; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* MyFatoorahPayment handles the payment process of MyFatoorah API endpoints |
10
|
|
|
* |
11
|
|
|
* @author MyFatoorah <[email protected]> |
12
|
|
|
* @copyright 2021 MyFatoorah, All rights reserved |
13
|
|
|
* @license GNU General Public License v3.0 |
14
|
|
|
*/ |
15
|
|
|
class MyFatoorahPayment extends MyFatoorah |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The file name used in caching the gateways data |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
public static $pmCachedFile = __DIR__ . '/mf-methods.json'; |
23
|
|
|
|
24
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* List available Payment Methods (POST API) |
28
|
|
|
* |
29
|
|
|
* @param double|int $invoiceValue Total invoice amount. |
30
|
|
|
* @param string $displayCurrencyIso Total invoice currency. |
31
|
|
|
* @param boolean $isCached It used to cache the gateways |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function initiatePayment($invoiceValue = 0, $displayCurrencyIso = '', $isCached = false) |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
$postFields = [ |
39
|
|
|
'InvoiceAmount' => $invoiceValue, |
40
|
|
|
'CurrencyIso' => $displayCurrencyIso, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$json = $this->callAPI("$this->apiURL/v2/InitiatePayment", $postFields, null, 'Initiate Payment'); |
44
|
|
|
|
45
|
|
|
$paymentMethods = ($json->Data->PaymentMethods) ?? []; |
46
|
|
|
|
47
|
|
|
if (!empty($paymentMethods) && $isCached) { |
48
|
|
|
file_put_contents(self::$pmCachedFile, json_encode($paymentMethods)); |
49
|
|
|
} |
50
|
|
|
return $paymentMethods; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* List available Cached Payment Gateways |
57
|
|
|
* |
58
|
|
|
* @return mixed of Cached payment methods. |
59
|
|
|
*/ |
60
|
|
|
public function getCachedVendorGateways() |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
if (file_exists(self::$pmCachedFile)) { |
64
|
|
|
$cache = file_get_contents(self::$pmCachedFile); |
65
|
|
|
return ($cache) ? json_decode($cache) : []; |
66
|
|
|
} else { |
67
|
|
|
return $this->initiatePayment(0, '', true); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* List available cached Payment Methods |
75
|
|
|
* |
76
|
|
|
* @param bool $isAppleRegistered Is site domain is registered with applePay and MyFatoorah or not |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getCachedCheckoutGateways($isAppleRegistered = false) |
81
|
|
|
{ |
82
|
|
|
|
83
|
|
|
$gateways = $this->getCachedVendorGateways(); |
84
|
|
|
|
85
|
|
|
$cachedCheckoutGateways = ['all' => [], 'cards' => [], 'form' => [], 'ap' => []]; |
86
|
|
|
foreach ($gateways as $gateway) { |
87
|
|
|
$cachedCheckoutGateways = $this->addGatewayToCheckoutGateways($gateway, $cachedCheckoutGateways, $isAppleRegistered); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
//add only one ap gateway |
91
|
|
|
$cachedCheckoutGateways['ap'] = $cachedCheckoutGateways['ap'][0] ?? []; |
92
|
|
|
|
93
|
|
|
return $cachedCheckoutGateways; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add the MyFatoorah gateway object to the a given Payment Methods Array |
100
|
|
|
* |
101
|
|
|
* @param object $gateway MyFatoorah gateway object. |
102
|
|
|
* @param array $checkoutGateways Payment Methods Array. |
103
|
|
|
* @param boolean $isAppleRegistered Is site domain is registered with applePay and MyFatoorah or not. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
protected function addGatewayToCheckoutGateways($gateway, $checkoutGateways, $isAppleRegistered) |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
if ($gateway->PaymentMethodCode == 'ap') { |
111
|
|
|
if ($isAppleRegistered) { |
112
|
|
|
$checkoutGateways['ap'][] = $gateway; |
113
|
|
|
} else { |
114
|
|
|
$checkoutGateways['cards'][] = $gateway; |
115
|
|
|
} |
116
|
|
|
$checkoutGateways['all'][] = $gateway; |
117
|
|
|
} else { |
118
|
|
|
if ($gateway->IsEmbeddedSupported) { |
119
|
|
|
$checkoutGateways['form'][] = $gateway; |
120
|
|
|
$checkoutGateways['all'][] = $gateway; |
121
|
|
|
} elseif (!$gateway->IsDirectPayment) { |
122
|
|
|
$checkoutGateways['cards'][] = $gateway; |
123
|
|
|
$checkoutGateways['all'][] = $gateway; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $checkoutGateways; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get Payment Method Object |
134
|
|
|
* |
135
|
|
|
* @param string $gateway MyFatoorah gateway object. |
136
|
|
|
* @param string $gatewayType The Search key ['PaymentMethodId', 'PaymentMethodCode']. |
137
|
|
|
* @param double|int $invoiceValue Total invoice amount. |
138
|
|
|
* @param string $displayCurrencyIso Total invoice currency. |
139
|
|
|
* |
140
|
|
|
* @return object |
141
|
|
|
* |
142
|
|
|
* @throws Exception |
143
|
|
|
*/ |
144
|
|
|
public function getOnePaymentMethod($gateway, $gatewayType = 'PaymentMethodId', $invoiceValue = 0, $displayCurrencyIso = '') |
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
$paymentMethods = $this->initiatePayment($invoiceValue, $displayCurrencyIso); |
148
|
|
|
|
149
|
|
|
$paymentMethod = null; |
150
|
|
|
foreach ($paymentMethods as $pm) { |
151
|
|
|
if ($pm->$gatewayType == $gateway) { |
152
|
|
|
$paymentMethod = $pm; |
153
|
|
|
break; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!isset($paymentMethod)) { |
158
|
|
|
throw new Exception('Please contact Account Manager to enable the used payment method in your account'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $paymentMethod; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the invoice/payment URL and the invoice id |
168
|
|
|
* |
169
|
|
|
* @param array $curlData Invoice information. |
170
|
|
|
* @param int|string $gatewayId MyFatoorah Gateway ID (default value: '0'). |
171
|
|
|
* @param int|string $orderId It used in log file (default value: null). |
172
|
|
|
* @param string $sessionId The payment session used in embedded payment. |
173
|
|
|
* @param string $notificationOption could be EML, SMS, LNK, or ALL. |
174
|
|
|
* |
175
|
|
|
* @return array of invoiceURL and invoiceURL |
176
|
|
|
*/ |
177
|
|
|
public function getInvoiceURL($curlData, $gatewayId = 0, $orderId = null, $sessionId = null, $notificationOption = 'Lnk') |
178
|
|
|
{ |
179
|
|
|
|
180
|
|
|
$this->log('------------------------------------------------------------'); |
181
|
|
|
|
182
|
|
|
$curlData['CustomerEmail'] = empty($curlData['CustomerEmail']) ? null : $curlData['CustomerEmail']; |
183
|
|
|
|
184
|
|
|
if (!empty($sessionId)) { |
185
|
|
|
return $this->embeddedPayment($curlData, $sessionId, $orderId); |
186
|
|
|
} elseif ($gatewayId == 'myfatoorah' || empty($gatewayId)) { |
187
|
|
|
return $this->sendPayment($curlData, $orderId, $notificationOption); |
188
|
|
|
} else { |
189
|
|
|
return $this->excutePayment($curlData, $gatewayId, $orderId); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* (POST API) |
197
|
|
|
* |
198
|
|
|
* @param array $curlData Invoice information. |
199
|
|
|
* @param int|string $gatewayId MyFatoorah Gateway ID. |
200
|
|
|
* @param int|string $orderId It used in log file (default value: null). |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
private function excutePayment($curlData, $gatewayId, $orderId = null) |
205
|
|
|
{ |
206
|
|
|
|
207
|
|
|
$curlData['PaymentMethodId'] = $gatewayId; |
208
|
|
|
|
209
|
|
|
$json = $this->callAPI("$this->apiURL/v2/ExecutePayment", $curlData, $orderId, 'Excute Payment'); //__FUNCTION__ |
210
|
|
|
|
211
|
|
|
return ['invoiceURL' => $json->Data->PaymentURL, 'invoiceId' => $json->Data->InvoiceId]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Create an invoice Link (POST API) |
218
|
|
|
* |
219
|
|
|
* @param array $curlData Invoice information. |
220
|
|
|
* @param int|string $orderId It used in log file (default value: null). |
221
|
|
|
* @param string $notificationOption could be EML, SMS, LNK, or ALL. |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
private function sendPayment($curlData, $orderId = null, $notificationOption = 'Lnk') |
226
|
|
|
{ |
227
|
|
|
|
228
|
|
|
$curlData['NotificationOption'] = $notificationOption; |
229
|
|
|
|
230
|
|
|
$json = $this->callAPI("$this->apiURL/v2/SendPayment", $curlData, $orderId, 'Send Payment'); |
231
|
|
|
|
232
|
|
|
return ['invoiceURL' => $json->Data->InvoiceURL, 'invoiceId' => $json->Data->InvoiceId]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Create an invoice using Embedded session (POST API) |
239
|
|
|
* |
240
|
|
|
* @param array $curlData Invoice information. |
241
|
|
|
* @param int|string $sessionId Session id used in payment process. |
242
|
|
|
* @param int|string $orderId It used in log file (default value: null). |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
|
|
private function embeddedPayment($curlData, $sessionId, $orderId = null) |
247
|
|
|
{ |
248
|
|
|
|
249
|
|
|
$curlData['SessionId'] = $sessionId; |
250
|
|
|
|
251
|
|
|
$json = $this->callAPI("$this->apiURL/v2/ExecutePayment", $curlData, $orderId, 'Embedded Payment'); |
252
|
|
|
return ['invoiceURL' => $json->Data->PaymentURL, 'invoiceId' => $json->Data->InvoiceId]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get session Data (POST API) |
259
|
|
|
* |
260
|
|
|
* @param string $userDefinedField Customer Identifier to dispaly its saved data. |
261
|
|
|
* @param int|string $orderId It used in log file (default value: null). |
262
|
|
|
* |
263
|
|
|
* @return object |
264
|
|
|
*/ |
265
|
|
|
public function getEmbeddedSession($userDefinedField = '', $orderId = null) |
266
|
|
|
{ |
267
|
|
|
|
268
|
|
|
$customerIdentifier = ['CustomerIdentifier' => $userDefinedField]; |
269
|
|
|
|
270
|
|
|
$json = $this->callAPI("$this->apiURL/v2/InitiateSession", $customerIdentifier, $orderId, 'Initiate Session'); |
271
|
|
|
return $json->Data; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Register Apple Pay Domain (POST API) |
278
|
|
|
* |
279
|
|
|
* @param string $url Site URL |
280
|
|
|
* |
281
|
|
|
* @return object |
282
|
|
|
*/ |
283
|
|
|
public function registerApplePayDomain($url) |
284
|
|
|
{ |
285
|
|
|
|
286
|
|
|
$domainName = ['DomainName' => parse_url($url, PHP_URL_HOST)]; |
287
|
|
|
return $this->callAPI("$this->apiURL/v2/RegisterApplePayDomain", $domainName, '', 'Register Apple Pay Domain'); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
291
|
|
|
} |
292
|
|
|
|