FosterCommerce / shipstation-connect

A plugin for Craft Commerce 2 that integrates with ShipStation.
https://plugins.craftcms.com/shipstationconnect
Other
7 stars 10 forks source link

Error with missing Country #24

Closed nathannerdymind closed 3 years ago

nathannerdymind commented 3 years ago

I am getting the error on ShipStation: An error occurred attempting to update orders: Error in XML. Reason: The 'Country' element is invalid - The value '' is invalid according to its datatype 'StringExactly2' - The actual length is less than the MinLength value.

I checked the shipping address on Craft Commerce orders and we have the country default to United States with the ISO set to US. I tried a few different options but nothing seems to work. Let me know if there is another custom setting that I need to do in order for this to work.

ShipStation Connect - 1.3.5 Craft Commerce - 3.2.3 Craft - 3.5.7

andrewcgregory commented 3 years ago

We saw this same issue. We were able to put a temporary fix in place (see https://github.com/FosterCommerce/shipstation-connect/issues/19 for the fix for updating the services/Xml.php to hardcode the country code to US). We have not yet identified what is causing the issue as our site only allows orders for the US and we have not found any order data that is incorrect.

sjcallender commented 3 years ago

Hey @nathannerdymind and @andrewcgregory, thanks for pointing this out. Somehow it slipped by our radar. We'll review and get back to you.

johnnynotsolucky commented 3 years ago

@nathannerdymind @andrewcgregory could you show us what an order with test data looks like which causes this error? Specifically the billing address and the shipping address.

andrewcgregory commented 3 years ago

@johnnynotsolucky I'm actually not sure what order is causing the issue. When it first showed up there was a large number of new orders in this store. We had checked through the order data and were not able to find any that stood out with abnormal billing or shipping info. Any recommendations on a way to find what order is actually triggering the issue?

nathannerdymind commented 3 years ago

@johnnynotsolucky We have the same temporary fix in place #19 . It was on every order with test shipping address info like: Shipping Address Nerdy Mind Testing street 80521 Fort Collins United States Colorado 0000000000

johnnynotsolucky commented 3 years ago

I've created a branch to try help debug.

If you could install the bugfix/address branch of the plugin and run the export again on your test install that would be great!

composer require fostercommerce/shipstationconnect:dev-bugfix/address

When there's an error it'll just spit out a JSON object with the order ID and its shipping and billing info. The address data should include the countryIso property.

andrewcgregory commented 3 years ago

@johnnynotsolucky I tried this on our staging environment.

Here are the steps/results:

  1. I made sure that we received the original error when trying to sync to ShipStation first.
  2. Installed your dev branch (had to adjust it to composer require fostercommerce/shipstationconnect:dev-bugfix/country, but I think that was your intention).
  3. Tried syncing the orders to ShipStation again.
  4. Received the error "An error occurred attempting to update orders: The remote server returned an error: (500) Internal Server Error." this time.
  5. I then re-installed v1.3.5 of shipstationconnect and tested and received the original "An error occurred attempting to update orders: Error in XML. Reason: The 'Country' element is invalid - The value '' is invalid according to its datatype 'StringExactly2' - The actual length is less than the MinLength value." error again.

Here is a screenshot of the error from within ShipStation:

Screen Shot 2021-02-13 at 2 14 53 PM

Just to be clear, the error we receive (with this test and previously) is in ShipStation itself, not within Craft.

johnnynotsolucky commented 3 years ago

Apologies @andrewcgregory, I thought you were testing the export locally by calling the endpoint directly. On your staging environment the error log should be available in your logs.

You can search for "Error processing action": https://github.com/FosterCommerce/shipstation-connect/blob/b618ac7e370f0780802085c181f2450d1e37449f/src/controllers/OrdersController.php#L82

Just to be clear, the error we receive (with this test and previously) is in ShipStation itself, not within Craft.

Got it. It seems that for some reason the country ISO isn't being sent to ShipStation from the plugin. I'm not sure why that is, and I'm hoping the log data will point us in the right direction to resolve this.

andrewcgregory commented 3 years ago

@johnnynotsolucky No problem. I pulled the logs and was able to find this error. I don't want to share all of the logs here, but I cleared out the personal info and I'll share what I can below. I can send you a link to full copy through an email or DM if you're in the Craft Discord channel if needed.

Partial log: error-log.txt

Address looks fine in the control panel:

Screen Shot 2021-02-19 at 10 18 41 AM
andrewcgregory commented 3 years ago

I did some more digging into the actual database tables today and it looks like we had one old customer that somehow did have addresses with a countryId = null and when they placed a recent order that pulled in this address it caused the syncing issue. We were able to get this address updated to include the country as it should have previously, and everything appears to be working correctly.

johnnynotsolucky commented 3 years ago

Thank you for the logs @andrewcgregory. @nathannerdymind were you able to confirm whether the country is not set on some orders?

You can use this query to see which orders don't have a country set in either shipping or billing address:

SELECT
    orders.id
FROM commerce_orders orders
JOIN commerce_addresses shippingAddresses ON orders.shippingAddressId = shippingAddresses.id
JOIN commerce_addresses billingAddresses ON orders.billingAddressId = billingAddresses.id
WHERE (
    shippingAddresses.countryId IS NULL
    OR billingAddresses.countryId IS NULL
)
;

We have an internal task to overhaul the code which validates orders and generates the XML for ShipStation. Part of that work would be to include better validation on the data so that these errors can be caught and easily resolved without much hassle.

benface commented 3 years ago

Closing for inactivity. @nathannerdymind, feel free to reopen if you are still experiencing this issue after checking that you don’t have any order where countryId = null.

aeu commented 2 years ago

We are getting this same error, but in our case it is happening with orders that don't have an address at all.