Mehal CA baseline framework for integrating multiple payment providers - such as credit cards, Stripe,...
A baseline framework for integrating multiple payment providers - such as credit cards, Stripe, PayPal, Apple Pay, Afterpay, BNPL services, and Venmo - into checkout and payment processing flows.
Integrating payment providers is one of the most complex challenges in fintech. Each provider — Stripe, Adyen, PayPal, or emerging BNPL and crypto services — comes with different APIs, error-handling models, and reliability characteristics. How do you build a system that standardizes this complexity without tightly coupling your business logic to individual providers?
This framework is designed for e-commerce platforms (e.g., Shopify, WooCommerce), SaaS businesses, marketplaces, fintech applications (e.g., Robinhood, Venmo), and platform businesses (e.g., Eventbrite, Patreon) that need to integrate multiple payment providers — such as Stripe, PayPal, Adyen, BNPL services, and crypto (emerging) — without building and maintaining separate integrations for each. It sits as an orchestration layer above providers, enabling companies to use multiple providers with unified reliability (resilience, failover), idempotency, fraud detection, and compliance patterns.
In this article, I'll walk through building a payment integration framework that demonstrates how to handle these challenges using Kafka, Redis, circuit breakers, and machine learning. This framework serves as a baseline that implements industry-standard design patterns and system architecture approaches used in payment systems.
Note: For implementation details, see the GitHub repository.
What you'll learn:
Payment integration isn't just about calling an API. Real-world challenges include:
Emerging Payment technologies Add Complexity
Beyond traditional card payments, emerging payment methods introduce additional challenges:
Each payment type requires specialized handling, but they all share common needs: idempotency, resilience, audit trails, and fraud detection. This architectural framework provides a unified architecture that standardizes these patterns while allowing provider-specific implementations.
The Solution: Traditional approaches — direct API calls with basic error handling — don't scale. We need an architectural framework that abstracts complexity while providing reliability guarantees across all payment types. This framework demonstrates how to achieve this using architectural patterns and design principles.
This framework consists of two main components:
The architecture follows an event-driven pattern where payment execution is synchronous (for immediate response), while risk evaluation happens asynchronously via Kafka events. This separation ensures payment processing isn't blocked by risk analysis, maintaining low latency for end users.
Payment Flow Sequence
The foundation of our framework is the PaymentGateway interface, which allows us to plug in any payment provider without changing core logic. This follows the Strategy Pattern, where different payment providers are interchangeable strategies.
Payment providers have vastly different APIs: Stripe uses REST with webhooks, PayPal has SOAP and REST endpoints, BNPL providers require multi-step flows, and crypto payments need blockchain integration. The pluggable gateway pattern abstracts these differences, allowing new providers to be added without changing core orchestration logic.
The Interface Design
The PaymentGateway interface defines a contract that all providers must implement. It requires providers to specify their type, execute payments, and optionally report health status.
Key Benefits
Gateway Discovery
The orchestrator uses Spring's dependency injection to automatically discover all PaymentGateway implementations. At startup, it builds a map from provider type to gateway instance, enabling O(1) lookup during payment execution.
Complete Implementation: PaymentGateway.java and MockPaymentGateway.java.
Network retries, user double-clicks, or system failures can cause the same payment to be processed multiple times, resulting in duplicate charges. We solve this with Redis-backed idempotency keys.
The IdempotencyService handles the cache logic. It checks Redis for existing results using the idempotency key. If found, it returns the cached result immediately. If not found, it proceeds with payment execution and stores the result in Redis with a 24-hour TTL.
Key Design Decisions
payment:idempotency: prefix allows easy key management and avoids collisions.
Integration with Payment Orchestrator
The orchestrator integrates idempotency checks before executing any payment. This ensures duplicate requests are caught before they reach payment providers.
Complete Flow: PaymentOrchestrator.java.
Payment providers can fail. Network issues, provider outages, or rate limits can cause temporary failures. We need resilience patterns that retry transient failures, fail fast when providers are down, and prevent cascading failures.
Circuit Breaker Pattern
The circuit breaker acts like an electrical circuit breaker: when failures exceed a threshold, it opens and immediately fails requests without calling the provider.
Retry Logic with Exponential Backoff
For transient failures, we retry with exponential backoff and jitter to prevent thundering herd problems.
Implementation Pattern
Using Resilience4j, we decorate the gateway call with both retry and circuit breaker.
Every payment event is published to Kafka for:
Events are published asynchronously after payment execution completes.
Architectural Benefits
The TransactionWindowAggregator maintains a sliding window of events per entity and computes features like velocity, failure rate, and average amount.
The risk engine uses ML scoring when available, with rule-based fallback. If the risk score exceeds the threshold, an alert is generated.
When extending this framework for production use, you would need to add:
This architectural framework demonstrates how to approach payment integration challenges using industry-standard patterns and design principles.
Repository: Payment Integration Framework
Key Files to Explore