Apetito24 API
Apetito24 API
Apetito24 API
First of all you should decide which kind of integration to use. You have two types of possible integrations:
Note: When you are using point to point method then you need an extra pair of credentials. This extra pair
of credentials correspond to the restaurant itself.
Authentication
$credentials = new Credentials();
$credentials>setClientId('your_client_id');
$credentials>setClientSecret('your_client_secret');
$credentials>setEnvironment(Environments::$DEVELOPMENT);
This kind of credentials will work for all the restaurants handled by the third party application.
$credentials = new Credentials();
$credentials>setClientId('your_client_id');
$credentials>setClientSecret('your_client_secret');
$credentials>setUsername('restaurant_username');
$credentials>setPassword('restaurant_secret');
$credentials>setEnvironment(Environments::$DEVELOPMENT);
Environments
This SDK supports multiple environments:
Development: Default environment, used for development only.
Staging: Pre-production environment, used before every release for final testings.
Production: Production environment, used for real life.
Note: Please be sure that you deploy the application with the right environment.
Basic usage
Whenever you want to use the SDK, you must include the .phar provided at the beginning and use the
classes that you need.
include './pyreceptionsdk.phar';
use ReceptionSDK\Http\Credentials;
Once you have the Credentials object created you must create an ApiClient object using the previous
credential object.
$credentials = new Credentials();
$credentials>setClientId('your_client_id');
$credentials>setClientSecret('your_client_secret');
$credentials>setEnvironment(Environments::$DEVELOPMENT);
try {
$api = new ApiClient($credentials);
$deliveryTimes = $api>order()>deliveryTime()>getAll();
} catch (ApiException $e) {
echo 'Uups an error has occurred!';
}
Important: You must instantiate the ApiClient at the very beginning of your application and save that
instance of ApiClient (for ex: in an static variable) and use that instance in the whole life cycle of your
application. Please do not instantiate the ApiClient inside a for loop for example.
Pagination
Some operations needs a PaginationOptions parameter. This parameter is to handle pagination.
You have the possibility to use the default configuration for pagination invoking the method create:
$paginationOptions = PaginationOptions::create();
This method by default sets a limit of fifteen results and an offset equal to zero. If you want to specify the
pagination options you can use:
$paginationOptions = PaginationOptions::create()>withOffset(10)>withLimit
(50);
This configuration sets a limit of fifty results and a offset equal to ten.
You can use the method next to iterate over the pages:
$paginationOptions = PaginationOptions::create();
$restaurants = [];
$newRestaurants = $api>restaurant()>getAll($paginationOptions);
$restaurants = array_merge($restaurants, $newRestaurants);
while (count($newRestaurants) > 0) {
$newRestaurants = $api>restaurant()>getAll($paginationOptions>next()
);
$restaurants = array_merge($restaurants, $newRestaurants);
}
Orders operations
First of all, you must import our delivery times and rejection messages, what is a delivery time? and a
rejection message?.
Important: DeliveryTimes and RejectMessages are dynamic, as well as their attributes, so do not harcode
them.
A good practice is to import them every day or on application start up, and save them somewhere in your
application.
You can get all the delivery times using this operation
$deliveryTimes = $api>order()>deliveryTime()>getAll();
List of delivery times
6 24 horas 24 horas
7 48 horas 48 horas
8 72 horas 72 horas
10 12 horas 12 horas
$rejectMessages = $api>order()>rejectMessage()>getAll();
There are two important attributes in RejectMessage that you have to consider: ForLogistics, ForPickup.
When rejecting an order that has the flag Logistics in true, you must not use/show reject messages that
have ForLogistics in false.
The same rule applies to orders with the flag PickUp in true.
The field to show in your system is DescriptionES.
Example:
$mySavedRejectMessages; // previously imported reject messages
$rejectMessagesForThisOrder = $mySavedRejectMessages;
foreach ($rejectMessagesForThisOrder as $i => $rejectMessage) {
if ($order>logistics) {
if (!$rejectMessage>forLogistics) {
unset($rejectMessagesForThisOrder[$i]);
}
}
if ($order>pickup) {
if (!$rejectMessage>forPickup) {
unset($rejectMessagesForThisOrder[$i]);
}
}
}
$orders = $api>order()>getAll(OrderState::$PENDING, PaginationOptions::cr
eate());
foreach ($orders as $order) {
doSomething($order);
}
When getting pending orders, the second parameter (PaginationOptions) is ignored and all pending orders
are returned.
Pagination options is considered when requesting confirmed or rejected orders.
$api>order()>getAll(
OrderState::$PENDING,
PaginationOptions::create(),
function ($order) use ($api) {
if (everythingOk($order)) {
$api>order()>confirm($order>id, $deliveryTimeId);
}
},
function (ApiException $e) {
throw $e;
}
);
Note: Keep in mind that you will receive one order per callback so be aware that your import mechanism
shouldn’t take so long.
Regular orders
The client order for food when the restaurant is opened. These kind of orders are for immediately delivery
and they have the properties pickup, preOrder and logistics in false. The restaurant should delivery the
order approximately at the time specified in deliveryDate
Pickup orders
Some orders are not for delivery, instead the client will pick up the order in the restaurant. All the pick up
orders have the property pickup in true, address in null and the estimated pickup time in pickupDate.
Anticipated orders
The client can order for food before the restaurant it’s opened, if that’s the case then the preOrder property
has the value true. The restaurant should delivery the order at the time specified in deliveryDate.
Logistics orders
This only applies if the restaurant delivery it’s handled by PedidosYa’s fleet, otherwise you should ignore
this.
All orders with logistics are identified by the property logistics in true. In this case you must pay attention
to the pickupDate property, this field contains the time that one rider will pick up the order to be delivered.
We handle two kinds of discounts as specified in the property type in the Discount Model:
The Order model contains a list of discounts, with the corresponding priority of each discount.
If the order was paid with an online payment method then the property online in the payment property of the
order is in true.
Confirm an order
Once you processed the order and it’s ready to be cooked you must confirm it indicating the estimated
delivery time.
Note: For Regular orders and Pick up orders the method for confirmation it’s the same, it’s mandatory to
specify the delivery time when the order is going to be confirmed, otherwise you must confirm the order
without a delivery time, if you specify one, you are going to receive an error.
try {
$deliveryTimeId = restaurantSelectedTime();
$order = orderToConfirm();
if ($order>preOrder || $order>logistics) {
$result = $api>order()>confirm($orderId);
} else {
$result = $api>order()>confirm($orderId, $deliveryTimeId);
}
if ($result) {
confirmOrderLocally();
}
} catch (ApiException $ex) {
if ($ex>getErrorCode() == ErrorCode::$INTERNAL_SERVER_ERROR) {
// Wait 5 seconds and retry again
// Don't retry more than three times
} else {
throw $ex;
}
}
You can use order and delivery time ids directly, or an object with an id property.
Reject an order
If you can’t process the order or the order cannot be delivered for some reason you should reject it.
try {
$rejectMessageId = restaurantRejectReason();
$orderId = orderToReject();
$result = $api>order()>reject($orderId, $rejectMessageId);
if ($result) {
rejectOrderLocally();
}
} catch (ApiException $ex) {
if ($ex>getErrorCode() == ErrorCode::$INTERNAL_SERVER_ERROR) {
// Wait 5 seconds and retry again
// Don't retry more than three times
} else {
throw $ex;
}
}
You can use order and reject message ids directly, or an object with an id property.
try {
$rejectMessageId = restaurantRejectReason();
$orderId = orderToReject();
$rejectNote = 'Rejection note';
$result = $api>order()>reject($orderId, $rejectMessageId, $rejectNote
);
if ($result) {
rejectOrderLocally();
}
} catch (ApiException $ex) {
if ($ex>getErrorCode() == ErrorCode::$INTERNAL_SERVER_ERROR) {
// Wait 5 seconds and retry again
// Don't retry more than three times
} else {
throw $ex;
}
}
Dispatch an order
Once the order is cooked and it is ready to deliver, you must call this method to indicate the order is on the
way to the client address.
try {
$orderId = orderToDispatch();
$result = $api>order()>dispatch($orderId);
if ($result) {
dispatchOrderLocally();
}
} catch (ApiException $ex) {
if ($ex>getErrorCode() == ErrorCode::$INTERNAL_SERVER_ERROR) {
// Wait 5 seconds and retry again
// Don't retry more than three times
} else {
throw $ex;
}
}
$orders = $api>order()>getAll(OrderState::$CONFIRMED, PaginationOptions::
create());
if (count($orders) > 0) {
doSomething($orders);
}
In case you want the rejected ones just change OrderState::$CONFIRMED with OrderState::$REJECTED
Note: This will not return all the orders, the response is limited to the last 15 orders as explained
in the pagination section.
A good practice it’s to save the order id in your system.
If you have the order id then you can ask for a particular order.
$orderId = orderId();
$order = $api>order()>get($orderId);
if ($order) {
doSomething($order);
}
The pickup date and the estimated delivery date are the necessary values to update in the order. The
driver’s name and coordinates are part of the aditional information attached to the order tracking info.
$orderId = orderId();
$tracking = $api>order()>tracking($orderId);
if ($tracking) {
$trackingState = $tracking>state;
// driver info
$driver = $tracking>driver;
// location info, lat & lng attributes for the driver's coordinates
$location = $driver>location
if ($trackingState == TrackingState::$PREPARING) {
updatePickupDate($tracking);
}
}
Restaurants operations
There are some operations to get restaurants you could receive orders, and change the state of those
restaurants to ONLINE or OFFLINE.
You can close a restaurant with its id and a specific range of dates (in restaurant’s country time). You can
also open a restaurant previously closed (you can only open
restaurant’s closed by the operation close) from a certain date.
Get restaurants
With this operation, you can get all restaurants that receives orders through your system.
$restaurants = $api>restaurant()>getAll(PaginationOptions::create());
Whit this operation, you can figure out PedidosYa’s id, to invoke later open and close operations if
necessary.
It is also possible to use a second parameter to get restaurants with a specific integration code.
Close a restaurant
For example, to close a restaurant for 2 hours from now, you can write:
$id = getRestaurantIdToClose(); // PedidosYa restaurant id
$now = new DateTime('now', new DateTimeZone('America/Montevideo'));
$from = $now>format('Ymd H:i:s');
$to = $now>add(new DateInterval('PT2H'))>format('Ymd H:i:s');
$reason = 'Without deliveries';
$close = $api>restaurant()>close($id, $from, $to, $reason);
Open a restaurant
$id = getRestaurantIdToClose(); // PedidosYa restaurant id
$now = new DateTime('now', new DateTimeZone('America/Montevideo'));
$from = $now>format('Ymd H:i:s');
$open = $api>restaurant()>open($id, $from);
Remind that to open a restaurant, it have to be closed before by you, so this operation can be use to delete
future close operations created.
This will remove all close requests with from greater than the from specified in this operation.
Menus operations
If product mapping it’s supported, then you should be able to handle stock and price changes.
It’s possible to disable, enable and change the price of products and optionals.
The change will be immediately visible in our website and apps.
If you want to disable a product through the integrationCode field then you need to use the menu()-
>product() operations, otherwise if it’s an option also mapped through the IntegrationCode field then you
have to use the menu()->option() operations.
For example:
$order = someOrder();
$productCode = $order>details[0]>product>integrationCode;
$optionCode = $order>details[0]>optionGroups[0]>options[0]>integrationC
ode;
// Important: restaurantId must be an integer
$restaurantId = intval($order>restaurant>id);
$restaurantCode = $order>restaurant>integrationCode;
$api>menu()>product()>disable($productCode);
$api>menu()>product()>disable($productCode, $restaurantId);
$api>menu()>product()>disable($productCode, $restaurantCode);
$api>menu()>option()>disable($optionCode);
$api>menu()>option()>disable($optionCode, $restaurantId);
$api>menu()>option()>disable($optionCode, $restaurantCode);
Note: If a product is also mapped as an option then you should call the operations separately.
Disable a product
$productCode = productCodeToDisable();
$api>menu()>product()>disable($productCode);
$productCode = productCodeToDisable();
$restaurantId = intval(restaurantId());
$api>menu()>product()>disable($productCode, $restaurantId);
Enable a product
$productCode = productCodeToEnable();
$api>menu()>product()>enable($productCode);
$productCode = productCodeToEnable();
$restaurantId = intval(restaurantId());
$api>menu()>product()>enable($productCode, $restaurantId);
$productCode = productCodeToEnable();
$restaurantCode = restaurantCode();
$api>menu()>product()>enable($productCode, $restaurantCode);
$productCode = productCodeToChangePrice();
$newPrice = doubleval(calculateNewPrice());
$api>menu()>product()>price($productCode, $newPrice);
$productCode = productCodeToChangePrice();
$restaurantId = intval(restaurantId());
$newPrice = doubleval(calculateNewPrice());
$api>menu()>product()>price($productCode, $newPrice, $restaurantId);
$optionCode = optionCodeToDisable();
$api>menu()>option()>disable($optionCode);
$optionCode = optionCodeToDisable();
$restaurantId = intval(restaurantId());
$api>menu()>option()>disable($optionCode, $restaurantId);
$optionCode = optionCodeToDisable();
$restaurantCode = restaurantCode();
$api>menu()>option()>disable($optionCode, $restaurantCode);
$optionCode = optionCodeToEnable();
$api>menu()>option()>enable($productCode);
$optionCode = optionCodeToEnable();
$restaurantId = intval(restaurantId());
$api>menu()>option()>enable($productCode, $restaurantId);
$productCode = optionCodeToEnable();
$restaurantCode = restaurantCode();
$api>menu()>option()>enable($productCode, $restaurantCode);
$optionCode = optionCodeToChangePrice();
$newPrice = doubleval(calculateNewPrice());
$api>menu()>option()>price($productCode, $newPrice);
$optionCode = optionCodeToChangePrice();
$restaurantId = intval(restaurantId());
$newPrice = doubleval(calculateNewPrice());
$api>menu()>option()>price($productCode, $newPrice, $restaurantId);
$optionCode = optionCodeToChangePrice();
$restaurantCode = restaurantCode();
$newPrice = doubleval(calculateNewPrice());
$api>menu()>option()>price($productCode, $newPrice, $restaurantCode);
Events operations
Some of the events operations are mandatory operations that must be implemented. Ignoring this
operations will indeed cause the close of the restaurant or other kind of penalizations over PedidosYa
platform.
$version = [];
$version['os'] = 'Ubuntu 16.04 x64';
$version['app'] = '1.3.5';
$api>event()>initialization($version);
$version = [];
$version['os'] = 'Ubuntu 16.04 x64';
$version['app'] = '1.3.5';
$restaurantId = intval(restaurantId());
$api>event()>initialization($version, $restaurantId);
$version = [];
$version['os'] = 'Ubuntu 16.04 x64';
$version['app'] = '1.3.5';
$restaurantCode = restaurantCode();
$api>event()>initialization($version, $restaurantCode);
try {
$api>event()>heartBeat();
} catch (ApiException $ex) {
// Something went wrong
} catch (Exception $ex) {
// Something went wrong
}
try {
$restaurantId = intval(restaurantId());
$api>event()>heartBeat($restaurantId);
} catch (ApiException $ex) {
// Something went wrong
} catch (Exception $ex) {
// Something went wrong
}
try {
$restaurantCode = restaurantCode();
$api>event()>heartBeat($restaurantCode);
} catch (ApiException $ex) {
// Something went wrong
} catch (Exception $ex) {
// Something went wrong
}
$orderId = orderReceivedId();
$api>event()>reception($orderId);
$orderId = orderReceivedId();
$restaurantId = intval(restaurantId());
$api>event()>reception($orderId, $restaurantId);
$orderId = orderReceivedId();
$restaurantCode = restaurantCode();
$api>event()>reception($orderId, $restaurantCode);
$orderId = orderReceivedId();
$restaurantId = intval(restaurantId());
$api>event()>acknowledgement($orderId, $restaurantId);
$orderId = orderReceivedId();
$restaurantCode = restaurantCode();
$api>event()>acknowledgement($orderId, $restaurantCode);
$orderId = orderReceivedId();
$api>event()>stateChange($orderId, OrderState::$CONFIRMED);
$orderId = orderReceivedId();
$restaurantId = intval(restaurantId());
$api>event()>stateChange($orderId, OrderState::$CONFIRMED, $restaurantId)
;
$orderId = orderReceivedId();
$restaurantCode = restaurantCode();
$api>event()>stateChange($orderId, OrderState::$CONFIRMED, $restaurantCod
e);
Warning event
This event represents that the reception system is in a warning state, for example: low battery, lack of
paper, etc.
You must provide the warning internal identification code and a description.
$warningCode = 'LOW_BAT';
$warningDescription = 'Low battery. Please plug in the device';
$api>event()>warning($warningCode, $warningDescription);
$warningCode = 'LOW_BAT';
$warningDescription = 'Low battery. Please plug in the device';
$restaurantId = intval(restaurantId());
$api>event()>warning($warningCode, $warningDescription, $restaurantId);
$warningCode = 'LOW_BAT';
$warningDescription = 'Low battery. Please plug in the device';
$restaurantCode = restaurantCode();
$api>event()>warning($warningCode, $warningDescription, $restaurantCode);
Error event
This event represents a error, for example: missing product code, can’t confirm order, error processing
order.
You must provide the error internal identification code and a description.
$errorCode = 'MISSING_CODE';
$errorDescription = 'Missing code for product with description \'Burger XL\
'';
$api>event()>error($errorCode, $errorDescription);
$errorCode = 'MISSING_CODE';
$errorDescription = 'Missing code for product with description \'Burger XL\
'';
$restaurantId = intval(restaurantId());
$api>event()>error($errorCode, $errorDescription, $restaurantId);
$errorCode = 'MISSING_CODE';
$errorDescription = 'Missing code for product with description \'Burger XL\
'';
$restaurantCode = restaurantCode();
$api>event()>error($errorCode, $errorDescription, $restaurantCode);
Models
In this chapter is the description of the different models you will be seen in different operation responses.
Address
All the needed information about the user address. The address is an attribute of the order.
complement (string): The user address complement. For example: Yellow house.
phone (string): The user address phone.
area (string): The user area, a.k.a: neighborhood.
corner (string): The user address street corner.
description (string): The address formatted: street, corner, number, etc. For example: ‘Plaza
Independencia 743 esquina Ciudadela’.
street (string): The user address street.
zipCode (string): Returns the user address zip code. This fields depends on the country, for example
in Uruguay doesn’t make much sense but in Brazil references to the user CEP.
notes (string): The address notes things such as ‘the blue house’, ‘the ring doesn’t work’, etc.
doorNumber (string): The user address door number.
coordinates (string): The user address coordinates.
city (string): The user city.
DeliveryTime
All the needed information about the possible delivery times for the orders.
Discount
All the information related to a discount.
DiscountType
Type of the payment discount in the payment attribute of the order.
Option
All the needed information about the product option.
OptionGroup
Order
All the order data. The order details, client information, payment method, etc.
application (string): Return the application type that the order has been created. It’s one of the
following: WEB, CALL, IPHONE, ANDROID, WIDGET, WINDOWS_PHONE, WEB_MOBILE, IPAD.
payment (Payment): The payment method of the order.
pickup (bool): true if the order is for pickup, false if is for delivery.
responseDate (DateTime): The order response date.
dispatchDate (DateTime): The order dispatch date
portal (Portal): The portal of the order that belongs to.
registeredDate (DateTime): The order registration date.
state (OrderState): The order state.
code (string): The order identification code.
restaurant (Restaurant): The order restaurant.
express (bool): true if the order is an express order, false if is for delivery.
id (int): The order identification number
details (array of Detail): The order details data.
discounts (array of Discount): The list of discounts applied.
address (Address): The user address.
deliveryDate: Returns the delivery date of the order.
notes (string): The order notes.
whiteLabel (string): The white label of the order that belongs to.
user (User): The user who made the order.
OrderState
PENDING (string): State of the order when is new and ready to be answered.
CONFIRMED (string): State of the order when is confirmed.
REJECTED (string): State of the order when is rejected.
Payment
All payment information of the order.
Portal
Product
All the product data.
RejectMessage
All information about possible rejection messages.
Restaurant
All the restaurant information of an order.
Section
All the information about the section of a product.
User
All the user data. The user email, full name, etc.
Driver
name (string): The driver’s name.
location (Location): The driver’s location.
Location
lat (double): The location’s latitude.
lng (double): The location’s longitude.