Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SalesTaxService.php - Request could not be completed #34

Closed
curtismorte opened this issue Jun 19, 2021 · 5 comments
Closed

SalesTaxService.php - Request could not be completed #34

curtismorte opened this issue Jun 19, 2021 · 5 comments

Comments

@curtismorte
Copy link

curtismorte commented Jun 19, 2021

Craft Version: 3.6.14
Craft Commerce Version: 3.2.14.1
Craft Avatax Version: 2.1.3
PHP Version: 8.0.2

When posting to action commerce/customer-addresses/save, I get the following error:

Exception – yii\base\Exception
Request could not be completed in .../vendor/surprisehighway/craft-avatax/src/services/SalesTaxService.php at line 723

The issue is that my tax code value of PA3000100 is somehow being converted to {"label":"PA3000100","value":"PA3000100","selected":true,"valid":true} from $t->withLine().

I realize $t is actually an instance of vendor/avalara/avataxclient/src/TransactionBuilder.php.

I was able to verify this issue by using the built in logger for the plugin. These were my modifications to SalesTaxService.php:614 and TransactionBuilder.php:446

# SalesTaxService.php#L614
Avatax::info(__FUNCTION__.'():  $taxCode is '.$taxCode);

// amount, $quantity, $itemCode, $taxCode)
$t = $t->withLine(
    $lineItem->subtotal,    // Total amount for the line item
    $lineItem->qty,         // Quantity
    $itemCode,              // Item Code
    $taxCode                // Tax Code - Default or Custom Tax Code.
);
Avatax::info(__FUNCTION__.'():  $taxCode is '.$taxCode);

# I added the getModel method for logging purposes
Avatax::info(json_encode($t->getModel()));
# TransactionBuilder.php:446
Avatax::info(__FUNCTION__.'(): Inspect tax code value before creating array: '.$taxCode);
$l = [
    'number' => $lineNumber,
    'quantity' => $quantity,
    'amount' => $amount,
    'taxCode' => $taxCode,
    'itemCode' => $itemCode
];
Avatax::info(__FUNCTION__.'(): Inspect tax code value after creating array: '.$taxCode);
Avatax::info(__FUNCTION__.'(): Value of $l that was just created '.json_encode($l));

Here is the corresponding log entries:

[Info] getTotalTax(): $taxCode is PA3000100
[Info] withLine(): Value of $l that was just created {"number":1,"quantity":6,"amount":3090,"taxCode":{"label":"PA3000100","value":"PA3000100","selected":true,"valid":true},"itemCode":"NBL-244-UED90-45L"}
[Info] withLine(): Inspect tax code value after creating array: PA3000100
[Info] withLine(): Inspect tax code value before creating array: PA3000100
[Info] getTotalTax(): $taxCode is PA3000100

Note the logs are in reverse order since they were coming from Craft's Control Panel

The problem is the value of $taxCode somehow turns into {"label":"PA3000100","value":"PA3000100","selected":true,"valid":true}.

A solution found during testing

I figured out that if you wrap the $taxCode parameter with strval(), then the issue doesn't happen and the request succeeds for TransactionBuilder.php:446

$l = [
            'number' => $lineNumber,
            'quantity' => $quantity,
            'amount' => $amount,
            'taxCode' => strval($taxCode),
            'itemCode' => $itemCode
        ];

Here is the full stack trace:

  1. in /vendor/surprisehighway/craft-avatax/src/services/SalesTaxService.phpat line 723
  2. in /vendor/surprisehighway/craft-avatax/src/services/SalesTaxService.php at line 122– surprisehighway\avatax\services\SalesTaxService::getTotalTax()
  3. in /vendor/surprisehighway/craft-avatax/src/adjusters/AvataxTaxAdjuster.php at line 24– surprisehighway\avatax\services\SalesTaxService::createSalesOrder()
  4. in /vendor/craftcms/commerce/src/elements/Order.php at line 1744– surprisehighway\avatax\adjusters\AvataxTaxAdjuster::adjust()
  5. in /vendor/craftcms/commerce/src/elements/Order.php at line 1819– craft\commerce\elements\Order::recalculate()
  6. in /vendor/craftcms/cms/src/services/Elements.php at line 2514– craft\commerce\elements\Order::afterSave()
  7. in /vendor/craftcms/cms/src/services/Elements.php at line 758– craft\services\Elements::_saveElementInternal()
  8. in /vendor/craftcms/commerce/src/services/Carts.php at line 109– craft\services\Elements::saveElement()
  9. in /vendor/craftcms/commerce/src/controllers/CustomerAddressesController.php at line 120– craft\commerce\services\Carts::getCart()
  10. craft\commerce\controllers\CustomerAddressesController::actionSave()
@curtismorte
Copy link
Author

Thinking about this a bit further, my solution that I found was to force the variable to give its current value (by calling strval()). I'm wondering if the variable you're passing as $taxCode to $t->withLine() is being changed somewhere else within your code? I didn't see any & prefixing variables to pass by reference however.

@curtismorte
Copy link
Author

Today I updated my composer packages for this Craft, Commerce, and this package.

The issue as I described is still there, but the exception that is thrown no longer occurs.

Here are my updated packages:
Craft Version: 3.6.17
Craft Commerce Version: 3.2.14.1
Craft Avatax Version: 2.1.4

@imagehat
Copy link
Collaborator

@curtismorte - For 2.1.4 we changed how errors are handled and the plugin now still logs the error now also with the request and response, but will fail silently instead of throwing an exception. But I'll see if we can look into your issue with the Transaction Builder.

Also, if you don't need or want to use Avalara's address validation service you can disable that in the plugin settings. That's the reason the plugin is being called when saving a default address.

@curtismorte
Copy link
Author

curtismorte commented Jul 6, 2021

Hey Guys,
I tracked this issue down.

An adjuster that was adding adjustments per LineItem was the source of the problem. Within SalesTaxService.php there is a condition that checks to see if the product has it's own tax code specified. Per the documentation, when you setup the product field with handle avataxTaxCode, you rely on the product field being of type "plaintext" for your implementation. We were using the type dropdown for the handle avataxTaxCode to be able to allow product / inventory managers to be able to select from a predefined list of tax codes as opposed to typing a string for each product.

Here is a simple fix for SalesTaxService.php:

// ------------------------------------------------------------------
// Null Coalescing - Concise.
// ------------------------------------------------------------------
// check to see if there is an Avatax Tax Code override specified
if(!empty($adjustmentLineItem->purchasable->product->avataxTaxCode))
{
    // Usage specific tor avataxTaxCode being a type "dropdown" or "plaintext"
    $discountCode = $adjustmentLineItem->purchasable->product->avataxTaxCode->value ?? $adjustmentLineItem->purchasable->product->avataxTaxCode;
}

// ------------------------------------------------------------------
// Simple Conditional - Easy to read
// ------------------------------------------------------------------
// check to see if there is an Avatax Tax Code override specified
if(!empty($adjustmentLineItem->purchasable->product->avataxTaxCode))
{
    // Usage specific tor avataxTaxCode being a type "plaintext"
    $discountCode = $adjustmentLineItem->purchasable->product->avataxTaxCode;
    // Usage specific tor avataxTaxCode being a type "dropdown"
    if(isset($adjustmentLineItem->purchasable->product->avataxTaxCode->value)) {
        $discountCode = $adjustmentLineItem->purchasable->product->avataxTaxCode->value;
    }
}

@imagehat
Copy link
Collaborator

imagehat commented Jul 6, 2021

@curtismorte,

Thanks for this! Being able to use a dropdown makes sense, and at one I feel like that worked, but maybe not.

At any rate I'll add that check and push an update in the next day or so, hopefully tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants