Get latest trip updates (Node.js)
In this use case, the user receives the various updates of an ongoing trip
Code snippets
Client calls the BAP server to trigger status:
router.post("/mobility/get_order_status", getOrderStatus);
async function get_order_status({ body }, res) {
try {
// .. Validate the client request before below function
await generateStatusRequest(body)
} catch (error) {
res.status(500).send(httpResponse("NACK", error));
}
};
BAP server generates the protocol request body
// Code to generate the protocol request body i.e. function generateStatusRequest() specified above
async function generateStatusRequest(body) {
/*
Example Request JSON :
{
"context": {
"domain": "nic2004:60221",
"country": "IND",
"city": "std:080",
"action": "status",
"core_version": "0.9.1",
"bap_id": "https://mock_bap.com/",
"bap_uri": "https://mock_bap.com/beckn/",
"bpp_id": "https://mock_bpp.com/",
"bpp_uri": "https://mock_bpp.com/beckn/",
"transaction_id": "1209849124",
"message_id": "123412423423",
"timestamp": "2021-03-23T10:00:40.065Z"
},
"message": {
"order_id": "trip_1"
}
}
*/
//The below code generates the above example JSON.
const transactionId = _.get(body, "transactionId");
// Returns the Context including MessageId
const context = createContext(transactionId);
const statusRequestBody = {
context,
message: {
// Construct from the request
}
};
//call protocol status
const response = await callStatus(statusRequestBody);
res
.status(200)
.send({ ...response.data, messageId: context["message_id"] });
}
BAP server calls protocol status to the network
async function callStatus(statusRequestBody) {
// It lookups the registry for BG OR BPP
let uri = lookup();
// Construct Header
const headers = constructAuthHeader(); // Auth Header with digital Signature
return axios({ url: `${uri}/mobility/status`, method: "POST", headers, data: statusRequestBody });
}
BAP receives protocol on_status
/*
Example Response JSON:
{
"context": {
"domain": "nic2004:60221",
"country": "IND",
"city": "std:080",
"action": "on_status",
"core_version": "0.9.1",
"bap_id": "https://mock_bap.com/",
"bap_uri": "https://mock_bap.com/beckn/",
"bpp_id": "https://mock_bpp.com/",
"bpp_uri": "https://mock_bpp.com/beckn/",
"transaction_id": "1209849124",
"message_id": "12341242343",
"timestamp": "2021-03-23T10:00:40.065Z"
},
"message": {
"order": {
"id":"trip_1",
"state":"Waiting for passenger",
"items":[
{
"id": "sedan_spot",
"quantity": {
"count": 1
}
}
],
"billing": {
"name": "John Doe",
"address": {
"door": "21A",
"name": "ABC Apartments",
"locality": "HSR Layout",
"city": "Bengaluru",
"state": "Karnataka",
"country": "India",
"area_code": "560102"
},
"email": "[email protected]",
"phone": "+919876543210"
},
"fulfillment": {
"tracking": true,
"start": {
"location": {
"id": "user-location",
"descriptor": {
"name": "Current user location"
},
"gps": "12.9349377,77.6055586"
},
"instructions": {
"name": "pick up instructions",
"short_desc": "Ask doorman to ring 21A"
},
"time": {
"label": "ETA",
"duration": "P12M"
},
"contact": {
"phone": "+919999999999",
"email": "[email protected]"
}
},
"end": {
"location": {
"gps": "12.914028, 77.638698"
}
},
"agent": {
"name": "Joe",
"image": "string",
"dob": "1990-08-17",
"gender": "Male",
"phone": "9876543210"
}
},
"quote": {
"price": {
"currency": "INR",
"value": "180"
},
"breakup": [
{
"title": "Sedan Spot Booking",
"price": {
"currency": "INR",
"value": "170"
}
},
{
"title": "Service Charge",
"price": {
"currency": "INR",
"value": "10"
}
}
]
},
"payment": {
"uri": "https://api.bpp.com/pay?amt=$180&mode=upi&vpa=bpp@upi",
"tl_method": "http/get",
"params": {
"amount": "180",
"mode": "upi",
"vpa": "bpp@upi"
},
"type": "ON-FULFILLMENT",
"status": "NOT-PAID"
}
}
}
}
*/
// Auth middleware authenticates the digital signature of the incoming request
router.post("/mobility/on_status", auth, onStatus);
async function onStatus({ body }, res) {
// Save the response to Database
await saveToDb(body);
};
Client polls BAP to get the on_status results
// Endpoint for the client to poll the search data based on the message id
async function getMessageById(req) {
try {
const messageId = _.get(req, "messageId");
// Get the data using message Id
const response = await getData(messageId);
res.status(200).send(httpResponse('ACK', "", response));
} catch(error) {
res.status(500).send(httpResponse("NACK", error));
}
};