Search code examples
shopifyshopify-api

How to cancel and restock order using Shopify API?


I'm building a simple order validation microservice for my Shopify store. Basically, the microservice will validate new orders and automatically cancel them is there is some kind of validation error (compliance reasons). I'm looking at the Shopify orders API, but it's still not completely clear to me the proper way to use it.

https://shopify.dev/docs/admin-api/rest/reference/orders/order#cancel-2020-01

This microservice would be triggered by the "Order created" web hook, so there is no concern that the order is partially or completely fulfilled.

What I want to do is to cancel the order, refund the payment to the customer, add a note, and restock the items. How can I do this with the Shopify API?


Solution

  • Well you need to cancel the order, for that you can use:

    POST /admin/api/2020-01/orders/#{order_id}/cancel.json
    

    After that you must calculate the refund:

    POST /admin/api/2020-01/orders/#{order_id}/refunds/calculate.json
    

    And then refund it using:

    POST /admin/api/2020-01/orders/#{order_id}/refunds.json
    

    Look in to restock_type for returning the items to the total count.

    An at any point you can make a request to:

    PUT /admin/api/2020-01/orders/#{order_id}.json
    

    in order to add the note.

    Please have in mind that you need to take in consideration the currency and location_id if the store is using multi-locations.

    And that should be the whole process.