3

I am trying to create order using REST API I have got an error message ordered: {"message":"%fieldName is a required field.","parameters":{"fieldName":"entity"}}"

What is entity field in order of magento2?.

Api.php

<?php
class MagentoClient {
  public $bearer_token = '';
  public $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) {
    $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');
  }

  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 setShipping($cart_id, $shipping) {
    return $this->request('/guest-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 getPaymentMethods($cart_id) {
    return $this->request('/guest-carts/' . $cart_id . '/payment-information', 'GET');
  }

}

place-order.php

<?php
require_once 'api.php';

// The URL to your Magento 2 instance (ending with /index.php/rest/V1)
$api_url = 'http://192.168.1.100/magento24/index.php/rest/V1/orders';
// Set the integrations access token.
$token = '00qlti27cwtsj2inmjg9w02yve991kib';
// Fill in the SKU of the product which should be ordered.
$sku = 'car';

$magento = new MagentoClient($token, $api_url);

$product = $magento->getProduct($sku);
$cart = $magento->createCart();
$cart = str_replace('"', '', $cart);

$order_filled = $magento->addToCart($cart, $sku, 1);
//var_dump($order_filled);

$ship_to = array (

  'addressInformation' =>
    array (
      'shippingAddress' =>
        array (
          'region' => 'Wien',
          'region_id' => 95,
          'country_id' => 'AT',
          'street' =>'aaa',
            array (
              0 => 'Fillgradergasse 12-14/1a',
            ),
          'company' => 'acolono GmbH',
          'telephone' => '1111111',
          'postcode' => '1060',
          'city' => 'Vienna',
          'firstname' => 'Martin',
          'lastname' => 'Testman',
          'email' => 'kamlesh@jivainfotech.com',
          'prefix' => 'address_',
          'region_code' => 'W',
          'sameAsBilling' => 1,
        ),
      'billingAddress' =>
        array (
          'region' => 'Wien',
          'region_id' => 95,
          'country_id' => 'AT',
          'street' =>
            array (
              0 => 'Fillgradergasse 12-14/1a',
            ),
          'company' => 'acolono GmbH',
          'telephone' => '1111111',
          'postcode' => '1060',
          'city' => 'Vienna',
          'firstname' => 'Martin',
          'lastname' => 'Testman',
          'email' => '[email protected]',
          'prefix' => 'address_',
          'region_code' => 'W',
        ),
      'shipping_method_code' => 'flatrate',
      'shipping_carrier_code' => 'flatrate',
    ),
);

$order_shipment = $magento->setShipping($cart, $ship_to);
// var_dump($order_shipment);

//$payment = $magento->getPaymentMethods($cart);
//var_dump($payment);

$ordered = $magento->placeOrder($cart, 'cashondelivery');
echo "\nordered:\n";
var_dump($ordered)

;

Can any one help me on this problem ? Any references or suggestions are highly appreciated. Thanks in advance.

1
  • You should probably give some code example how do you call the API Mar 2, 2017 at 17:23

1 Answer 1

0

All seems good with your code I have tried it and it's working .Check with your instance custom modules and access token.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.