This repository contains the Payment Service for the E-commerce ecosystem. It is a robust financial transaction handler designed with the Strategy Pattern to support multiple payment methods (Credit Card via Stripe, Pix) and utilizes Circuit Breakers to ensure high availability when communicating with external gateways.
- Multi-Gateway Support:
- Stripe Integration: Secure credit card processing using the Stripe API.
- Pix Support: Native implementation structure for Brazilian instant payments.
- Strategy Pattern: Easily extensible architecture to add new payment methods (PayPal, Crypto, etc.) without modifying core logic.
- Fault Tolerance:
- Implemented Resilience4j Circuit Breaker and TimeLimiter to handle Stripe API failures or latency gracefully.
- Fallback mechanisms defined in
StripeClientFallbackFactory.
- Asynchronous Processing:
- Consumes payment requests via RabbitMQ (
PaymentConsumer). - Publishes
PaymentConcludedEventupon successful transactions.
- Consumes payment requests via RabbitMQ (
- Data Integrity:
- Transactional consistency using PostgreSQL.
- Database migrations managed by Flyway.
This service uses a Factory + Strategy pattern to select the correct payment processor at runtime:
// Simplified Logic
PaymentStrategy strategy = paymentFactory.getStrategy(paymentType); // Returns CreditCardStrategy or PixStrategy
strategy.process(paymentRequest);src/main/java/com/example/payment/
├── config/ # Resilience4j & RabbitMQ Configs
├── consumer/ # RabbitMQ Listeners
├── integration/stripe/ # Feign Clients & Fallbacks for Stripe
├── factory/ # PaymentFactory (Strategy selection)
├── strategy/ # PaymentStrategy Interface & Implementations
└── service/ # Business Logic
- Language: Java 21
- Framework: Spring Boot 4.0.0
- Cloud: Spring Cloud 2025.1.0 (OpenFeign)
- Database: PostgreSQL
- Resilience: Resilience4j (Circuit Breaker)
- Messaging: RabbitMQ
- Build Tool: Gradle 9.0
Create a .env file in the root directory based on .env.example:
# Database
POSTGRES_HOST=db
POSTGRES_DB=payment_db
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret
# RabbitMQ
RABBITMQ_DEFAULT_USER=guest
RABBITMQ_DEFAULT_PASS=guest
SPRING_RABBITMQ_HOST=rabbitmq
SPRING_RABBITMQ_PORT=5672
RABBITMQ_QUEUE_PAYMENT=payment_queue
# External Gateways
STRIPE_SECRET_KEY=sk_test_your_stripe_key_hereThe service is integrated into the shared e-commerce network.
-
Build and Start:
docker-compose up -d --build
-
Verify Health: The service exposes Actuator endpoints for health and metrics.
- Health Check:
http://localhost:8082/payment/api/actuator/health - Prometheus:
http://localhost:8082/payment/api/actuator/prometheus
- Health Check:
-
API Documentation: Access the Swagger UI at:
http://localhost:8082/payment/api/docs/swagger-ui/index.html
-
Install Dependencies:
./gradlew clean build -x test -
Run Application:
./gradlew bootRun
The service is configured to handle external failures:
- Sliding Window: 10 calls.
- Failure Threshold: 50% (Open circuit if 5/10 fail).
- Wait Duration: 5 seconds in open state before retrying.
- Timeout: 5 seconds per call to Stripe.
- Fork the repository.
- Create a feature branch.
- Implement a new
PaymentStrategyif adding a new method. - Submit a Pull Request.
Distributed under the MIT License.