|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PLEASE FILL YOUR PUBLIC KEY AND PRIVATE KEY |
|
5
|
|
|
*/ |
|
6
|
|
|
const PUBLIC_KEY = ''; //Set your public key |
|
7
|
|
|
const PRIVATE_KEY = ''; //Set your private key |
|
8
|
|
|
const ORDER_ID = ''; |
|
9
|
|
|
|
|
10
|
|
|
try { |
|
11
|
|
|
session_start(); |
|
12
|
|
|
$method = (isset($_GET['action']) && $_GET['action']) ? ($_GET['action']) : 'createOrder'; |
|
13
|
|
|
call_user_func($method); |
|
14
|
|
|
} catch (Exception $e) { |
|
15
|
|
|
echo $e->getMessage(); |
|
16
|
|
|
exit; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create order in Pagantis |
|
21
|
|
|
* |
|
22
|
|
|
* @throws Exception |
|
23
|
|
|
*/ |
|
24
|
|
|
function createOrder() |
|
25
|
|
|
{ |
|
26
|
|
|
// There are 3 objects which are mandatory: User object, ShoppingCart object and Configuration object. |
|
27
|
|
|
//1. User Object |
|
28
|
|
|
writeLog('Creating User object'); |
|
29
|
|
|
writeLog('Adding the address of the user'); |
|
30
|
|
|
$userAddress = array(); |
|
31
|
|
|
$userAddress['zip_code'] = '28008'; |
|
32
|
|
|
$userAddress['full_name'] = 'María Sanchez Escudero'; |
|
33
|
|
|
$userAddress['country_code'] = 'ES'; |
|
34
|
|
|
$userAddress['city'] = 'Madrid'; |
|
35
|
|
|
$userAddress['address'] = 'Paseo de la Castellana, 95'; |
|
36
|
|
|
$userAddress['dni'] = '59661738Z'; |
|
37
|
|
|
$userAddress['fix_phone'] = '911231234'; |
|
38
|
|
|
$userAddress['mobile_phone'] = '600123123'; |
|
39
|
|
|
$userAddress['national_id'] = '59661738Z'; |
|
40
|
|
|
|
|
41
|
|
|
$orderBillingAddress = $userAddress; |
|
42
|
|
|
|
|
43
|
|
|
$orderShippingAddress = array(); |
|
44
|
|
|
$orderShippingAddress['zip_code'] = '08029'; |
|
45
|
|
|
$orderShippingAddress['full_name'] = 'Alberto Escudero Sanchez'; |
|
46
|
|
|
$orderShippingAddress['country_code'] = 'ES'; |
|
47
|
|
|
$orderShippingAddress['city'] = 'Barcelona'; |
|
48
|
|
|
$orderShippingAddress['address'] = 'Avenida de la diagonal 525'; |
|
49
|
|
|
$orderShippingAddress['dni'] = '77695544A'; |
|
50
|
|
|
$orderShippingAddress['fix_phone'] = '931232345'; |
|
51
|
|
|
$orderShippingAddress['mobile_phone'] = '600123124'; |
|
52
|
|
|
$orderShippingAddress['national_id'] = '77695544A'; |
|
53
|
|
|
|
|
54
|
|
|
writeLog('Adding the purchases of the customer, if there are.'); |
|
55
|
|
|
$orderHistory = array ( |
|
56
|
|
|
0 => array ( |
|
57
|
|
|
'date' => '2020-01-31', |
|
58
|
|
|
'amount' => 989, |
|
59
|
|
|
), |
|
60
|
|
|
1 => array ( |
|
61
|
|
|
'date' => '2020-01-31', |
|
62
|
|
|
'amount' => 898, |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
writeLog('Adding the information of the user'); |
|
67
|
|
|
$orderUser = array(); |
|
68
|
|
|
$orderUser['full_name'] = 'María Sanchez Escudero'; |
|
69
|
|
|
$orderUser['email'] = '[email protected]'; |
|
70
|
|
|
$orderUser['date_of_birth'] = '1985-12-30'; |
|
71
|
|
|
$orderUser['address'] = ''; |
|
72
|
|
|
$orderUser['dni'] = '59661738Z'; |
|
73
|
|
|
$orderUser['national_id'] = '59661738Z'; |
|
74
|
|
|
$orderUser['fix_phone'] = '911231234'; |
|
75
|
|
|
$orderUser['mobile_phone'] = '600123123'; |
|
76
|
|
|
$orderUser['address'] = $userAddress; |
|
77
|
|
|
$orderUser['billing_address'] = $orderBillingAddress; |
|
78
|
|
|
$orderUser['shipping_address'] = $orderShippingAddress; |
|
79
|
|
|
$orderUser['order_history'] = $orderHistory; |
|
80
|
|
|
writeLog('Created User object'); |
|
81
|
|
|
|
|
82
|
|
|
//2. ShoppingCart Object |
|
83
|
|
|
writeLog('Creating ShoppingCart object'); |
|
84
|
|
|
writeLog('Adding cart products. Minimum 1 required'); |
|
85
|
|
|
|
|
86
|
|
|
$product = array(); |
|
87
|
|
|
$product['amount'] = '59999'; |
|
88
|
|
|
$product['quantity'] = 1; |
|
89
|
|
|
$product['description'] = 'TV LG UltraPlana'; |
|
90
|
|
|
|
|
91
|
|
|
$details = array(); |
|
92
|
|
|
$details['shipping_cost'] = 0; |
|
93
|
|
|
$details['products'][0] = $product; |
|
94
|
|
|
|
|
95
|
|
|
$orderShoppingCart = array(); |
|
96
|
|
|
$orderShoppingCart['details'] = $details; |
|
97
|
|
|
$orderShoppingCart['order_reference'] = ORDER_ID; |
|
98
|
|
|
$orderShoppingCart['promoted_amount'] = 0; // This amount means that the merchant will asume the interests. |
|
99
|
|
|
$orderShoppingCart['total_amount'] = 59999; |
|
100
|
|
|
writeLog('Created OrderShoppingCart object'); |
|
101
|
|
|
|
|
102
|
|
|
//3. Configuration Object |
|
103
|
|
|
writeLog('Creating Configuration object'); |
|
104
|
|
|
writeLog('Adding urls to redirect the user according each case'); |
|
105
|
|
|
$confirmUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=confirmOrder"; |
|
106
|
|
|
$errorUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=cancelOrder"; |
|
107
|
|
|
|
|
108
|
|
|
$orderConfigurationUrls = array(); |
|
109
|
|
|
$orderConfigurationUrls['cancel'] = $errorUrl; |
|
110
|
|
|
$orderConfigurationUrls['ko'] = $errorUrl; |
|
111
|
|
|
$orderConfigurationUrls['authorized_notification_callback'] = $confirmUrl; |
|
112
|
|
|
$orderConfigurationUrls['rejected_notification_callback'] = $confirmUrl; |
|
113
|
|
|
$orderConfigurationUrls['ok'] = $confirmUrl; |
|
114
|
|
|
|
|
115
|
|
|
writeLog('Adding channel info'); |
|
116
|
|
|
$orderChannel = array(); |
|
117
|
|
|
$orderChannel['assisted_sale'] = false; |
|
118
|
|
|
$orderChannel['type'] = 'ONLINE'; |
|
119
|
|
|
|
|
120
|
|
|
$orderConfiguration = array(); |
|
121
|
|
|
$orderConfiguration['channel'] = $orderChannel; |
|
122
|
|
|
$orderConfiguration['urls'] = $orderConfigurationUrls; |
|
123
|
|
|
writeLog('Created Configuration object'); |
|
124
|
|
|
|
|
125
|
|
|
$order = array(); |
|
126
|
|
|
$order['configuration'] = $orderConfiguration; |
|
127
|
|
|
$order['shopping_cart'] = $orderShoppingCart; |
|
128
|
|
|
$order['user'] = $orderUser; |
|
129
|
|
|
|
|
130
|
|
|
writeLog('Preparing connection'); |
|
131
|
|
|
if (PUBLIC_KEY=='' || PRIVATE_KEY == '') { |
|
|
|
|
|
|
132
|
|
|
throw new \Exception('You need set the public and private key'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
writeLog('Creating Pagantis order'); |
|
136
|
|
|
$params_json = json_encode($order); |
|
137
|
|
|
|
|
138
|
|
|
$cliente = curl_init(); |
|
139
|
|
|
curl_setopt($cliente, CURLOPT_URL, "https://api.pagamastarde.com/v2/orders/"); |
|
140
|
|
|
curl_setopt($cliente, CURLOPT_POST, 1); |
|
141
|
|
|
curl_setopt($cliente, CURLOPT_SSL_VERIFYPEER, false); |
|
142
|
|
|
curl_setopt($cliente, CURLOPT_POSTFIELDS, $params_json); |
|
143
|
|
|
curl_setopt($cliente, CURLOPT_RETURNTRANSFER, true); |
|
144
|
|
|
curl_setopt($cliente, CURLOPT_HTTPHEADER, array( |
|
145
|
|
|
"Content-Type:application/json", |
|
146
|
|
|
"Authorization: Bearer ".PRIVATE_KEY |
|
147
|
|
|
)); |
|
148
|
|
|
|
|
149
|
|
|
$raw_response = curl_exec($cliente); |
|
150
|
|
|
$order = json_decode($raw_response); |
|
151
|
|
|
if (is_object($order)) { |
|
152
|
|
|
//If the order is correct and created then we have the redirection URL here: |
|
153
|
|
|
$url = $order->action_urls->form; |
|
154
|
|
|
$_SESSION['order_id'] = $order->id; |
|
155
|
|
|
writeLog(json_encode( |
|
156
|
|
|
$order, |
|
157
|
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT |
|
158
|
|
|
)); |
|
159
|
|
|
} else { |
|
160
|
|
|
throw new \Exception('Order not created'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// You can use our test credit cards to fill the Pagantis form |
|
164
|
|
|
writeLog("Redirecting to Pagantis form => $url"); |
|
165
|
|
|
header('Location:'. $url); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Confirm order in Pagantis |
|
170
|
|
|
* |
|
171
|
|
|
* @throws Exception |
|
172
|
|
|
*/ |
|
173
|
|
|
function confirmOrder() |
|
174
|
|
|
{ |
|
175
|
|
|
/* Once the user comes back to the OK url or there is a notification upon callback url you will have to confirm |
|
176
|
|
|
* the reception of the order. If not it will expire and will never be paid. |
|
177
|
|
|
* |
|
178
|
|
|
* Add this parameters in your database when you create a order and map it to your own order. Or search orders by |
|
179
|
|
|
* your own order id. Both options are possible. |
|
180
|
|
|
*/ |
|
181
|
|
|
|
|
182
|
|
|
writeLog('Getting order information'); |
|
183
|
|
|
|
|
184
|
|
|
writeLog('Preparing connection'); |
|
185
|
|
|
if (PUBLIC_KEY=='' || PRIVATE_KEY == '') { |
|
|
|
|
|
|
186
|
|
|
throw new \Exception('You need set the public and private key'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$cliente = curl_init(); |
|
190
|
|
|
curl_setopt($cliente, CURLOPT_URL, "https://api.pagamastarde.com/v2/orders/".$_SESSION['order_id']."/"); |
|
191
|
|
|
curl_setopt($cliente, CURLOPT_RETURNTRANSFER, true); |
|
192
|
|
|
curl_setopt($cliente, CURLOPT_HTTPHEADER, array( |
|
193
|
|
|
"Content-Type:application/json", |
|
194
|
|
|
"Authorization: Bearer ".PRIVATE_KEY |
|
195
|
|
|
)); |
|
196
|
|
|
|
|
197
|
|
|
$raw_response = curl_exec($cliente); |
|
198
|
|
|
$order = json_decode($raw_response); |
|
199
|
|
|
if (is_object($order) && $order->status == "AUTHORIZED") { |
|
200
|
|
|
//If the order exists, and the status is authorized, means you can mark the order as paid. |
|
201
|
|
|
|
|
202
|
|
|
//DO WHATEVER YOU NEED TO DO TO MARK THE ORDER AS PAID IN YOUR OWN SYSTEM. |
|
203
|
|
|
writeLog('Confirming order'); |
|
204
|
|
|
|
|
205
|
|
|
$cliente = curl_init(); |
|
206
|
|
|
curl_setopt($cliente, CURLOPT_URL, "https://api.pagamastarde.com/v2/orders/".$order->id."/confirm"); |
|
207
|
|
|
curl_setopt($cliente, CURLOPT_CUSTOMREQUEST, "PUT"); |
|
208
|
|
|
curl_setopt($cliente, CURLOPT_RETURNTRANSFER, true); |
|
209
|
|
|
curl_setopt($cliente, CURLOPT_HTTPHEADER, array( |
|
210
|
|
|
"Content-Type:application/json", |
|
211
|
|
|
"Authorization: Bearer ".PRIVATE_KEY |
|
212
|
|
|
)); |
|
213
|
|
|
|
|
214
|
|
|
$raw_response = curl_exec($cliente); |
|
215
|
|
|
$order = json_decode($raw_response); |
|
216
|
|
|
writeLog("Order confirmed"); |
|
217
|
|
|
writeLog(json_encode( |
|
218
|
|
|
$order, |
|
219
|
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT |
|
220
|
|
|
)); |
|
221
|
|
|
$message = "The order {$_SESSION['order_id']} has been confirmed successfully"; |
|
222
|
|
|
} else { |
|
223
|
|
|
$message = "The order {$_SESSION['order_id']} can't be confirmed"; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/* The order has been marked as paid and confirmed in Pagantis so you will send the product to your customer and |
|
227
|
|
|
* Pagantis will pay you in the next 24h. |
|
228
|
|
|
*/ |
|
229
|
|
|
|
|
230
|
|
|
echo $message; |
|
231
|
|
|
exit; |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Action after redirect to cancelUrl |
|
236
|
|
|
*/ |
|
237
|
|
|
function cancelOrder() |
|
238
|
|
|
{ |
|
239
|
|
|
$message = "The order {$_SESSION['order_id']} can't be created"; |
|
240
|
|
|
|
|
241
|
|
|
echo $message; |
|
242
|
|
|
exit; |
|
|
|
|
|
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* UTILS |
|
247
|
|
|
*/ |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Write log file |
|
251
|
|
|
* |
|
252
|
|
|
* @param $message |
|
253
|
|
|
*/ |
|
254
|
|
|
function writeLog($message) |
|
255
|
|
|
{ |
|
256
|
|
|
file_put_contents('pagantis.log', "$message.\n", FILE_APPEND); |
|
257
|
|
|
} |
|
258
|
|
|
|