Skip to content

Commit

Permalink
Release2.4.1 - Queue error fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
nirav-patel-avalara committed Nov 4, 2022
1 parent 3e337b6 commit 5ff2291
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 44 deletions.
2 changes: 1 addition & 1 deletion BaseProvider/Framework/Rest/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function restCall(
$params
);
$body = (string) $response->getBody();
$JsonBody = json_decode($body, $getArray);
$JsonBody = json_decode((string)$body, $getArray);
if (($this->responseType == 'array') && (!is_null($JsonBody))) {
return $JsonBody;
} else {
Expand Down
2 changes: 1 addition & 1 deletion BaseProvider/Model/Queue/Consumer/ApiLogConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function consume(\ClassyLlama\AvaTax\BaseProvider\Api\Data\QueueInterface
$success = true;
$response = [];
$payload = $queueJob->getPayload();
$payload = json_decode($payload, true);
$payload = json_decode((string)$payload, true);
$client = $this->restClient;
if (count($payload) > 0) {
foreach($payload as $method=>$arguments) {
Expand Down
2 changes: 1 addition & 1 deletion Block/Adminhtml/Form/Field/CustomShippingMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CustomShippingMethods extends \Magento\Config\Block\System\Config\Form\Fie
*/
public static function parseSerializedValue($config)
{
$parsedValue = (array)json_decode($config ?? '', true);
$parsedValue = (array)json_decode((string)$config ?? '', true);
$shippingCodesById = [];

foreach ($parsedValue as $value) {
Expand Down
73 changes: 57 additions & 16 deletions Framework/Interaction/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,7 @@ protected function handleException($exception, $request = null, $logLevel = LOG_

if ($response !== null) {
try {
$logMessage = __(
'AvaTax connection error: %1',
trim(
array_reduce(
(array)$response['error']['details'],
function ($error, $detail) {
if (isset($detail['severity']) && $detail['severity'] !== 'Exception' && $detail['severity'] !== 'Error') {
return $error;
}

return $error . ' ' . $detail['description'];
},
''
)
)
);
$logMessage = $this->prepareErrorForHandleException($response);
} catch (\Exception $ex) {
$logMessage = __(
'AvaTax connection error: %1', $ex->getMessage());
Expand Down Expand Up @@ -205,6 +190,62 @@ function ($error, $detail) {
throw new AvataxConnectionException($logMessage, $exception);
}

/**
* prepare Error or Errors to Handle Exception
*
* @param array $response
* @return \Magento\Framework\Phrase
*/
protected function prepareErrorForHandleException(array $response)
{
$logMessage = __("AvaTax connection error");
if (isset($response['error'])) {
$logMessage = __(
'AvaTax connection error: %1',
trim(
array_reduce(
(array)$response['error']['details'],
function ($error, $detail) {
if (isset($detail['severity']) && $detail['severity'] !== 'Exception' && $detail['severity'] !== 'Error') {
return $error;
}

return $error . ' ' . $detail['description'];
},
''
)
)
);
}
if (isset($response['errors'])) {
$messages = '';
foreach($response['errors'] as $error){
if ($messages != '') {
$messages .= ' ';
}
if (is_string($error)) {
$messages .= ' '.$error;
}
if (is_array($error)) {
$messages .= trim(
array_reduce(
(array)$error,
function ($err1, $err2) {
return $err1 . ' ' . $err2;
},
''
)
);
}
}
$messages = trim($messages);
if ($messages != '') {
$logMessage = __("AvaTax connection error: %1", $messages);
}
}
return $logMessage;
}

/**
* Convert a simple object to a data object
*
Expand Down
4 changes: 3 additions & 1 deletion Framework/Interaction/Rest/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public function getTax( $request, $isProduction = null, $scopeId = null, $scopeT
'customerCode' => $request->getCustomerCode(),
'dateTime' => $request->getDate(),
]);

$this->customsConfigHelper->initNextIncrementForWithParameter();

$this->setTransactionDetails($transactionBuilder, $request);
$this->setLineDetails($transactionBuilder, $request);
$logContext['extra']['LineCount'] = $transactionBuilder->getCurrentLineNumber() - 1;
Expand Down Expand Up @@ -388,6 +389,7 @@ public function getTaxBatch(
'customerCode' => $request->getCustomerCode(),
'dateTime' => $request->getDate(),
]);
$this->customsConfigHelper->initNextIncrementForWithParameter();
$this->setTransactionDetails($transactionBuilder, $request);
try {
$this->setLineDetails($transactionBuilder, $request);
Expand Down
2 changes: 1 addition & 1 deletion Helper/AvaTaxClientWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function executeRequest($verb, $apiUrl, $guzzleParams)

// The body is already encoded as JSON, we need to decode it first so we don't double-encode it
if (is_string($guzzleParams['body'])) {
$guzzleParams['body'] = json_decode($guzzleParams['body']);
$guzzleParams['body'] = json_decode((string)$guzzleParams['body']);
}

$this->logger->debug(
Expand Down
16 changes: 10 additions & 6 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,14 @@ public function isAddressTaxable(\Magento\Framework\DataObject $address, $storeI
$isTaxable = true;
// Filtering just by country (not region)
if (!$this->getFilterTaxByRegion($storeId)) {
$countryFilters = explode(',', $this->getTaxCalculationCountriesEnabled($storeId));
$countryFilters = explode(',', (string)$this->getTaxCalculationCountriesEnabled($storeId));
$countryId = $address->getCountryId();
if (!in_array($countryId, $countryFilters)) {
$isTaxable = false;
}
// Filtering by region within countries
} else {
$regionFilters = explode(',', $this->getRegionFilterList($storeId));
$regionFilters = explode(',', (string)$this->getRegionFilterList($storeId));
$entityId = $address->getRegionId() ?: $address->getCountryId();
if (!in_array($entityId, $regionFilters)) {
$isTaxable = false;
Expand Down Expand Up @@ -1281,7 +1281,7 @@ public function getShippingTaxCode($store = null)
*/
public function getTableExemptions()
{
return explode(",", $this->scopeConfig->getValue(self::XML_PATH_AVATAX_ADVANCED_AVATAX_TABLE_EXEMPTIONS));
return explode(",", (string)$this->scopeConfig->getValue(self::XML_PATH_AVATAX_ADVANCED_AVATAX_TABLE_EXEMPTIONS));
}

/**
Expand All @@ -1292,7 +1292,7 @@ public function getConfigDataArray(string $configPath)
{
return explode(
',',
$this->scopeConfig->getValue(
(string)$this->scopeConfig->getValue(
$configPath
)
);
Expand Down Expand Up @@ -1336,10 +1336,14 @@ public function getTaxationPolicy($store = null)
*/
public function getVATTransport($store = null)
{
return stripslashes( $this->scopeConfig->getValue(
$VATTransportMapping = $this->scopeConfig->getValue(
self::XML_PATH_AVATAX_VAT_TRANSPORT,
ScopeInterface::SCOPE_STORE,
$store
) );
);
if (is_null($VATTransportMapping)) {
$VATTransportMapping = '';
}
return stripslashes((string)$VATTransportMapping);
}
}
23 changes: 20 additions & 3 deletions Helper/CustomsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function getGroundShippingMethods($store = null, $scopeType = ScopeInterf
{
return explode(
',',
$this->scopeConfig->getValue(
(string)$this->scopeConfig->getValue(
self::XML_PATH_AVATAX_CUSTOMS_GROUND_SHIPPING_METHODS,
$scopeType,
$store
Expand All @@ -156,7 +156,7 @@ public function getOceanShippingMethods($store = null, $scopeType = ScopeInterfa
{
return explode(
',',
$this->scopeConfig->getValue(
(string)$this->scopeConfig->getValue(
self::XML_PATH_AVATAX_CUSTOMS_OCEAN_SHIPPING_METHODS,
$scopeType,
$store
Expand All @@ -174,7 +174,7 @@ public function getAirShippingMethods($store = null, $scopeType = ScopeInterface
{
return explode(
',',
$this->scopeConfig->getValue(
(string)$this->scopeConfig->getValue(
self::XML_PATH_AVATAX_CUSTOMS_AIR_SHIPPING_METHODS,
$scopeType,
$store
Expand Down Expand Up @@ -262,6 +262,23 @@ public function getShippingTypeForMethod($method, $scopeId = null, $scopeType =
// Return default method
return $this->getDefaultShippingType($scopeId, $scopeType);
}

/**
* Init parameters next increment for each new transaction
*
* @return CustomsConfig
*/
public function initNextIncrementForWithParameter()
{
$this->withParameterIncrementId = 0;
return $this;
}

/**
* Next place for parameters in a transaction
*
* @return int
*/
public function getNextIncrementForWithParameter()
{
return $this->withParameterIncrementId++;
Expand Down
4 changes: 2 additions & 2 deletions Helper/Multishipping/Checkout/AddressValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function validateAddress(
$address
) {
$result = [];
if (in_array($address->getCountryId(), explode(',', $this->customerAddressBlock->getCountriesEnabled()))) {
if (in_array($address->getCountryId(), explode(',', (string)$this->customerAddressBlock->getCountriesEnabled()))) {
/** @var AddressInterface $result */
try {
$validAddress = $this->validation->validateAddress($address,
Expand All @@ -110,7 +110,7 @@ public function validateAddress(
'validAddressHtml' => $this->prepareAddressString($validAddress, $changedFields),
'originalAddressHtml' => $this->prepareAddressString($address),
'hasChoice' => $this->customerAddressBlock->getChoice(),
'instructions' => json_decode($this->customerAddressBlock->getInstructions()),
'instructions' => json_decode((string)$this->customerAddressBlock->getInstructions()),
];
}
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Source/CarrierMethodProviders/Fedex.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getConfiguredMethods()
try {
$allowedMethods = $this->carrier->getConfigData('allowed_methods');
if ($allowedMethods) {
return explode(",", $allowedMethods);
return explode(",", (string)$allowedMethods);
} else {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Source/CarrierMethodProviders/UPS.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getConfiguredMethods()
try {
$allowedMethods = $this->carrier->getConfigData('allowed_methods');
if ($allowedMethods) {
return explode(",", $allowedMethods);
return explode(",", (string)$allowedMethods);
} else {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Source/CarrierMethodProviders/USPS.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getConfiguredMethods()
try {
$allowedMethods = $this->carrier->getConfigData('allowed_methods');
if ($allowedMethods) {
return explode(",", $allowedMethods);
return explode(",", (string)$allowedMethods);
} else {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Source/CarrierShippingMethodsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function getCarrierConfig($configPath)
{
list($scopeType, $scopeId) = $this->getScopeInfo();

return explode(',', $this->scopeConfig->getValue($configPath, $scopeType, $scopeId));
return explode(',', (string)$this->scopeConfig->getValue($configPath, $scopeType, $scopeId));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/Source/RegionFilterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ protected function getCountryList()
$scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
}

return explode(',', $this->config->getTaxCalculationCountriesEnabled($scopeId, $scopeType));
return explode(',', (string)$this->config->getTaxCalculationCountriesEnabled($scopeId, $scopeType));
}
}
2 changes: 1 addition & 1 deletion Model/Plugin/CartTotalRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function afterGet(TotalRepository $subject, TotalsInterface $totals, $car
continue;
}

$message = json_decode($address->getAvataxMessages());
$message = json_decode((string)$address->getAvataxMessages());

if(is_array($message)) {
$messages[] = $message;
Expand Down
24 changes: 20 additions & 4 deletions Model/Queue/Processing/NormalProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ protected function processWithAvaTax(Queue $queue, $entity): GetTaxResponseInter
$e->getMessage()
);

$this->handleErrorofProcessWithAvaTax($message, $queue, $entity, $e);

throw new Exception($message, 0, $e);
}

return $processSalesResponse;
}

/**
* @param \Magento\Framework\Phrase $message
* @param Queue $queue
* @param InvoiceInterface|CreditmemoInterface $entity
* @param Exception $e
* @return void
*/
protected function handleErrorofProcessWithAvaTax(\Magento\Framework\Phrase $message, Queue $queue, $entity, Exception $e): void
{
try {
// Log the error
$this->avaTaxLogger->error(
$message,
Expand All @@ -185,11 +203,9 @@ protected function processWithAvaTax(Queue $queue, $entity): GetTaxResponseInter
// Update the queue record
// and add comment to order
$this->resetQueueingForProcessing($queue, $message, $entity);

throw new Exception($message, null, $e);
} catch (Exception $e) {
// do nothing
}

return $processSalesResponse;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Plugin/Checkout/Model/ShippingInformationManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function aroundSaveAddressInformation(

$enabledAddressValidationCountries = explode(
',',
$this->config->getAddressValidationCountriesEnabled($storeId)
(string)$this->config->getAddressValidationCountriesEnabled($storeId)
);
if (!in_array($shippingAddress->getCountryId(), $enabledAddressValidationCountries)) {
$shouldValidateAddress = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function getJoinDirectivesForType($extensibleEntityClass)
}

$extensibleInterfaceName = $this->extensionAttributesFactory->getExtensibleInterfaceName($extensibleEntityClass);
$stringArray = explode('\\', $extensibleInterfaceName);
$stringArray = explode('\\', (string)$extensibleInterfaceName);
$extensibleEntityName = strtolower(str_replace('Interface', '', end($stringArray)));

return array_filter(
Expand Down

0 comments on commit 5ff2291

Please sign in to comment.