Magento 2 API Create an order

The following code will convert the quote to order and will return the order id.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/customer/token";
 $data = array("username" => "Test.User1521623008@brandmuscle.com", "password" => "Pramod1&");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);

$customerData =[
 'paymentMethod' => [
 "method" => 'quickpay'
 ],
 'billing_address' => [
 "region" => 'New York',
 "region_id" => "43",
 "region_code" => "NY",
 "country_id" => "US",
 "street" => array("123 Oak Ave"),
 "postcode" => "10577",
 "city" => "Purchase",
 "firstname" => "Pramod",
 "lastname" => "Prasad",
 "customer_id" => "16",
 "email" => "Test.User1521623008@brandmuscle.com",
 "telephone" => "(512) 555-1111",
 ],
 ];

$requestUrl =$magentoURL.'index.php/rest/V1/carts/mine/payment-information';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *