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
|
|
|
use Ecommerce\UserAdds\Field; |
15
|
|
|
|
16
|
|
|
class RussianPost extends \Ecommerce\DeliveryProvider { |
17
|
|
|
static $name = 'PickPoint - курьерская служба'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
static function request($cart) { |
|
|
|
|
20
|
|
|
$city = ''; |
21
|
|
|
foreach ($cart->deliveryInfos as $deliveryInfo) { |
22
|
|
|
if ($deliveryInfo->field->code == 'index') { |
23
|
|
|
$city = $deliveryInfo->value; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
if (!$city) { |
27
|
|
|
$cityItem = static::getCity($cart); |
28
|
|
|
if ($cityItem) { |
29
|
|
|
$data = json_decode($cityItem->data, true); |
30
|
|
|
if (!empty($data['PostCodeList'])) { |
31
|
|
|
$city = explode(',', $data['PostCodeList'])[0]; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
if (!$city) { |
36
|
|
|
return []; |
37
|
|
|
} |
38
|
|
|
$senderCity = '101000'; |
39
|
|
|
|
40
|
|
|
$data = [ |
41
|
|
|
'object' => 4030, |
42
|
|
|
'weight' => '3000', |
43
|
|
|
'date' => date('Ymd'), |
44
|
|
|
//'sumoc' => $cart->itemsSum()->sums[0], |
|
|
|
|
45
|
|
|
'from' => $senderCity, |
46
|
|
|
'to' => $city, |
47
|
|
|
'delivery' => 1, |
48
|
|
|
]; |
49
|
|
|
$result = \Cache::get('russianPostCalc', $data, function ($data) { |
50
|
|
|
return file_get_contents('http://tariff.russianpost.ru/tariff/v1/calculate?json&' . http_build_query($data)); |
51
|
|
|
}, 4 * 60 * 60); |
52
|
|
|
return json_decode($result, true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
static function calcPrice($cart) { |
|
|
|
|
56
|
|
|
$result = static::request($cart); |
57
|
|
|
$curId = $cart->delivery ? $cart->delivery->currency_id : 0; |
58
|
|
|
if (empty($result['paynds'])) { |
59
|
|
|
return new \Money\Sums([$curId => 0]); |
60
|
|
|
} |
61
|
|
|
$sum = $result['paynds']; |
62
|
|
|
return new \Money\Sums([$curId => round($sum / 100 * 1.1, 2)]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
static function deliveryTime($cart) { |
|
|
|
|
66
|
|
|
$result = static::request($cart); |
67
|
|
|
|
68
|
|
|
if (!empty($result['delivery'])) { |
69
|
|
|
return $result['delivery']; |
70
|
|
|
} |
71
|
|
|
if (!empty($result['from']) && !empty($result['to'])) { |
72
|
|
|
$url = "http://postcalc.ru/web.php?Extended=1&Output=json&From={$result['from']}&Weight=3000&Valuation=0&Step=0&Date=22.11.2017&IBase=p&ProcessingFee=0&PackingFee=0&Round=0.01&VAT=1&To={$result['to']}"; |
73
|
|
|
$result = \Cache::get('russianPostTimeCalc', $url, function ($data) { |
74
|
|
|
return file_get_contents($data); |
75
|
|
|
}, 4 * 60 * 60); |
76
|
|
|
$result = json_decode($result, true); |
77
|
|
|
if (!empty($result['ПростаяБандероль']['СрокДоставки'])) { |
78
|
|
|
return ['min' => $result['ПростаяБандероль']['СрокДоставки']]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
return ['min' => 0, 'max' => 0]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
static function availablePayTypeGroups($cart) { |
|
|
|
|
87
|
|
|
return ['online']; |
88
|
|
|
} |
89
|
|
|
} |
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.