Re: Marking order transaction as paid with REST Admin API

Solved

Marking order transaction as paid with REST Admin API

Plac
Tourist
3 0 0

Hello!

 

I have a Shopify store where clients will place orders and would pay manually but there is a web hook attached to the order creation which works well. I would like to mark the order as PAID when manual payment is done using REST API.

 

When someone does an order the transaction type is sale as per my observation from the API. I am trying to update the transaction but I only get HTML response with a continue button

 

I am POSTing to this URL https://rwmomotest.myshopify.com/admin/api/orders/917885812833/transactions.json

 

with this JSON payload

 

{
  "transaction": {
    "currency": "RWF",
    "amount": "4730",
    "gateway": "MTN MOMO",
    "kind": "sale",
    "parent_id": 1128723841121,
    "status" : "success"
  }
}

But the response I am getting is an HTML content with a continue button

 

which is below

 

<html>
    <body>
        <noscript>
            <a href="https://app.shopify.com/services/login/identity?destination_uuid=6e156790-8b40-4662-ac08-461b27fdc1bf&amp;return_to=https%3A%2F%2Fapp.shopify.com%2Fservices%2Flogin%2Fidentity_callback%3Fshop_name%3Drwmomotest%26state%3D1R9Lp0vWElSDwov7iYc3DeTP7IX7v-dwNj-nhK0tEYeheCPAu5TN1jeXhl9cavu_pkqV3iKpZTsDBmFOrFEIkalEqOUmu0FoGJ4uYzrI-L_1ZvXTLLH3X1WYffnNr69PLQR_JeYy1uhWyRuZWNIcOwM3qLU2iCWJ5sTmFi2t4BQ8m0H4EtfqpjVYwXLxAmF12u-3CCIOgz5djnLViVO4z31V3x8a1LZ3xTpIlukHbvY%253D&amp;ui_locales=en&amp;upgradeable=false&amp;ux=shop">Continue</a>
        </noscript>
        <script type="text/javascript">
      window.location = "https:\/\/app.shopify.com\/services\/login\/identity?destination_uuid=6e156790-8b40-4662-ac08-461b27fdc1bf\u0026return_to=https%3A%2F%2Fapp.shopify.com%2Fservices%2Flogin%2Fidentity_callback%3Fshop_name%3Drwmomotest%26state%3D1R9Lp0vWElSDwov7iYc3DeTP7IX7v-dwNj-nhK0tEYeheCPAu5TN1jeXhl9cavu_pkqV3iKpZTsDBmFOrFEIkalEqOUmu0FoGJ4uYzrI-L_1ZvXTLLH3X1WYffnNr69PLQR_JeYy1uhWyRuZWNIcOwM3qLU2iCWJ5sTmFi2t4BQ8m0H4EtfqpjVYwXLxAmF12u-3CCIOgz5djnLViVO4z31V3x8a1LZ3xTpIlukHbvY%253D\u0026ui_locales=en\u0026upgradeable=false\u0026ux=shop";
    </script>
    </body>
</html>

and it renders like this

 

Capture.PNG

 

This turned out really weird as I have been following several guides with the same result.

 

Please help mark these transactions as PAID with REST API

 

Regards,

Plac

Accepted Solution (1)
Shier
Shopify Partner
1 1 4

This is an accepted solution.

This Method doesn't work. I have tried it to mark an order as paid but it doesn't work like this. To verify my statement, I went on to add a note in an order and that was successfully updated in my orders as specified in the docs. So please fellas don't waste time using this method

View solution in original post

Replies 9 (9)

AmardeepSingh
Shopify Partner
94 5 15

Hi Plac,

If you only want to make the "Financial Status" as Paid then you should consider making a PUT request like this -

 

PUT 

/admin/api/2019-04/orders/#{order_id}.json

 

{
  "order": {
    "id": order_id,
    "financial_status": "Paid"
  }
}

 

Plac
Tourist
3 0 0

Hello,

Thanks for great solution. However, trying it I the financial status never changed and I get the response of the order only but sincerely the financial_status does not change. Also It's described in the documentation that financial_status can only be set when the order is created

Shier
Shopify Partner
1 1 4

This is an accepted solution.

This Method doesn't work. I have tried it to mark an order as paid but it doesn't work like this. To verify my statement, I went on to add a note in an order and that was successfully updated in my orders as specified in the docs. So please fellas don't waste time using this method

jhcao23
Shopify Partner
31 1 15

you are right! Shopify REST API stated that only a few fields can be made to Order, which doesn't include the status, which is the key part that Shopify takes over control for the platform profit driven purpose: https://help.shopify.com/en/api/reference/orders/order.

One helpful link goes here: https://community.shopify.com/c/Shopify-APIs-SDKs/How-to-mark-order-as-paid-through-REST-Admin-API/t....

 

 

zainbaig
Visitor
1 0 0

Have you tried it ? 
i did through this method and status is not changing.

Gregarican
Shopify Partner
1033 86 291

Looking at the latest API version docs (https://shopify.dev/docs/admin-api/rest/reference/orders/order?api[version]=2020-04), it doesn't appear that the Order endpoint supports setting the financial_status property after the fact. In other words, you can set it with a new POST request that creates an order, but you cannot set it with a PUT request for an existing order. Perhaps someone on the Shopify side can speak to this? 

_JB
Shopify Staff (Retired)
836 100 223

In other words, you can set (financial_status) with a new POST request that creates an order, but you cannot set it with a PUT request for an existing order.

This is correct. The status can only be manually set when creating a new order, once an order exists the financial_status will update automatically based on the state of transactions attached to the order (eg. an authorization being captured, or a pending payment being "Marked as Paid" from the admin).

JB | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Platanomelón
Shopify Partner
14 0 27

So there is no way to "Mark as paid" via Transactions API?

LuffyOnePiece
Shopify Partner
643 93 116

I am trying to convert the draft order to order using order API. The Problem is either the order can be mark as paid and achieve or is unpaid. Can you please help me out to figure out solution for this ?

public function createorder(Request $request){
$shop = ShopifyApp::shop();
$shop_domain = ShopifyApp::shop();
$shopdomain = $shop_domain->shopify_domain;
$shopify_token = $shop_domain->shopify_token;
$api = new BasicShopifyAPI();
$api->setVersion('2020-04'); 
$api->setShop($shopdomain);
$api->setAccessToken($shopify_token);
$cartdata = json_decode($request->cartdata);
$customer_id = $request->customer_id;
$customer_id = str_replace("gid://shopify/Customer/","",$customer_id);
$filepath = $request->filepath;
// print_r(json_encode($customer_id));
// dd();
$markaspaid = $request->markaspaid;
$shipping_data = (array)json_decode($request->shipping_data);
// print_r(json_encode($cartdata));
// dd();
$lineItems = array();
$total_discount_amt = 0;
foreach ($cartdata as $cartitem){
$cartitem = (array)$cartitem;
 
$productline['variant_id'] = $cartitem['variantid'];
    $productline['quantity'] = $cartitem['qty'];
 
$discount_amt = (float)$cartitem['individual_discount_amt'];
$lineproperties = $cartitem['properties'];
if($discount_amt){
//$lineproperties[] = array('name'=>"Unit price",'value'=>'$'.$cartitem['price_for_customer']);
//$lineproperties[] = array('name'=>"Discount per item",'value'=>'$'.$discount_amt);
$total_discount_amt = $discount_amt*$cartitem['qty'];
    $productline['applied_discount'] = array(
    "title"=> "Custom",
        "description"=> "Custom discount",
        "value_type"=> "fixed_amount",
        "value"=> $discount_amt,
        "amount"=> $total_discount_amt
    );
}
if(!empty($lineproperties)){
$productline['properties'] = $lineproperties;
}     
    
    $lineItems[] = $productline;
}
 
//order attributes
$additional_info = (array)json_decode($request->additional_info);
$order_note = $additional_info['order_note'];
$order_date = $additional_info['date'];
$po_number = $additional_info['po'];
$note_attribute = array();
if($order_date){
array_push($note_attribute, array('name'=>'Date','value'=>$order_date));
$draft_order['tags'] = $order_date;
}
if($po_number){
array_push($note_attribute, array('name'=>'Reference','value'=>$po_number));
// $draft_order['tags'] = ucwords(trim($po_number));
}
 
if ($filepath != "false") {
$filelink = '/ordernotes/'.$filepath.'';
array_push($note_attribute, array('name'=>'filepath','value'=>$filelink));
}
 
 
//total order discounts
$order_discount = (array)json_decode($request->discount_info);
// print_r(json_encode(count($order_discount)));
// dd();
if(count($order_discount)>2){
$discount_percent = $order_discount['percent'];
$discount_amt = $order_discount['amt'];
$default_reason = $discount_percent.'% off';
$discount_reason = $order_discount['reason']?$discount_percent.'% off('.$order_discount['reason'].')':$default_reason;
$applied_discount = '';
if($discount_percent){
$applied_discount = array(
  "title"=> $discount_reason,
  "description"=> $discount_reason,
  "value"=> $discount_amt,
  "value_type"=> "fixed_amount",
  "amount"=> $discount_amt
);
}
if($applied_discount){
$draft_order["applied_discount"] = $applied_discount;
}
 
}
//order shipping
$order_shipping = (array)json_decode($request->shipping_info);
$shipping_type = $order_shipping['type'];
$shipping_name = $order_shipping['name'];
$shipping_amt = $order_shipping['amt'];
$shipping_line = '';
if($shipping_type){
$shipping_line = array(
  "custom"=> true,
  "price"=> $shipping_amt,
  "title"=> $shipping_name
);
}
 
 
 
if($shipping_line){
$draft_order["shipping_line"] = $shipping_line;
}
 
$draft_order["line_items"] = $lineItems;
//$draft_order['total_discounts'] = $total_discount_amt;
if(!empty($order_note)){
$draft_order['note'] = $order_note;
}
 
if(count($note_attribute)){
$draft_order['note_attributes'] = $note_attribute;
}
 
$draft_order['send_receipt'] = true;
// print_r(count($shipping_data));
// dd();
if (count($shipping_data)>0) {
$first_name = $shipping_data["first_name"];
$address1 = $shipping_data["address1"];
$phone = $shipping_data["phone"];
$city = $shipping_data["city"];
$zip = $shipping_data["zip_code"];
$province = $shipping_data["province_code"];
$country = $shipping_data["country_name"];
$last_name = $shipping_data["last_name"];
$company = $shipping_data["company"];
$address2 = $shipping_data["address2"];
$countrycode = $shipping_data["countrycode"];;
$province = $shipping_data["province"];
$shippingaddress = array(
      "first_name" => $first_name,
      "address1" => $address1,
      "phone" => $phone,
      "city" => $city,
      "zip" =>$zip,
      "province" => $province,
      "country" => $country,
      "last_name" => $last_name,
      "address2" => $address2,
      "company" => $company,      
      "country_code"=> $countrycode,
      "province_code"=> $province
    );
    $billing_address = array(
      "first_name" => $first_name,
      "address1" => $address1,
      "phone" => $phone,
      "city" => $city,
      "zip" =>$zip,
      "province" => $province,
      "country" => $country,
      "last_name" => $last_name,
      "address2" => $address2,
      "company" => $company,      
      "country_code"=> $countrycode,
      "province_code"=> $province
    );
    $draft_order['shipping_address'] = array('shipping_address'=>$shippingaddress);
    $draft_order['billing_address'] = array('billing_address'=>$billing_address);
// Adding note attributes to order */
// $draft_order['note_attributes'] = $noteAttributes;
    }
    if($customer_id){
    $draft_order['customer'] = array('id'=>$customer_id);
    $draft_order['use_customer_default_address'] = true;
    }
    $draftOrderArgs = array('draft_order' => $draft_order );
    // print_r(json_encode($draftOrderArgs));
    // dd();
try
{
# Making an API request can throw an exception
$draftorder = $shop->api()->rest('POST', '/admin/api/2020-04/draft_orders.json', $draftOrderArgs);
// print_r(json_encode($draftorder));
  //    dd();
if($draftorder){
$draftorder_id = $draftorder->body->draft_order->id;
// print_r(json_encode($draftorder_id));
//        dd();
    if ($markaspaid == 'true') {
    $orderquery = 'mutation draftOrderComplete {
  draftOrderComplete(id:"gid://shopify/DraftOrder/'.$draftorder_id.'", paymentPending:false) {
    draftOrder {
      id
      order{
      id
      name
      }
    }
    userErrors {
      field
      message
    }
  }
}';
    }else{
$orderquery = 'mutation draftOrderComplete {
  draftOrderComplete(id:"gid://shopify/DraftOrder/'.$draftorder_id.'", paymentPending:true) {
    draftOrder {
      id
      order{
      id
      name
      }
    }
    userErrors {
      field
      message
    }
  }
}';
 
}
 
$orderdata =$api->graph($orderquery);
// print_r(json_encode($orderdata));
//        dd();
$order_id = $orderdata->body->draftOrderComplete->draftOrder->order->id;
// mark as paid orderid
$orderqueryforpaid = 'mutation orderMarkAsPaid {
  orderMarkAsPaid(input: "'.$order_id.'") {
    order {
      id
    }
    userErrors {
      field
      message
    }
  }
}';
$orderresponse =$api->graph($orderqueryforpaid);
 
$order_id = str_replace("gid://shopify/Order/","",$order_id);
$order_id = $orderdata->body->draftOrderComplete->draftOrder->order->name;
print_r(json_encode($order_id));
        dd();
}
}
catch (shopify\ApiException $e)
{
# HTTP status code was >= 400 or response contained the key 'errors'
echo $e;
print_R($e->getRequest());
print_R($e->getResponse());
}
catch (shopify\CurlException $e)
{
# cURL error
echo $e;
print_R($e->getRequest());
print_R($e->getResponse());
}
 
}
Sandeep Pangeni
Need help with your store? sandeeppangeni17@gmail.com
For quick response, Contact In WhatsApp +9779867521184