To show how an order is made, a complete example will be shown using several endpoints so that it can be seen how they are integrated among them. this example will be step by step.
Searching business
The first step for place order is to look for a business, in this case we will do it by location and delivery type. For this, we will use the "Get Business" endpoint.
**Resource**: `https://apiv4.ordering.co/v400/en/demo/business?type=1&location=40.689799,-73.84395900000004¶ms=name,slug,logo,header,location,description,food,alcohol,groceries,laundry,zones,delivery_price,minimum,featured,about`
Important
- Use The param
params
for soft load, just params needed- This request only returns business available.
Response:
{
"error": false,
"result": [
{
"id": 7,
"name": "Twirly Laundry",
"slug": "thetwirlylaundry",
"logo": "https://res.cloudinary.com/ordering/image/upload/v1534196961/ak4o8bfgxcpoue0fx1xa.jpg",
"header": "https://res.cloudinary.com/ordering/image/upload/v1534196946/dl8r7vye0zoilnuevors.jpg",
"location": {
"lat": 40.7228702,
"lng": -74.00508250000001,
"zipcode": -1,
"zoom": 15
},
"description": "",
"food": false,
"alcohol": false,
"groceries": false,
"laundry": true,
"delivery_price": 0,
"minimum": 5,
"featured": true,
"about": "",
"timezone": "America/New_York",
"open": false,
"today": null,
"distance": 14.069888444010674,
"delivery_zone": 20
},
{
"id": 22,
"name": "The Laundry Home",
"slug": "thelaundryhome",
"logo": "https://res.cloudinary.com/ordering/image/upload/v1534196763/lmp3g6yipooxaihqzy8g.jpg",
"header": "https://res.cloudinary.com/ordering/image/upload/v1534196750/ikb2motaoq97cu4avfrl.jpg",
"location": {
"lat": 40.717664000000006,
"lng": -73.99222789999999,
"zipcode": -1,
"zoom": 15
},
"description": "",
"food": false,
"alcohol": false,
"groceries": false,
"laundry": true,
"delivery_price": 2,
"minimum": 1,
"featured": true,
"about": "",
"timezone": "America/New_York",
"open": false,
"today": null,
"distance": 12.876184948275343,
"delivery_zone": 15
},
...
]
}
With this we have obtained the business with delivery available for this location with their information, Ordering API also returns the delivery price for that location. In the business information is a slug included which it will use to select a business.
Selecting a business
Using the slug, it can be made a complete load because is only one business and see the entire product catalog, and well, now we choose "Twirly Laundry", and for this, we use its slug
which is" thetwirlylaundry "and we make the request for the next step:
Resource: https://apiv4.ordering.co/v400/en/demo/business/thetwirlylaundry?location=40.689799,-73.84395900000004&type=1
Note that the request continues to include location and type, otherwise the API would not return anything, unless you were an administrator or business owner.
Response:
{
"error": false,
"result": {
"id": 7,
"name": "Twirly Laundry",
"email": "[email protected]",
"slug": "thetwirlylaundry",
"schedule": [],
"description": "",
"about": "",
"logo": "https://res.cloudinary.com/ordering/image/upload/v1534196961/ak4o8bfgxcpoue0fx1xa.jpg",
"header": "https://res.cloudinary.com/ordering/image/upload/v1534196946/dl8r7vye0zoilnuevors.jpg",
"phone": "12382348",
"cellphone": "2382348",
"owner_id": 1,
"city_id": 1,
"address": "19 Grand Street, New York, NY, USA",
"address_notes": null,
"zipcode": null,
"location": {
"lat": 40.7228702,
"lng": -74.00508250000001,
"zipcode": -1,
"zoom": 15
},
"featured": true,
"timezone": "America/New_York",
"currency": "USD",
"food": false,
"alcohol": false,
"groceries": false,
"laundry": true,
"use_printer": false,
"printer_id": null,
"minimum": 5,
"delivery_price": 0,
"always_deliver": false,
"tax_type": 2,
"tax": 0,
"delivery_time": "0:0",
"pickup_time": "0:0",
"service_fee": 0,
"fixed_usage_fee": 0,
"percentage_usage_fee": 0,
"enabled": true,
"checkoutfields": {},
"reviews": {},
"open": true,
"today": {},
"categories": [],
"valid_service": true,
"num_zones": 2,
"distance": 14.069888444010674,
"minzone": {},
"delivery_zone": 20,
"gallery": [],
"offers": [],
"city": {},
"webhooks": [],
"paymethods": []
}
}
With this information, we can start the base of what is to make an order like, start the cart selecting the products (Click here for more information about order products) you want.
Selecting products
The params categories
brings all categories and categories bring products. So that it can be better understood we will divide each thing and then we will assemble a "package" that will indicate everything that the product carries.
For example, this is a full product in a category:
{
business_id: 7,
enabled: true,
id: 248,
image: "https://res.cloudinary.com/ordering2/image/upload/v1539098943/nibbgz67hfbzlrlflqus.jpg",
name: "Main menu",
products: [
{
category_id: 248,
description: "",
enabled: true,
extras: [
{
business_id: 41,
description: null,
enabled: true,
id: 49,
name: "McBonalds option1",
options: [
{
conditioned: false,
extra_id: 49,
id: 53,
max: 1,
min: 1,
name: "Size",
rank: 1,
respect_to: null,
suboptions: [
{
extra_option_id: 53,
id: 172,
image: null,
name: "small",
price: 1,
rank: 1,
sku: null,
}
]
}
],
pivot: {
product_id: 1319,
extra_id: 49
}
}
],
featured: false,
gallery: [],
id: 1319,
ingredients: [
{
id: 5,
image: null,
name: "bacon",
product_id: 1319,
},
{
id: 6,
image: null,
name: "chedar",
product_id: 1319,
}
],
inventoried: false,
name: "Chipotle BBQ Snack Wrap (Grilled or Crispy)",
price: 25,
quantity: 0,
sku: null,
}
],
rank: 7,
}
Seeing all the data, we have the product (Chipotle BBQ Snack Wrap) with its extras (size) and its ingredients (bacon, cheddar). Let's suppose we want it 2 small sizes and with bacon so we'll have to put together in a JSON with the information as shown below:
[
{
"id": 1319, // The product id
"quantity": 2, //The product quantity
"comment": "Example comment",
"ingrdients": [5], //ingredients used Ids
"options": [
{
"id": 53, //Option Id
"suboptions": [172] // Suboption ids
},
]
},
]
This is the information we need in the way we need for the param products
.
Checking out fields (Optional)
Business also brings a param called checkoutfields
which serves to verify the required fields in the frontend comparing with the parameters required
and enabled
.
{
name: {name: "Name", type: "text", required: true, enabled: true},
address: {name: "Address", type: "text", required: true, enabled: true},
address_notes: {name: "Address notes", type: "text", required: false, enabled: true},
city_dropdown_option: {name: "City and dropdown option", type: "select", required: false, enabled: false},
coupon: {name: "Coupon", type: "text", required: false, enabled: true},
driver_tip: {name: "Driver tip", type: "text", required: false, enabled: true},
email: {name: "Email", type: "text", required: true, enabled: true},
lastname: {name: "Last Name", type: "text", required: false, enabled: false},
mobile_phone: {name: "Mobile phone", type: "text", required: false, enabled: true},
name: {name: "Name", type: "text", required: true, enabled: true},
}
Selecting paymethod
The business information contains the payment methods available for this business as shown below:
[
{
business_id: 41,
created_at: "2018-10-06 18:18:26",
data: {},
enabled: true,
id: 54,
paymethod: {
created_at: "2018-10-06 18:18:25",
deleted_at: null,
enabled: true,
gateway: "cash",
id: 1,
name: "Cash",
updated_at: "2018-10-06 18:18:25",
},
paymethod_id: 1,
sandbox: false,
updated_at: "2018-10-06 18:18:26",
},
{id: 3, paymethod_id: 29, business_id: 41, sandbox: true, enabled: true,…},
{id: 4, paymethod_id: 31, business_id: 41, sandbox: true, enabled: true,…},
{id: 5, paymethod_id: 32, business_id: 41, sandbox: true, enabled: true,…},
{id: 54, paymethod_id: 1, business_id: 41, sandbox: false, enabled: true,…},
{id: 55, paymethod_id: 2, business_id: 41, sandbox: false, enabled: true,…},
{id: 56, paymethod_id: 22, business_id: 41, sandbox: false, enabled: true,…},
]
Now for this example, we will use the cash payment method, as this is a payment without the requirement, we can directly access to create the order. but a customer must be added to the order first. more about payments
Adding a customer to the order.
To make an order, especially delivery type, it is necessary to have the client's data, these have a special structure [customer structure] (https://developers.ordering.co/reference#section-customers) to be added to the correct order.
{
"id": 336,
"name": "Jorge",
"lastname": "Diaz",
"email": "[email protected]",
"dropdown_option_id": null,
"address": "500 5th Ave, New York, NY 10110, USA",
"address_notes": "The red house",
"zipcode": null,
"cellphone": "3016439796",
"phone": "3702781",
"location": {
"lat": 40.7314123,
"lng": -73.9969848
},
}
Building order data
Having all the required priority data, we proceed to construct the data that we want to add for the order. Driver's tip, taxes, delivery location.
{
paymethod_id: 1, // we selected cash
business_id: 7, // Mcbonals
delivery_type: 2, // delivery
driver_tip: 15, // tip in percenge
pay_data: {}, // payment information (cash dont have)
delivery_zone_id: 5, //delivery zone destination
delivery_datetime: delivery_datetime,
location: { // the specific location
"lat": 40.7228702,
"lng": -74.00508250000001,
"zipcode": -1,
"zoom": 15
},
//special data below
customer: {
"id": 336,
"name": "Jorge",
"lastname": "Diaz",
"email": "[email protected]",
"dropdown_option_id": null,
"address": "500 5th Ave, New York, NY 10110, USA",
"address_notes": "The red house",
"zipcode": null,
"cellphone": "3016439796",
"phone": "3702781",
"location": {
"lat": 40.7314123,
"lng": -73.9969848
},
},
products: [
{
"id": 1319, // The product id
"quantity": 2, //The product quantity
"comment": "Example comment",
"ingrdients": [5], //ingredients used Ids
"options": [
{
"id": 53, //Option Id
"suboptions": [172] // Suboption ids
},
]
},
]
}
Placing order
This is the last one for place an order, create order, for this, we use the respective endpoint.
POST
-https://apiv4.ordering.co/v400/en/demo/orders
The data for this request is order
<?php
$input = json_decode(file_get_contents('php://input'));
//IMPUT MUST BE TRANSFOR TO THIS
// {
// "paymethod_id": 1, //INTEGER: IDENTIFIER OF PAYMETHOD IN ORDERING
// "business_id": 7, //INTEGER: IDENTIFIER OF THE STORE IN ODERING
// "delivery_type": "1", //INTEGER: THE ORDER TYPE IN ORDERING
// "driver_tip": 10, //FLOAT: THE TIP FOR THE DRIVER
// "delivery_zone_id": 20, //INTEGER: IDENTIFIER OF THE DELIVERY ZONE IN ORDERING
// "location": { //JSON: THE LOCATION OF THE CUSTOMER
// "lat": 40.7127753,
// "lng": -74.0059728
// ],
// "code": "M86O35RZIgCDKaQmXMXHwBjoe96z2Z", // RANDOM STRING TO AVOID COLITION WITH SIMILAR ORDERS
// "products": "[{\"id\":489,\"code\":\"UyWj4a\",\"quantity\":1,\"options\":[],\"ingredients\":[]},{\"id\":488,\"code\":\"gPQAhf\",\"quantity\":1,\"options\":[],\"ingredients\":[]}]",
// "customer_id": 2, //CUSTOMER IDENTIFIER IF REGISTERED ON ORDERING
// "customer": "{\"id\":2,\"name\":\"Customer\",\"middle_name\":null,\"lastname\":\"Demo\",\"second_lastname\":null,\"photo\":null,\"email\":\"[email protected]\",\"cellphone\":\"\",\"address\":\"New York, NY, USA\",\"location\":\"{\\\"lat\\\":40.7127753,\\\"lng\\\":-74.0059728}\",\"tag\":\"other\"}"
// }
//CUSTOMER OBJECT
$customer = [
"id" => 2,
"name" => "Customer", //Customer Name (string)
"middle_name" => null, //(string)
"lastname" => "Demo", //Customer LastName (string)
"second_lastname" => null, //(string)
"photo" => null, //(string) url
"email" => "[email protected]", //Customer Email (string)
"cellphone" => "", //customer phone (string)
"address" => "New York, NY, USA", //Customer Address (string)
"location" => "{\"lat\":40.7127753,\"lng\":-74.0059728}", //Customer location Must be a valid JSON string
"tag" => "other" //Just a tag to identify the addresses of the customer
];
//PRODUCTS OBJECT
$products = [
[
"id" => 489, //INTEGER: IDENTIFIER OF PRODUCT IN ORDERING
"code" => "UyWj4a", //STRING: RAMDOM IDEMTIFIER TO AVOID COLITION BETWEN TO SIMILAR PRODUCTS IN THE ORDER
"quantity" => 1, //INTEGER: QUANTITY OF PRODUCT SELECTED
"options" => [
"id" => 53,
"suboptions" => [
[
"id" => 172, //INTEGER: IDENTIFIER OF THE SUBOPTION
"position" => "whole", //STRING : SIDE OF SUBOPTION LEFT, RIGT, WHOLE.
"quantity" => 1 //INTEGER: QUANTITY OF SUBOPTION
]
]
],
[
"id" => 54,
"suboptions" => [
[
"id" => 175,
"position" => "whole",
"quantity" => 1
]
]
],
[
"id" => 55,
"suboptions" => [
[
"id" => 177,
"position" => "whole",
"quantity" => 1
]
]
],
"ingredients" => []
],
[
"id" => 488,
"code" => "gPQAhf",
"quantity" => 1,
"options" => [],
"ingredients" => []
]
];
//ORDER OBJECT
$order = [
"paymethod_id" => 1, //INTEGER => IDENTIFIER OF PAYMETHOD IN ORDERING
"business_id" => 7, //INTEGER => IDENTIFIER OF THE STORE IN ODERING
"delivery_type" => "1", //INTEGER => THE ORDER TYPE IN ORDERING
"driver_tip" => 10, //FLOAT => THE TIP FOR THE DRIVER
"delivery_zone_id" => 20, //INTEGER => IDENTIFIER OF THE DELIVERY ZONE IN ORDERING
"location" => [ //JSON: THE LOCATION OF THE CUSTOMER
"lat" => 40.7127753,
"lng" => -74.0059728
],
"code" => "M86O35RZIgCDKaQmXMXHwBjoe96z2Z", // RANDOM STRING TO AVOID COLITION WITH SIMILAR ORDERS
"products" => json_encode($products),
"customer_id" => 2, //CUSTOMER IDENTIFIER IF REGISTERED ON ORDERING
"customer" => json_decode($customer)
];
//NOTES REPLACE ALL THE VALUES WITH YOUR INPUT
function request($url, $method, $additional_headers, $data = null)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (in_array($method, ['PUT', 'POST'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
}
$additional_headers[] = 'Accept: application/json';
$additional_headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $additional_headers);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
//https://apiv4.ordering.co/v400/en/demo/orders
$api = "https://apiv4.ordering.co";
$version = 'v400';
$lang = 'en';
$project = 'demo';
$api_key = 'haiq567a7@215';
$url = "{$api}/{$version}/{$lang}/{$project}/orders";
$headers[] = 'x-api-key: '.$api_key;
$create_order = request($url, 'POST', $headers, json_encode($order));