FROM CACHE - en_header

How to mark order as paid through REST Admin API

oskarscl
New Member
1 0 0

Hi!

I have a webhook in shopify that calls our backend when an order is paid. However, I realized that this is not triggered if you create the order through the Admin REST API as paid. 

So my solution was to create the order as financial_status=pending, and then mark it as payed to trigger this webhook.

I quickly realized that simply setting financial_status to paid was not allowed and that you shoud use orders/#orderid/transactions to create a transaction that will in turn update the financial status.

However, I can't find information on how to do this. The examples include "parent_id", which I don't know how to get.

How would I recreate exactly what is happening when you press "Mark as Paid" in the Shopify web admin with the REST API?

OR

Can I somehow make Shopify trigger the "Order payment" webhook when an order is created with financial_status=paid.

Thanks!

Replies 8 (8)
SahinVardar
Shopify Partner
1 0 3

Hey,

 

it took me while to figure this out and I can feel your pain brother. So here we go. 

 

Here an overview of the three steps you have to do. After that I will go through the steps through an example which will make the steps more clear.

 

1. You have to create the order with an initial successful authorization transaction. This will create a transaction for your newly created order and this is where you will get the parent_id.

2. Query the transaction for your newly created order. Now take the id of that transaction which will act as the paren_id for the next step.

3. Create a transaction which captures transaction and mark it as paid.

 

Here an example

 

Step 1: Create order with an initial transaction successfully authorized transaction and set the financial status to pending.

 

endpoint: POST admin/orders.json

 

{
    "order": {
        "email": "",
        "financial_status": "pending",
        "currency": "EUR",
        "fulfillment_status": "fulfilled",
        "send_receipt": false,
        "send_fulfillment_receipt": false,
        "inventory_behaviour": "bypass",
        "transactions": [
            {
                "amount": "66.00",
                "kind": "authorization",
                "status": "success"
            }
        ],
        "line_items": [
            {
                "variant_id": 21970810568804,
                "quantity": "1"
            }
        ],
        "tags": "imported"
    }
}

The response to the request will include the id of the new order you have created. Something like this.

 

{
    "order": {
        "id": 740399743076
       }
}

 

Step 2: Query the transaction of that order to get the transaction id which we will later use as as an parent_id to mark the order as paid

 

endpoint: GET admin/orders/740399743076/transactions.json

 

The response will look like this

 

{
    "transactions": [
        {
            "id": 938542432356,
            "order_id": 740399743076,
            "kind": "authorization",
            "gateway": "",
            "status": "success",
            "message": null,
            "created_at": "2019-01-23T07:31:32-12:00",
            "test": false,
            "authorization": null,
            "location_id": null,
            "user_id": null,
            "parent_id": null,
            "processed_at": "2019-01-23T07:31:32-12:00",
            "device_id": null,
            "receipt": {},
            "error_code": null,
            "source_name": "2688293",
            "amount": "66.00",
            "currency": "EUR",
            "admin_graphql_api_id": "gid://shopify/OrderTransaction/938542432356"
        }
    ]
}

Step 3: Mark the order as paid by capturing the transaction 

 

endpoint: POST admin/orders/740399743076/transactions.json

 

{
  "transaction": {
    "kind": "capture",
    "gateway": "manual",
    "amount": "66.00",
    "parent_id": "938542432356",
    "status": "success",
    "currency": "EUR"
  }
}

The response will be something like this

 

{
    "transaction": {
        "id": 938547282020,
        "order_id": 740399743076,
        "kind": "capture",
        "gateway": "",
        "status": "success",
        "message": "Marked the  payment as received",
        "created_at": "2019-01-23T07:32:28-12:00",
        "test": false,
        "authorization": null,
        "location_id": null,
        "user_id": null,
        "parent_id": 938542432356,
        "processed_at": "2019-01-23T07:32:28-12:00",
        "device_id": null,
        "receipt": {},
        "error_code": null,
        "source_name": "2688293",
        "amount": "66.00",
        "currency": "EUR",
        "admin_graphql_api_id": "gid://shopify/OrderTransaction/938547282020"
    }
}

Let me know if that helps you?  Or when when something is not quite clear from my explanation.

 

Best

Sahin 

 

Plac
Tourist
3 0 0

