Updating billing details of an active order (Node.js)

In this use case, the user sometimes updates the billing details of the order after the order has been placed. The seller then updates the order with the new billing details and sends back the updated order object.

Update

Code snippets

BPP receives protocol update

/*
Example Request JSON:
{
    "context": {
        "domain": "local_retail",
        "country": "IND",
        "city": "std:080",
        "action": "update",
        "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-06-23T07:33:02.655Z"
    },
    "message": {
        "update_target": "billing",
        "order": {
            "id":"298310283",
            "state":"Order placed",
            "provider": {
                "id": "pooja-stores",
                "locations": [
                    {
                        "id": "pooja-stores.koramangala"
                    }
                ]
            },
            "items": [
                {
                    "id": "item_1",
                    "quantity": {
                        "count": 1
                    }
                },
                {
                    "id": "item_4",
                    "quantity": {
                        "count": 2
                    }
                }
            ],
            "billing": {
                "name": "Jane Doe",
                "address": {
                    "door": "21B",
                    "name": "EFG Apartments",
                    "locality": "Electronic City",
                    "city": "Bengaluru",
                    "state": "Karnataka",
                    "country": "India",
                    "area_code": "560104"
                },
                "email": "[email protected]",
                "phone": "+919777543210",
                "created_at": "2021-06-15T07:08:36.211Z",
                "updated_at": "2021-06-15T07:08:36.211Z"
            },
            "fulfillment": {
                "type": "home-delivery",
                "tracking": false,
                "start": {
                    "location": {
                        "id": "pooja-stores.koramangala",
                        "descriptor": {
                            "name": "Pooja Stores"
                        },
                        "gps": "12.9349377,77.6055586"
                    },
                    "time": {
                        "range": {
                            "start": "2021-06-15T07:09:30.000Z",
                            "end": "2021-06-15T07:10:30.000Z"
                        }
                    },
                    "instructions": {
                        "name": "pick up instructions",
                        "short_desc": "Provide the order id"
                    },
                    "contact": {
                        "phone": "+919999999999",
                        "email": "[email protected]"
                    }
                },
                "end": {
                    "location": {
                        "gps": "12.835407, 77.662524",
                        "address": {
                            "door": "21B",
                            "name": "EFG Apartments",
                            "locality": "Electronic City",
                            "city": "Bengaluru",
                            "state": "Karnataka",
                            "country": "India",
                            "area_code": "560104"
                        }
                    },
                    "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": "+919777543210",
                        "email": "[email protected]"
                    }
                }
            },
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "180"
                },
                "breakup": [
                    {
                        "title": "Brown Bread 400 gm",
                        "price": {
                            "currency": "INR",
                            "value": "40"
                        }
                    },
                    {
                        "title": "Good Life Toned Milk 1L",
                        "price": {
                            "currency": "INR",
                            "value": "120"
                        }
                    }
                ],
                "ttl": "P4D"
            },
            "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": "180",
                    "mode": "upi",
                    "vpa": "bpp@upi"
                },
                "type": "ON-ORDER",
                "status": "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('/local_retail/update', auth, update);

    function update({ headers, body }, res) {
        try {
            const message = _.get(body, "message");
            const context = _.get(body, "context");
            if (!context) {
                return res.status(400).send(httpResponse("NACK", "Missing Context"));
            }
            if (!message) {
                return res.status(400).send(httpResponse("NACK", "Missing Message"));
            }
            // ... Returns the ack immediately and continue the processing after validation
            res.status(200).send(httpResponse("ACK"));
            processUpdate(headers, body)
        } catch (error) {
            res.status(500).send(httpResponse("NACK", error));
        }
    };

BPP processes the status

    function processUpdate(requestHeaders, updateRequestBody) {
        // Execute business logic here
        let updateResponse = {}
        generateResponse(requestHeaders, updateResponse)
    }

BPP generates response

    async function generateResponse(){
/*
Example Request JSON :
{
    "context": {
        "domain": "local_retail",
        "country": "IND",
        "city": "std:080",
        "action": "on_update",
        "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-06-23T07:33:02.655Z"
    },
    "message": {
        "order": {
            "id":"298310283",
            "state":"Order placed",
            "provider": {
                "id": "pooja-stores",
                "locations": [
                    {
                        "id": "pooja-stores.koramangala"
                    }
                ]
            },
            "items": [
                {
                    "id": "item_1",
                    "quantity": {
                        "count": 1
                    }
                },
                {
                    "id": "item_4",
                    "quantity": {
                        "count": 2
                    }
                }
            ],
            "billing": {
                "name": "Jane Doe",
                "address": {
                    "door": "21B",
                    "name": "EFG Apartments",
                    "locality": "Electronic City",
                    "city": "Bengaluru",
                    "state": "Karnataka",
                    "country": "India",
                    "area_code": "560104"
                },
                "email": "[email protected]",
                "phone": "+919777543210",
                "created_at": "2021-06-15T07:08:36.211Z",
                "updated_at": "2021-06-15T07:08:36.211Z"
            },
            "fulfillment": {
                "type": "home-delivery",
                "tracking": false,
                "start": {
                    "location": {
                        "id": "pooja-stores.koramangala",
                        "descriptor": {
                            "name": "Pooja Stores"
                        },
                        "gps": "12.9349377,77.6055586"
                    },
                    "time": {
                        "range": {
                            "start": "2021-06-15T07:09:30.000Z",
                            "end": "2021-06-15T07:10:30.000Z"
                        }
                    },
                    "instructions": {
                        "name": "pick up instructions",
                        "short_desc": "Provide the order id"
                    },
                    "contact": {
                        "phone": "+919999999999",
                        "email": "[email protected]"
                    }
                },
                "end": {
                    "location": {
                        "gps": "12.835407, 77.662524",
                        "address": {
                            "door": "21B",
                            "name": "EFG Apartments",
                            "locality": "Electronic City",
                            "city": "Bengaluru",
                            "state": "Karnataka",
                            "country": "India",
                            "area_code": "560104"
                        }
                    },
                    "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": "+919777543210",
                        "email": "[email protected]"
                    }
                }
            },
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "180"
                },
                "breakup": [
                    {
                        "title": "Brown Bread 400 gm",
                        "price": {
                            "currency": "INR",
                            "value": "40"
                        }
                    },
                    {
                        "title": "Good Life Toned Milk 1L",
                        "price": {
                            "currency": "INR",
                            "value": "120"
                        }
                    }
                ],
                "ttl": "P4D"
            },
            "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": "180",
                    "mode": "upi",
                    "vpa": "bpp@upi"
                },
                "type": "ON-ORDER",
                "status": "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.
        let onUpdateResponseBody = {}
        //call protocol on_update
        await callOnUpdate(requestHeaders, onUpdateResponseBody);
    }

BPP calls protocol on_update

    async function callOnUpdate(requestHeaders, onUpdateResponseBody) {
        // Take the subscriber Id from the header and calls the registry to get the url. If already cached it need not to call again
        const uri = await lookup(requestHeaders);
        // Construct Header
        const headers = constructAuthHeader(); // Auth Header with digital Signature
        return axios({ url: `${uri}/local_retail/on_track`, method: "POST", headers, data: onUpdateResponseBody })
    }