Passed
Pull Request — master (#26)
by
unknown
01:43
created

setAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.8666
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,
48
        $orderShippingAddress);
49
    writeLog('Created User object', $fileName, $withDate);
50
51
    //2. ShoppingCart Object
52
    writeLog('Creating ShoppingCart object', $fileName, $withDate);
53
    writeLog('Adding the purchases of the customer, if there are.', $fileName, $withDate);
54
    $orderHistory = setOrderHistory();
0 ignored issues
show
Unused Code introduced by
The assignment to $orderHistory is dead and can be removed.
Loading history...
55
56
    writeLog('Adding cart products. Minimum 1 required', $fileName, $withDate);
57
    $product = setProduct();
58
59
    $details = setProductDetails($product);
60
61
    $orderShoppingCart = setShoppingCart($details, ORDER_ID);
62
    writeLog('Created OrderShoppingCart object', $fileName, $withDate);
63
64
    //3. Configuration Object
65
    writeLog('Creating Configuration object', $fileName, $withDate);
66
    writeLog('Adding urls to redirect the user according each case', $fileName, $withDate);
67
68
    $orderConfigurationUrls = setConfigurationUrls();
69
70
    writeLog('Adding channel info', $fileName, $withDate);
71
    $orderChannel = setOrderChannel();
72
73
    $orderConfiguration = setOrderConfiguration($orderChannel,
74
        $orderConfigurationUrls);
75
    writeLog('Created Configuration object', $fileName, $withDate);
76
77
    $order = sendOrder($orderConfiguration, $orderShoppingCart,
78
        $orderUser);
79
80
    writeLog('Creating OrdersApiClient', $fileName, $withDate);
81
    $orderClient = getClient();
82
83
    writeLog('Creating Pagantis order', $fileName, $withDate);
84
    $order = $orderClient->createOrder($order);
85
86
    writeLog("Pagantis Order Created ID: "
87
        . jsonEncoded($_SESSION['order_id']), $fileName, $withDate);
88
89
    processOrder($order);
90
    $url = getFormURL($order);
91
    writeLog("Session " . $_SESSION['order_id'], $fileName, $withDate);
92
    // You can use our test credit cards to fill the Pagantis form
93
    writeLog("Redirecting to Pagantis form => $url", $fileName, $withDate);
94
    header('Location:' . $url);
95
}
96
97
/**
98
 * Confirm order in Pagantis
99
 *
100
 * @throws \Httpful\Exception\ConnectionErrorException
101
 * @throws \Pagantis\OrdersApiClient\Exception\ClientException
102
 * @throws \Pagantis\OrdersApiClient\Exception\HttpException
103
 * @throws \Exception
104
 */
105
function confirmOrder()
106
{
107
    /* Once the user comes back to the OK url or there is a notification upon callback url you will have to confirm
108
     * the reception of the order. If not it will expire and will never be paid.
109
     *
110
     * Add this parameters in your database when you create a order and map it to your own order. Or search orders by
111
     * your own order id. Both options are possible.
112
     */
113
114
    $fileName = basename(__FILE__);
115
    writeLog('Creating OrdersApiClient', $fileName, true);
116
    $orderClient = new \Pagantis\OrdersApiClient\Client(PUBLIC_KEY, PRIVATE_KEY);
117
118
    $order = $orderClient->getOrder($_SESSION['order_id']);
119
120
    if ($order instanceof \Pagantis\OrdersApiClient\Model\Order
121
        && $order->getStatus() == \Pagantis\OrdersApiClient\Model\Order::STATUS_AUTHORIZED
122
    ) {
123
        //If the order exists, and the status is authorized, means you can mark the order as paid.
124
125
        //DO WHATEVER YOU NEED TO DO TO MARK THE ORDER AS PAID IN YOUR OWN SYSTEM.
126
        writeLog('Confirming order', $fileName, true);
127
        $order = $orderClient->confirmOrder($order->getId());
128
129
        writeLog('Order confirmed', $fileName, true);
130
        writeLog(json_encode(
131
            $order->export(),
132
            JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT
133
        ), $fileName, true);
134
        $message = "The order {$_SESSION['order_id']} has been confirmed successfully";
135
    } else {
136
        $message = "The order {$_SESSION['order_id']} can't be confirmed";
137
    }
138
139
    /* The order has been marked as paid and confirmed in Pagantis so you will send the product to your customer and
140
     * Pagantis will pay you in the next 24h.
141
     */
142
143
    echo $message;
144
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
145
}
146
147
/**
148
 * Action after redirect to cancelUrl
149
 */
