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());
}
}