Discriminating between a return and cancellation when no restock has been selected.

Solved
beerd
Tourist
4 1 0

During development on a new payment gateway we ran into an interesting (and frustrating) edge case:

Situation: there's an order with 1 product with quantity 2. 1 out the 2 is fulfilled, the other is still unfulfilled.

Action: a refund is done on one of the two; and the option "Restock item" is unselected.

Problem: We need to identify which line item is refunded; so we wait for the `refund/create` webhook.

Since "Restock item" is unselected, the payload field `refund_line_items[0].restock_type` has the value `no_restock`. Outside of that we have no data to discriminate whether the refunded item is the one that has been fulfilled or not.

See the following screenshot for what this data looks like. This is the payload when 1/2 is fulfilled and the other isn't, and we are refunding both at the same time. Can you spot which one is which?

The `line_item` field gives us the overall line, so it also says `quantity: 2` in there and isn't of added value for this problem.

 

image.png

 

 

 

0 Likes
beerd
Tourist
4 1 0

Any thoughts?

 

0 Likes
_JB
Shopify Staff
Shopify Staff
817 96 176

Hey @beerd,

I tried this and when I refund the unfulfilled item, the line_item object in the webhook payload shows these 2 fields:

23-51-4h2t0-ztwrr

When I refund the fulfilled item, the field show:

23-53-6ox7z-bgril

 

Hope that helps.

JB | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes
beerd
Tourist
4 1 0

Hi @_JB! Thanks so much for the response :). The forum did not allow me to post using the <code> tags, so I'm sorry for fields not being styled correctly.

The fulfillable_quantity and fulfillment_status provide valuable picture, but not enough information for fully determining the situation. Let me explain through an example.

Shop A

Has an order with a Product X with quantity 3. 1 out of the 3 has already been shipped (fulfilled) to the customer. Another 1 out of the 3 (unfulfilled) has been refunded already without restocking.

Shop A now decides to do a refund on the already shipped (fulfilled) item without restocking. The data at this time is as follows:

Order:

fulfillable_quantity: 1

fulfillment_status: "partial"

Refund:

quantity: 1

restock_type: "no_restock"

Shop B

Has an order with a Product Y with quantity 3. 1 out of 3 have already been shipped (fulfilled) to the customer. That shipped item has been refunded already without restocking.

Shop B decides to cancel an unfulfilled item without restocking. The data at this time is as follows:

Order:

fulfillable_quantity: 1

fulfillment_status: "partial"

Refund:

quantity: 1

restock_type: "no_restock"

There's no way to now determine upon receiving the refund/create webhook or the refund payment gateway request whether the refund was on an unfulfilled or fulfilled line item.

Since the previous refund was also without restocking we can't use the order's historic refund data. The only way I can see this working is if we somehow can get a fulfillable_quantity from before the refund. That way we can check whether the refund quantity equals the decrease in fulfillable_quantity.

But, the best solution would be to have an extra field in the refund_line_items field such as status: "fulfilled" | "unfulfilled"

I realize this seems like a very specific edge case, but we're working on a payment gateway with a lot of traffic and thus have to cover all our basis.

 

 

0 Likes
beerd
Tourist
4 1 0

This is an accepted solution.

We ended up using a snapshot of the order pre-refund. We could then diff the fulfillable_quantity and determine whether the refund is a refund on unfulfilled or fulfilled line items.

0 Likes