150
function cancelOrder()
151
{
152
    $message = "The order {$_SESSION['order_id']} can't be created";
153
154
    echo $message;
155
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
156
}
157
158
/**
159
 * Internal Functions
160
 */
161
/**
162
 * @return \Pagantis\OrdersApiClient\Model\Order\User\Address
163
 */
164
function setAddress()
165
{
166
    $userAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address();
167
168
    $userAddress
169
        ->setZipCode('28031')
170
        ->setFullName('María Sanchez Escudero')
171
        ->setCountryCode('ES')
172
        ->setCity('Madrid')
173
        ->setAddress('Paseo de la Castellana, 95')
174
        ->setDni('59661738Z')
175
        ->setNationalId('59661738Z')
176
        ->setFixPhone('911231234')
177
        ->setMobilePhone('600123123');
178
    return $userAddress;
179
}
180
181
/**
182
 * @return \Pagantis\OrdersApiClient\Model\Order\User\Address
183
 */
184
function setShippingAddress()
185
{
186
    $orderShippingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address();
187
188
    $orderShippingAddress
189
        ->setZipCode('08029')
190
        ->setFullName('Alberto Escudero Sanchez')
191
        ->setCountryCode('ES')
192
        ->setCity('Barcelona')
193
        ->setAddress('Avenida de la diagonal 525')
194
        ->setDni('77695544A')
195
        ->setNationalId('59661738Z')
196
        ->setFixPhone('931232345')
197
        ->setMobilePhone('600123124');
198
    return $orderShippingAddress;
199
}
200
201
/**
202
 * @param $userAddress
203
 * @param $orderBillingAddress
204
 * @param $orderShippingAddress
205
 *
206
 * @return \Pagantis\OrdersApiClient\Model\Order\User
207
 */
208
function setOrder(
209
    $userAddress,
210
    $orderBillingAddress,
211
    $orderShippingAddress
212
) {
213
    $orderUser = new \Pagantis\OrdersApiClient\Model\Order\User();
214
    $orderUser
215
        ->setFullName('María Sanchez Escudero')
216
        ->setAddress($userAddress)
217
        ->setBillingAddress($orderBillingAddress)
218
        ->setShippingAddress($orderShippingAddress)
219
        ->setDateOfBirth('1985-12-30')
220
        ->setEmail('[email protected]')
221
        ->setFixPhone('911231234')
222
        ->setMobilePhone('600123123')
223
        ->setDni('59661738Z')
224
        ->setNationalId('59661738Z');
225
    return $orderUser;
226
}
227
228
/**
229
 * @return \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory
230
 */
231
function setOrderHistory()
232
{
233
    $orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory();
234
    $orderUser = new \Pagantis\OrdersApiClient\Model\Order\User();
235
    $orderHistory
236
        ->setAmount('2499')
237
        ->setDate('2010-01-31');
238
    $orderUser->addOrderHistory($orderHistory);
239
    return $orderHistory;
240
}
241
242
/**
243
 * @return \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product
244
 */
245
function setProduct()
246
{
247
    $product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product();
248
    $product
249
        ->setAmount('59999')
250
        ->setQuantity('1')
251
        ->setDescription('TV LG UltraPlasma');
252
    return $product;
253
}
254
255
/**
256
 * @param \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product $product
257
 *
258
 * @return \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details
259
 */
260
function setProductDetails(
261
    \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product $product
262
) {
263
    $details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details();
264
    $details->setShippingCost('0');
265
    $details->addProduct($product);
266
    return $details;
267
}
268
269
/**
270
 * @param $details
271
 * @param $orderID
272
 *
273
 * @return \Pagantis\OrdersApiClient\Model\Order\ShoppingCart
274
 */
275
function setShoppingCart($details, $orderID)
276
{
277
    $orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart();
278
279
    $orderShoppingCart
280
        ->setDetails($details)
281
        ->setOrderReference($orderID)
282
        ->setPromotedAmount(0) // This amount means that the merchant will assume the interests.
283
        ->setTotalAmount('59999');
284
    return $orderShoppingCart;
285
}
286
287
/**
288
 * @return \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls
289
 */
