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