Sending tracking information of a delivery order (Java)

In this use case, the user requests for a live delivery tracking link that can be rendered on the application.

Track

Code snippets

BPP receives protocol track

/*
Example Request JSON:
{
    "context": {
        "domain": "delivery",
        "country": "IND",
        "city": "std:080",
        "action": "track",
        "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-23T08:29:27.933Z"
    },
    "message": {
        "order_id": "order_1"
    }
}
*/
    // BPP Track Endpoint
    @PostMapping("/bpp/track")
    public ResponseEntity track(
            @RequestHeader HttpHeaders headers,
            @RequestBody TrackRequest request) {
        var response = bppApplicationService.track(request, headers);
        return ResponseEntity.ok(response);
    }

BPP processes the track

    public Response track(TrackRequest request, HttpHeaders headers) {
        var onTrackRequest = new OnTrackRequest();
        // Validate the headers
        var isHeadersValid = validateHeaders(headers);
        // Construct and return error
        if (!isHeadersValid) return null;

        // Track the given order
        var trackOrder = request.getMessage().getOrderId();

        // Execute business logic here

        // Generate the response
        onTrackRequest = generateTrackingResponse(trackOrder);

        //invoke on_track api
        return invokeOnTrack(onTrackRequest, headers);
    }
}

BPP generates response

/*
Example Response JSON:
{
    "context": {
        "domain": "delivery",
        "country": "IND",
        "city": "std:080",
        "action": "on_track",
        "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-23T08:31:04.045Z"
    },
    "message": {
        "tracking": {
            "url": "https://track.bpp.com?order_id=order_1",
            "status": "active"
        }
    }
}
*/
    public OnTrackRequest generateTrackingResponse(TrackingDetails trackingDetails) {
        var trackResult = TrackResponseBuilder.builder.result(trackingDetails).build();
        // Convert response into protocol object similar to example above
        return trackResult;
    }

BPP calls protocol on_track

    public Response invokeOnTrack(OnTrackRequest trackResponse, HttpHeaders headers) {
        if(trackResponse.getError() == null) {
            // Call BAP on_track 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_track,
                constructResponseHeaders(),
                trackResponse,
                OnTrackRequest.class);
            return Response.of("ACK", null);
        } else {
            return Response.of("NACK", null);
        }
    }