Passed
Pull Request — master (#12)
by
unknown
02:42
created

writeLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
//Require the Client library using composer: composer require pagamastarde/orders-api-client
4
require_once('../vendor/autoload.php');
5
6
/**
7
 * PLEASE FILL YOUR PUBLIC KEY AND PRIVATE KEY
8
 */
9
const PUBLICKEY = ''; //Set your public key
10
const PRIVATEKEY = ''; //Set your private key
11
const ORDERIDENTIFICATION = 'order_4159972708';
12
13
try {
14
    session_start();
15
    $method = ($_GET['action']) ? ($_GET['action']) : 'createOrder';
16
    call_user_func($method);
17
} catch (Exception $e) {
18
    echo $e->getMessage();
19
    exit;
20
}
21
22
function createOrder()
23
{
24
    // There are 3 objects which are mandatory: User object, ShoppingCart object and Configuration object.
25
    //1. User Object
26
    writeLog('Creating User object');
27
    writeLog('Adding the address of the user');
28
    $userAddress =  new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address();
29
    $userAddress
30
        ->setZipCode('28031')
31
        ->setFullName('María Sanchez Escudero')
32
        ->setCountryCode('ES')
33
        ->setCity('Madrid')
34
        ->setAddress('Paseo de la Castellana, 95')
35
        ->setDni('59661738Z')
36
        ->setFixPhone('911231234')
37
        ->setMobilePhone('600123123');
38
39
    $orderBillingAddress = $userAddress;
40
41
    $orderShippingAddress =  new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address();
42
    $orderShippingAddress
43
        ->setZipCode('08029')
44
        ->setFullName('Alberto Escudero Sanchez')
45
        ->setCountryCode('ES')
46
        ->setCity('Barcelona')
47
        ->setAddress('Avenida de la diagonal 525')
48
        ->setDni('77695544A')
49
        ->setFixPhone('931232345')
50
        ->setMobilePhone('600123124');
51
52
    writeLog('Adding the information of the user');
53
    $orderUser = new \PagaMasTarde\OrdersApiClient\Model\Order\User();
54
    $orderUser
55
        ->setFullName('María Sanchez Escudero')
56
        ->setAddress($userAddress)
57
        ->setBillingAddress($orderBillingAddress)
58
        ->setShippingAddress($orderShippingAddress)
59
        ->setDateOfBirth('1985-12-30')
60
        ->setEmail('[email protected]')
61
        ->setFixPhone('911231234')
62
        ->setMobilePhone('600123123')
63
        ->setDni('59661738Z');
64
    writeLog('Created User object');
65
66
    //2. ShoppingCart Object
67
    writeLog('Creating ShoppingCart object');
68
    writeLog('Adding the purchases of the customer, if there are.');
69
    $orderHistory = new \PagaMasTarde\OrdersApiClient\Model\Order\User\OrderHistory();
70
    $orderHistory
71
        ->setAmount('2499')
72
        ->setDate('2010-01-31');
73
    $orderUser->addOrderHistory($orderHistory);
74
75
    writeLog('Adding cart products');
76
    $product = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details\Product();
77
    $product
78
        ->setAmount('59999')
79
        ->setQuantity('1')
80
        ->setDescription('TV LG UltraPlana');
81
82
    $details = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details();
83
    $details->setShippingCost('0');
84
    $details->addProduct($product);
85
86
    $orderShoppingCart = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart();
87
    $orderShoppingCart
88
        ->setDetails($details)
89
        ->setOrderReference(ORDERIDENTIFICATION)
90
        ->setPromotedAmount(0) // This amount means that the merchant will asume the interests.
91
        ->setTotalAmount('59999');
92
    writeLog('Created OrderShoppingCart object');
93
94
    //3. Configuration Object
95
    writeLog('Creating Configuration object');
96
    writeLog('Adding urls to redirect the user according each case');
97
    $confirmUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=confirmOrder";
98
    $errorUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=cancelOrder";
99
    $orderConfigurationUrls = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Urls();
100
    $orderConfigurationUrls
101
        ->setCancel($errorUrl)
102
        ->setKo($errorUrl)
103
        ->setNotificationCallback($confirmUrl)
104
        ->setOk($confirmUrl);
105
106
    writeLog('Adding channel info');
107
    $orderChannel = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel();
108
    $orderChannel
109
        ->setAssistedSale(false)
110
        ->setType(\PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE);
111
112
    $orderConfiguration = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration();
113
    $orderConfiguration
114
        ->setChannel($orderChannel)
115
        ->setUrls($orderConfigurationUrls);
116
    writeLog('Created Configuration object');
117
118
    $order = new \PagaMasTarde\OrdersApiClient\Model\Order();
119
    $order
120
        ->setConfiguration($orderConfiguration)
121
        ->setShoppingCart($orderShoppingCart)
122
        ->setUser($orderUser);
123
124
    writeLog('Creating client variable');
125
    if (PUBLICKEY=='' || PRIVATEKEY == '') {
0 ignored issues
show
introduced by
The condition PUBLICKEY == '' is always true.
Loading history...
126
        throw new \Exception('You need set the public and private key');
127
    }
128
    $orderClient = new \PagaMasTarde\OrdersApiClient\Client(PUBLICKEY, PRIVATEKEY);
129
130
    writeLog('Creating Paga+Tarde order');
131
    $order = $orderClient->createOrder($order);
132
    if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order) {
133
        //If the order is correct and created then we have the redirection URL here:
134
        $url = $order->getActionUrls()->getForm();
135
        $_SESSION['order_id'] = $order->getId();
136
        writeLog(json_encode($order->export(), JSON_PRETTY_PRINT));
137
    } else {
138
        throw new \Exception('Order not created');
139
    }
140
141
    // You can use our test credit cards to fill the Paga+Tarde form
142
    writeLog("Redirecting to Paga+Tarde form => $url");
143
    header('Location:'. $url);
144
}
145
146
function confirmOrder()
147
{
148
    /* Once the user comes back to the OK url or there is a notification upon callback url you will have to confirm
149
     * the reception of the order. If not it will expire and will never be paid.
150
     *
151
     * Add this parameters in your database when you create a order and map it to your own order. Or search orders by
152
     * your own order id. Both options are possible.
153
     */
154
155
    writeLog('Creating client variable');
156
    $orderClient = new \PagaMasTarde\OrdersApiClient\Client(PUBLICKEY, PRIVATEKEY);
157
158
    $order = $orderClient->getOrder($_SESSION['order_id']);
159
160
    if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order &&
161
        $order->getStatus() == \PagaMasTarde\OrdersApiClient\Model\Order::STATUS_AUTHORIZED) {
162
        //If the order exists, and the status is authorized, means you can mark the order as paid.
163
164
        //DO WHATEVER YOU NEED TO DO TO MARK THE ORDER AS PAID IN YOUR OWN SYSTEM.
165
        writeLog('Confirming order');
166
        $order = $orderClient->confirmOrder($_SESSION['order_id']);
167
168
        writeLog('Order confirmed');
169
        writeLog(json_encode($order->export(), JSON_PRETTY_PRINT));
170
        $message = "The order {$_SESSION['order_id']} has been confirmed successfully";
171
    } else {
172
        $message = "The order {$_SESSION['order_id']} can't be confirmed";
173
    }
174
175
    /* The order has been marked as paid and confirmed in Paga+Tarde so you will send the product to your customer and
176
     * Paga+Tarde will pay you in the next 24h.
177
     */
178
179
    echo $message;
180
    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...
181
}
182
183
function cancelOrder()
184
{
185
    $message = "The order {$_SESSION['order_id']} can't be created";
186
187
    echo $message;
188
    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...
189
}
190
191
/**
192
 * UTILS
193
 */
194
195
function writeLog($message)
196
{
197
    file_put_contents('pmt.log', "$message.\n", FILE_APPEND);
198
}