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
|
|
|
} else { |
124
|
|
|
if ($gateway->IsEmbeddedSupported) { |
125
|
|
|
$checkoutGateways['form'][] = $gateway; |
126
|
|
|
$checkoutGateways['all'][] = $gateway; |
127
|
|
|
} elseif (!$gateway->IsDirectPayment) { |
128
|
|
|
$checkoutGateways['cards'][] = $gateway; |
129
|
|
|
$checkoutGateways['all'][] = $gateway; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $checkoutGateways; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get Payment Method Object |
140
|
|
|
* |
141
|
|
|
* @param string $gateway MyFatoorah gateway object. |
142
|
|
|
* @param string $searchKey The Search key ['PaymentMethodId', 'PaymentMethodCode']. |
143
|
|
|
* @param double|int $invoiceAmount The display invoice total amount. |
144
|
|
|
* @param string $currencyIso The display invoice currency ISO. |
145
|
|
|
* |
146
|
|
|
* @return object |
147
|
|
|
* |
148
|
|
|
* @throws Exception |
149
|
|
|
*/ |
150
|
|
|
public function getOnePaymentMethod($gateway, $searchKey = 'PaymentMethodId', $invoiceAmount = 0, $currencyIso = '') |
151
|
|
|
{ |
152
|
|
|
|
153
|
|
|
$paymentMethods = $this->initiatePayment($invoiceAmount, $currencyIso); |
154
|
|
|
|
155
|
|
|
$paymentMethod = null; |
156
|
|
|
foreach ($paymentMethods as $pm) { |
157
|
|
|
if ($pm->$searchKey == $gateway) { |
158
|
|
|
$paymentMethod = $pm; |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!isset($paymentMethod)) { |
164
|
|
|
throw new Exception('Please contact Account Manager to enable the used payment method in your account'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $paymentMethod; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the invoice/payment URL and the invoice id |
174
|
|
|
* |
175
|
|
|
* @param array $curlData Invoice information. |
176
|
|
|
* @param int|string $gatewayId MyFatoorah Gateway ID (default value: '0'). |
177
|
|
|
* @param int|string $orderId It used in log file (default value: null). |
178
|
|
|
* @param string $sessionId The payment session used in embedded payment. |
179
|
|
|
* @param string $ntfOption The notificationOption for send payment. It could be EML, SMS, LNK, or ALL. |
180
|
|
|
* |
181
|
|
|
* @return array of invoiceURL and invoiceURL |
182
|
|
|
*/ |
183
|
|
|
public function getInvoiceURL($curlData, $gatewayId = 0, $orderId = null, $sessionId = null, $ntfOption = 'Lnk') |
184
|
|
|
{ |
185
|
|
|
|
186
|
|
|
$this->log('------------------------------------------------------------'); |
187
|
|
|
|
188
|
|
|
$curlData['CustomerReference'] = $curlData['CustomerReference'] ?? $orderId; |
189
|
|
|
|
190
|
|
|
if (!empty($sessionId)) { |
191
|
|
|
$curlData['SessionId'] = $sessionId; |
192
|
|
|
|
193
|
|
|
$data = $this->executePayment($curlData); |
194
|
|
|
return ['invoiceURL' => $data->PaymentURL, 'invoiceId' => $data->InvoiceId]; |
195
|
|
|
} elseif ($gatewayId == 'myfatoorah' || empty($gatewayId)) { |
196
|
|
|
if (empty($curlData['NotificationOption'])) { |
197
|
|
|
$curlData['NotificationOption'] = $ntfOption; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$data = $this->sendPayment($curlData); |
201
|
|
|
return ['invoiceURL' => $data->InvoiceURL, 'invoiceId' => $data->InvoiceId]; |
202
|
|
|
} else { |
203
|
|
|
$curlData['PaymentMethodId'] = $gatewayId; |
204
|
|
|
|
205
|
|
|
$data = $this->executePayment($curlData); |
206
|
|
|
return ['invoiceURL' => $data->PaymentURL, 'invoiceId' => $data->InvoiceId]; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Create an invoice Link (POST API) |
214
|
|
|
* |
215
|
|
|
* @param array $curlData Invoice information, check https://docs.myfatoorah.com/docs/send-payment#request-model. |
216
|
|
|
* |
217
|
|
|
* @return object |
218
|
|
|
*/ |
219
|
|
|
public function sendPayment($curlData) |
220
|
|
|
{ |
221
|
|
|
|
222
|
|
|
$this->preparePayment($curlData); |
223
|
|
|
|
224
|
|
|
$json = $this->callAPI("$this->apiURL/v2/SendPayment", $curlData, $curlData['CustomerReference'], 'Send Payment'); |
225
|
|
|
return $json->Data; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Create an Payment Link (POST API) |
232
|
|
|
* |
233
|
|
|
* @param array $curlData Invoice information, check https://docs.myfatoorah.com/docs/execute-payment#request-model. |
234
|
|
|
* |
235
|
|
|
* @return object |
236
|
|
|
*/ |
237
|
|
|
public function executePayment($curlData) |
238
|
|
|
{ |
239
|
|
|
|
240
|
|
|
$this->preparePayment($curlData); |
241
|
|
|
|
242
|
|
|
$json = $this->callAPI("$this->apiURL/v2/ExecutePayment", $curlData, $curlData['CustomerReference'], 'Execute Payment'); |
243
|
|
|
return $json->Data; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Prepare payment array for SendPayment and ExecutePayment |
250
|
|
|
* |
251
|
|
|
* @param array $curlData Invoice information |
252
|
|
|
*/ |
253
|
|
|
private function preparePayment(&$curlData) |
254
|
|
|
{ |
255
|
|
|
|
256
|
|
|
$curlData['CustomerReference'] = $curlData['CustomerReference'] ?? null; |
257
|
|
|
$curlData['SourceInfo'] = $curlData['SourceInfo'] ?? 'MyFatoorah PHP Library ' . $this->version; |
258
|
|
|
|
259
|
|
|
if (empty($curlData['CustomerEmail'])) { |
260
|
|
|
$curlData['CustomerEmail'] = null; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get session Data |
268
|
|
|
* |
269
|
|
|
* @param string $userDefinedField Customer Identifier to display its saved data. |
270
|
|
|
* @param int|string $logId It used in log file, example you can use the orderId (default value: null). |
271
|
|
|
* |
272
|
|
|
* @return object |
273
|
|
|
*/ |
274
|
|
|
public function getEmbeddedSession($userDefinedField = '', $logId = null) |
275
|
|
|
{ |
276
|
|
|
|
277
|
|
|
$curlData = ['CustomerIdentifier' => $userDefinedField]; |
278
|
|
|
|
279
|
|
|
return $this->InitiateSession($curlData, $logId); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get session Data (POST API) |
286
|
|
|
* |
287
|
|
|
* @param array $curlData Session properties. |
288
|
|
|
* @param int|string $logId It used in log file, example you can use the orderId (default value: null). |
289
|
|
|
* |
290
|
|
|
* @return object |
291
|
|
|
*/ |
292
|
|
|
public function InitiateSession($curlData, $logId = null) |
293
|
|
|
{ |
294
|
|
|
|
295
|
|
|
$json = $this->callAPI("$this->apiURL/v2/InitiateSession", $curlData, $logId, 'Initiate Session'); |
296
|
|
|
return $json->Data; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Register Apple Pay Domain (POST API) |
303
|
|
|
* |
304
|
|
|
* @param string $url Site URL |
305
|
|
|
* |
306
|
|
|
* @return object |
307
|
|
|
*/ |
308
|
|
|
public function registerApplePayDomain($url) |
309
|
|
|
{ |
310
|
|
|
|
311
|
|
|
$domainName = ['DomainName' => parse_url($url, PHP_URL_HOST)]; |
312
|
|
|
return $this->callAPI("$this->apiURL/v2/RegisterApplePayDomain", $domainName, '', 'Register Apple Pay Domain'); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
//----------------------------------------------------------------------------------------------------------------------------------------- |
316
|
|
|
} |
317
|
|
|
|