Issues (31)

examples/simpleTest.php (3 issues)

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