Back to Coding

How Do You Add Payments Without Getting It Wrong?

How Do You Add Payments Without Getting It Wrong?
AI Tutorials Team 25 March 2026

Payments are the one area where a mistake affects somebody else's money, and where the requirements are set by parties who take them seriously. There is one rule that removes most of the difficulty, and it is worth stating before anything else.

What is the rule?

Card details never touch your server. The payment provider supplies a component that collects them directly, and your application only ever sees a token representing the result.

This is not merely good practice: handling card data yourself brings a compliance regime that no small project should be anywhere near, and the hosted component removes almost all of it.

What does your application actually do?

  • Create an intent. Telling the provider what is being charged.
  • Show their component. Which collects the details.
  • Receive confirmation. Through a notification from the provider.
  • Record what happened. Against the right account or order.
  • Handle failure. Which is more common than people expect.

Where do implementations go wrong?

Trusting the browser. The characteristic mistake is treating a successful-looking return from the front end as proof of payment, which is a value the user can influence.

Payment is confirmed by a notification from the provider to your server, verified as genuine, and nothing else counts.

What is a webhook and why does it matter?

A request the provider sends to your application when something happens. It is the authoritative record, it arrives out of band, and it may arrive twice, out of order, or while your application is down.

Handling it correctly means verifying the signature, being safe to receive twice, and recording what happened rather than assuming.

What does generated code typically miss?

Signature verification, duplicate handling and failure paths. The common path works and the awkward parts are the ones that determine whether your records match the provider's, which is the thing that matters when somebody queries a charge.

Check this payment integration specifically for: whether the webhook signature is verified, whether receiving the same event twice would cause a double charge or a duplicate record, and what happens if the payment fails after the user has left the page.

Currency and tax deserve early decisions too, since displaying a price without knowing whether it includes tax is a question somebody will ask on the first day.

What about subscriptions?

Considerably more involved than one-off payments. Renewals, failed renewals, cancellations, refunds, upgrades mid-period and proration are each a case, and getting one wrong means charging somebody incorrectly.

Using the provider's own subscription handling rather than building the logic yourself is nearly always right.

What are the obligations beyond the code?

Consumer rights, refunds, clear pricing including tax, and terms people can read before paying. In the UK there are specific requirements about cancellation for things bought online, which apply regardless of the size of the operation.

These are worth reading about before launch rather than after somebody asks.

How do you test it?

In the provider's test mode, with their test cards, including the ones designed to fail. Testing only the successful path leaves every failure branch unexercised, and those are the branches that produce angry emails.

What about invoices and records?

Worth arranging before taking money rather than afterwards. Somebody who pays will eventually want a receipt, and you will eventually need records of what was charged, refunded and disputed for your own accounts.

Most providers generate receipts and keep the authoritative record, which covers the majority of it. What your application needs is enough of its own record to answer a question without logging into the provider, and a way to link a payment to the account it belongs to. That linkage is the part generated integrations most often leave loose.

What is the summary?

Use the provider's component, trust only their notifications, verify the signature, handle duplicates, and test the failures. Five things, all straightforward, and together they cover the great majority of what goes wrong with payments in small applications.