Skip to content

Commit

Permalink
fix: empty tariff get answer
Browse files Browse the repository at this point in the history
  • Loading branch information
vermorag committed Nov 20, 2024
1 parent 58ae2b1 commit 2860a0a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
12 changes: 2 additions & 10 deletions src/Actions/CalculateDeliveryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function __invoke(array $package, int $instanceID, bool $addTariffsToOffi
'address' => trim($package['destination']['city']),
'country_code' => trim($package['destination']['country']),
],
'packages' => $this->getPackagesData($package['contents']),
];

try {
Expand All @@ -59,9 +60,6 @@ public function __invoke(array $package, int $instanceID, bool $addTariffsToOffi
// do nothing
}

$deliveryParam['packages'] = $this->getPackagesData($package['contents']);
unset($deliveryParam['packages']['weight_orig_unit']);

if ($this->method->insurance) {
$deliveryParam['services'][] = [
'code' => 'INSURANCE',
Expand Down Expand Up @@ -192,13 +190,7 @@ function_exists('wcml_get_woocommerce_currency_option') ?
$deliveryParam['services'] = array_merge($serviceList, $deliveryParam['services'] ?? []);
}

$tariffInfo = $api->calculateGet($deliveryParam);

if (empty($tariffInfo)) {
return $tariff;
}

$cost = $tariffInfo['total_sum'];
$cost = $api->calculateGet($deliveryParam) ?? $tariff['cost'];

if (isset($rule['type'])) {
if ($rule['type'] === 'amount') {
Expand Down
14 changes: 10 additions & 4 deletions src/CdekApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function barcodeGet(string $uuid): ?array
* @throws ApiException
* @throws LegacyAuthException
*/
public function calculateGet(array $deliveryParam): array
public function calculateGet(array $deliveryParam): ?float
{
$request = [
'type' => $deliveryParam['type'],
Expand All @@ -94,12 +94,18 @@ public function calculateGet(array $deliveryParam): array
'services' => array_key_exists('services', $deliveryParam) ? $deliveryParam['services'] : [],
];

return HttpClient::sendJsonRequest(
$resp = HttpClient::sendJsonRequest(
"{$this->apiUrl}calculator/tariff",
'POST',
$this->tokenStorage->getToken(),
$request,
)->json();

if (empty($resp['total_sum'])) {
return null;
}

return (float)$resp['total_sum'];
}

/**
Expand Down Expand Up @@ -210,8 +216,8 @@ public function citySuggest(string $q, string $country): array
'GET',
$this->tokenStorage->getToken(),
[
'name' => $q,
'country_code' => $country,
'name' => $q,
'country_code' => $country,
],
)->json();
}
Expand Down

0 comments on commit 2860a0a

Please sign in to comment.