290
function setConfigurationUrls()
291
{
292
    $confirmUrl
293
        = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=confirmOrder";
294
    $errorUrl
295
        = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=cancelOrder";
296
297
    $orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls();
298
    $orderConfigurationUrls
299
        ->setCancel($errorUrl)
300
        ->setKo($errorUrl)
301
        ->setAuthorizedNotificationCallback($confirmUrl)
302
        ->setRejectedNotificationCallback($confirmUrl)
303
        ->setOk($confirmUrl);
304
    return $orderConfigurationUrls;
305
}
306
307
/**
308
 * @return \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel
309
 */
310
function setOrderChannel()
311
{
312
    $orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel();
313
    $orderChannel
314
        ->setAssistedSale(false)
315
        ->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE);
316
    return $orderChannel;
317
}
318
319
/**
320
 * @param $orderChannel
321
 * @param $orderConfigurationUrls
322
 *
323
 * @return \Pagantis\OrdersApiClient\Model\Order\Configuration
324
 */
325
function setOrderConfiguration(
326
    $orderChannel,
327
    $orderConfigurationUrls
328
) {
329
    $orderConfiguration
330
        = $orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration();
0 ignored issues
show
Unused Code introduced by
The assignment to $orderConfiguration is dead and can be removed.
Loading history...
331
    $orderConfiguration
332
        ->setChannel($orderChannel)
333
        ->setUrls($orderConfigurationUrls);
334
    return $orderConfiguration;
335
}
336
337
/**
338
 * @param $orderConfiguration
339
 * @param $orderShoppingCart
340
 * @param $orderUser
341
 *
342
 * @return \Pagantis\OrdersApiClient\Model\Order
343
 */
344
function sendOrder(
345
    $orderConfiguration,
346
    $orderShoppingCart,
347
    $orderUser
348
) {
349
    $order = new \Pagantis\OrdersApiClient\Model\Order();
350
    $order
351
        ->setConfiguration($orderConfiguration)
352
        ->setShoppingCart($orderShoppingCart)
353
        ->setUser($orderUser);
354
    return $order;
355
}
356
357
/**
358
 * @param $method
359
 *
360
 * @return bool
361
 */
362
363
function isFunctionNameValid($method)
364
{
365
    return function_exists($method);
366
}
367
368
/**
369
 * @param $order
370
 *
371
 * @return bool
372
 */
373
function isOrderIdValid($order)
374
{
375
    if (!$order instanceof \Pagantis\OrdersApiClient\Model\Order) {
376
        return false;
377
    }
378
    return true;
379
}
380
381
/**
382
 * @return \Pagantis\OrdersApiClient\Client
383
 * @throws \Httpful\Exception\ConnectionErrorException
384
 * @throws \Pagantis\OrdersApiClient\Exception\ClientException
385
 * @throws Exception
386
 */
387
function getClient()
388
{
389
    if (PUBLIC_KEY == '' || PRIVATE_KEY == '') {
0 ignored issues
show
introduced by
The condition PUBLIC_KEY == '' is always true.
Loading history...
390
        throw new \Exception('You need set the public and private key');
391
    }
392
    $orderClient = new \Pagantis\OrdersApiClient\Client(PUBLIC_KEY, PRIVATE_KEY);
393
    return $orderClient;
394
}
395
396
/**
397
 * @param \Pagantis\OrdersApiClient\Model\Order $order
398
 *
399
 * @throws Exception
400
 */
401
function processOrder(\Pagantis\OrdersApiClient\Model\Order $order)
402
{
403
    if (!isOrderIdValid($order)) {
404
        throw new \Exception('Order not valid');
405
    }
406
    //If the order is correct and created then we have the redirection URL here:
407
    // $url = $order->getActionUrls()->getForm();
408
    $_SESSION['order_id'] = $order->getId();
409
    writeLog(jsonEncoded($order->export()), basename(__FILE__), $withDate = true);
410
}
411
412
/**
413
 * @param \Pagantis\OrdersApiClient\Model\Order $order
414
 *
415
 * @return string
416
 */
417
function getFormURL(\Pagantis\OrdersApiClient\Model\Order $order)
418
{
419
    $url = $order->getActionUrls()->getForm();
420
    return $url;
421
}
422
423
/**
424
 * @return bool
425
 */
426
function isGetActionValid()
427
{
428
429
    if (!$_GET['action']) {
430
        return false;
431
    }
432
    return true;
433
}
434
435
/**
436
 * @return mixed|string
437
 */
438
function get_GetMethod()
439
{
440
    if (!isGetActionValid()) {
441
       return 'createOrder';
442
443
    };
444
    $method = json_decode(json_encode($_GET));
445
    return $method->action;
446
}