Designing Payment Processing System for E-commerce

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Let's design a payment processing service for E-commerce store, where users can process their payments in a secure manner.

Difficulty Level: Medium

1. What is a Payment Processing System?

In today's digital marketplace, a Payment Processing System is a technological framework that facilitates the transaction of money between buyers and sellers in an e-commerce environment. It securely captures and processes payment information from customers, ensuring funds are properly transferred from the customer's bank or credit account to the merchant's account. This system supports various payment methods, such as credit cards, debit cards, digital wallets, and bank transfers, making it versatile for different shopping scenarios.Additionally, it provides vital services such as fraud prevention using machine learning algorithms, currency conversion via real-time exchange rate APIs, and transaction reconciliation through automated matching systems to ensure that each transaction is secure, accurate, and compliant with financial regulations.

In this lesson, we will design a payment processing service where a user can securely manage their financial transactions. But before getting into the details we will first understand what entities are involved in the payment processing process.So that we can design a more appropriate design for it.

2. Payment Processing Service Entities

To design an efficient payment processing service for an e-commerce store, it's crucial to grasp the high-level flow of money and the core entities involved. This understanding helps in creating a system that is both secure and reliable.

At a high level, the payment process can be viewed in terms of how funds move from the buyer to the seller. Let’s consider a practical example to illustrate this:

Image

Imagine a user, Alice, shopping on an e-commerce site like eBay. Alice selects a product and completes her purchase. The money she pays flows into eBay's account. Although the funds are now with eBay, eBay doesn't own all the money. Instead, eBay holds it temporarily on behalf of the seller, Bob, who listed the product. eBay charges a small fee for this service. Once Alice receives her product, eBay releases the remaining funds to Bob's bank account.

With this overview in mind, let’s explore the core entities involved in this process:

User/Customer: The individual or entity making the payment, such as a shopper purchasing items online.

Merchant: The individual or entity receiving the payment.

Payment Gateway: A service that authorizes and processes payments between the customer and the merchant, typically using APIs like REST or SOAP, and protocols such as 3-D Secure for enhanced security.

Issuing Bank: The bank that issued the customer’s payment card.

Acquiring Bank: The bank that processes the payment on behalf of the merchant and communicates with the card networks.

Card Networks: Networks such as Visa, MasterCard, and American Express that facilitate communication between the issuing and acquiring banks using standards like ISO 8583 for message formatting.

Now let's understand the complete payment cycle:

Payment Entities Workflow
Payment Entities Workflow
  1. The customer selects items and enters payment details at checkout. The merchant’s site sends these details to the Payment Gateway, which encrypts and transmits them to the Acquiring Bank.

  2. The Acquiring Bank, in collaboration with the Card Network (e.g., Visa, MasterCard), forwards the details to the Issuing Bank.

  3. The Issuing Bank verifies the account, checks funds, and either approves or declines the transaction, sending a response back through the Card Network to the Acquiring Bank.

  4. The Acquiring Bank sends the response to the Payment Gateway, which informs the merchant. If approved, the merchant completes the sale, and the customer receives a confirmation. If declined, the customer is notified to use another payment method.

  5. The Acquiring Bank collects approved transactions from the merchant and submits them to the Card Networks, which facilitate the transfer of funds from the Issuing Banks to the Acquiring Bank.

  6. The Acquiring Bank deposits the funds into the merchant’s account, minus any processing fees. The merchant receives the payment, completing the transaction cycle.

  7. Both the merchant and the acquiring bank reconcile payments, ensuring all transactions are accounted for and managing any disputes or chargebacks that may arise.

Now let's move over to our design requirements so that we can design a system for the process.

3. Requirements and Goals of the System

We are designing a payment processing system for e-commerce, focusing on secure, efficient, and reliable transaction handling.

Functional Requirements

  • Transaction Processing: Handle the complete lifecycle of transactions, supporting various payment methods and managing states such as pending, authorized, captured, and refunded using state machine patterns.
  • Payment Validation: Validate transactions for authenticity and accuracy to prevent fraud.
  • Multi-Currency Support: Support transactions in multiple currencies with real-time currency conversion.
  • Instant Feedback: Inform users of their transaction status within 1-2 seconds after initiating the payment.
  • Payment Service Integration: Seamlessly integrate with various payment services to facilitate diverse payment methods and enhance transaction processing capabilities.

