1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//Require the Client library using composer: composer require pagantis/orders-api-client |
4
|
|
|
require_once('../vendor/autoload.php'); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* PLEASE FILL YOUR PUBLIC KEY AND PRIVATE KEY |
8
|
|
|
*/ |
9
|
|
|
const PUBLIC_KEY = ''; //Set your public key |
10
|
|
|
const PRIVATE_KEY = ''; //Set your public key |
11
|
|
|
const ORDER_ID = 'order_4159972708'; |
12
|
|
|
|
13
|
|
|
try { |
14
|
|
|
session_start(); |
15
|
|
|
$method = ($_GET['action']) ? ($_GET['action']) : 'createOrder'; |
16
|
|
|
call_user_func($method); |
17
|
|
|
} catch (\Exception $e) { |
18
|
|
|
echo $e->getMessage(); |
19
|
|
|
exit; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create order in Pagantis |
25
|
|
|
* |
26
|
|
|
* @throws \Httpful\Exception\ConnectionErrorException |
27
|
|
|
* @throws \Pagantis\OrdersApiClient\Exception\ClientException |
28
|
|
|
* @throws \Pagantis\OrdersApiClient\Exception\HttpException |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
|
function createOrder() |
32
|
|
|
{ |
33
|
|
|
// There are 3 objects which are mandatory: User object, ShoppingCart object and Configuration object. |
34
|
|
|
//1. User Object |
35
|
|
|
$withDate = true; |
36
|
|
|
writeLog('Creating User object', $withDate); |
37
|
|
|
$userAddress = setAddress(); |
38
|
|
|
$orderBillingAddress = setAddress(); |
39
|
|
|
writeLog('Adding the address of the user' . jsonEncoded($orderBillingAddress), |
40
|
|
|
$withDate); |
41
|
|
|
$orderShippingAddress = setShippingAddress(); |
42
|
|
|
|
43
|
|
|
writeLog('Adding the information of the user', $withDate); |
44
|
|
|
$orderUser = setOrder($userAddress, $orderBillingAddress, |
45
|
|
|
$orderShippingAddress); |
46
|
|
|
writeLog('Created User object', $withDate); |
47
|
|
|
|
48
|
|
|
//2. ShoppingCart Object |
49
|
|
|
writeLog('Creating ShoppingCart object', $withDate); |
50
|
|
|
writeLog('Adding the purchases of the customer, if there are.', $withDate); |
51
|
|
|
$orderHistory = setOrderHistory(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
writeLog('Adding cart products. Minimum 1 required', $withDate); |
54
|
|
|
$product = setProduct(); |
55
|
|
|
|
56
|
|
|
$details = setProductDetails($product); |
57
|
|
|
|
58
|
|
|
$orderShoppingCart = setShoppingCart($details, ORDER_ID); |
59
|
|
|
writeLog('Created OrderShoppingCart object', $withDate); |
60
|
|
|
|
61
|
|
|
//3. Configuration Object |
62
|
|
|
writeLog('Creating Configuration object', $withDate); |
63
|
|
|
writeLog('Adding urls to redirect the user according each case', $withDate); |
64
|
|
|
|
65
|
|
|
$orderConfigurationUrls = setConfigurationUrls(); |
66
|
|
|
|
67
|
|
|
writeLog('Adding channel info', $withDate); |
68
|
|
|
$orderChannel = setOrderChannel(); |
69
|
|
|
|
70
|
|
|
$orderConfiguration = setOrderConfiguration($orderChannel, |
71
|
|
|
$orderConfigurationUrls); |
72
|
|
|
writeLog('Created Configuration object', $withDate); |
73
|
|
|
|
74
|
|
|
$order = sendOrder($orderConfiguration, $orderShoppingCart, |
75
|
|
|
$orderUser); |
76
|
|
|
|
77
|
|
|
writeLog('Creating OrdersApiClient', $withDate); |
78
|
|
|
$orderClient = getClient(); |
79
|
|
|
|
80
|
|
|
writeLog('Creating Pagantis order', $withDate); |
81
|
|
|
$order = $orderClient->createOrder($order); |
82
|
|
|
processOrder($order); |
83
|
|
|
$url = getFormURL($order); |
84
|
|
|
// You can use our test credit cards to fill the Pagantis form |
85
|
|
|
writeLog("Redirecting to Pagantis form => $url", $withDate); |
86
|
|
|
header('Location:' . $url); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Confirm order in Pagantis |
91
|
|
|
* |
92
|
|
|
* @throws \Httpful\Exception\ConnectionErrorException |
93
|
|
|
* @throws \Pagantis\OrdersApiClient\Exception\ClientException |
94
|
|
|
* @throws \Pagantis\OrdersApiClient\Exception\HttpException |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
function confirmOrder() |
98
|
|
|
{ |
99
|
|
|
/* Once the user comes back to the OK url or there is a notification upon callback url you will have to confirm |
100
|
|
|
* the reception of the order. If not it will expire and will never be paid. |
101
|
|
|
* |
102
|
|
|
* Add this parameters in your database when you create a order and map it to your own order. Or search orders by |
103
|
|
|
* your own order id. Both options are possible. |
104
|
|
|
*/ |
105
|
|
|
$withDate = true; |
106
|
|
|
writeLog('Creating OrdersApiClient', $withDate); |
107
|
|
|
$orderClient = new \Pagantis\OrdersApiClient\Client(PUBLIC_KEY, PRIVATE_KEY); |
108
|
|
|
|
109
|
|
|
$order = $orderClient->getOrder($_SESSION['order_id']); |
110
|
|
|
|
111
|
|
|
if ($order instanceof \Pagantis\OrdersApiClient\Model\Order |
112
|
|
|
&& $order->getStatus() == \Pagantis\OrdersApiClient\Model\Order::STATUS_AUTHORIZED |
113
|
|
|
) { |
114
|
|
|
//If the order exists, and the status is authorized, means you can mark the order as paid. |
115
|
|
|
|
116
|
|
|
//DO WHATEVER YOU NEED TO DO TO MARK THE ORDER AS PAID IN YOUR OWN SYSTEM. |
117
|
|
|
writeLog('Confirming order', $withDate); |
118
|
|
|
$order = $orderClient->confirmOrder($order->getId()); |
119
|
|
|
|
120
|
|
|
writeLog('Order confirmed', $withDate); |
121
|
|
|
writeLog(json_encode( |
122
|
|
|
$order->export(), |
123
|
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT |
124
|
|
|
), $withDate); |
125
|
|
|
$message = "The order {$_SESSION['order_id']} has been confirmed successfully"; |
126
|
|
|
} else { |
127
|
|
|
$message = "The order {$_SESSION['order_id']} can't be confirmed"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/* The order has been marked as paid and confirmed in Pagantis so you will send the product to your customer and |
131
|
|
|
* Pagantis will pay you in the next 24h. |
132
|
|
|
*/ |
133
|
|
|
|
134
|
|
|
echo $message; |
135
|
|
|
exit; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Action after redirect to cancelUrl |
140
|
|
|
*/ |
141
|
|
|
function cancelOrder() |
142
|
|
|
{ |
143
|
|
|
$message = "The order {$_SESSION['order_id']} can't be created"; |
144
|
|
|
|
145
|
|
|
echo $message; |
146
|
|
|
exit; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Internal Functions |
151
|
|
|
*/ |
152
|
|
|
/** |
153
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\User\Address |
154
|
|
|
*/ |
155
|
|
|
function setAddress() |
156
|
|
|
{ |
157
|
|
|
$userAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
158
|
|
|
|
159
|
|
|
$userAddress |
160
|
|
|
->setZipCode('28031') |
161
|
|
|
->setFullName('María Sanchez Escudero') |
162
|
|
|
->setCountryCode('ES') |
163
|
|
|
->setCity('Madrid') |
164
|
|
|
->setAddress('Paseo de la Castellana, 95') |
165
|
|
|
->setDni('59661738Z') |
166
|
|
|
->setNationalId('59661738Z') |
167
|
|
|
->setFixPhone('911231234') |
168
|
|
|
->setMobilePhone('600123123'); |
169
|
|
|
return $userAddress; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\User\Address |
174
|
|
|
*/ |
175
|
|
|
function setShippingAddress() |
176
|
|
|
{ |
177
|
|
|
$orderShippingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
178
|
|
|
|
179
|
|
|
$orderShippingAddress |
180
|
|
|
->setZipCode('08029') |
181
|
|
|
->setFullName('Alberto Escudero Sanchez') |
182
|
|
|
->setCountryCode('ES') |
183
|
|
|
->setCity('Barcelona') |
184
|
|
|
->setAddress('Avenida de la diagonal 525') |
185
|
|
|
->setDni('77695544A') |
186
|
|
|
->setNationalId('59661738Z') |
187
|
|
|
->setFixPhone('931232345') |
188
|
|
|
->setMobilePhone('600123124'); |
189
|
|
|
return $orderShippingAddress; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param $userAddress |
194
|
|
|
* @param $orderBillingAddress |
195
|
|
|
* @param $orderShippingAddress |
196
|
|
|
* |
197
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\User |
198
|
|
|
*/ |
199
|
|
|
function setOrder( |
200
|
|
|
$userAddress, |
201
|
|
|
$orderBillingAddress, |
202
|
|
|
$orderShippingAddress |
203
|
|
|
) { |
204
|
|
|
$orderUser = new \Pagantis\OrdersApiClient\Model\Order\User(); |
205
|
|
|
$orderUser |
206
|
|
|
->setFullName('María Sanchez Escudero') |
207
|
|
|
->setAddress($userAddress) |
208
|
|
|
->setBillingAddress($orderBillingAddress) |
209
|
|
|
->setShippingAddress($orderShippingAddress) |
210
|
|
|
->setDateOfBirth('1985-12-30') |
211
|
|
|
->setEmail('[email protected]') |
212
|
|
|
->setFixPhone('911231234') |
213
|
|
|
->setMobilePhone('600123123') |
214
|
|
|
->setDni('59661738Z') |
215
|
|
|
->setNationalId('59661738Z'); |
216
|
|
|
return $orderUser; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory |
221
|
|
|
*/ |
222
|
|
|
function setOrderHistory() |
223
|
|
|
{ |
224
|
|
|
$orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory(); |
225
|
|
|
$orderUser = new \Pagantis\OrdersApiClient\Model\Order\User(); |
226
|
|
|
$orderHistory |
227
|
|
|
->setAmount('2499') |
228
|
|
|
->setDate('2010-01-31'); |
229
|
|
|
$orderUser->addOrderHistory($orderHistory); |
230
|
|
|
return $orderHistory; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product |
235
|
|
|
*/ |
236
|
|
|
function setProduct() |
237
|
|
|
{ |
238
|
|
|
$product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
239
|
|
|
$product |
240
|
|
|
->setAmount('59999') |
241
|
|
|
->setQuantity('1') |
242
|
|
|
->setDescription('TV LG UltraPlasma'); |
243
|
|
|
return $product; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product $product |
248
|
|
|
* |
249
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details |
250
|
|
|
*/ |
251
|
|
|
function setProductDetails( |
252
|
|
|
\Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product $product |
253
|
|
|
) { |
254
|
|
|
$details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
255
|
|
|
$details->setShippingCost('0'); |
256
|
|
|
$details->addProduct($product); |
257
|
|
|
return $details; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param $details |
262
|
|
|
* @param $orderID |
263
|
|
|
* |
264
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\ShoppingCart |
265
|
|
|
*/ |
266
|
|
|
function setShoppingCart($details, $orderID) |
267
|
|
|
{ |
268
|
|
|
$orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart(); |
269
|
|
|
|
270
|
|
|
$orderShoppingCart |
271
|
|
|
->setDetails($details) |
272
|
|
|
->setOrderReference($orderID) |
273
|
|
|
->setPromotedAmount(0) // This amount means that the merchant will assume the interests. |
274
|
|
|
->setTotalAmount('59999'); |
275
|
|
|
return $orderShoppingCart; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls |
280
|
|
|
*/ |
281
|
|
|
function setConfigurationUrls() |
282
|
|
|
{ |
283
|
|
|
|
284
|
|
|
$confirmUrl |
285
|
|
|
= "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=confirmOrder"; |
286
|
|
|
$errorUrl |
287
|
|
|
= "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=cancelOrder"; |
288
|
|
|
|
289
|
|
|
$orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls(); |
290
|
|
|
$orderConfigurationUrls |
291
|
|
|
->setCancel($errorUrl) |
292
|
|
|
->setKo($errorUrl) |
293
|
|
|
->setAuthorizedNotificationCallback($confirmUrl) |
294
|
|
|
->setRejectedNotificationCallback($confirmUrl) |
295
|
|
|
->setOk($confirmUrl); |
296
|
|
|
return $orderConfigurationUrls; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel |
301
|
|
|
*/ |
302
|
|
|
function setOrderChannel() |
303
|
|
|
{ |
304
|
|
|
$orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel(); |
305
|
|
|
$orderChannel |
306
|
|
|
->setAssistedSale(false) |
307
|
|
|
->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE); |
308
|
|
|
return $orderChannel; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param $orderChannel |
313
|
|
|
* @param $orderConfigurationUrls |
314
|
|
|
* |
315
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order\Configuration |
316
|
|
|
*/ |
317
|
|
|
function setOrderConfiguration( |
318
|
|
|
$orderChannel, |
319
|
|
|
$orderConfigurationUrls |
320
|
|
|
) { |
321
|
|
|
$orderConfiguration |
322
|
|
|
= $orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration(); |
|
|
|
|
323
|
|
|
$orderConfiguration |
324
|
|
|
->setChannel($orderChannel) |
325
|
|
|
->setUrls($orderConfigurationUrls); |
326
|
|
|
return $orderConfiguration; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param $orderConfiguration |
331
|
|
|
* @param $orderShoppingCart |
332
|
|
|
* @param $orderUser |
333
|
|
|
* |
334
|
|
|
* @return \Pagantis\OrdersApiClient\Model\Order |
335
|
|
|
*/ |
336
|
|
|
function sendOrder( |
337
|
|
|
$orderConfiguration, |
338
|
|
|
$orderShoppingCart, |
339
|
|
|
$orderUser |
340
|
|
|
) { |
341
|
|
|
$order = new \Pagantis\OrdersApiClient\Model\Order(); |
342
|
|
|
$order |
343
|
|
|
->setConfiguration($orderConfiguration) |
344
|
|
|
->setShoppingCart($orderShoppingCart) |
345
|
|
|
->setUser($orderUser); |
346
|
|
|
return $order; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param $order |
351
|
|
|
* |
352
|
|
|
* @return bool |
353
|
|
|
*/ |
354
|
|
|
function isOrderIdValid($order) |
355
|
|
|
{ |
356
|
|
|
if (!$order instanceof \Pagantis\OrdersApiClient\Model\Order) { |
357
|
|
|
return false; |
358
|
|
|
} |
359
|
|
|
return true; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @return \Pagantis\OrdersApiClient\Client |
364
|
|
|
* @throws \Httpful\Exception\ConnectionErrorException |
365
|
|
|
* @throws \Pagantis\OrdersApiClient\Exception\ClientException |
366
|
|
|
* @throws Exception |
367
|
|
|
*/ |
368
|
|
|
function getClient() |
369
|
|
|
{ |
370
|
|
|
if (PUBLIC_KEY == '' || PRIVATE_KEY == '') { |
|
|
|
|
371
|
|
|
throw new \Exception('You need set the public and private key'); |
372
|
|
|
} |
373
|
|
|
$orderClient = new \Pagantis\OrdersApiClient\Client(PUBLIC_KEY, PRIVATE_KEY); |
374
|
|
|
return $orderClient; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param \Pagantis\OrdersApiClient\Model\Order $order |
379
|
|
|
* |
380
|
|
|
* @throws Exception |
381
|
|
|
*/ |
382
|
|
|
function processOrder(\Pagantis\OrdersApiClient\Model\Order $order) |
383
|
|
|
{ |
384
|
|
|
if (isOrderIdValid($order)) { |
385
|
|
|
//If the order is correct and created then we have the redirection URL here: |
386
|
|
|
// $url = $order->getActionUrls()->getForm(); |
387
|
|
|
$_SESSION['order_id'] = $order->getId(); |
388
|
|
|
writeLog(jsonEncoded($order->export()), $withDate); |
|
|
|
|
389
|
|
|
} else { |
390
|
|
|
throw new \Exception('Order not valid'); |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @param \Pagantis\OrdersApiClient\Model\Order $order |
396
|
|
|
* |
397
|
|
|
* @return string |
398
|
|
|
*/ |
399
|
|
|
function getFormURL(\Pagantis\OrdersApiClient\Model\Order $order) |
400
|
|
|
{ |
401
|
|
|
|
402
|
|
|
$url = $order->getActionUrls()->getForm(); |
403
|
|
|
return $url; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* UTILS |
408
|
|
|
*/ |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param $message |
412
|
|
|
* @param $withDate |
413
|
|
|
* |
414
|
|
|
* @return false|int |
415
|
|
|
*/ |
416
|
|
|
|
417
|
|
|
function writeLog( |
418
|
|
|
$message, |
419
|
|
|
$withDate |
420
|
|
|
) { |
421
|
|
|
$dateFormat = '[D M j G:i:s o]'; |
422
|
|
|
if ($withDate) { |
423
|
|
|
$date = getCurrentDate($dateFormat); |
424
|
|
|
return file_put_contents('logs/pagantis.old.log', "$date - 'CREATE ORDER' - $message.\n", |
425
|
|
|
FILE_APPEND); |
426
|
|
|
} |
427
|
|
|
return file_put_contents('logs/pagantis.old.log', "$message.\n", |
428
|
|
|
FILE_APPEND); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param $dateFormat |
433
|
|
|
* |
434
|
|
|
* @return false|string |
435
|
|
|
*/ |
436
|
|
|
function getCurrentDate($dateFormat) |
437
|
|
|
{ |
438
|
|
|
$currentDate = date($dateFormat); |
439
|
|
|
return $currentDate; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* @param $object |
444
|
|
|
* |
445
|
|
|
* @return false|string |
446
|
|
|
*/ |
447
|
|
|
function jsonEncoded($object) |
448
|
|
|
{ |
449
|
|
|
return json_encode($object, |
450
|
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT |
451
|
|
|
); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @param array $array |
456
|
|
|
* @param $key |
457
|
|
|
* |
458
|
|
|
* @return mixed |
459
|
|
|
*/ |
460
|
|
|
function getValueOfKey(array $array, $key) |
461
|
|
|
{ |
462
|
|
|
if (!is_string($key)) { |
463
|
|
|
new \Exception($key . ' must be a string' . gettype($key) |
464
|
|
|
. ' was provided'); |
465
|
|
|
} |
466
|
|
|
$value = $array[$key]; |
467
|
|
|
return $value; |
468
|
|
|
} |