Correct way to calculation refunds of an order?

Solved
Highlighted
New Member
7 0 0

I'm an app developer and we're now calculating refunds on order basis for our users. Noticed there are three fields related in "order.refunds":

- refund_line_items: I assume items here are fully refunded of their sold price & tax. For example if I see an item with subtotal 19.95 and tax 1.65, I assume the refund is 21.6.

- order_adjustment: from the docs I see this includes "shipping cost" and "differences between calculated and actual refund amounts". Shipping cost is straightforward, so like if there's a $10 cost I'll add $10 to the total refund amount. For the 2nd kind of entries I haven't found an example yet, but I assume I'll need to subtract the amount of it from the total refund amount.

- transactions: this looks somehow duplicate. Is this the actual refund made to the buyer, like total of the above? I also see gift card entries in this field, should I ignore it when calculating?

0 Likes
Highlighted
Shopify Partner
349 25 52

Hi @kenxu0927 ,

It's not clear what your question is. The correct way is how it's described in the docs and what's been proven in the real world. Why are you calculating your refunds when there's an API for that?

Calculations API 

POST /admin/api/2020-07/orders/450789469/refunds/calculate.json

This is straightforward, you're going to send Calculations API the items you want to refund. An example of such a request below:

{
  "refund": {
    "shipping": {
      "full_refund": true
    },
    "refund_line_items": [
      {
        "line_item_id": 518995019,
        "quantity": 1,
        "restock_type": "no_restock"
      }
    ]
  }
}

 

You take the transactions response from Calculations and POST to Refunds endpoint. 

 

Regards,

Sam Bazargan - Owner

 

 

Custom Apps and Themes Support | Email us: hello@achieveapplabs.com | Follow us on Twitter @achieveapplabs | New Monthly Support by Achieve Applabs www.achieveapplabs.com
0 Likes
Highlighted
New Member
7 0 0

Hi Sam,

My question is, based on the "refunds" field of an order object (returned from the GET /orders/{id}.json endpoint), how could I get the "total refund amount" of this order. We are not making refunds on behalf of our users (the sellers) but extracting the total refund amount from the order object. I checked refund calculation endpoint and looks like it returns an almost identical format to order.refunds, not a total amount.

One example with real data is like:

[
  {
        "id": 661666988145,
        "order_id": 2415770468465,
        "refund_line_items": [
            {
                "id": 164205625457,
                "line_item_id": 5145381142641,
                "quantity": 1,
                "restock_type": "cancel",
                "location_id": 35927359601,
                "subtotal": "39.90",
                "total_tax": "4.27",
            }
        ],
        "transactions": [],
        "order_adjustments": []
    },
    {
        "id": 668569698417,
        "order_id": 2415770468465,
        "refund_line_items": [
            {
                "id": 171796693105,
                "line_item_id": 5145381109873,
                "quantity": 2,
                "restock_type": "cancel",
                "location_id": 35989192817,
                "subtotal": "139.80",
                "total_tax": "14.97",
            },
            {
                "id": 171796725873,
                "line_item_id": 5145381142641,
                "quantity": 1,
                "restock_type": "cancel",
                "location_id": 35989192817,
                "subtotal": "39.90",
                "total_tax": "4.28",
            }
        ],
        "transactions": [
            {
                "id": 3094383951985,
                "order_id": 2415770468465,
                "kind": "refund",
                "gateway": "gift_card",
                "status": "success",
                "message": null,
                "created_at": "2020-07-07T21:11:19+02:00",
                "test": false,
                "authorization": null,
                "location_id": null,
                "user_id": null,
                "parent_id": 2975174230129,
                "processed_at": "2020-07-07T21:11:19+02:00",
                "device_id": null,
                "receipt": {
                    "gift_card_id": 424086896753,
                    "gift_card_last_characters": "2h6c"
                },
                "error_code": null,
                "source_name": "web",
                "amount": "100.00",
                "currency": "SEK",
                "admin_graphql_api_id": "gid://shopify/OrderTransaction/3094383951985"
            },
            {
                "id": 3094383984753,
                "order_id": 2415770468465,
                "kind": "void",
                "gateway": "Swish",
                "status": "success",
                "message": "Marked the Swish payment as voided",
                "created_at": "2020-07-07T21:11:20+02:00",
                "test": false,
                "authorization": null,
                "location_id": null,
                "user_id": null,
                "parent_id": 2975174295665,
                "processed_at": "2020-07-07T21:11:20+02:00",
                "device_id": null,
                "receipt": {},
                "error_code": null,
                "source_name": "web",
                "amount": "0.00",
                "currency": "SEK",
                "admin_graphql_api_id": "gid://shopify/OrderTransaction/3094383984753"
            }
        ],
        "order_adjustments": [
            {
                "id": 102158565489,
                "order_id": 2415770468465,
                "refund_id": 668569698417,
                "amount": "-79.46",
                "tax_amount": "-9.54",
                "kind": "shipping_refund",
                "reason": "Shipping refund",
                "amount_set": {
                    "shop_money": {
                        "amount": "-79.46",
                        "currency_code": "SEK"
                    },
                    "presentment_money": {
                        "amount": "-79.46",
                        "currency_code": "SEK"
                    }
                },
                "tax_amount_set": {
                    "shop_money": {
                        "amount": "-9.54",
                        "currency_code": "SEK"
                    },
                    "presentment_money": {
                        "amount": "-9.54",
                        "currency_code": "SEK"
                    }
                }
            }
        ]
    }
]

Should I use the line item totals (39.9+4.27)*1 + (139.8+14.97)*2 + (39.9+4.28)*1 plus shipping (79.46+9.54) as the final amount, or should I use another way to calculate? Thanks!

0 Likes
Highlighted
Shopify Partner
349 25 52

This is an accepted solution.

@kenxu0927 ,

 

No you would not multiply the subtotals with the quantity. Is it not feasible to just cycle through only transactions adding the amount field? That way you know that's what's actually been credited to customer.

Regards,

Sam Bazargan - Owner

Custom Apps and Themes Support | Email us: hello@achieveapplabs.com | Follow us on Twitter @achieveapplabs | New Monthly Support by Achieve Applabs www.achieveapplabs.com
0 Likes
Highlighted
New Member
7 0 0

Yeah sorry I made a mistake in using subtotals. So if go back to the above example... does it mean the refund is simply a gift card of 100 SEK? I was just confused about the number since those in other fields don't add up to 100.

0 Likes
Highlighted
Shopify Partner
349 25 52

There’s nothing in the docs that would imply an order_adjustment results in a transaction. So your answer would be yes only the gift card amount was credited to the customer.

Interesting enough order_adjustments are read only and can be created automatically by Shopify to account for discrepancies.

Regards,

 

Sam Bazargan - Owner

Custom Apps and Themes Support | Email us: hello@achieveapplabs.com | Follow us on Twitter @achieveapplabs | New Monthly Support by Achieve Applabs www.achieveapplabs.com
0 Likes