This project demonstrates the Adapter Design Pattern using a Spring Boot application. The example focuses on integrating multiple payment gateways (e.g., PayPal, Stripe, Razorpay, Google Pay) through a unified adapter, making the application more maintainable, scalable and adaptable to future changes.
- Unified Integration: A single adapter interface to interact with multiple payment gateways
- Scalability: Easily add new payment gateways without modifying the core business logic
- Maintainability: Centralized logic reduces redundancy and improves code organization
- Real-World Example: Suitable for e-commerce platforms handling diverse payment options
- Java
- Spring Boot
- Maven
- REST APIs
Each payment gateway provides its own API, resulting in scattered, repetitive, and hard-to-maintain integration code.
The Adapter Design Pattern bridges these differences by providing a unified interface for all payment gateways, allowing the application to communicate seamlessly with them.
- Adapter Interface: Defines the contract for all payment gateways
- Concrete Adapters: Implement the interface and encapsulate the logic specific to each gateway
- Payment Service: Calls the appropriate adapter based on the gateway specified by the client
-
Process Payment
- URL:
/api/v1/payment/process - Method:
POST - Headers:
Content-Type: application/json - Request Body (JSON):
{ "amount": 100.50, "gateway": "paypal" } - Response (JSON):
{ "success": true, "transactionId": 1234567890 }
- URL:
-
Supported Gateways:
paypalstripegpay
-
Client Request
The client specifies theamountandgatewayin the JSON request. -
Adapter Selection
ThePaymentServicedynamically selects the appropriate adapter based on thegateway. -
Payment Processing
The selected adapter processes the payment and returns a success response along with a transaction ID.
-
Clone the repository:
git clone https://github.com/18-RAJAT/Spring-Adapter-Integration-Design.git
-
Navigate to the project directory:
cd Spring-Adapter-Integration-Design -
Build the project:
mvn clean install
-
Run the application:
mvn spring-boot:run
Request:
{
"amount": 150.75,
"gateway": "paypal"
}Response:
{
"success": true,
"transactionId": 1234567890
}Request:
{
"amount": 99.99,
"gateway": "stripe"
}Response:
{
"success": true,
"transactionId": 987654321
}Request:
{
"amount": 200.00,
"gateway": "unknown"
}Response:
{
"timestamp": "2024-12-13T17:00:00.123+00:00",
"status": 400,
"error": "Bad Request",
"message": "Unsupported payment gateway: unknown",
"path": "/api/v1/payment/process"
}- Integrate more payment providers like Razorpay or Square
- Improve error messages and add logging for failed transactions
- Use secure tokens or API keys for communication with payment gateways

