Updating drop location of an active order (Java)

In this use case, the user requests the delivery agency to change the drop location of an active delivery. Sometimes, the delivery agency accepts the change if it’s terms of service allows it to do so. If for some reason, this is not possible, the user is prompted to cancel the order and place a fresh delivery request.

Update

Code snippets

BPP receives protocol update

/*
Example Request JSON:
{
    "context": {
        "domain": "delivery",
        "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": "fulfillment",
        "order": {
            "id": "order_1",
            "state": "Active",
            "items": [
                {
                    "id": "standard",
                    "quantity": {
                        "count": 1
                    }
                }
            ],
            "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": {
                        "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": "Don't ring doorbell"
                    },
                    "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": "40"
                },
                "breakup": [
                    {
                        "title": "Standard delivery",
                        "price": {
                            "currency": "INR",
                            "value": "40"
                        }
                    }
                ],
                "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": "40",
                    "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"
        }
    }
}
*/
    // BPP Update Endpoint
    @PostMapping("/bpp/update")
    public ResponseEntity update(
            @RequestHeader HttpHeaders headers,
            @RequestBody UpdateRequest request) {
        var response = bppApplicationService.update(request, headers);
        return ResponseEntity.ok(response);
    }

BPP processes the update

    public Response update(UpdateRequest request, HttpHeaders headers) {
        var onUpdateRequest = new OnUpdateRequest();
        // Validate the headers
        var isHeadersValid = validateHeaders(headers);
        // Construct and return error
        if (!isHeadersValid) return null;

        // Update the given order
        var updateOrder = request.getMessage().getOrder();

        // Execute business logic here

        // Generate the response
        onUpdateRequest = generateUpdatedOrderResponse(updateOrder);

        //invoke on_update api
        return invokeOnUpdate(onUpdateRequest, headers);
    }
}

BPP generates response

/*
Example Response JSON:
{
    "context": {
        "domain": "delivery",
        "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": "order_1",
            "state": "Active",
            "items": [
                {
                    "id": "standard",
                    "quantity": {
                        "count": 1
                    }
                }
            ],
            "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": {
                        "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": "Don't ring doorbell"
                    },
                    "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": "50"
                },
                "breakup": [
                    {
                        "title": "Standard delivery",
                        "price": {
                            "currency": "INR",
                            "value": "50"
                        }
                    }
                ],
                "ttl": "P4D"
            },
            "payment": {
                "uri": "https://api.bpp.com/pay?amt=$amount&txn_id=ksh87yriuro34iyr3p5&mode=upi&vpa=bpp@upi",
                "tl_method": "http/get",
                "params": {
                    "transaction_id": "ksh87yriuro34iyr3p5",
                    "amount": "10",
                    "mode": "upi",
                    "vpa": "bpp@upi"
                },
                "type": "ON-ORDER",
                "status": "NOT-PAID"
            },
            "created_at": "2021-06-23T07:33:02.655Z",
            "updated_at": "2021-06-23T07:33:02.655Z"
        }
    }
}
*/
    public OnUpdateRequest generateUpdatedOrderResponse(UpdatedOrderDetails updatedDetails) {
        var updatedResult = UpdateResponseBuilder.builder.result(updateingDetails).build();
        // Convert response into protocol object similar to example above
        return updatedResult;
    }

BPP calls protocol on_update

    public Response invokeOnUpdate(OnUpdateRequest updateResponse, HttpHeaders headers) {
        if(updateResponse.getError() == null) {
            // Call BAP on_update api with the order response

            // Call to look up function which returns the the public key and BAP Endpoint to be called 
            var url = lookUp(headers);

            var response = apiClient.post(url[0] + Context.ActionEnum.on_update,
                constructResponseHeaders(),
                updateResponse,
                OnUpdateRequest.class);
            return Response.of("ACK", null);
        } else {
            return Response.of("NACK", null);
        }
    }