Non-Functional Requirements

  • Scalability: Handle a high volume of transactions without performance degradation.
  • Performance: Ensure low-latency processing with transaction feedback within 1-2 seconds.
  • Security: Implement robust security measures to protect sensitive payment information.
  • Reliability: Guarantee high availability and ensure no transaction data is lost, with redundancy and failover mechanisms in place.

4. Capacity Estimation and Constraints

Let's assume that we have 3.8 million daily active users, and on average, each user makes 2 transactions daily; this gives us 7.6 million transactions per day.

Storage Estimation: Assuming each transaction record is 500 bytes on average (including transaction ID, user ID, amount, currency, timestamp, and status). To store all the transactions for one day, we would need approximately 3.8TB of storage.

<center> 7.6 million transactions * 500 bytes = 3.8 GB/day </center>

To store five years of transaction history, we would need approximately 6.93 TB of storage.

<center> 3.8 TB * 365 days * 5 years ≈ 6.93 TB </center>

Besides transaction records, we also need to store additional data such as user information, payment method details, and transaction logs. This calculation does not account for data compression and replication.

Bandwidth Estimation: If our service is processing 7.6 million transactions per day, and each transaction request/response pair is approximately 1 KB (including headers and metadata), this will result in about 88MB of incoming and outgoing data per second.

<center> (7.6 million transactions * 1 KB) / 86400 seconds ≈ 88 KB/s </center>

Since each transaction involves both an incoming request and an outgoing response, we need the same amount of bandwidth for both upload and download.

These estimations help in planning the necessary infrastructure to support the payment processing system and ensure it meets performance and reliability standards. So let's move to next section where we will discuss the API's that will be used in this process.

5. System API Design

We can have REST APIs to expose the functionality of our service. The following could be the definitions of the APIs for processing payments:

  • Initiate a Payment Transaction
POST /transactions

Request Parameters:

api_key (string): The API key of a registered account to authenticate the request, validated against a database.

sender_id (string): Unique identifier of the user sending the payment.

receiver_id (string): Unique identifier of the user receiving the payment.

amount (decimal): The amount to be transferred.

currency (string): The currency code (e.g., USD, EUR).

payment_method (string): The payment method (e.g., credit card, PayPal).

description (string): A brief description of the transaction.

Response:

200 OK: Returns a JSON object containing the transaction details.

{ "transaction_id": "tx_123456789", "status": "pending", "timestamp": "2024-05-29T10:45:00Z" }
  • Retrieve a Transaction Status
GET /transactions/{transaction_id}

Request Parameters:

api_key (string): The API key of a registered account to authenticate the request.

transaction_id (string): The identifier of the transaction to retrieve.

Response:

200 OK: Returns a JSON object containing the transaction status.

{ "transaction_id": "tx_123456789", "status": "completed", "amount": 100.00, "currency": "USD", "timestamp": "2024-05-29T10:45:00Z", "description": "Payment for order #1234" }
  • Refund a Transaction
POST /transactions/{transaction_id}/refund

Request Parameters:

api_key (string): The API key of a registered account to authenticate the request.

transaction_id (string): The identifier of the transaction to refund.

amount (decimal): The amount to be refunded.

reason (string): A brief description of the refund reason.

Response:

200 OK: Returns a JSON object containing the refund details.

{ "refund_id": "rf_987654321", "transaction_id": "tx_123456789", "status": "processed", "amount": 100.00, "timestamp": "2024-05-29T11:00:00Z" }

These APIs will facilitate the core functionalities of the payment processing system, ensuring secure and efficient handling of transactions.

6. Database Schema

The database schema for the payment processing service is designed to efficiently process and manage user's transactions. The schema ensures scalability and secure payment processing for users.

Database Schema
Database Schema

