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.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.
Solved! Go to the solution
I tried this and when I refund the unfulfilled item, the line_item object in the webhook payload shows these 2 fields:
When I refund the fulfilled item, the field show:
Hope that helps.
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.
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:
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:
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.