How Payment Gateways Really Work (From Card to Bank Account)
a clear, step-by-step breakdown of how money really moves in modern online payments
Most developers use payment gateways like M-Pesa, Flutterwave, Paystack or Airtel Money integrations every day…..
but very few actually understand what happens behind the scenes when a user clicks “Pay”.
This article breaks it down in a simple, developer-friendly way.
By the end, you’ll understand:
What a payment gateway really does
What happens in the background step by step
Why payments fail
Why webhooks are essential
How money actually reaches your bank account
1. What is a Payment Gateway (Really) ?
A payment gateway is not just a “payment button”.
It is a secure communication layer between:
Your app (merchant)
The customer’s bank
Card networks (Visa, Mastercard)
Payment processors (Stripe, etc.)
Its job is simple:
Move money safely from the customer to the merchant.
But under the hood, it’s a complex system of validation, fraud detection and banking communication.
2. The Full Payment Flow (Step by Step)
Let’s follow what happens when a user pays $10 on your app.
Step 1: User enters payment details
The user enters:
Card number
Expiry date
CVV
Important:
Your app should NEVER store this data (PCI compliance).
Step 2: Payment gateway tokenizes the card
Instead of sending raw card data everywhere, the gateway converts it into a token.
Example:
Card → tok_abc123xyzWhy?
Security
Fraud prevention
Compliance
Step 3: Authorization request is sent
The gateway sends a request to:
Card network (Visa / Mastercard)
Then to issuing bank (customer’s bank)
The bank checks:
Is there enough money?
Is the card valid?
Is this suspicious?
Step 4: Bank approves or declines
The bank responds:
Approved
Declined
If approved:
Money is temporarily reserved (not yet transferred)
Step 5: Capture (finalizing payment)
Now the payment is “captured”.
This means:
The merchant is officially entitled to the money.
Step 6: Settlement (money transfer)
After a delay (usually 1–3 days):
Bank transfers money to card network
Card network sends it to payment processor
Payment processor sends it to merchant’s bank
Final result:
Your dashboard shows:
“Payment successful”
But behind the scenes, it was a multi-step banking operation.
3. Why Webhooks are Critical
This is where many developers get confused.
A webhook is:
A server-to-server notification sent by the payment gateway.
Example:
Payment succeeded → webhook sent to your backend
Why you cannot rely only on frontend
Frontend can fail:
user closes tab
network breaks
redirect fails
So the real source of truth is:
Webhook (backend confirmation)
Example flow:
User pays
Frontend says “success”
BUT backend waits for webhook
Webhook confirms payment
You unlock product / subscription
4. Why Payments Fail (even when everything looks correct)
This is one of the most important parts.
Payments fail for reasons unrelated to your code.
Common reasons:
1. Bank decline
insufficient funds
card blocked
fraud suspicion
2. 3D Secure failure
User doesn’t confirm OTP / bank authentication
3. Currency issues
Some banks block:
USD transactions
international payments
4. Fraud detection systems
Payment gateways may block:
unusual location
repeated attempts
suspicious behavior
5. Network or timeout issues
Request fails before completion
Important insight:
A failed payment is not always a technical bug.
5. Security: What Developers MUST Understand
Payment systems are highly regulated.
Never do this:
Store raw card data
Log sensitive payment info
Send card data through your backend unnecessarily
Always do this:
Use tokenization
Use HTTPS everywhere
Verify webhooks using signatures
Avoid trusting frontend responses
PCI Compliance (simplified)
If you handle card data directly, you must follow strict security rules.
That’s why most developers use:
Stripe Checkout
PayPal hosted pages
6. The Real-World System (Simple View)
Here is the simplified mental model:
User → Payment Gateway → Bank → Card Network → Bank → Gateway → YouBut in reality:
multiple validation layers exist
fraud detection runs in parallel
settlement happens later
7. Key Mental Models to remember
If you remember only these, you understand payments:
1. Frontend is NOT truth
Only backend + webhook is truth.
2. Payment is a 2-step process
Authorization (approval)
Capture (final payment)
3. Money does NOT move instantly
It is a delayed settlement system.
4. Failures are mostly external
Banks, fraud systems, or networks ; not your code.
conclusion
Payment gateways look simple on the surface, but they are actually built on top of:
banking infrastructure
fraud detection systems
card networks
compliance systems
Understanding this makes you a better engineer, not just a user of APIs.