The database schema for the payment processing system includes several key tables:

  • Users Table: Stores basic user information like user ID, name, email, hashed password, and account creation date.
  • Transactions Table: Keeps details of each transaction, including sender and receiver IDs, amount, currency, payment method, status, timestamp, and description.
  • Payment Methods Table: Records payment methods linked to users, storing encrypted payment details.
  • Refunds Table: Tracks refunds associated with transactions, including refund amount, reason, status, and timestamp.
  • Transaction Logs Table: Maintains logs for transactions to provide an audit trail, with log messages and timestamps.

This schema ensures secure, efficient management of user data, transaction details, payment methods, refunds, and logs, supporting the core functionalities of a payment processing system.

7. High-Level Design

At a high level, the payment processing system will support two main scenarios: processing payments from the buyer's side and handling order fulfillment from the seller's side. The system will use Payment Service to manage all interactions and update the databases for transaction data and ledgers.

Image

All the steps are as follows:

  1. The buyer initiates a request to buy something with a payment by sending payment details (such as amount, currency, and payment method) to the Payment System via a web interface or mobile application.

  2. The Payment System receives the payment request from the buyer and begins processing it. This includes validating the payment details and ensuring that all necessary information is available.

  3. The Payment System stores the payment information in the Payment_info DB. This database keeps track of all payment transactions, including the status, amount, payer, and payee details.

  4. After storing the payment information, the Payment System sends a request to the Payment Executor to execute the payment. The Payment Executor is responsible for communicating with external payment gateways or financial institutions to complete the transaction.

  5. The Payment Executor processes the payment request by interacting with the necessary external entities (e.g., banks, card networks), ensuring the transfer of funds from the buyer’s account to the E-commerce store’s account.

  6. Once the e-commerce store receives the payment it notifies the seller to fulfill the buyer's order. and receive payment from the e-commerce companies bank account in a secure manner

  7. Concurrently, the Payment System maintains a ledger of all transactions in the Ledger DB. This database ensures that all transactions are recorded for audit and reconciliation purposes, keeping a log of credits and debits associated with each transaction.

Now that we have understood the HLD design it's time for us to further understand how different components work together in our system.

9. Payment Service

The Payment Service is the core component of a payment processing system, responsible for managing and executing transactions between buyers and sellers. Its primary role is to ensure secure, efficient, and reliable handling of payments. This involves validating payment details, processing the transaction, logging the transaction data, and sending notifications about the transaction status. The Payment Service acts as an intermediary, ensuring that all necessary checks and validations are performed before a transaction is executed by the Payment Executor.

Let's understand the key functions and necessary checks and validations required:

Key Functions

The Payment Service operates through four main functions:

  1. Payment Validation: This step ensures that all payment details are correct, detects any fraudulent activities, and verifies compliance with financial regulations.
  2. Payment Execution: After validation, the service processes payment requests, interacts with banks and payment gateways, and updates the transaction statuses.
  3. Transaction Logging: It maintains detailed logs for audits and ensures accurate financial records by recording debits and credits.
  4. Notification System: The service sends real-time updates to buyers and sellers and generates alerts for any issues or anomalies.

Compliance and Validation Before Execution

Before a transaction is executed by the Payment Executor, the Payment Service performs several compliance and validation checks:

  • Currency Conversion Accuracy: Validates the accuracy of currency conversion rates if the transaction involves different currencies.
  • Transaction Limits: Verifies that the transaction does not exceed predefined limits set by the user’s account or regulatory guidelines.
  • Authorization Checks: Confirms that the user initiating the transaction is authorized to use the payment method provided.

Workflow Diagram

The following diagram illustrates the flow of a transaction through the Payment Service:

Image
Image

This interconnected workflow ensures that transactions are secure, compliant, and processed efficiently. Now there are a few other things that need clarifications before moving to next component.

What happens if the payment details provided are incorrect? If the payment details are incorrect, the Payment Validation step will detect the error. The transaction will not proceed until all details are correct and validated. This step ensures that errors are caught early, preventing potential issues later in the process.

How does the Payment Service handle potential fraud? The service includes fraud detection mechanisms in the Payment Validation step, analyzing transaction patterns and behaviors to identify suspicious activities. If potential fraud is detected, the transaction is flagged and may be halted for further investigation.

