|

Integrating Payments and Invoicing Into a Web App

Diligent Creators blog cover on integrating payments and invoicing into a web app, showing a laptop, phone calculator, cash and printed invoices on a desk.

Short answer: To add payments and invoicing to a web app, decide what you are charging for first, let a payment provider handle card data through its hosted checkout or hosted fields, treat the provider’s webhook as the source of truth for what was paid, and generate invoices from your own records rather than from the payment screen. Most problems come from skipping those four decisions, not from the code itself.

Taking money inside your own product looks easy in a tutorial: add a button, call an API, show a thank-you page. In a real business it turns into a set of questions that nobody asked in the first meeting. What happens when a customer pays but closes the tab before your page loads? How do you refund part of an order? Who issues the invoice, and with what number? What does your finance person need at month end?

This guide walks through the decisions in the order we would make them on a web app project. It is general engineering and product advice, not legal or tax advice. Tax rules, invoice formats and provider availability differ by country and by type of business, so check the specifics with your accountant and your chosen provider. If you are scoping a whole product, our guides to web app development cost and process and the web app requirements document show where payments fit among the other work. If you want a team to build it, see our web application development services.

Step 1: decide what you are actually charging for

The payment code follows from the billing model, so settle the model first. The common shapes are:

  • One-off payment: a single charge for an order, a booking or a service.
  • Subscription: a recurring charge on a schedule, with plans, upgrades, downgrades and cancellations.
  • Usage-based: a charge calculated from what the customer used, often billed after the period ends.
  • Invoice on terms: a business customer receives an invoice and pays later by bank transfer or another route.

Many products mix two of these. Write down, in plain sentences, what a customer sees at each point: price, tax, discount, due date, receipt. If you cannot describe it on a page, you cannot build it reliably. Include the awkward cases: refunds, failed payments, a customer who changes plan halfway through a period, and a customer who disputes a charge.

Step 2: choose a payment provider that fits your business and your customers

Not every provider is available to every business or every customer. Availability depends on where your company is registered, where your customers pay from, which currencies and payment methods you need, and how the provider reviews new accounts. Check these things directly with the provider before you design around it:

  • Can your business entity open an account, and what documents are required?
  • Which payment methods do your customers actually use: cards, bank transfer, wallets, cash on delivery or something else?
  • Which currencies can you charge and settle in, and how do payouts reach your bank?
  • What are the fees, and how are refunds and disputes handled?
  • Does the documentation include a sandbox, clear webhook events and server libraries for your stack?

For apps aimed at Pakistani customers, local wallets and bank-based methods may matter more than cards. We cover that in mobile payments integration for apps in Pakistan. Keep your own code behind a thin internal “payments” module, so that changing or adding a provider later means changing one part of the app and not every page.

Step 3: keep card details off your servers

The safest and simplest design is one where your application never touches raw card numbers. Most providers offer a hosted checkout page or hosted input fields that sit inside your page but send card data straight to the provider. Your server receives a token or a reference instead. This keeps most of the burden of handling card data with the provider and reduces your own compliance scope. The card industry’s security standard, PCI DSS, still applies to your business in some form, so read your provider’s guidance on which level or questionnaire applies to you.

Never log card numbers, never put them in analytics events, and never store them in your own database, even temporarily “for debugging”. For wider hygiene on the rest of the application, see web app security basics every founder should ask about.

Step 4: treat the webhook as the source of truth

The most common payment bug is trusting the browser. A customer pays, the provider redirects them to your “success” URL, and your code marks the order as paid. Then the customer closes the tab before the redirect, or the redirect is faked, or the payment is still pending, and your records are wrong.

A sturdier pattern looks like this:

  1. Create an order or invoice record in your database with a status such as “pending”.
  2. Create a payment session with the provider and attach your order’s ID to it.
  3. Send the customer to pay.
  4. Receive the provider’s server-to-server notification (a webhook), verify its signature, and only then mark the order as paid.
  5. Use the redirect page just to show the customer a friendly message, and let it read the order status from your database.

