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