Book a free call

How to Integrate a Payment Gateway into Your Checkout

Payment gateway integration connects your checkout to a card processor. Choose hosted checkout or a direct API, test in a sandbox, then go live.

Hertiv engineering teamReviewed by a senior engineer
7 min read
How to Integrate a Payment Gateway into Your Checkout
On this page

Short answer

Payment gateway integration is the work of connecting a website or app checkout to a payment gateway, so customers can pay by card and the money reaches the seller. It covers choosing a gateway, adding its checkout or API, testing payments in a sandbox, and setting up confirmations, refunds and error handling before the first real order.

What is payment gateway integration?

Payment gateway integration is the connection between a shop’s checkout and the service that takes card payments. When a customer enters card details and clicks pay, the gateway checks the card, asks the bank to approve the payment, and tells the shop whether it worked, all in a few seconds.

The gateway sits between three parties: the shop, the customer’s bank and the seller’s bank. It moves the card details safely, applies fraud checks, and returns a clear result for each payment. The shop never needs to store card numbers itself, which is the point: card data is handled by a specialist, under rules the shop does not have to build from scratch.

Integration is different from building a gateway. Building one means creating the payment service itself, which is a regulated business. Integrating means plugging an existing gateway into your checkout, which is what almost every shop does. The wider guide to creating a payment gateway covers the build side; this guide covers the integration side.

Hosted checkout or direct API: which fits?

There are two ways to connect a gateway: a hosted checkout page or a direct API. In a hosted checkout, the customer is sent to the gateway’s own payment page and returned to the shop when the payment is done. In a direct API integration, the payment form lives inside the shop’s own checkout and talks to the gateway behind the scenes.

OptionControl over designCard-data rulesSpeed to launch
Hosted checkoutLow, gateway pageLightest, card data stays with gatewayDays
Direct APIFull, your own formHeavier, needs card-data checksWeeks
Payment linksNone, link by messageLightest, no checkout codeHours
Mobile SDKFull, inside your appHeavier, app store rules applyWeeks

Most small shops should start with hosted checkout. It is faster to launch, the gateway handles the card-data rules, and the customer sees a familiar payment page.

A direct API suits shops that need full control over the checkout design, or that take payments inside a complex flow, but it needs more development time and a closer look at the card-data rules. A crypto payment gateway follows the same two options, with the added question of whether the shop receives crypto or fiat.

What do you need before you start?

Before you start, you need a gateway account, API keys, a sandbox and a plan for testing. The account is the business relationship: the gateway checks the business, its owners and its bank account before it will move money. That check can take days, so start it early.

API keys come in two sets: test keys for the sandbox and live keys for real payments. Keep them apart. Test keys should be safe to share with developers, live keys should be stored as secrets and never appear in code, logs or screenshots. A leaked live key lets anyone create charges on the account, so treat it like a password.

The sandbox is a copy of the gateway that moves no real money. It comes with test card numbers that always succeed, always fail or always need extra checks, so the team can try every outcome before launch. The shop also needs return addresses for success and failure, and a place to receive payment confirmations, which is where webhooks come in.

How do you integrate a gateway in six steps?

You integrate a gateway by choosing the connection type, adding the checkout, testing every outcome in the sandbox, then switching on live payments with monitoring in place.

  1. Choose the gateway and the connection type. Compare fees, supported cards, payout timing and the checkout options, then pick hosted or direct API.
  2. Create the account and get the keys. Finish the business checks, then store the test and live keys separately.
  3. Build the checkout. Add the payment form or hosted redirect, the order total and the customer details the gateway needs.
  4. Handle the result. Show a clear success page and a clear failure page, and record the payment against the order.
  5. Set up webhooks. Receive payment confirmations from the gateway and update the order only when the webhook arrives.
  6. Test, then go live. Run every test card in the sandbox, switch to live keys, and take one small real payment first.

Keep the order and the payment as two separate records. The order says what the customer bought; the payment says what was charged, when, and with what result. That separation makes refunds, disputes and accounting far easier later. The same six steps apply to a crypto payment gateway, with the added choice of which coins to accept and whether to settle in crypto or fiat.

How do webhooks confirm payments?

Webhooks confirm payments by sending a message from the gateway to the shop whenever a payment changes state: approved, failed, refunded or disputed. The shop listens for those messages and updates the order to match, so the shop’s record always reflects what the gateway actually did.

The webhook matters because the customer’s browser is not reliable. A customer can close the tab, lose the connection or press the back button after paying, so the success page may never load. The webhook arrives regardless, which is why orders should only be marked as paid when the webhook arrives, never from the browser alone.

Two details make webhooks safe. First, the shop must check the signature on each message, so a fake message cannot mark an unpaid order as paid. Second, the shop must handle the same message arriving twice, because gateways resend messages they think were missed.

The safe design is to process each payment event once, keyed by its unique id, and to reply quickly so the gateway stops resending. The crypto payment gateway service follows the same pattern for crypto payments.

How do refunds, failures and retries work?

Refunds, failures and retries are part of the integration, not an afterthought. A refund is a second payment in the opposite direction, made through the same gateway, and it should be tied to the original payment so the shop can trace it. Partial refunds are common, so the system should allow refunding part of an order without refunding the whole thing.

Failed payments need a clear path. The gateway returns a reason, such as insufficient funds, an expired card or a bank decline, and the shop should show the customer a plain message and offer a second try with a different card. Never show the raw bank error code to the customer, and never retry a payment automatically without telling them.

Retries should be limited and visible. A card that fails three times in a row is unlikely to succeed on the fourth, and repeated attempts can look like fraud to the bank. The sensible design is to let the customer retry once or twice, then offer another payment method.

Disputes, where a customer asks their bank to reverse a charge, arrive as webhook events too, and the shop needs the order and delivery records ready to answer them.

Example (hypothetical): a small shop adds card payments

Example (hypothetical): a small online shop takes 400 orders a month, with an average order value of $45. Monthly sales are 400 times $45, which is $18,000. The shop adds a hosted checkout from a gateway that charges 2.9% plus $0.30 per order in this example.

The monthly fees are 2.9% of $18,000, which is $522, plus 400 times $0.30, which is $120. Total fees are $522 plus $120, which is $642 a month. The shop keeps $18,000 minus $642, which is $17,358, before its own product and shipping costs.

The shop tests the integration with the gateway’s test cards first: one that always succeeds, one that always fails and one that asks for extra checks. All three behave as expected, so the shop switches to live keys and takes one small real payment of $5 before opening the checkout to all customers. That first live payment catches a wrong return address that the sandbox had not shown.

What are the common mistakes?

The most common gateway integration mistakes are marking orders paid from the browser, mixing test and live keys, and leaving refunds and disputes until after launch.

  • Trusting the browser. Marking an order paid when the success page loads, instead of waiting for the webhook.
  • Mixing test and live keys. A live key in test code, or a test key in production, both cause real failures.
  • No webhook signature check. Accepting any message that claims to be a payment confirmation.
  • Refunds as an afterthought. No refund flow at launch means manual work and angry customers later.
  • Raw error codes to customers. Bank codes confuse buyers; show a plain message and a next step.

The fix is a short launch checklist: webhook signature check, test cards for every outcome, separate keys, a refund test and one small live payment. Run that checklist before the first real customer pays, and the integration will behave the same on day one hundred as it did on day one.

Key takeaways

  • Integration connects your checkout to a gateway; building a gateway is a different job
  • Hosted checkout launches fastest; a direct API gives full design control
  • Mark orders paid only when the webhook arrives, never from the browser
  • Keep test and live keys apart, and check every webhook signature
  • Test refunds and failures before launch, not after

Frequently asked questions

What is a payment gateway?

A payment gateway is a service that takes card payments for a shop. When a customer pays, the gateway checks the card, asks the bank to approve the payment and tells the shop whether it worked. The shop never stores the card numbers itself, because the gateway handles the card data under the card industry rules.

How long does payment gateway integration take?

A hosted checkout can be live in days, since the gateway provides the payment page and the shop only adds a redirect and a confirmation handler. A direct API integration usually takes weeks, because the shop builds its own payment form, handles the card-data rules and tests every outcome in the sandbox first.

What is a webhook in payments?

A webhook is a message the gateway sends to the shop whenever a payment changes state, such as approved, failed, refunded or disputed. The shop uses it to update the order, because the customer’s browser is not reliable: a closed tab or lost connection can stop the success page loading even after a payment succeeds.

Do I need to be PCI compliant to take card payments?

Yes, in the sense that card data must be handled to the card industry standard, but a hosted checkout keeps the burden light because the card data stays with the gateway. A direct API integration needs more checks. The gateway’s documents explain which level applies, and the shop should follow them before going live.

What happens if a customer disputes a payment?

The customer asks their bank to reverse the charge, and the gateway notifies the shop through a webhook. The shop then needs the order, delivery and communication records to answer the dispute. Good records win disputes; missing records lose them, so the order and payment records should be kept together.

Can I accept crypto payments through a gateway?

Yes. A crypto payment gateway accepts coins at checkout and can settle in crypto or convert to fiat for the shop. The integration follows the same six steps as a card gateway, with the added choices of which coins to accept and how settlement works.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: software products in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Adding payments to your product? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Crypto Payment Gateway service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call