Magento 2 – How to Create Order Using REST API

apicheckoutmagento2restsales-order

I need to use the Magento REST API to create an order from a mobile client. In my case, mobile side will implement the payment directly using PayPal SDK. What I need to do is to create an order by setting the payment method to money order and perform a guest checkout. Which API(s) should I use to achieve this?

Best Answer

I finally figured it out. Here is what I'm doing.

Get a single product

curl -g -X GET "$base_url/index.php/rest/V1/products/24-MB05/" \
-H "Authorization: Bearer $token" 

Create cart

curl -g -X POST "$base_url/index.php/rest/V1/guest-carts/" \
-H "Authorization: Bearer $token" 

Get Cart

curl -g -X GET "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5" \
-H "Authorization: Bearer $token" 

Add Product To Cart

curl -g -X POST "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/items" \
-H "Authorization: Bearer $token" \
-H "Content-Type:application/json" \
 -d '{ "cartItem": { "quote_id": "56241bf6bc084cd7589426c8754fc9c5", "sku": "24-MB05", "qty": 1 } }'

Add shipping information

curl -g -X POST "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/shipping-information" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type:application/json" \
     -d '
{
    "addressInformation": {
        "shippingAddress": {
            "region": "MH",
            "region_id": 0,
            "country_id": "IN",
            "street": [
                "Chakala,Kalyan (e)"
            ],
            "company": "abc",
            "telephone": "1111111",
            "postcode": "12223",
            "city": "Mumbai",
            "firstname": "Sameer",
            "lastname": "Sawant",
            "email": "abc@abc.com",
            "prefix": "address_",
            "region_code": "MH",
            "sameAsBilling": 1
        },
        "billingAddress": {
            "region": "MH",
            "region_id": 0,
            "country_id": "IN",
            "street": [
                "Chakala,Kalyan (e)"
            ],
            "company": "abc",
            "telephone": "1111111",
            "postcode": "12223",
            "city": "Mumbai",
            "firstname": "Sameer",
            "lastname": "Sawant",
            "email": "abc@abc.com",
            "prefix": "address_",
            "region_code": "MH"
        },
        "shipping_method_code": "flatrate",
        "shipping_carrier_code": "flatrate"
    }
}
 '

Get Payment Method

curl -g -X GET "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/payment-information" \
    -H "Authorization: Bearer $token" 

Place Order

curl -g -X PUT "$base_url/index.php/rest/V1/guest-carts/56241bf6bc084cd7589426c8754fc9c5/order" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type:application/json" \
     -d '
{
    "paymentMethod": {
        "method": "checkmo"
    }
}'                      
Related Topic