How are transaction statuses updated and communicated? After a transaction is processed, the Payment Execution function updates the transaction status in the system. The Notification System then sends real-time updates to both buyers and sellers, informing them of the transaction status. If any issues are detected, alerts are generated to notify the relevant parties.

What happens if a transaction exceeds predefined limits? If a transaction exceeds predefined limits, the Transaction Limits check will detect this and prevent the transaction from proceeding. This ensures that all transactions comply with user account settings and regulatory guidelines.

Payment Executor

Once the Payment Service has processed the payment and completed all necessary checks and validations, it sends a request to the Payment Executor for the actual execution of the transaction. The Payment Executor finalizes the transaction by communicating with the necessary financial institutions to transfer funds. It acts as the intermediary that ensures the payment is completed securely and efficiently.

Now that we understand the role of the Payment Executor, let's explore the different methods it can use to communicate with financial institutions and select the one best suited for our needs.

Direct Connection

When it comes to processing payments, a Direct Connection involves linking your payment system directly to banks and card networks like Visa and MasterCard. This approach provides complete control over the entire payment process, including maintaining the payment interaction in-house.

How It Works:

Imagine a customer, Jane, who is making a purchase on your website. She enters her payment details, which your system then encrypts to ensure security. These encrypted details are sent directly to the bank or card network. The financial institution performs necessary checks, such as verifying funds and conducting fraud checks, and then sends back an approval or denial to your system. Once approved, your system finalizes the transaction and initiates the transfer of funds to your merchant account. Let's look at the flow chart below to understand the process.

+--------+ +----------------+ +-----------------------+ +---------------------------+ | Jane | | Payment Service| | Payment Executor | | Financial service| +--------+ +----------------+ +-----------------------+ +---------------------------+ | | | | | Initiates purchase | | | |------------------------>| | | | | | | | Enters payment details | | | |------------------------>| | | | | Encrypts payment details | | | |------------------------------->| | | | | Sends encrypted details | | | |-------------------------------------->| | | | | | | | Financial institution performs checks | | | | (e.g., verifies funds, fraud checks) | | | | | | | | Sends approval/denial | | | |<--------------------------------------| | | Receives response | | | |<-------------------------------| | | | | | | Finalizes transaction | | | |------------------------>| | | | | | | | Initiates fund transfer | | | |------------------------>| | | | | | Transfers funds to merchant account | | | |-------------------------------------->| | | | | | Transaction complete | | | |<------------------------| | | +--------+ +----------------+ +-----------------------+ +---------------------------+

Challenges:

  1. Technical Complexity: Managing direct connections with multiple banks and card networks can be technically demanding. Your IT team needs to have the necessary skills to handle these integrations securely and efficiently. This often involves significant investment in technical infrastructure and ongoing maintenance.

  2. Security Compliance: Handling sensitive payment data directly means you must comply with stringent security standards like PCI-DSS. Achieving and maintaining compliance can be complex and costly. It requires regular security audits and updates to protect against evolving threats.

  3. Resource Intensive: Setting up and maintaining a Direct Connection involves substantial initial investment in both hardware and software. Additionally, ongoing efforts are needed to ensure the system remains secure and compliant, which can strain resources over time.

Despite these challenges, a Direct Connection offers some notable benefits. It provides complete control over the payment process, allowing for potential customization and optimization tailored to your specific business needs. Additionally, by eliminating intermediaries, you can potentially save on transaction fees.

However, the high complexity and resource demands mean that this approach is often best suited for larger enterprises with substantial technical and financial resources.

While the Direct Connection method gives you full control and can reduce transaction fees, it comes with significant challenges in terms of technical complexity, security compliance, and resource requirements. Assessing whether your business has the capacity to manage these aspects effectively is crucial.

Next, we will explore the Payment Service Provider (PSP) approach, which addresses many of the challenges associated with Direct Connection and may offer a more practical solution for many businesses.

Payment Service Provider (PSP)

After examining the Direct Connection approach and its challenges, let’s consider an alternative: using an external service known as a Payment Service Provider (PSP). A PSP acts as an intermediary between your business and the financial institutions, handling the complex aspects of payment processing on your behalf. Now, let’s explore how a PSP can simplify and secure the payment process.

How it works

