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