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
|
|
View Code Duplication |
foreach ($cart->delivery->fields as $field) { |
|
|
|
|
22
|
|
|
if ($field->code === 'index') { |
23
|
|
|
if (!empty($_POST['deliveryFields'][$field->id]) && is_string($_POST['deliveryFields'][$field->id])) { |
24
|
|
|
$city = $_POST['deliveryFields'][$field->id]; |
25
|
|
|
} elseif (isset($cart->deliveryInfos[$field->id])) { |
26
|
|
|
$city = $cart->deliveryInfos[$field->id]->value; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
if (!$city) { |
31
|
|
|
$fieldInfo = Field::get('deliveryfield_city', 'code'); |
32
|
|
|
$field = \Ecommerce\Delivery\Field::get('city', 'code'); |
33
|
|
|
if (isset($cart->infos[$fieldInfo->id])) { |
34
|
|
|
$item = \Ecommerce\Delivery\Field\Item::get([['id', $cart->infos[$fieldInfo->id]->value], ['delivery_field_id', $field->id]]); |
35
|
|
|
if ($item) { |
36
|
|
|
$data = json_decode($item->data, true); |
37
|
|
|
if (!empty($data['PostCodeList'])) { |
38
|
|
|
$city = explode(',', $data['PostCodeList'])[0]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
if (!$city) { |
44
|
|
|
return []; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
$senderCity = '101000'; |
48
|
|
|
|
49
|
|
|
$data = [ |
50
|
|
|
'object' => 4030, |
51
|
|
|
'weight' => '1000', |
52
|
|
|
'date' => date('Ymd'), |
53
|
|
|
//'sumoc' => $cart->itemsSum()->sums[0], |
|
|
|
|
54
|
|
|
'from' => $senderCity, |
55
|
|
|
'to' => $city, |
56
|
|
|
'delivery' => 1, |
57
|
|
|
]; |
58
|
|
|
$result = \Cache::get('russianPostCalc', $data, function ($data) { |
59
|
|
|
return file_get_contents('http://tariff.russianpost.ru/tariff/v1/calculate?json&' . http_build_query($data)); |
60
|
|
|
}); |
61
|
|
|
return json_decode($result, true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
static function calcPrice($cart) { |
|
|
|
|
65
|
|
|
$result = static::request($cart); |
66
|
|
|
if (empty($result['pay'])) { |
67
|
|
|
return new \Money\Sums([$cart->delivery->currency_id => 0]); |
68
|
|
|
} |
69
|
|
|
$sum = $result['pay']; |
70
|
|
|
return new \Money\Sums([$cart->delivery->currency_id => round($sum / 100 * 1.1, 2)]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
static function deliveryTime($cart) { |
|
|
|
|
74
|
|
|
$result = static::request($cart); |
75
|
|
|
if (!empty($result['delivery'])) { |
76
|
|
|
return $result['delivery']; |
77
|
|
|
} |
78
|
|
|
return ['min' => 0, 'max' => 0]; |
79
|
|
|
} |
80
|
|
|
static function availablePayTypeGroups($cart) { |
|
|
|
|
81
|
|
|
return ['online']; |
82
|
|
|
} |
83
|
|
|
} |
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.