Contacting delivery support for an order (Java)

In this use case, the user wants to contact the customer support due to an issue with the delivery of an order. Here the pharmacy returns the support info of the delivery service provider.

Support

Code snippets

Client calls the BAP server to trigger support:

    @PostMapping("/healthcare_pharmacy/contact_delivery")
    public ResponseEntity contactDelivery(
            @RequestHeader HttpHeaders headers,
            @RequestBody ClientSupportRequest request) {
        var response = bapApplicationService.support(request, headers);
        return ResponseEntity.ok(response);
    }

BAP server generates the protocol request body


/*
Example Request JSON:
{
    "context": {
        "domain": "nic2004:52311",
        "country": "IND",
        "city": "std:080",
        "action": "support",
        "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-03-23T10:00:40.065Z"
    },
    "message": {
        "ref_id": "fulfillment_1"
    }
}
*/

    public Response support(ClientSupportRequest request, HttpHeaders headers) {

        // Generate message id
        var messageId = UUID.randomUUID().toString();

        // Check if transaction id exists in the request.
        // Generate if not exists
        var txnId = StringUtils.hasText(request.getTransactionId())
                ? request.getTransactionId()
                : UUID.randomUUID().toString();

        // Construct the Context based on the request parameters
        var context = Context.builder().domain(request.getDomain())
                .action(Context.ActionEnum.support)
                .messageId(messageId)
                .transactionId(txnId)
                .transactionId(UUID.randomUUID().toString())
                .timestamp(new Date().toString())
                .build();

        // Construct the protocol specific object to be passed to the BPP
        // to get the support contact with the id of the provider
        var supportRequest = SupportRequest.builder()
                .context(context)
                .message(SupportMessage.builder()
                        .refId(request.getRefId())
                        .build())
                .build();

        return invokeSupport(supportRequest, headers);
    }
}

BAP server calls protocol support to the network

    public Response invokeSupport(SupportRequest request, HttpHeaders headers) {

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

        // Call BPP Support api from the returned endpoint.
        // Construct request headers with the public key
        var responseEntity = apiClient.post(url[0] + Context.ActionEnum.support,
                constructRequestHeaders(),
                request,
                Response.class);

        // Validate the received response
        if (responseEntity.getBody() == null || responseEntity.getBody().getError() != null ||
                "NACK".equals(responseEntity.getBody().getMessage().getAck().getStatus())) {
            // Return custom error to the client
            return null;
        }
        return Response.of("ACK", null);
    }

BAP receives protocol on_support

/*
Example Response JSON:
{
    "context": {
        "domain": "nic2004:52311",
        "country": "IND",
        "city": "std:080",
        "action": "on_support",
        "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-03-23T10:00:40.065Z"
    },
    "message": {
        "phone": "+919898989898",
        "email": "[email protected]",
        "uri": "http://support.bpp.com?fulfillment_id=1"
    }
}
*/
    @PostMapping("/bap/on_support")
    public ResponseEntity onSupport(
            @RequestHeader HttpHeaders headers,
            @RequestBody OnSupportRequest request) {
        var response = bapCallbackApplicationService.onSupport(request, headers);
        return ResponseEntity.ok(response);
    }

    public Response onSupport(OnSupportRequest request, HttpHeaders headers) {
        // Validate the headers received
        var isHeadersValid = validateHeaders(headers);
        // Construct and return error if the received headers are invalid
        if (!isHeadersValid) return null;

        // Store the data received based on message id for the client to poll
        saveToDB(request);

        return Response.of("ACK", null);
    }

Client polls BAP to get the on_support results

    // Endpoint for the client to poll to get the support information
    @GetMapping("/healthcare_pharmacy/on_get_support")
    public ResponseEntity onGetSupport(
            @PathVariable(ClientRoutes.PARAM_MESSAGE_ID) String messageId,
            @RequestHeader HttpHeaders headers) {
        var data = bapApplicationService.get(messageId);
        return ResponseEntity.ok(data);
    }