Adding an offer the cart and receiving a discounted quote (Java)

In this use case, the user applies an offer to a cart to avail a discount on a particular item or the total cart value. This offer is usually entered by the user as a code that when added, requests the seller to update the quote. Sometimes, the code may be used to avail additional services like free shipping, free packaging etc

Select

Code snippets

Client calls the BAP server to trigger select:

    @PostMapping("/local_retail/add_offer")
    public ResponseEntity addOffer(
            @RequestHeader HttpHeaders headers,
            @RequestBody ClientSelectRequest request) {
        var response = bapApplicationService.generateSelectRequest(request, headers);
        return ResponseEntity.ok(response);
    }

BAP server generates the protocol request body


/*
Example Request JSON:
{
    "context": {
        "domain": "local_retail",
        "country": "IND",
        "city": "std:080",
        "action": "select",
        "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": {
        "order": {
            "offers": [
                {
                    "id": "offer_1"
                }
            ]
        }
    }
}
*/

    public Response generateSelectRequest(ClientSelectRequest 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.select)
                .messageId(messageId)
                .transactionId(txnId)
                .transactionId(UUID.randomUUID().toString())
                .timestamp(new Date().toString())
                .build();

        // Construct the protocol specific object to be passed to the BPP/BG
        // Add the selected item(s) from the catalog to get a draft quote
        var selectRequest = SelectRequest.builder()
                .context(context)
                .message(Message.builder().order(Order.builder()
                        .items(ClientAssembler.of(request.getItems()))
                        .build()).build())
                .build();

        return invokeSelect(selectRequest, headers);
    }
}

BAP server calls protocol select to the network

    public Response invokeSelect(SelectRequest 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 Select api from the returned endpoint.
        // Construct request headers with the public key
        var selectResponse = apiClient.post(url[0] + Context.ActionEnum.select,
                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 selectResponse.getBody();
    }

BAP receives protocol on_select

/*
Example Response JSON:
{
    "context": {
        "domain": "local_retail",
        "country": "IND",
        "city": "std:080",
        "action": "on_select",
        "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": {
        "order": {
            "items": [
                {
                    "id": "item_1",
                    "price" : {
                        "currency": "INR",
                        "value": "40"
                    },
                    "quantity": {
                        "selected": {
                            "count": 1
                        }
                    }
                },
                {
                    "id": "item_4",
                    "price" : {
                        "currency": "INR",
                        "value": "60"
                    },
                    "quantity": {
                        "selected": {
                            "count": 2
                        }
                    }
                }
            ],
            "offers": [
                {
                    "id": "offer_1"
                }
            ],
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "110"
                },
                "breakup": [
                    {
                        "title": "Brown Bread 400 gm",
                        "price": {
                            "currency": "INR",
                            "value": "40"
                        }
                    },
                    {
                        "title": "Good Life Toned Milk 1L",
                        "price": {
                            "currency": "INR",
                            "value": "120"
                        }
                    },
                    {
                        "title": "50 rupees off",
                        "price": {
                            "currency": "INR",
                            "value": "-50"
                        }
                    }
                ],
                "ttl": "P4D"
            }
        }
    }
}
*/
    @PostMapping("/bap/on_select")
    public ResponseEntity onSelect(
            @RequestHeader HttpHeaders headers,
            @RequestBody OnSelectRequest request) {
        var response = bapCallbackApplicationService.onSelect(request, headers);
        return ResponseEntity.ok(response);
    }

    public Response onSelect(OnSelectRequest 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_select results

    // Endpoint for the client to poll the select data based on the message id
    @GetMapping("/local_retail/on_select")
    public ResponseEntity selectByMessageId(
            @PathVariable(ClientRoutes.PARAM_MESSAGE_ID) String messageId,
            @RequestHeader HttpHeaders headers) {
        var data = bapApplicationService.get(messageId);
        return ResponseEntity.ok(data);
    }