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