Sending tracking information of an order (Java)
In this use case, the user usually clicks on "Track Order" or sometimes, the application automatically renders the tracking screen as part of the order screen. Here the pharmacy usually sends a tracking link to the tracking page of the order.
Code snippets
BPP receives protocol track
/*
Example Request JSON:
{
"context": {
"domain": "local_retail",
"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": "nic2004:52110",
"country": "IND",
"city": "std:080",
"action": "on_track",
"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": {
"tracking": {
"tl_method": "http/get",
"url": "https://track.mock_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);
}
}