Saturday 17 September 2016

Magento 2 introduced rest api's to build mobile shopping cart. In this blog i am going to explain basic REST API CART flow so that magento developers are able to successfully place an order using REST API's.

STEP 1 - CREATE CART

               /V1/carts/mine/


              Method - POST

              Response - Cart Id (For Eg - 39)

              
               

              

STEP 2 - GET CART DATA

             /V1/carts/mine

Method - GET

Response - Cart Data





STEP 3 - Set Billing Address and Shipping Address To Cart

/V1/carts/mine/billing-address

METHOD - POST

REQUEST -





RESPONSE - Address Id (For Eg - 60)






STEP 4 - Add items to cart


/V1/carts/mine/items

  METHOD - POST

REQUEST -





RESPONSE - Items are added in cart and their respective data is returned.





STEP 5 - Estimate Shipping Methods By Address Id

             /V1/carts/mine/estimate-shipping-methods-by-address-id

              METHOD - POST

              REQUEST -

           

           
               RESPONSE - Cart Shipping Methods are returned

   

 
STEP 6 - POST SHIPPING INFORMATION

     /V1/carts/mine/shipping-information 

     METHOD - POST

    REQUEST -

     {
"addressInformation": {
"shipping_address": {
"id": 60,
"customer_id": 6,
"region": "Arkansas",
"region_id": 5,
"country_id": "US",
"street": [
"Whitefield,New York"
],
"company": "IBM",
"telephone": "94354545",
"postcode": "234533",
"city": "New York",
"firstname": "              ",
"lastname": "               ",
"prefix": "address_",
"region_code": "AR"
},
"billing_address": {
"id": 60,
"customer_id": 6,
"region": "Arkansas",
"region_id": 5,
"country_id": "US",
"street": ["Whitefield,New York"],
"company": "IBM",
"telephone": "94354545",
"postcode": "234533",
"city": "New York",
"firstname": "              ",
"lastname": "               ",
"prefix": "address_",
"region_code": "AR"
},
"shipping_method_code": "flatrate",
"shipping_carrier_code": "flatrate"
    }
      }



      RESPONSE -



   {
"payment_methods": [{
"code": "cashondelivery",
"title": "Cash On Delivery"
}],
"totals": {
"grand_total": 8.78,
"base_grand_total": 8.78,
"subtotal": 3.78,
"base_subtotal": 3.78,
"discount_amount": 0,
"base_discount_amount": 0,
"subtotal_with_discount": 3.78,
"base_subtotal_with_discount": 3.78,
"shipping_amount": 5,
"base_shipping_amount": 5,
"shipping_discount_amount": 0,
"base_shipping_discount_amount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"weee_tax_applied_amount": null,
"shipping_tax_amount": 0,
"base_shipping_tax_amount": 0,
"subtotal_incl_tax": 3.78,
"shipping_incl_tax": 5,
"base_shipping_incl_tax": 5,
"base_currency_code": "AED",
"quote_currency_code": "AED",
"items_qty": 2,
"items": [{
"item_id": 95,
"price": 1.89,
"base_price": 1.89,
"qty": 2,
"row_total": 3.78,
"base_row_total": 3.78,
"row_total_with_discount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"tax_percent": 0,
"discount_amount": 0,
"base_discount_amount": 0,
"discount_percent": 0,
"price_incl_tax": 1.89,
"base_price_incl_tax": 1.89,
"row_total_incl_tax": 3.78,
"base_row_total_incl_tax": 3.78,
"options": "[]",
"weee_tax_applied_amount": null,
"weee_tax_applied": null,
"name": "Refreshing"
}],
"total_segments": [{
"code": "subtotal",
"title": "Subtotal",
"value": 3.78
}, {
"code": "shipping",
"title": "Shipping & Handling (%1)",
"value": 5
}, {
"code": "tax",
"title": "Tax",
"value": 0,
"extension_attributes": {
"tax_grandtotal_details": []
}
}, {
"code": "grand_total",
"title": "Grand Total",
"value": 8.78,
"area": "footer"
}]
   }
   }

STEP 7 - POST PAYMENT INFORMATION

     /V1/carts/mine/payment-information

 METHOD - POST

NOTE - In Magento 2.0.5 after you hit Payment Information api url you might get "Shipping Address is not set" error. That happens because session data is not getting set with rest api. You need to create custom webservice url which updates shipping-method column value in quote_address table. This custom webservice url must be called before payment-information api url so that payment-information api url executes successfully.

In Magento 2.1 this issue is solved.


 REQUEST -

   {
"paymentMethod": {
"poNumber": "1",
"method": "cashondelivery"
},
"billingAddress": {
"id": 60,
"region": "Arkansas",
"regionId": 5,
"regionCode": "AR",
"countryId": "US",
"street": [
"Whitefield,New York"
],
"company": "IBM",
"telephone": "94354545",
"postcode": "234533",
"city": "New York",
"firstname": "             ",
"lastname": "              ",
"prefix": "address_",
"customerId": 6,
"email": "                       ",
"sameAsBilling": 1,
"customerAddressId": 0,
"saveInAddressBook": 1
}
}

     RESPONSE - Order is created and order id is returned



So as you see these are the basic steps to create a successful order through REST API in Magento 2.




      






6 comments:

  1. I think REST API and Power BI can easily solve complex IT problems.

    Powerbi Read Rest

    ReplyDelete
  2. Due to the rapid increase in eCommerce marketing, the overall economy has increased over the past time. Along with this crucial time, online growth for Magento eCommerce development became more important. In short, Adobe sensei revealed new product recommendations for Artificial intelligence (AI) as well as machine level language to customers, including businesses.

    ReplyDelete
  3. How can I create order using 3rd party payment Gateways like Payu, Paytm, RazorPay etc. and then how to handle the payment flow using rest apis.

    ReplyDelete
  4. Thank you for sharing such useful information. I really enjoyed while reading your article and it is good to know the latest updates. Do post more. And also read about Magento 2 Development Company

    ReplyDelete
  5. Hi I have question since one of my magento project is using 2.1. What if I am redirecting user to collect payment from payment gateway. I believe in that scenario order is should be in pending state and then once completed should again update order status. What are the steps for it.

    ReplyDelete