Imagine a customer, Jane, making a purchase on your website. She selects her items and clicks the "checkout" button, triggering your payment service to start the process. Instead of managing everything internally, your payment service sends a payment registration request to the PSP. This request includes all necessary details like the amount, currency, and a unique transaction identifier.

The PSP responds by generating a unique token for the transaction, which your system securely stores. Jane is then redirected to a secure, PSP-hosted payment page where she enters her payment details, such as her credit card number, name, and expiration date. The PSP handles this sensitive information, ensuring it never touches your system.

Once Jane submits her payment details, the PSP processes the transaction by communicating with the relevant banks or card networks, performing fraud checks, and verifying funds. Upon approval, the PSP updates your payment service with the transaction status via a webhook. Jane is then redirected back to your website, where she can see the updated order status based on the payment outcome.

+--------+ +--------------------+ +------------------+ +----------------------+ | Jane | | Payment Service | | PSP-Hosted Page | | Financial Institutions| +--------+ +--------------------+ +------------------+ +----------------------+ | | | | | Initiates checkout | | | |----------------------->| | | | | | | | | Registers payment request | | | |------------------------------>| | | | | | | | Receives payment token | | | |<------------------------------| | | | | | | Redirects to PSP | | | |----------------------->| | | | | | PSP communicates with | | | | Financial Institutions | | | |------------------------------->| | | | | | | | Receives authorization response | | | |<-------------------------------| | | Receives payment status | | | |<------------------------------| | | Redirects back to | | | | website with status | | | |<-----------------------| | | +--------+ +--------------------+ +------------------+ +----------------------+ | Jane | | Payment Service | | PSP-Hosted Page | | Financial Institutions| +--------+ +--------------------+ +------------------+ +----------------------+

Challenges:

1 Dependency on Third-Party: Relying on a PSP means your payment processing is dependent on their uptime, performance, and security measures. If they have an issue, it can affect your transactions.

2 Limited Control: Unlike a direct connection, you have less say over how the payment process is managed and customized.

3 Potentially Higher Fees: PSPs often charge more for each transaction compared to direct connections.

4 Integration Complexity: Even though it's simpler than a direct connection, integrating with a PSP still requires some technical setup and maintenance.

Comparing Direct Connection vs. PSP

Let's compare both techniques pros and cons to finalize the better option

Direct Connection

Pros:

  • Full control over the payment process.
  • Potential for lower transaction fees.
  • Customization and optimization tailored to your business needs.

Cons:

  • High technical complexity.
  • Significant initial and ongoing resource investment.
  • Strict security compliance requirements.

PSP

Pros:

  • Simplified payment processing.
  • Reduced complexity in handling transactions.
  • PSP handles security compliance.
  • Faster setup and easier maintenance.

Cons:

  • Dependency on the PSP's infrastructure and performance.
  • Higher transaction fees.
  • Limited control over the payment process.

Final Decision: Given the overall complexity, resource requirements, and ease of integration, we've decided to go with a Payment Service Provider (PSP) for our payment processing service. This approach streamlines the payment process, minimizes technical and compliance challenges, and is well-suited for our needs. When choosing a payment processing method, consider your business's capacity to manage technical demands, compliance requirements, and resource allocation.

Now Let's see how looks in our system:

Image

This diagram highlights the key steps in the Payment Executor's workflow:

  1. Payment Service: The process starts with the Payment Service, which handles initial payment validation, compliance checks, fraud detection, and initial payment execution preparation.

  2. Payment Service Provider (PSP): The Payment Executor then interacts with a Payment Service Provider (PSP). The PSP serves as an intermediary, managing the integration with external banks and card networks to execute the payment.

  3. External Banks and Card Networks: The PSP processes the payment through the necessary banking and card network channels to complete the transaction. This workflow ensures that all transactions are thoroughly validated and executed securely and efficiently.

This workflow ensures that all transactions are thoroughly validated and executed securely and efficiently.

Now that we have understood the PSP its time for us to move to the next component of the system, which is account ledgers.

11. Ledgers

In simple terms, a ledger is a detailed record-keeping system that tracks all financial transactions within a payment processing system. It ensures that every transaction is accurately documented, making it easy to review and audit financial activity.

