Online payments and payment gateways
A payment gateway handles card payments for an app. This lesson follows one payment from the user's phone to the bank.
You finish a dinner order in a food delivery app. At checkout you pick a card, type the number, the expiration date and the security code, and tap Pay. The screen shows that the payment is being confirmed. A few seconds later the order status changes to paid.
Four parties take part in a checkout: the user's browser or app, your server, the gateway and the bank. Press Play and watch where the card number goes. Your server receives only a token.
More detail
The message in this part
Why the card skips your server
Examples of gateways are PayMongo, Stripe and Xendit. The browser sends the card directly to the gateway and gets back a token, which is an opaque code that stands for the card. Your server only handles the token. If card numbers passed through a machine you own, that machine would fall under PCI DSS (the card industry's security rules), and so would every system it talks to. With a token, that burden stays with the gateway, which is set up to meet those rules.
Step 9 reverses the roles
The gateway calls your server, so your API is the server in that exchange. The signature header is the only check that stops anyone on the internet from marking their own order paid.
Why every message is JSON
No person reads these messages. Two programs at two companies are exchanging facts, and JSON was designed for that. SOAP is an older message standard that is written in XML. The same charge request is shown here in three formats so you can compare them.
When XML is still the right choice
JSON carries values and no rules. It cannot state that amount must be a positive integer or that currency must be a real ISO code (a standard currency code such as PHP), so each check is written in application code on both sides. An XML schema states the rules once, and a parser enforces them before your code runs. XML-DSig, a standard for digital signatures, can sign the whole document. When the thing being exchanged is money between institutions, that is worth six times the payload. Several Philippine banks still offer exactly this. If your project connects to a real bank and not a modern gateway, expect SOAP and plan about a week for the integration.
When a connection stalls, people press Pay again. Press Pay three times and read the total. Then switch on the idempotency key, press New checkout and try again.
More detail
Why a double charge happens
A POST is not idempotent, so each press can create a separate charge. HTTP verbs and status codes explains the term. The key is a UUID, which is a random unique code.
What the request looks like
Your server has a fixed number of workers. Capacity is the number of workers divided by the seconds each request takes. Compare a request that waits for a slow gateway with one that puts the call on a queue.
If your server waits for the payment gateway, your site is as slow as the gateway. Moving the call out of the request gives twenty times the capacity here.
You can now explain how a payment stays safe, charged once and fast.
More detail
Here rps means requests per second. In the second case the gateway reports the result later by webhook, which is a request the gateway sends to your server. The same change solves Card C in Group exercises. Server capacity shows how to measure capacity.