The payment lifecycle
What really happens after the customer taps "Pay", and the states your code must track until the money is in your bank.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
This is a made-up example, but each rule in it comes from processor docs.
Meera's team runs an online furniture shop in India. To avoid refunds for items that are out of stock, the shop takes card payments in two steps. At checkout it only places a hold on the card. When a sofa ships, a job captures the money.
In one busy month, some sofas shipped late, 9 or 10 days after checkout. The capture job logged "error" for those orders, and nobody read the log. The sofas went out anyway.
Their processor's rule was simple. For many online card payments, a hold lasts about 7 days. If you do not capture before it expires, the funds are released and the payment becomes canceled. 3 The shop gave away the sofas for free.
Nobody saw it for weeks. The order table said "paid" because the hold had worked. The team found it only at month end. The money in the bank was lower than the sum of "paid" orders, and finance asked why.
Three bugs, one root cause. The team treated a payment as one event, "paid". It is really a series of states over many days, and each state can fail.
The idea in simple words
A card payment has several parties.
- Customer: the person who pays with a card.
- Merchant: your business.
- Payment processor or gateway: a company such as Stripe or Adyen that your code talks to.
- Acquiring bank: processes card payments for merchants. It sends the authorization request to the customer's bank through the card networks. 2
- Card network: the network of a card brand, such as Visa or Mastercard. 1
- Issuing bank: the bank that gave the customer the card. 1
The payment then moves through states. Your code must track each one.
- Authorization: the issuing bank checks that funds are there and holds the amount. 1
- Capture: the money moves from the issuing bank toward you. 1
- Settlement: the customer's bank transfers the funds to the processor. Adyen marks a payment Settled at this point. 5
- Payout: the processor sends money from your available balance to your bank account. 6
The key idea. "Paid" is not one moment. A payment is a state machine that runs for days. Store each state, learn about changes from verified webhooks, and check the final money against the processor's report.
How it works
Step through one ₹1,000 card payment.
- The customer pays ₹1,000. Your shop asks the processor to authorize. The issuing bank checks the card and holds ₹1,000. Later a webhook tells your shop the money is held, not yet taken.
- The order ships, so your shop captures the payment. Capture must happen before the hold expires. A webhook says the payment succeeded. Your shop still has no money in its bank.
- Days later the banks settle. The money reaches the processor, and your balance becomes available. On Stripe, a balance.available event can tell you this. How many days depends on the country and the processor.
- The processor sends a payout to your bank account. A webhook says the payout is expected to arrive, and it can still fail later. Your team matches each payout to the processor's report.
Authorize, then capture
You can capture right away. You can also authorize and capture in two steps, with the capture later. Hotels, for example, often authorize the full amount before a guest arrives and capture at checkout. 3
With Stripe, you set capture_method=manual. After authorization, the payment's status becomes requires_capture. 3 You can capture less than you held, and Stripe releases the rest automatically. 3
The hold does not last forever. Stripe says an online card authorization is usually valid for 7 days. For Visa payments that you start without the customer, the table says 5 days, and the exact window is 4 days and 18 hours. 3 Other payment methods have their own windows. 3 If the hold expires before capture, the funds are released and the payment is canceled. 3 On Stripe, the charge's capture_before field shows when the hold expires. 3 Build an alert for holds that are about to expire.
If you no longer want the money, cancel before capture. At Stripe, canceling costs nothing, while a refund after success might cost a fee. 7
3-D Secure
Sometimes the issuing bank wants proof that the real cardholder is paying. With 3-D Secure (3DS), the bank may ask for a password, a one-time code sent to the phone, or a biometric check. 10 Rules in the EEA, the UK, India, Japan and Australia may require it for card payments. 10 On Stripe, the payment waits in the requires_action status until the customer finishes this step. 4
Settlement takes days
Capture is not cash. The money still has to travel between banks. Stripe gives settlement timing per country as "T+X" days. "T" is when the payment was confirmed or captured. 6
Stripe's table has an "initial settlement timing" column, which applies to your first payout. There it lists 2 business days for the United States. For India, it lists 2 business days for domestic charges and 5 for international ones. 6 Later payouts follow a default timing, which can differ. 6 Your processor and contract may differ too, so read your own terms.
Then a payout sends the money to your bank. Stripe's payout.paid event means the payout is expected to arrive. If it then fails, a payout.failed event comes later. 12
Refunds and chargebacks
A refund is started by you. At Stripe, you can refund part or all of a payment, but never more than the original charge. 7 It is not instant. Stripe says the customer sees the credit about 5 to 10 business days later, depending on the bank. 7 A refund can also fail, for example on a closed bank account. 7
A chargeback, also called a dispute, is started by the cardholder with their issuer. The network reverses the payment at once. Stripe then takes the amount and a dispute fee from your balance. 8 On Stripe, you usually have 7 to 21 days to respond, depending on the card network. If you miss the deadline, you lose. 9 If you counter the dispute, the cardholder's bank decides the outcome. Stripe says this can take up to 3 months. 9
So money you counted can leave your account weeks after a "successful" sale. Your ledger needs entries for refunds, disputes and fees, not only sales.
Webhooks: verify, then dedupe
Many results arrive later, for example when a bank confirms a payment or a customer disputes a charge. The processor tells you with a webhook. 11 Treat each one with care.
- Verify the signature. Without it, an attacker could send a fake "payment succeeded" event and get free goods. 11 Stripe signs each event with HMAC-SHA256 in the
Stripe-Signatureheader. Verify it on the raw request body. 11 - Reject old events. The signed timestamp stops replay attacks. Stripe's libraries allow 5 minutes by default. 11
- Dedupe. The same event can arrive more than once. Save the event IDs you have processed and skip repeats. 11 This is the same idea as in Idempotency, retries and timeouts.
- Do not trust order. Events can arrive out of order. If one is missing, fetch the object from the API. 11
- Answer fast. Return 2xx before slow work, and process from a queue. Stripe retries failed deliveries for up to three days in live mode. 11
Reconcile with the settlement report
Webhooks can be late or lost, and your code has bugs. So once a day, compare your records with the processor's. For automatic payouts, Stripe's payout reconciliation report lists every payment, refund, dispute and fee inside each payout. 13 Match each line to your ledger and explain every difference. Meera's team would have seen the missing money in the first daily check.
PCI DSS scope
PCI DSS is the security standard for anyone who stores, processes or transmits card data. 15 If your servers handle raw card numbers, you may face more than 300 security controls. 14
Most teams avoid that. They use hosted fields or a hosted checkout page, so the card number goes straight from the browser to the processor. It never passes through your servers. 14 You may still store the card brand, the last four digits and the expiry date. 14 Your scope shrinks, but you still must attest to PCI compliance every year. 14
Trade-offs
| Option | Customer sees | Risk | Use it when |
|---|---|---|---|
| Capture at once | One charge. | Refunds for items you cannot deliver. Refunds can cost fees. 7 | Digital goods and in-stock items that ship the same day. |
| Authorize now, capture later | A hold, then a charge. Some statements do not show the difference. 3 | The hold expires before capture, and the payment is canceled. 3 | Stock checks, hotels, rentals, or a final amount that may drop. |
| Trust webhooks only | Nothing. | Missed or late events leave orders in the wrong state. | Never alone. |
| Webhooks plus daily reconciliation | Nothing. | More work for your team. | Always, for real money. |
| Your own card form | Your design. | Raw card data on your servers, and a much larger PCI scope. 14 | Rarely. Only with a strong reason and a security team. |
| Hosted fields or checkout | The processor's fields in your page. | Less control over the look. | The default for most teams. |
Simple rule: store every payment state, verify and dedupe every webhook, and reconcile every payout.
At work: how a senior uses this
- Draw the payment state machine for your product, with every state and every exit, before you write code.
- Ask: "What happens if this webhook never arrives? If it arrives twice? If it arrives before the one before it?"
- Store the processor's payment ID and status next to your order. Never mark an order "paid" from the browser's redirect alone.
- Alert on holds close to expiry, captures that fail, refunds that fail and new disputes.
- Run reconciliation daily, and give someone the job of explaining each difference.
- Keep card numbers out of your systems, including logs and support tools. Use the processor's hosted fields.
With AI
What to ask the agent
"Draw the state machine of a payment in our code: every status, every event that changes it, and every place we write it. Mark any state with no exit."
"Review our webhook handler. Does it verify the signature on the raw body, reject old timestamps, dedupe by event ID and return 2xx before slow work? Show the line for each."
"Write a daily job that matches each payout line in the processor's report to our ledger. List rows found on only one side."
What to check yourself
- The handler uses the processor's official library to verify signatures. AI agents sometimes parse the JSON first, which breaks the raw-body check.
- The dedupe check and the business update happen in one database transaction.
- No settlement days, hold windows or fees are hard-coded from memory. They must match your processor's docs and contract.
- Amounts are integers in minor units, as in Money and double-entry ledgers.
- Test cards and test webhooks run in CI for capture failure, refund failure and a dispute.
- No card number or CVC appears in logs, prompts or test data.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 15 sources and related lessons
- Stripe documentation, How cards work (opens in a new tab)
The steps of an online card payment, and plain definitions of card issuer and card network. - Adyen Knowledge Hub, What's an acquiring bank and why you need one (opens in a new tab)
What an acquirer does and how it sends authorization requests through the card schemes. - Stripe documentation, Place a hold on a payment method (opens in a new tab)
Separate authorization and capture, partial capture, and how long a hold stays valid for each card brand. - Stripe documentation, How Payment Intents and Setup Intents work (opens in a new tab)
The status of a payment, from requires_payment_method to succeeded or canceled. - Adyen documentation, Payments lifecycle (opens in a new tab)
Another processor's statuses, from Received and Authorised to SentForSettle, Settled and Refunded. - Stripe documentation, Payouts (opens in a new tab)
Settlement timing as T+X days per country, and payout schedules. - Stripe documentation, Refund and cancel payments (opens in a new tab)
Cancel versus refund, partial refunds, refund timing and failed refunds. - Stripe documentation, Disputes (opens in a new tab)
What a chargeback is and how the money is pulled back from you. - Stripe documentation, Respond to disputes (opens in a new tab)
Response deadlines, evidence, and who decides the outcome. - Stripe documentation, 3D Secure authentication (opens in a new tab)
What 3D Secure is and where regulation may require it. - Stripe documentation, Receive Stripe events in your webhook endpoint (opens in a new tab)
Signature checks, replay protection, duplicates, ordering and retries. - Stripe API reference, Types of events (opens in a new tab)
What each event means, such as payment_intent.succeeded and payout.paid. - Stripe documentation, Payout reconciliation report (opens in a new tab)
How to match each payout in your bank with the payments, refunds, disputes and fees inside it. - Stripe documentation, Integration security guide (opens in a new tab)
PCI DSS in practice, and how hosted payment fields keep card data off your servers. - PCI Security Standards Council, PCI DSS (opens in a new tab)
The official home of the card data security standard and who it applies to.
Case studies: Airbnb: Avoiding double payments with an idempotency library · Shopify: Ten habits for building payment systems that hold up · Stripe: Idempotency keys make API retries safe
Related: Money and double-entry ledgers · Idempotency, retries and timeouts · Webhooks and async APIs · Reconciliation · PCI-DSS and tokenisation (coming soon) · Payment rails: UPI and card networks (coming soon) · Fraud and risk systems (coming soon)
- Money and double-entry ledgers: how to record each state as balanced entries.
- Idempotency, retries and timeouts: safe retries for capture and refund calls.
- Coming next on this line: Reconciliation, PCI-DSS and tokenisation and Payment rails: UPI and card networks.