@SahinVardar Thanks for the help. I am facing slightly same issue but there is only one difference. My orders are created from the Shopify's frontend-my store's website on shopify platform which create a transaction of kind="sale". Whenever I do a request to change the transaction status, I get a weird response of HTML with continue button, which when opened in the browser takes me to my store only. I am using  2019-04 API version.

 

In detail I send the JSON to admin/api/2019-04/orders/<ORDER_ID>/transactions.json with this JSON payload

{
  "transaction": {
    "currency": "RWF",
    "amount": "4730",
    "gateway": "MTN MOMO",
    "kind": "sale",
    "parent_id": <TRANSACTION_ID>,
    "status" : "paid"
  }
}

Then I received this response

Capture.PNG

Please help me to mark those order as Paid.

 

Regards,

Plac

edywladson
New Member
2 0 0

I did everything as you said, but I'm getting the following error:

 

{"errors":{"transaction":"Required parameter missing or invalid"}}

What may be causing this error?

 

 

Below my code in PHP:

$data = array(
    "order" => array(
        "transaction" => [
            [
              "kind" => "capture",
"gateway" => "manual", "amount" => 50.00, "parent_id" => "ID TRANSACTION HERE", "currency" => "BRL" ] ], ) ); $json = json_encode($data); // $string = http_build_query($data); $url_shopify = "https://".$API_KEY.":".$SECRET."@".$STORE_URL."/admin/api/2019-07/orders/ID ORDER HERE/transactions.json"; $ch = curl_init($url_shopify); //set the url $data_string = json_encode($data); //encode the product as json curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //specify this as a POST curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); //set the POST string curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //specify return value as string curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); //specify that this is a JSON call $server_output = curl_exec ($ch); //get server output if you wish to error handle / debug // echo $server_output; curl_close ($ch); //close the connection

 

 

ble
New Member
2 0 2

@edywladson missing status perhaps?

 

"status":"paid"

 

nabeelhamad
Tourist
4 0 1

Hi,

 

I was getting the same error and I tried doing it in a different way as below and it worked. Instead of sending multiple requests, send the capture message also along with the order (probably there has to be the authorization transaction also before the capture transaction); below is the message json (note that the financial_status has to be paid):

 

{
"order": {
"line_items": [
{
"title": "NJUI JUILORP SHADIF KITROL ",
"price": 35,
"quantity": 1,
"variant_id": 31363251077165,
"pre_tax_price": 29.17,
"tax_lines": [
{
"title": "VAT",
"price": "5.83",
"rate": 0.2,
"price_set": {
"shop_money": {
"amount": "5.83",
"currency_code": "INR"
},
"presentment_money": {
"amount": "5.83",
"currency_code": "INR"
}
}
}
]
}
],
"note_attributes": [
{
"name": "Date",
"value": "26-02-2020"
}
],
"customer": {
"first_name": "Mrs xyz",
"last_name": "Yumas",
"email": ""
},
"billing_address": {
"first_name": "Mr xyz",
"last_name": "Yumas",
"address1": "20 Kolim Nuew, Buidle Kaora",
"city": "Nujilei",
"province": "Njieloip",
"country": "JU",
"phone": "98324879674",
"zip": "234-345-098"
},
"shipping_address": {
"first_name": "Mr xyz",
"last_name": "Yumas",
"address1": "20 Kolim Nuew, Buidle Kaora",
"city": "Nujilei",
"province": "Njieloip",
"country": "JU",
"phone": "98324879674",
"zip": "234-345-098"
},
"transactions": [
{
"kind": "authorization",
"status": "success",
"amount": 35
},
{
"kind": "capture",
"status": "success",
"amount": 35
}
],
"financial_status": "paid",
"reference": "BHUIO-ID-528583881222",
"note": "",
"gateway": "BHYT",
"currency": "INR",
"total_tax": "5.83",
"taxes_included": true
}
}
Brian_Horner
Shopify Partner
5 0 0

Did you get this error resolved @edywladson ? We are having the same issue so any thoughts would be appreciated.

Wrooker_com
New Member
7 0 0

If you are using Postman to send request, then remove cookies from the request. Postman attaches the cookie to call.

hbedoyarootstak
New Member
1 0 0

this problem it's because of cookies, POST request to shopify can't contains any cookies, delete all postman cookies and try again.