2

I want to create order with Magento 2 API using check/money order payment method. I want to save check no. in database it's throwing following error:-

ordered: string(104) "{"message":"Internal Error. Details are available in Magento log file. Report ID: webapi-58bd682da0738"}"

It's generate error in log file

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) 
    {

        // 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');   
    }

    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 = 'checkmo',$additional_information='12345') 
    {
        $payment = array(
          'paymentMethod' => array('method' => $payment_method ,'additional_information' => $additional_information)
        ); //var_dump($payment);exit;
        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';
// 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);
//echo $product; exit;
$cart = $magento->createCart();
$cart = str_replace('"', '', $cart);

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

$ship_to = array (
  'addressInformation' =>
    array (
      'shippingAddress' =>
        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' => 'testman@acolono.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' => 'testman@acolono.com',
          '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, 'checkmo', '12345');
echo "\nordered:\n";
var_dump($ordered);
?>

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.