Created
March 1, 2024 13:53
-
-
Save danielborzov/04a380672aca4bcb7e1cb0d4d2420258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/addons/rus_sdek/Tygh/Shippings/Services/Sdek.php b/app/addons/rus_sdek/Tygh/Shippings/Services/Sdek.php | |
index fd551b9079..6a4668e60d 100644 | |
--- a/app/addons/rus_sdek/Tygh/Shippings/Services/Sdek.php | |
+++ b/app/addons/rus_sdek/Tygh/Shippings/Services/Sdek.php | |
@@ -15,6 +15,7 @@ | |
namespace Tygh\Shippings\Services; | |
use Tygh\Enum\Addons\RusSdek\DeliveryPointType; | |
+use Tygh\Enum\YesNo; | |
use Tygh\Tygh; | |
use Tygh\Http; | |
use Tygh\Registry; | |
@@ -34,6 +35,11 @@ class Sdek implements IService, IPickupService | |
*/ | |
private $_allow_multithreading = false; | |
+ /** | |
+ * @var string Access token | |
+ */ | |
+ private $access_token; | |
+ | |
/** | |
* The currency in which the carrier calculates shipping costs. | |
* | |
@@ -44,8 +50,6 @@ class Sdek implements IService, IPickupService | |
/** @var array $shipping_info Shipping data */ | |
protected $shipping_info; | |
- private $version = "1.0"; | |
- | |
/** | |
* Stack for errors occured during the preparing rates process | |
* | |
@@ -86,6 +90,57 @@ class Sdek implements IService, IPickupService | |
public $goods = []; | |
+ /** | |
+ * Updates the access token if necessary | |
+ * | |
+ * @return void | |
+ */ | |
+ protected function getAccessToken() | |
+ { | |
+ if (!empty($this->access_token)) { | |
+ return; | |
+ } | |
+ | |
+ $shipping_settings = $this->shipping_info['service_params']; | |
+ | |
+ $params = [ | |
+ 'grant_type' => 'client_credentials', | |
+ 'client_id' => $shipping_settings['authlogin'], | |
+ 'client_secret' => $shipping_settings['authpassword'], | |
+ ]; | |
+ | |
+ $extra = [ | |
+ 'headers' => [ | |
+ 'Content-type' => 'application/x-www-form-urlencoded', | |
+ ], | |
+ ]; | |
+ | |
+ $url = 'https://api.cdek.ru/v2/oauth/token?parameters'; | |
+ | |
+ $response = Http::post($url, $params, $extra); | |
+ $data = json_decode($response, true); | |
+ if (empty($data['access_token'])) { | |
+ return; | |
+ } | |
+ | |
+ $this->access_token = $data['access_token']; | |
+ } | |
+ | |
+ /** | |
+ * Provides content of Access Request. | |
+ * | |
+ * @return array<array-key, string> Access Request | |
+ */ | |
+ private function getAccessRequest(): array | |
+ { | |
+ $this->getAccessToken(); | |
+ | |
+ return [ | |
+ 'Authorization: Bearer ' . $this->access_token, | |
+ 'Content-Type: application/json', | |
+ ]; | |
+ } | |
+ | |
/** | |
* Returns shipping service information | |
* @return array information | |
@@ -258,37 +313,32 @@ class Sdek implements IService, IPickupService | |
$this->city_id = $_code = RusSdek::cityId($location); | |
$_code_sender = $shipping_settings['from_city_id']; | |
- $url = 'https://api.cdek.ru/calculator/calculate_price_by_json.php'; | |
- $r_url = 'http://lk.cdek.ru:8080/calculator/calculate_price_by_json.php'; | |
+ $url = 'https://api.cdek.ru/v2/calculator/tariff'; | |
$post = []; | |
- $post['version'] = isset($this->version) ? $this->version : ''; | |
if (!empty($shipping_settings['dateexecute'])) { | |
$timestamp = TIME + $shipping_settings['dateexecute'] * SECONDS_IN_DAY; | |
- $dateexecute = date('Y-m-d', $timestamp); | |
+ $dateexecute = date(DATE_ISO8601, $timestamp); | |
} else { | |
- $dateexecute = date('Y-m-d'); | |
- } | |
- | |
- $post['dateExecute'] = $dateexecute; | |
- | |
- if (!empty($shipping_settings['authlogin'])) { | |
- $post['authLogin'] = $shipping_settings['authlogin']; | |
- $post['secure'] = !empty($shipping_settings['authpassword']) ? md5($post['dateExecute'] . '&' . $shipping_settings['authpassword']) : ''; | |
+ $dateexecute = date(DATE_ISO8601); | |
} | |
- $post['senderCityId'] = (int) $_code_sender; | |
- $post['receiverCityId'] = (int) $_code; | |
- $post['tariffId'] = $shipping_settings['tariffid']; | |
+ $post['date'] = $dateexecute; | |
+ $post['from_location'] = [ | |
+ 'code' => (int) $_code_sender | |
+ ]; | |
+ $post['to_location'] = [ | |
+ 'code' => (int) $_code | |
+ ]; | |
+ $post['tariff_code'] = $shipping_settings['tariffid']; | |
- $post['goods'] = $this->goods; | |
+ $post['packages'] = $this->goods; | |
$request_data = array( | |
'method' => 'post', | |
'url' => $url, | |
'data' => json_encode($post), | |
- 'r_url' => $r_url, | |
); | |
return $request_data; | |
@@ -301,26 +351,21 @@ class Sdek implements IService, IPickupService | |
*/ | |
public function getSimpleRates() | |
{ | |
- $response = array(); | |
+ $response = []; | |
$data = $this->getRequestData(); | |
if (!empty($data)) { | |
$key = md5($data['data']); | |
$sdek_data = fn_get_session_data($key); | |
- $extra = array( | |
- 'headers' => array( | |
- 'Content-Type: application/json' | |
- ), | |
- 'timeout' => SDEK_TIMEOUT, | |
- 'log_preprocessor' => '\Tygh\Http::unescapeJsonResponse' | |
- ); | |
- | |
if (empty($sdek_data)) { | |
+ $extra = [ | |
+ 'headers' => $this->getAccessRequest(), | |
+ 'timeout' => SDEK_TIMEOUT, | |
+ 'log_preprocessor' => '\Tygh\Http::unescapeJsonResponse' | |
+ ]; | |
+ | |
$response = Http::post($data['url'], $data['data'], $extra); | |
- if (empty($response)) { | |
- $response = Http::post($data['r_url'], $data['data'], $extra); | |
- } | |
fn_set_session_data($key, $response); | |
} else { | |
$response = $sdek_data; | |
@@ -338,17 +383,16 @@ class Sdek implements IService, IPickupService | |
*/ | |
public function processResponse($response) | |
{ | |
- $return = array( | |
+ $return = [ | |
'cost' => false, | |
'error' => false, | |
- ); | |
+ ]; | |
if (!empty($response)) { | |
$result = json_decode($response); | |
$result_array = json_decode(json_encode($result), true); | |
- if (empty($this->_error_stack) && !empty($result_array['result'])) { | |
- | |
+ if (empty($this->_error_stack) && !empty($result_array)) { | |
$rates = $this->_getRates($result_array); | |
if (empty($rates)) { | |
@@ -381,12 +425,12 @@ class Sdek implements IService, IPickupService | |
{ | |
$rates = array(); | |
$sdek_delivery = fn_get_schema('sdek', 'sdek_delivery', 'php', true); | |
- if (!empty($response['result']['price'])) { | |
- $rates['price'] = $response['result']['price']; | |
- if (!empty($response['result']['deliveryPeriodMin']) && !empty($response['result']['deliveryPeriodMax'])) { | |
+ if (!empty($response['total_sum'])) { | |
+ $rates['price'] = $response['total_sum']; | |
+ if (!empty($response['period_min']) && !empty($response['period_max'])) { | |
$plus = $this->shipping_info['service_params']['dateexecute']; | |
- $min_time = $plus + $response['result']['deliveryPeriodMin']; | |
- $max_time = $plus + $response['result']['deliveryPeriodMax']; | |
+ $min_time = $plus + $response['period_min']; | |
+ $max_time = $plus + $response['period_max']; | |
if ($min_time == $max_time) { | |
$date = $min_time . ' ' . __('days'); | |
} else { | |
@@ -399,10 +443,10 @@ class Sdek implements IService, IPickupService | |
$rec_city_code = $this->city_id; | |
$tarif_id = $this->shipping_info['service_params']['tariffid']; | |
- if (!empty($rec_city_code) && (!empty($sdek_delivery[$tarif_id]['terminals']) && $sdek_delivery[$tarif_id]['terminals'] == 'Y') && $tarif_id == $response['result']['tariffId']) { | |
- $params = array( | |
+ if (!empty($rec_city_code) && (!empty($sdek_delivery[$tarif_id]['terminals']) && YesNo::toBool($sdek_delivery[$tarif_id]['terminals']))) { | |
+ $params = [ | |
'cityid' => $rec_city_code | |
- ); | |
+ ]; | |
if (!empty($sdek_delivery[$tarif_id]['postamat'])) { | |
$params['type'] = DeliveryPointType::POSTAMAT; | |
} else { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment