Confirmation of a postpaid order (Node.js)
In this use case, the user usually clicks on confirm and the order gets placed. Usually a moment later, the order gets confirmed by the pharmacy with the latest status update on the fulfillment of the order
Code snippets
Client calls the BAP server to trigger confirm:
router.post("/healthcare_pharmacy/confirm_postpaid_order", confirmOrder);
async function confirmOrder({ body }, res) {
try {
// .. Validate the client request before below function
await generateConfirmOrderRequest(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 generateConfirmOrderRequest() specified above
async function generateConfirmOrderRequest(clientRequestBody) {
/*
Example Request JSON :
{
"context": {
"domain": "nic2004:52311",
"country": "IND",
"city": "std:080",
"action": "confirm",
"core_version": "0.9.2",
"bap_id": "https://mock_bap.com/",
"bap_uri": "https://mock_bap.com/beckn/",
"transaction_id": "1239890342",
"message_id": "123793824",
"timestamp": "2021-03-23T10:00:40.065Z"
},
"message": {
"order": {
"items": [
{
"id": "vit_c",
"price" : {
"currency": "INR",
"value": "500"
},
"quantity": {
"selected": {
"count": 1
}
}
},
{
"id": "a_500",
"price" : {
"currency": "INR",
"value": "500"
},
"quantity": {
"selected": {
"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": {
"type": "HOME-DELIVERY",
"start": {
"location": {
"id": "abc_medicines_koramangala",
"descriptor": {
"name": "ABC Medicines"
},
"gps": "12.9349377,77.6055586"
},
"contact": {
"phone": "+919999999999",
"email": "[email protected]"
}
},
"end": {
"location": {
"gps": "12.914028, 77.638698",
"address": {
"door": "21A",
"name": "ABC Apartments",
"locality": "HSR Layout",
"city": "Bengaluru",
"state": "Karnataka",
"country": "India",
"area_code": "560102"
}
},
"time": {
"range": {
"start": "2021-06-15T07:11:36.212Z",
"end": "2021-06-15T07:12:36.212Z"
}
},
"instructions": {
"name": "drop off instructions",
"short_desc": "Leave at door step"
},
"contact": {
"phone": "+919876543210",
"email": "[email protected]"
}
},
"customer": {
"person":{
"name": "John Doe",
"image": "https://mock_bap.com/prescription1.jpg",
"dob": "1991-08-12"
}
}
},
"quote": {
"price": {
"currency": "INR",
"value": "1100"
},
"breakup": [
{
"title": "Vitamin C",
"price": {
"currency": "INR",
"value": "500"
}
},
{
"title": "Antibiotics",
"price": {
"currency": "INR",
"value": "500"
}
},
{
"title": "Delivery Charge",
"price": {
"currency": "INR",
"value": "100"
}
}
],
"ttl": "P1H"
},
"payment": {
"uri": "https://api.bpp.com/pay?amt=$amount&txn_id=ksh87yriuro34iyr3p4&mode=upi&[email protected]",
"tl_method": "http/get",
"params": {
"transaction_id": "ksh87yriuro34iyr3p4",
"amount": "180",
"mode": "upi",
"vpa": "[email protected]"
},
"type": "ON-FULFILLMENT",
"status": "NOT-PAID"
},
"created_at": "2021-06-23T07:33:02.655Z",
"updated_at": "2021-06-23T07:33:02.655Z"
}
}
}
*/
//The below code generates the above example JSON.
const transactionId = _.get(clientRequestBody, "transactionId");
// Returns the Context including MessageId
const context = createContext(transactionId);
let confirmRequestBody = {
context,
message: {
// construct from the request
}
}
//call protocol confirm order
const response = await callConfirm(confirmRequestBody);
res
.status(200)
.send({ ...response.data, messageId: context["message_id"] });
}
BAP server calls protocol confirm to the network
async function callConfirm(confirmRequestBody) {
// 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}/healthcare_pharmacy/confirm`, method: "POST", data: confirmRequestBody, headers});
}
BAP receives protocol on_confirm
/*
Example Response JSON:
{
"context": {
"domain": "nic2004:52311",
"country": "IND",
"city": "std:080",
"action": "on_confirm",
"core_version": "0.9.2",
"bap_id": "https://mock_bap.com/",
"bap_uri": "https://mock_bap.com/beckn/",
"transaction_id": "1239890342",
"message_id": "123793824",
"timestamp": "2021-03-23T10:00:40.065Z"
},
"message": {
"order": {
"id": "order_1",
"state": "Active",
"items": [
{
"id": "vit_c",
"price" : {
"currency": "INR",
"value": "500"
},
"quantity": {
"selected": {
"count": 1
}
}
},
{
"id": "a_500",
"price" : {
"currency": "INR",
"value": "500"
},
"quantity": {
"selected": {
"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": {
"type": "HOME-DELIVERY",
"start": {
"location": {
"id": "abc_medicines_koramangala",
"descriptor": {
"name": "ABC Medicines"
},
"gps": "12.9349377,77.6055586"
},
"contact": {
"phone": "+919999999999",
"email": "[email protected]"
}
},
"end": {
"location": {
"gps": "12.914028, 77.638698",
"address": {
"door": "21A",
"name": "ABC Apartments",
"locality": "HSR Layout",
"city": "Bengaluru",
"state": "Karnataka",
"country": "India",
"area_code": "560102"
}
},
"time": {
"range": {
"start": "2021-06-15T07:11:36.212Z",
"end": "2021-06-15T07:12:36.212Z"
}
},
"instructions": {
"name": "drop off instructions",
"short_desc": "Leave at door step"
},
"contact": {
"phone": "+919876543210",
"email": "[email protected]"
}
},
"customer": {
"person":{
"name": "John Doe",
"image": "https://mock_bap.com/prescription1.jpg",
"dob": "1991-08-12"
}
}
},
"quote": {
"price": {
"currency": "INR",
"value": "1100"
},
"breakup": [
{
"title": "Vitamin C",
"price": {
"currency": "INR",
"value": "500"
}
},
{
"title": "Antibiotics",
"price": {
"currency": "INR",
"value": "500"
}
},
{
"title": "Delivery Charge",
"price": {
"currency": "INR",
"value": "100"
}
}
],
"ttl": "P1H"
},
"payment": {
"uri": "https://api.bpp.com/pay?amt=$amount&txn_id=ksh87yriuro34iyr3p4&mode=upi&[email protected]",
"tl_method": "http/get",
"params": {
"transaction_id": "ksh87yriuro34iyr3p4",
"amount": "180",
"mode": "upi",
"vpa": "[email protected]"
},
"type": "ON-FULFILLMENT",
"status": "NOT-PAID"
},
"created_at": "2021-06-23T07:33:02.655Z",
"updated_at": "2021-06-23T07:33:02.655Z"
}
}
}
*/
// Auth middleware authenticates the digital signature of the incoming request
router.post("/healthcare_pharmacy/on_confirm", auth, onTrack);
async function onTrack({ body }, res) {
// Save the response to Database
await saveToDb(body);
};
Client polls BAP to get the on_confirm 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));
}
};