Cancellation of a consultation appointment with reason for cancellation (Java)

In this use case, the user or the healthcare service provider (doctor, clinic, hospital etc) cancels an appointment from either end

Cancel

Code snippets

BPP receives protocol cancel

/*
Example Request JSON:
{
    "context": {
        "domain": "nic2004:85121",
        "country": "IND",
        "city": "std:080",
        "action": "cancel",
        "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",
        "cancellation_reason_id": "reason_3"
    }
}
*/
    // BPP Cancel Endpoint
    @PostMapping("/bpp/cancel")
    public ResponseEntity cancel(
            @RequestHeader HttpHeaders headers,
            @RequestBody CancelRequest request) {
        var response = bppApplicationService.cancel(request, headers);
        return ResponseEntity.ok(response);
    }

BPP processes the cancel

    public Response cancel(CancelRequest request, HttpHeaders headers) {
        var onCancelRequest = new OnCancelRequest();
        // Validate the headers
        var isHeadersValid = validateHeaders(headers);
        // Construct and return error
        if (!isHeadersValid) return null;

        // Cancel the given order
        var cancelOrder = request.getMessage().getOrderId();

        // Execute business logic here

        // Generate the response
        onCancelRequest = generateCancelledOrderResponse(cancelOrder);

        //invoke on_cancel api
        return invokeOnCancel(onCancelRequest, headers);
    }
}

BPP generates response

/*
Example Response JSON:
{
    "context": {
        "domain": "nic2004:85121",
        "country": "IND",
        "city": "std:080",
        "action": "on_status",
        "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": "Cancelled",
            "items": [
                {
                    "id": "dr_veena_slot_1",
                    "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": {
                "type": "IN-PERSON",
                "end": {
                    "location": {
                        "id": "good-health-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]"
                    }
                },
                "agent": {
                    "name": "Dr. Veena H",
                    "dob": "1989-08-12",
                    "gender": "Female",
                    "tags": {
                        "doctor_registration_no": "12345"
                    },
                    "phone": "+919876543210",
                    "email": "[email protected]"
                },
                "customer": {
                    "person": {
                        "name": "Remya C",
                        "dob": "1989-08-12",
                        "gender": "Female"
                    },
                    "contact": {
                        "phone": "+919898987676",
                        "email": "[email protected]"
                    }
                }
            },
            "quote": {
                "price": {
                    "currency": "INR",
                    "value": "1100"
                },
                "breakup": [
                    {
                        "title": "Consultation 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": "1100",
                    "mode": "upi",
                    "vpa": "bpp@upi"
                },
                "type": "ON-FULFILLMENT",
                "status": "NOT-PAID"
            }
        }
    }
}
*/
    public OnCancelRequest generateCancelledOrderResponse(CancelledOrderDetails cancelOrderDetails) {
        var cancelResult = CancelResponseBuilder.builder.result(cancelOrderDetails).build();
        // Convert response into protocol object similar to example above
        return cancelResult;
    }

BPP calls protocol on_cancel

    public Response invokeOnCancel(OnCancelRequest cancelResponse, HttpHeaders headers) {
        if(cancelResponse.getError() == null) {
            // Call BAP on_cancel 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);

             // Call BAP on_cancel api with the cancelled order object
            var response = apiClient.post(url[0] + Context.ActionEnum.on_cancel,
                constructResponseHeaders(),
                cancelResponse,
                OnCancelRequest.class);
            return Response.of("ACK", null);
        } else {
            return Response.of("NACK", null);
        }
    }