Two details save you from painful incidents. First, make your webhook handler idempotent: providers can send the same event more than once, and handling it twice must not issue two receipts or two shipments. Store the event ID and skip repeats. Second, respond quickly with a success code and do slow work in the background, because providers retry when they do not get a prompt answer.

Step 5: model money carefully

Money in a database deserves a little more care than a normal number:

  • Store amounts as integers in the smallest currency unit, or use a decimal type designed for money. Floating-point numbers produce rounding errors.
  • Store the currency with every amount. Do not assume one currency forever.
  • Keep line items. An order with a total is not enough. Store each item, quantity, unit price, discount and tax so you can explain the total later.
  • Never overwrite history. A refund is a new record linked to the original payment, not an edit.
  • Record the provider’s reference for every payment, refund and dispute so support can find it in the provider’s dashboard.

Step 6: build invoicing as its own feature

An invoice is a document your business issues, and it is not the same thing as a payment confirmation. Treat it as its own part of the system. The details depend on your country and the type of business, so ask your accountant what an invoice must contain and how long you must keep it. In general, plan for:

  • Unique, sequential invoice numbers that are never reused and generated by your application, not typed by hand.
  • Seller and buyer details, issue date, due date and currency.
  • Itemised lines with tax shown according to the rules that apply to you.
  • A status: draft, issued, paid, partly paid, void or refunded.
  • A stored copy of the issued document, usually a PDF, so that a later change to a price or an address does not alter an invoice already sent.
  • Credit notes for corrections, rather than editing an issued invoice.

If your customers are registered businesses in Pakistan, there may also be requirements to report invoices to the tax authority. Our post on FBR digital invoicing and marketing explains how that looks from a business point of view; confirm the technical and legal details with your tax adviser and the official sources.

Step 7: plan for failure and for support

Payment features are judged by how they behave on the worst day. Design these cases before launch:

  • Declined or failed payments: show a clear message and let the customer try again without creating a duplicate order.
  • Pending payments: some methods settle later. The order should wait in a visible “pending” state.
  • Refunds, full and partial: a button for support staff, an audit trail, and a message to the customer.
  • Disputes and chargebacks: keep the evidence you will need, such as order details, delivery records and communication.
  • Reconciliation: a regular comparison between your records and the provider’s reports, so mismatches are found early.
  • Test mode and live mode: separate keys, clearly labelled, with a checklist for switching over.

Give your support team a screen where they can search an order, see its payment history and the provider reference, and issue a refund within their permissions. Without it, every question becomes a developer task.

A checklist before you go live

  1. The billing model, including refunds and edge cases, is written down and agreed.
  2. The provider account is approved for your business, with the payment methods you need.
  3. Card data never touches your servers or logs.
  4. Webhooks are verified, idempotent and tested with repeated and out-of-order events.
  5. Amounts, currency and line items are stored correctly.
  6. Invoices have unique numbers, a stored copy and a status, and your accountant has reviewed the format.
  7. Failed, pending and refunded flows have been tested end to end in the provider’s sandbox.
  8. Support can find and act on any payment.
  9. Live keys are stored as secrets, not in code.

Frequently asked questions

Should we build our own payment system or use a provider?

Use a provider. Handling card data, fraud checks and bank connections yourself is costly and risky. Build the parts that are specific to your business, such as orders, invoices and the internal payments module.

What is the difference between a payment and an invoice?

A payment is money moving from the customer. An invoice is a document that records what was sold and what is owed. They are linked, but one can exist without the other: an invoice may be issued before payment, and a payment may cover several invoices.

Why do we need webhooks if the customer is redirected back to our site?

Because the redirect can fail or be faked, while a signed server-to-server notification is reliable. Use the webhook to update the order and the redirect only to show a message.

How should we test payments?

Use the provider’s sandbox for success, failure, pending, refund and repeated-webhook cases. Then run one small, real transaction after going live and refund it, following the provider’s rules.

Do we need a lawyer or an accountant?

For tax treatment, invoice content and data protection, yes. Developers can build the system, but the rules that apply to your business should come from qualified advisers and official sources.

If you are planning payments or invoicing in a web app and want an experienced team to scope and build it, talk to us about web application development.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *