|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* INJI |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
6
|
|
|
* @link http://inji.ru/ |
|
7
|
|
|
* @copyright 2017 Alexey Krupskiy |
|
8
|
|
|
* @license https://github.com/injitools/Inji/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ecommerce\DeliveryProvider; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class RussianPost extends \Ecommerce\DeliveryProvider { |
|
15
|
|
|
static $name = 'PickPoint - курьерская служба'; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param \Ecommerce\Cart $cart |
|
19
|
|
|
* @return \Money\Sums |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
static function request($cart) { |
|
|
|
|
|
|
23
|
|
|
$city = ''; |
|
24
|
|
View Code Duplication |
foreach ($cart->delivery->fields as $field) { |
|
|
|
|
|
|
25
|
|
|
if ($field->code === 'index') { |
|
26
|
|
|
if (!empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) { |
|
27
|
|
|
$city = $_POST['deliveryFields'][$field->id]; |
|
28
|
|
|
} elseif (isset($cart->deliveryInfos[$field->id])) { |
|
29
|
|
|
$city = $cart->deliveryInfos[$field->id]->value; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
if (!$city) { |
|
34
|
|
|
return []; |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
$senderCity = '101000'; |
|
37
|
|
|
|
|
38
|
|
|
$url = 'http://tariff.russianpost.ru/tariff/v1/calculate?json&'; |
|
|
|
|
|
|
39
|
|
|
$data = [ |
|
40
|
|
|
'object' => 4030, |
|
41
|
|
|
'weight' => '1', |
|
42
|
|
|
'date' => date('Ymd'), |
|
43
|
|
|
'sumoc' => $cart->itemsSum()->sums[0], |
|
44
|
|
|
'from' => $senderCity, |
|
45
|
|
|
'to' => $city, |
|
46
|
|
|
'closed' => 1, |
|
47
|
|
|
'service' => 2, |
|
48
|
|
|
'isavia' => 0, |
|
49
|
|
|
'delivery' => 1 |
|
50
|
|
|
]; |
|
51
|
|
|
$result = \Cache::get('russianPostCalc', $data, function ($data) { |
|
52
|
|
|
return file_get_contents('http://tariff.russianpost.ru/tariff/v1/calculate?json&' . http_build_query($data)); |
|
53
|
|
|
}); |
|
54
|
|
|
return json_decode($result, true); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
static function calcPrice($cart) { |
|
|
|
|
|
|
58
|
|
|
$result = static::request($cart); |
|
59
|
|
|
if ($result) { |
|
60
|
|
|
return new \Money\Sums([$cart->delivery->currency_id => 0]); |
|
61
|
|
|
} |
|
62
|
|
|
$sum = !empty($result['tariff'][0]['ground']['valnds']) ? $result['tariff'][0]['ground']['valnds'] : (!empty($result['tariff'][0]['avia']['valnds']) ? $result['tariff'][0]['avia']['valnds'] : 0); |
|
63
|
|
|
return new \Money\Sums([$cart->delivery->currency_id => $sum / 100 * 1.1]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
static function deliveryTime($cart) { |
|
|
|
|
|
|
67
|
|
|
$result = static::request($cart); |
|
68
|
|
|
return $result['delivery']; |
|
69
|
|
|
} |
|
70
|
|
|
} |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.