-
Notifications
You must be signed in to change notification settings - Fork 11
/
ZboziKonverze.php
396 lines (354 loc) · 9.76 KB
/
ZboziKonverze.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* Provides access to ZboziKonverze service.
*
* Example of usage:
*
* \code
* try {
* // Initialize
* $zbozi = new ZboziKonverze(ID PROVOZOVNY, "TAJNY KLIC");
*
* // Set order details
* $zbozi->setOrder(array(
* "orderId" => "CISLO OBJEDNAVKY",
* "email" => "[email protected]",
* "deliveryType" => "CESKA_POSTA",
* "deliveryPrice" => 80,
* "otherCosts" => 20,
* "paymentType" => "dobírka",
* ));
*
* // Add bought items
* $zbozi->addCartItem(array(
* "itemId" => "1357902468",
* "productName" => "Samsung Galaxy S3 (i9300)",
* "quantity" => 1,
* "unitPrice" => 5000.50,
* ));
*
* $zbozi->addCartItem(array(
* "itemId" => "2468013579",
* "productName" => "BARUM QUARTARIS 165/70 R14 81 T",
* "quantity" => 4,
* "unitPrice" => 600,
* ));
*
* // Finally send request
* $zbozi->send();
*
* } catch (ZboziKonverzeException $e) {
* // Error should be handled according to your preference
* error_log("Chyba konverze: " . $e->getMessage());
* }
* \endcode
*
* @author Zbozi.cz <[email protected]>
*/
class ZboziKonverze {
/**
* Endpoint URL
*
* @var string BASE_URL
*/
const BASE_URL = 'https://%%DOMAIN%%/action/%%SHOP_ID%%/conversion/backend';
/**
* Private identifier of request creator
*
* @var string $PRIVATE_KEY
*/
public $PRIVATE_KEY;
/**
* Public identifier of request creator
*
* @var string $SHOP_ID
*/
public $SHOP_ID;
/**
* Identifier of this order
*
* @var string $orderId
*/
public $orderId;
/**
* Customer email
* Should not be set unless customer allows to do so.
*
* @var string $email
*/
public $email;
/**
* How the order will be transfered to the customer
*
* @var string $deliveryType
*/
public $deliveryType;
/**
* Cost of delivery (in CZK)
*
* @var float $deliveryPrice
*/
public $deliveryPrice;
/**
* How the order was paid
*
* @var string $paymentType
*/
public $paymentType;
/**
* Other fees (in CZK)
*
* @var string $otherCosts
*/
public $otherCosts;
/**
* Array of CartItem
*
* @var array $cart
*/
public $cart = array();
/**
* Determine URL where the request will be send to
*
* @var boolean $sandbox
*/
private $sandbox;
/**
* Set if sandbox URL will be used.
*
* @param boolean $val
*/
public function useSandbox($val) {
$this->sandbox = $val;
}
/**
* Check if string is not empty
*
* @param string $question String to test
* @return boolean
*/
private static function isNullOrEmptyString($question) {
return (!isset($question) || trim($question)==='');
}
/**
* Initialize ZboziKonverze service
*
* @param string $shopId Shop identifier
* @param string $privateKey Shop private key
* @throws ZboziKonverzeException can be thrown if \p $privateKey and/or \p $shopId
* is missing or invalid.
*/
public function __construct($shopId, $privateKey)
{
if ($this::isNullOrEmptyString($shopId)) {
throw new ZboziKonverzeException('shopId si mandatory');
} else {
$this->SHOP_ID = $shopId;
}
if ($this::isNullOrEmptyString($privateKey)) {
throw new ZboziKonverzeException('privateKey si mandatory');
} else {
$this->PRIVATE_KEY = $privateKey;
}
$this->sandbox = false;
}
/**
* Sets customer email
*
* @param string $email Customer email address
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Adds order ID
*
* @param int $orderId Order identifier
*/
public function addOrderId($orderId)
{
$this->orderId = $orderId;
}
/**
* Adds ordered product using name
*
* @param string $productName Ordered product name
*/
public function addProduct($productName)
{
$item = new CartItem();
$item->productName = $productName;
$this->cart[] = $item;
}
/**
* Adds ordered product using item ID
*
* @param string $itemId Ordered product item ID
*/
public function addProductItemId($itemId)
{
$item = new CartItem();
$item->itemId = $itemId;
$this->cart[] = $item;
}
/**
* Adds ordered product using array which can contains
* \p productName ,
* \p itemId ,
* \p unitPrice ,
* \p quantity
*
* @param array $cartItem Array of various CartItem attributes
*/
public function addCartItem($cartItem)
{
$item = new CartItem();
if (array_key_exists("productName", $cartItem)) {
$item->productName = $cartItem["productName"];
}
if (array_key_exists("itemId", $cartItem)) {
$item->itemId = $cartItem["itemId"];
}
if (array_key_exists("unitPrice", $cartItem)) {
$item->unitPrice = $cartItem["unitPrice"];
}
if (array_key_exists("quantity", $cartItem)) {
$item->quantity = $cartItem["quantity"];
}
$this->cart[] = $item;
}
/**
* Sets order attributes within
* \p email ,
* \p deliveryType ,
* \p deliveryPrice ,
* \p orderId ,
* \p otherCosts ,
* \p paymentType ,
*
* @param array $orderAttributes Array of various order attributes
*/
public function setOrder($orderAttributes) {
if (array_key_exists("email", $orderAttributes) && $orderAttributes["email"]) {
$this->email = $orderAttributes["email"];
}
$this->deliveryType = $orderAttributes["deliveryType"];
$this->deliveryPrice = $orderAttributes["deliveryPrice"];
$this->orderId = $orderAttributes["orderId"];
$this->otherCosts = $orderAttributes["otherCosts"];
$this->paymentType = $orderAttributes["paymentType"];
}
/**
* Creates HTTP request and returns response body
*
* @param string $url URL
* @return boolean true if everything is perfect else throws exception
* @throws ZboziKonverzeException can be thrown if connection to Zbozi.cz
* server cannot be established.
*/
protected function sendRequest($url)
{
$encoded_json = json_encode(get_object_vars($this));
if (extension_loaded('curl'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3 /* seconds */);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
if ($response === false) {
throw new ZboziKonverzeException('Unable to establish connection to ZboziKonverze service: ' . curl_error($ch));
}
}
else
{
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => $encoded_json,
),
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false) {
throw new ZboziKonverzeException('Unable to establish connection to ZboziKonverze service');
}
}
$decoded_response = json_decode($response, true);
if ((int)($decoded_response["status"] / 100) === 2) {
return true;
} else {
throw new ZboziKonverzeException('Request was not accepted: ' . $decoded_response['statusMessage']);
}
}
/**
* Returns endpoint URL
*
* @return string URL where the request will be called
*/
private function getUrl()
{
$url = $this::BASE_URL;
$url = str_replace("%%SHOP_ID%%", $this->SHOP_ID, $url);
if ($this->sandbox) {
$url = str_replace("%%DOMAIN%%", "sandbox.zbozi.cz", $url);
} else {
$url = str_replace("%%DOMAIN%%", "www.zbozi.cz", $url);
}
return $url;
}
/**
* Sends request to ZboziKonverze service and checks for valid response
*
* @return boolean true if everything is perfect else throws exception
* @throws ZboziKonverzeException can be thrown if connection to Zbozi.cz
* server cannot be established or mandatory values are missing.
*/
public function send()
{
$url = $this->getUrl();
// send request and check for valid response
try {
$status = $this->sendRequest($url);
return $status;
} catch (Exception $e) {
throw new ZboziKonverzeException($e->getMessage());
}
}
};
class CartItem {
/**
* Item name
*
* @var string $productName
*/
public $productName;
/**
* Item identifier
*
* @var string $itemId
*/
public $itemId;
/**
* Price per one item (in CZK)
*
* @var float $unitPrice
*/
public $unitPrice;
/**
* Number of items ordered
*
* @var int $quantity
*/
public $quantity;
};
/**
* Thrown when an service returns an exception
*/
class ZboziKonverzeException extends Exception {};