Payment Validation
Problem Statement
We are building a payment processing module. The system supports multiple payment types:
CREDIT_CARD
UPI
WALLET
NET_BANKING
Each payment type requires a different set of validations, and each validation must be executed in a specific order. For example:
CREDIT_CARD:
Step 1: Balance Check
Step 2: Card Expiry Validation
Step 3: Authorize Payment
WALLET:
Step 1: Balance Check
Step 2: Wallet Active Check
Step 3: Fraud Detection
Instead of writing switch-cases or hardcoded flows, we want to define reusable validation steps (handlers) and let Spring dynamically execute them in order per payment type.
Design Goals
Maintainable, testable, and extensible structure
Decouple payment type from validation logic
Dynamically register validation logic using Spring
Apply validation steps in the correct order
Support different validation chains per payment type
Patterns Involved
Strategy Pattern: Different strategy for each payment type.
Chain of Responsibility Pattern: Validation handlers processed in order.
Template Method Pattern (Optional): For executing steps in a defined sequence.
Solution
PaymentType.java
PaymentType.javaPaymentRequest.java
PaymentRequest.javaPaymentValidationStep.java
PaymentValidationStep.javaValidators (validators/)
validators/)A. BalanceCheckValidator.java
BalanceCheckValidator.javaB. CardExpiryValidator.java
CardExpiryValidator.javaC. WalletActiveValidator.java
WalletActiveValidator.javaPaymentValidatorService.java
PaymentValidatorService.javaPaymentController.java
PaymentController.javaPaymentValidationApplication.java
PaymentValidationApplication.javaExample Request (POST)
POST /api/payments/validate
Console Output
Last updated