Adding billing and shipping details during checkout and receiving the final quote and payment terms for the diagnostic service (Node.js)

In this use case, the user provides all the information required by the diagnostic center to book an appointment and receives the final invoice with payment terms

Init

Code snippets

Client calls the BAP server to trigger init:

    router.post("/healthcare_diagnostics/init_order", initializeOrder);

    async function initializeOrder({ body }, res) {
        try {

            //  .. Validate the client request before below function
            await generateInitializeOrderRequest(body)
        } catch (error) {
            res.status(500).send(httpResponse("NACK", error));
        }
    };

BAP server generates the protocol request body

    async function generateInitializeOrderRequest(clientRequestBody) {
/*
Example Request JSON :
{
    "context": {
        "domain": "nic2004:85195",
        "country": "IND",
        "city": "std:080",
        "action": "init",
        "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": "health_checkup",
                    "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": {
                "type": "IN-PERSON",
                "end": {
                    "location": {
                        "id": "srs-koramangala",
                        "gps": "12.9349377,77.6055586"
                    },
                    "time": {
                        "range": {
                            "start": "2021-08-11T04:30:00.000Z",
                            "end": "2021-08-11T05:30:00.000Z"
                        }
                    },
                    "contact": {
                        "phone": "+919876543210",
                        "email": "[email protected]"
                    }
                }
            }
        }
    }
}
*/
        //The below code generates the above example JSON.
        const transactionId = _.get(clientRequestBody, "transactionId");
        // Returns the Context including MessageId
        const context = createContext(transactionId);
        const userBillingDetails = _.get(clientRequestBody, "userBillingDetails");
        const locationDetails = _.get(clientRequestBody, "locationDetails");
        const pickupInstructions = _.get(clientRequestBody, "pickupInstructions");
        const dropInstructions = _.get(clientRequestBody, "dropInstructions");
        const initRequestBody = {
            context,
            message: {
                // construct from the request
            };
        };
        //call protocol init order
        const response = await callInit(initRequestBody);
        res
        .status(200)
        .send({ ...response.data, messageId: context["message_id"] });
    }

BAP server calls protocol init to the network

    async function callInit(initRequestBody) {
        // 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_diagnostics/init`, method: "POST", data: initRequestBody, headers});
    }

BAP receives protocol on_init

/*
Example Response JSON:
{
    "context": {
        "domain": "nic2004:85195",
        "country": "IND",
        "city": "std:080",
        "action": "on_init",
        "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": "health_checkup",
                    "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": {
                "end": {
                    "location": {
                        "id": "srs-koramangala",
                        "gps": "12.9349377,77.6055586"
                    },
                    "time": {
                        "range": {
                            "start": "2021-08-11T04:30:00.000Z",
                            "end": "2021-08-11T05:30:00.000Z"
                        }
                    },
                    "contact": {
                        "phone": "+919876543210",
                        "email": "[email protected]"
                    }
                }
            },
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "1100"
                },
                "breakup": [
                    {
                        "title": "Lab Charges",
                        "price": {
                            "currency": "INR",
                            "value": "1000"
                        }
                    },
                    {
                        "title": "Service Charge",
                        "price": {
                            "currency": "INR",
                            "value": "100"
                        }
                    }
                ],
                "ttl": "P1H"
            },
            "payment": {
                "uri": "https://api.bpp.com/pay?amt=$amount&txn_id=ksh87yriuro34iyr3p4&mode=upi&vpa=bpp@upi",
                "tl_method": "http/get",
                "params": {
                    "transaction_id": "ksh87yriuro34iyr3p4",
                    "amount": "1100",
                    "mode": "upi",
                    "vpa": "bpp@upi"
                },
                "type": "ON-FULFILLMENT",
                "status": "NOT-PAID"
            }
        }
    }
}
*/
    // Auth middleware authenticates the digital signature of the incoming request
    router.post("/healthcare_diagnostics/on_init", auth, onTrack);

    async function onTrack({ body }, res) {
        // Save the response to Database
        await saveToDb(body);
    };

Client polls BAP to get the on_init 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));
        }
    };