Confirmation of a postpaid diagnostic service (Java)

In this use case, the user confirms confirms the appointment with the diagnostic center

Confirm

Code snippets

BPP receives protocol confirm

/*
Example Request JSON:
{
    "context": {
        "domain": "nic2004:85195",
        "country": "IND",
        "city": "std:080",
        "action": "confirm",
        "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": "health_checkup",
                    "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": {
                "end": {
                    "location": {
                        "id": "srs-koramangala",
                        "gps": "12.9349377,77.6055586"
                    },
                    "time": {
                        "range": {
                            "start": "2021-08-11T04:30:00.000Z",
                            "end": "2021-08-11T05:30:00.000Z"
                        }
                    },
                    "contact": {
                        "phone": "+919876543210",
                        "email": "[email protected]"
                    }
                }
            },
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "1100"
                },
                "breakup": [
                    {
                        "title": "Lab Charges",
                        "price": {
                            "currency": "INR",
                            "value": "1000"
                        }
                    },
                    {
                        "title": "Service Charge",
                        "price": {
                            "currency": "INR",
                            "value": "100"
                        }
                    }
                ],
                "ttl": "P1H"
            },
            "payment": {
                "uri": "https://api.bpp.com/pay?amt=$amount&txn_id=ksh87yriuro34iyr3p4&mode=upi&vpa=bpp@upi",
                "tl_method": "http/get",
                "params": {
                    "transaction_id": "ksh87yriuro34iyr3p4",
                    "amount": "180",
                    "mode": "upi",
                    "vpa": "bpp@upi"
                },
                "type": "ON-FULFILLMENT",
                "status": "NOT-PAID"
            },
            "created_at": "2021-06-23T07:33:02.655Z",
            "updated_at": "2021-06-23T07:33:02.655Z"
        }
    }
}
*/
    // BPP Confirm Endpoint
    @PostMapping("/bpp/confirm")
    public ResponseEntity confirm(
            @RequestHeader HttpHeaders headers,
            @RequestBody ConfirmRequest request) {
        var response = bppApplicationService.confirm(request, headers);
        return ResponseEntity.ok(response);
    }

BPP processes the confirm

    public Response confirm(ConfirmRequest request, HttpHeaders headers) {
        var onConfirmRequest = new OnConfirmRequest();
        // Validate the headers
        var isHeadersValid = validateHeaders(headers);
        // Construct and return error
        if (!isHeadersValid) return null;

        // Confirm the given order
        var confirmOrder = request.getMessage().getOrder();

        // Execute business logic here

        // Generate the response
        onConfirmRequest = generateConfirmResponse(confirmOrder);

        //invoke on_conrim api
        return invokeOnConfirm(onConfirmRequest, headers);
    }
}

BPP generates response

/*
Example Response JSON:
{
    "context": {
        "domain": "nic2004:85195",
        "country": "IND",
        "city": "std:080",
        "action": "on_confirm",
        "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": {
            "id": "order_1",
            "state": "Active",
            "items": [
                {
                    "id": "health_checkup",
                    "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": {
                "end": {
                    "location": {
                        "id": "srs-koramangala",
                        "gps": "12.9349377,77.6055586"
                    },
                    "time": {
                        "range": {
                            "start": "2021-08-11T04:30:00.000Z",
                            "end": "2021-08-11T05:30:00.000Z"
                        }
                    },
                    "contact": {
                        "phone": "+919876543210",
                        "email": "[email protected]"
                    }
                }
            },
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "1100"
                },
                "breakup": [
                    {
                        "title": "Lab Charges",
                        "price": {
                            "currency": "INR",
                            "value": "1000"
                        }
                    },
                    {
                        "title": "Service Charge",
                        "price": {
                            "currency": "INR",
                            "value": "100"
                        }
                    }
                ],
                "ttl": "P1H"
            },
            "payment": {
                "uri": "https://api.bpp.com/pay?amt=$amount&txn_id=ksh87yriuro34iyr3p4&mode=upi&vpa=bpp@upi",
                "tl_method": "http/get",
                "params": {
                    "transaction_id": "ksh87yriuro34iyr3p4",
                    "amount": "180",
                    "mode": "upi",
                    "vpa": "bpp@upi"
                },
                "type": "ON-FULFILLMENT",
                "status": "NOT-PAID"
            },
            "created_at": "2021-06-23T07:33:02.655Z",
            "updated_at": "2021-06-23T07:33:02.655Z"
        }
    }
}
*/
    public OnConfirmRequest generateConfirmResponse(OrderDetails orderResponse) {
        var confirmResult = ConfirmResponseBuilder.builder.result(orderResponse).build();
        // Convert response into protocol object similar to example above
        return confirmResult;
    }

BPP calls protocol on_confirm

    public Response invokeOnConfirm(OnConfirmRequest confirmResponse, HttpHeaders headers) {
        if(confirmResponse.getError() == null) {
            // Call BAP on_confirm 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_confirm,
                constructResponseHeaders(),
                confirmResponse,
                OnConfirmRequest.class);
            return Response.of("ACK", null);
        } else {
            return Response.of("NACK", null);
        }
    }