Send latest trip updates (Java)

In this use case, the user receives the various updates of an ongoing trip

Status

Code snippets

BPP receives protocol status

/*
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"
    }
}
*/
    // BPP Status Endpoint
    @PostMapping("/bpp/status")
    public ResponseEntity status(
            @RequestHeader HttpHeaders headers,
            @RequestBody StatusRequest request) {
        var response = bppApplicationService.status(request, headers);
        return ResponseEntity.ok(response);
    }

BPP processes the status

    public Response status(StatusRequest request, HttpHeaders headers) {
        var onStatusRequest = new OnStatusRequest();
        // Validate the headers
        var isHeadersValid = validateHeaders(headers);
        // Construct and return error
        if (!isHeadersValid) return null;

        // Get the trip status 
        var orderId = request.getMessage().getOrderId();

        // Execute business logic here

        // Generate the response
        onStatusRequest = generateStatusResponse(orderId);

        //invoke on_status api
        return invokeOnStatus(onStatusRequest, headers);
    }
}

BPP generates response

/*
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"
            }
        }
    }
}
*/
    public OnStatusRequest generateStatusResponse(String orderId) {
        var statusResult = StatusResponseBuilder.builder.result(orderId).build();
        // Convert response into protocol object similar to example above
        return statusResult;
    }

BPP calls protocol on_status

    public Response invokeOnStatus(OnStatusRequest statusResponse, HttpHeaders headers) {
        if(statusResponse.getError() == null) {
            // Call BAP on_status api with the status response

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

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