Example of REST API
To place an order using the Reward Points, you have to complete the following preconditions:
- The customer should be registered and logged in, as the module doesn't work with guest customers
- You should have at least one configured and enabled Payment method: Stores > Settings > Configuration > Sales > Payment Methods
- Also, there should be at least one configured and enabled Shipping method on Stores > Settings > Configuration > Sales > Shipping Methods
- To spend points, you have to configure a Spending rule according to your requirements: Reward Points > Spending Rules
- Your customer as well should have points on his account
Our example below is intended to provide a step-by-step guide for placing an order using points.
Here are the token and customers ID in case you wish to perform these actions in our demo store:
<token>
- xdezikuhh6ryzrgr6ggzi51byafd5bj0<customerId>
- 6
- Firstly you should get the customer's cart:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPOST "http://rewards.m2.mirasvit.com/index.php/rest/V1/customers/<customerId>/carts"
- Secondly, When we have got the cart ID, we can add an item on the cart:
<cartId>
- current active cart ID returned in previous requestcurl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPOST "http://rewards.m2.mirasvit.com/index.php/rest/V1/carts/<cartId>/items" -d'{"cart_item":{"quote_id":<cartId>,"sku":"24-MB03","qty":1,"price":19.00}}'
- After adding the items, we can then check out for the total information:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPOST "http://rewards.m2.mirasvit.com/index.php/rest/V1/carts/<cartId>/totals-information" -d'{"cartId":<cartId>,"addressInformation":{"address":{"region": "New York","region_id": 43,"region_code": "NY","country_id": "US","street": ["123 Oak Ave"],"postcode": "10577","city": "Purchase","firstname": "Jane","lastname": "Doe","customer_id": 4,"email": "[email protected]","telephone": "(512) 555-1111","same_as_billing": 1}}}'
- Get the available shipping address:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPOST "http://rewards.m2.mirasvit.com/index.php/rest/V1/carts/<cartId>/estimate-shipping-methods" -d'{"address": {"region": "New York","region_id": 43,"region_code": "NY","country_id": "US","street": ["123 Oak Ave"],"postcode": "10577","city": "Purchase","firstname": "Jane","lastname": "Doe","customer_id": 4,"email": "[email protected]","telephone": "(512) 555-1111","same_as_billing": 1}}'
- Now we have the address where we want to receive the items ordered, we will be able to set down the necessary shipping method:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPOST "http://rewards.m2.mirasvit.com/index.php/rest/V1/carts/<cartId>/shipping-information" -d'{"addressInformation": {"shipping_address": {"region": "New York","region_id": 43,"region_code": "NY","country_id": "US","street": ["123 Oak Ave"],"postcode": "10577","city": "Purchase","firstname": "Jane","lastname": "Doe","email": "[email protected]","telephone": "512-555-1111"},"billing_address": {"region": "New York","region_id": 43,"region_code": "NY","country_id": "US","street": ["123 Oak Ave"],"postcode": "10577","city": "Purchase","firstname": "Jane","lastname": "Doe","email": "[email protected]","telephone": "512-555-1111"},"shipping_carrier_code": "flatrate","shipping_method_code": "flatrate"}}'
- We can now add the reward points required:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPOST "http://rewards.m2.mirasvit.com/index.php/rest/V1/rewards/<cartId>/apply/1148"
- Get the payment methods available:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XGET "http://rewards.m2.mirasvit.com/index.php/rest/V1/carts/<cartId>/payment-methods"
- Once we have checked the payment methods we have available, - we can pick one from them:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPUT "http://rewards.m2.mirasvit.com/index.php/rest/V1/carts/<cartId>/selected-payment-method" -d'{"method": {"method": "checkmo"}}'
- All required steps are complete and we can place order:
curl -H "Content-Type: application/json" -H "Authorization: Bearer <token>" -XPUT "http://rewards.m2.mirasvit.com/index.php/rest/V1/carts/<cartId>/order"