You can use following method to create order using api.
Order Place:
<?php
require_once 'api.php';
require dirname(__FILE__) . '/../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/../abstract.php';
$obj = $bootstrap->getObjectManager();
$resource = $obj->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
// The URL to your Magento 2 instance (ending with /index.php/rest/V1)
$api_url = 'site_url/index.php/rest/V1';
// Set the integrations access token.
$token = 'auth_token';
$magento = new MagentoClient($token, $api_url);
$json = $_POST['json_obj'];
$json_cart_array =array();
$json_cart_array = json_decode($json,true);
$cart_id = $json_cart_array['cart_id'];
$payment_method = $json_cart_array['payment_method'];
//$payment_method = $json_cart_array['payment_method'];
// Create customer cart using customer id.
$ordered = $magento->placeOrderCust($cart_id, $payment_method);
$result1 = json_decode($ordered, 1);
//print_r($result1);exit();
API Class:
<?php
class MagentoClient {
public $bearer_token = 'auth_token';
public $base_url = 'magento_base_url';
public function __construct($token, $base_url) {
$this->base_url = $base_url;
$this->bearer_token = $token;
}
public function request($endpoint, $method = 'GET', $body = FALSE) {
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->base_url . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$headers = array();
$headers[] = "Authorization: Bearer " . $this->bearer_token;
if ($body) {
$headers[] = "Content-Type: application/json";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
return $result;
}
public function getProduct($product_id) {
return $this->request('/products/' . $product_id . '/', 'GET');
}
public function createCart() {
return $this->request('/guest-carts/', 'POST');
// var_dump($this->request('/guest-carts/', 'POST'));
// die;
}
public function addToCart($cart_id, $product_sku, $quantity = 1) {
$order = array(
'cartItem' => array(
'quote_id' => $cart_id,
'sku' => $product_sku,
'qty' => $quantity,
)
);
return $this->request('/guest-carts/' . $cart_id . '/items',
'POST',
json_encode($order)
);
}
public function addToCartCust($cart_id, $product_sku, $quantity = 1,$options) {
if(isset($options['extension_attributes']['configurable_item_options']) && $options != '' && count($options['extension_attributes']['configurable_item_options']) > 0){
$order = array(
'cartItem' => array(
'quote_id' => $cart_id,
'sku' => $product_sku,
'qty' => $quantity,
'product_option' => $options
)
);
}else{
$order = array(
'cartItem' => array(
'quote_id' => $cart_id,
'sku' => $product_sku,
'qty' => $quantity
)
);
}
//json_encode($order);
return $this->request('/carts/' . $cart_id . '/items',
'POST',
json_encode($order)
);
}
public function setShipping($cart_id, $shipping) {
return $this->request('/guest-carts/' . $cart_id . '/shipping-information',
'POST',
json_encode($shipping)
);
}
public function setShippingCust($cart_id, $shipping) {
return $this->request('/carts/' . $cart_id . '/shipping-information',
'POST',
json_encode($shipping)
);
}
public function placeOrder($cart_id, $payment_method = 'cashondelivery') {
$payment = array(
'paymentMethod' => array('method' => $payment_method)
);
return $this->request('/guest-carts/' . $cart_id . '/order',
'PUT',
json_encode($payment)
);
}
public function placeOrderCust($cart_id, $payment_method = 'cashondelivery') {
$payment = array(
'paymentMethod' => array('method' => $payment_method)
);
return $this->request('/carts/' . $cart_id . '/order',
'PUT',
json_encode($payment)
);
}
public function getPaymentMethods($cart_id) {
return $this->request('/guest-carts/' . $cart_id . '/payment-information', 'GET');
}
public function createCustCart($cid)
{
return $this->request('/customers/' . $cid . '/carts', 'POST');
}
}
This code is available on github
but now i forgot the link.
Hope it helps.