Double-Entry Ledger System A crucial principle in ledger systems is the double-entry principle (also known as double-entry accounting or bookkeeping). This principle is fundamental to any payment system and is key to accurate bookkeeping.

Double-Entry Principle In a double-entry system, every payment transaction is recorded in two separate ledger accounts with the same amount: one account is debited, and the other is credited. This ensures that the total sum of all transactions is always zero, providing end-to-end traceability and ensuring consistency throughout the payment cycle.

Example of Double-Entry System:

Account A (Debit): +$100

Account B (Credit): -$100

This method ensures that if one account loses money, another account gains the same amount, maintaining balance and accuracy.

So in our system we are using ledgers along with the payment_infodb to maintain ledger and other transaction request related data concurrently.

Image

Now that we have explored all the major components of the system let;s take a look to some of the problems that are causing issues and how we are tackling with them.

12. Internal and External service communication

As we utilize external services like Payment Service Providers (PSPs) for processing payments, a critical aspect we must address is ensuring reliable communication between our internal system and these external services. Ensuring this communication is robust and efficient is essential for the smooth functioning of our system. So let's understand how this workflow works.

Image
  • The customer initiates a payment on the merchant's platform.

  • The merchant platform sends the payment details (amount, currency, payment method, etc.) to the Payment Service.

  • The Payment Service validates the payment details, ensuring they are correct and comply with necessary regulations.

    • The Payment Service prepares the payment request for further processing.
  • The Payment Executor sends an authentication request to the PSP, including necessary credentials or tokens.

  • The Payment Executor receives the authentication response from the PSP, indicating whether the authentication was successful.

  • Upon successful authentication, the Payment Executor sends the prepared payment request to the PSP.

  • The Payment Executor receives the response from the PSP, which includes whether the payment was approved or declined.

  • The Payment Service updates the transaction status in the transaction database based on the PSP's response.

  • The Payment Service notifies the customer about the transaction status (approved or declined).

  • The Payment Service notifies the merchant about the transaction status.

  • The Payment Service periodically reconciles transactions with the PSP to ensure all transactions are accounted for accurately.

Now, another question comes to mind: How do we manage all these communications within this workflow? To effectively handle these communications, we can employ specific communication patterns that ensure the entire workflow remains seamless and efficient. In our case, we can utilize two primary communication patterns.

1. Synchronous Communication

2. Asynchronous Communication

Let's explore each method one by one to select which one is better for our system.

Synchronous and Asynchronous Communications: Engaging Scenarios and Detailed Insights

1. Synchronous Communication

Synchronous communication occurs when a request is sent, and the sender waits for an immediate response before proceeding. This style ensures that each step of the process is completed before moving to the next, maintaining a direct and continuous line of communication throughout the transaction.

Scenario: Limited Edition Sneaker Sale

Imagine Alice, eagerly waiting online for a limited edition sneaker release. These sneakers are not only exclusive but highly sought after, meaning they sell out within minutes.

  • Process:

    • As the sale goes live, Alice selects her size and hits the checkout button. Her payment details are sent from the merchant's platform directly to the Payment Service Provider (PSP) synchronously.
    • The PSP immediately processes Alice's payment details and, within seconds, responds with a transaction approval or denial.
  • Advantages:

    • Provides real-time transaction processing, essential for flash sales.
    • Simplifies implementation as each transaction is processed step-by-step.
  • Disadvantages:

    • Dependency on PSP's availability; any server issues could result in lost sales during critical moments.
    • Challenges scalability during high traffic, potentially slowing down when multiple users attempt purchases simultaneously.

2. Asynchronous Communication

Asynchronous communication involves sending a request and continuing with other tasks without waiting for an immediate response. The sender proceeds with other operations, and the response is handled once it arrives, independently of the original request timing.

Scenario: Custom-Built Computer Order

Bob decides to treat himself to a high-end, custom-built gaming computer. Given the various components and options, his order involves complex verifications and assembly schedules.

  • Process:

    • Bob customizes his gaming rig on the vendor’s website and submits his order. His payment information, along with the details of the custom components, is processed asynchronously.
    • He immediately receives a confirmation that his order has been received. Meanwhile, the vendor checks component availability and schedules assembly in the background, updating Bob on each step via email as his build progresses.
  • Advantages:

    • Facilitates handling of multiple, complex transactions simultaneously.
    • Minimizes the impact of a single point of failure, as delayed responses from component suppliers do not stall the entire system.
  • Disadvantages:

    • Delays in feedback can lead to customer uncertainty, especially in high-value transactions like Bob's.
    • Requires complex backend management to handle asynchronous callbacks and updates.

Comparison and Selection

When evaluating which communication method to implement in a payment processing system, both synchronous and asynchronous communications offer significant benefits depending on the transaction context:

  • Synchronous communication provides instant feedback and a straightforward implementation, making it ideal for time-sensitive transactions where immediate confirmation is essential for customer satisfaction.
  • Asynchronous communication excels in handling high volumes of complex transactions, offering scalability and resilience that are crucial for systems where operations must proceed without interruption.

Given the broader needs for scalability, reliability, and efficiency, asynchronous communication often stands out as the more suitable choice for modern payment processing systems. It supports large transaction volumes and ensures smooth, uninterrupted service across a variety of transaction types.

While asynchronous communication is more efficient and scalable, it does raise concerns about potential data loss. However, modern systems mitigate this risk through robust mechanisms such as message queuing, retries, and acknowledgments, which will be discussed in the next section.

13 Ensuring Reliable Payment Processing

To effectively manage the complexities and potential issues associated with asynchronous communication, modern payment processing systems employ several robust mechanisms. These mechanisms ensure data integrity, reliability, and efficient handling of high transaction volumes. The key mechanisms include message queuing, retries, and acknowledgments.

1. Message Queuing

Message queuing involves using a message broker to handle and store messages between services. This ensures that requests are not lost and can be processed even if some services are temporarily unavailable.

Benefits:

  • Decouples services: Enhances scalability by separating service dependencies.
  • Prevents data loss: Ensures that messages are not lost even if the recipient service is down.
  • Manages high traffic: Efficiently handles the flow of requests, balancing load during peak times.

2. Retries

Retries involve automatically resending a request if the initial attempt fails. This mechanism is crucial for handling transient errors and network issues.

Benefits:

  • Increases success rates: Improves the likelihood of completing transactions by retrying failed requests.
  • Minimizes impact of failures: Reduces the effect of temporary issues, such as network disruptions or service downtimes.
  • Enhances reliability: Ensures higher overall system reliability and user satisfaction.

3. Acknowledgments

Acknowledgments confirm that a message or request has been successfully received and processed. This mechanism ensures that the system accurately tracks the status of each transaction.

Benefits:

  • Prevents duplication: Avoids duplicate processing of the same transaction by confirming receipt and processing.
  • Ensures accurate tracking: Maintains precise records of transaction statuses, enhancing data integrity.
  • Improves reliability: Enhances the overall reliability of the system by confirming successful transactions.

By implementing these mechanisms, modern payment processing systems ensure that transactions are processed reliably, efficiently, and securely, even in the face of potential network or service disruptions. Let's incorporate them in our system and see how it looks.

Image

The updated diagram shows a reliable payment processing system. The customer initiates a payment, which the Payment Service validates and logs before sending it to the Message Queue. The PSP Service (e.g., Stripe, PayPal) retrieves and processes the request, interacting with Card Services (e.g., Visa, MasterCard). An acknowledgment is sent back through the Message Queue to the Payment Service. If the request fails, the system retries until it succeeds.

Now there is another question arises how does our system decide which transactions are retried and prevent retry loop issues?

When a payment transaction fails, it doesn't always mean it's the end of the road. Payment systems utilize retry queues and dead letter queues to manage these failures and ensure successful transactions whenever possible. Let's see how our system works.

Image

Initial Failure: An error occurs during processing. This could be due to various reasons:

  • Network Issues: A temporary loss of connection between the payment system and the bank or payment processor.

  • Insufficient Funds: The customer doesn't have enough money in their account.

  • Incorrect Details: The customer entered the wrong card number, expiration date, or CVV.

  • System Error: A glitch or bug in the payment system itself.

Error Analysis: The payment system quickly analyzes the error to determine its nature:

  • Retryable Error: If the error is likely temporary (e.g., network timeout, intermittent service outage), the system marks the transaction as retryable. Non-Retryable Error: If the error is permanent (e.g., invalid card number, insufficient funds), the system marks the transaction as non-retryable. Retry Queue or Database:

  • Retryable Transaction: The transaction is placed in a retry queue. This is a temporary holding area where transactions wait for subsequent processing attempts. Non-Retryable Transaction: The transaction details, along with the error information, are logged in a database. This information is valuable for troubleshooting, reporting, and customer service. Retry Attempts:

Retry Queue or Database:

  • Retryable Transaction: The transaction is placed in a retry queue. This is a temporary holding area where transactions wait for subsequent processing attempts.

  • Non-Retryable Transaction: The transaction details, along with the error information, are logged in a database. This information is valuable for troubleshooting, reporting, and customer service. Retry Attempts:

Retry Attempts:

  • The payment system periodically checks the retry queue for pending transactions.

  • It attempts to reprocess each transaction in the queue.

  • If a retry succeeds, the payment goes through, and the transaction is removed from the retry queue.

  • If a retry fails, the system checks the number of previous attempts:

    • Retry Limit Not Reached: The transaction remains in the retry queue for further attempts.
    • Retry Limit Reached: The transaction is moved to the dead letter queue.

Dead Letter Queue:

  • This queue acts as a final destination for transactions that have repeatedly failed or are deemed non-retryable.
  • Transactions in the dead letter queue require manual intervention. A human operator will investigate the errors, resolve them if possible, or contact the customer for further information.

14. Consistency

In a payment processing system, ensuring consistency is crucial to maintaining accurate records and preventing issues like double spending or discrepancies in transaction logs. Consistency guarantees that all transactions are recorded accurately, and the state of the system reflects the true financial state of users and merchants.

Adding a Queue for Ledger and Payment Info DB

To ensure consistency and reliability in updating the ledger and payment_info databases, we can introduce a message queue. This queue will handle the asynchronous processing of database updates, ensuring that operations are performed in an orderly and fault-tolerant manner.

Benefits of Using a Queue:

  • Decoupling: Separates the processing logic from the transaction submission, improving system modularity.
  • Retry Mechanism: Automatically retries failed operations, reducing the risk of data loss.
  • Load Management: Manages and balances the load on the databases, preventing overload during peak times.
  • Fault Tolerance: Provides resilience against temporary system failures by storing messages until they are successfully processed.

Reconciliation Flow

Reconciliation is the process of ensuring that the transactions recorded in the internal systems match with the records of external entities (such as PSPs, banks, and card networks). This process is critical for identifying and resolving discrepancies, ensuring financial integrity.

Reconciliation Workflow:

  1. Periodic Fetching: Regularly fetch transaction records from external systems (PSPs, banks, card networks).
  2. Matching Records: Compare fetched records with internal transaction logs and ledger entries.
  3. Identify Discrepancies: Highlight any mismatches between internal and external records.
  4. Resolve Discrepancies: Investigate and correct mismatches, which may involve reprocessing transactions or contacting external entities for clarification.
  5. Update Records: Update internal records to reflect the resolved state.
  6. Reporting: Generate reconciliation reports for audit and compliance purposes.

Diagram of the Workflow

Below is the diagram illustrating the workflow for adding a queue for ledger and payment info DB, as well as the reconciliation flow:

Image

By implementing queues for database updates and a thorough reconciliation process, the system ensures high consistency. However, one issue remains:

what happens if any of the single instances for the ledger or payment_info databases fail? Database replication and failover mechanisms are essential for ensuring the reliability and continuity of a payment processing system. Database replication involves keeping copies of databases on separate servers, allowing for quick switching if one server fails, thereby minimizing downtime and data loss. A failover mechanism complements this by automatically switching operations to a backup database if the primary one fails, ensuring seamless continuity of service. Together, these methods are critical for maintaining high availability and preventing financial losses due to unexpected database failures.

TAGS
System Design Fundamentals
System Design Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;