Skip to content

Source code

How to Build a Payment Gateway in Python

Python is a common choice for the back end of platforms that take payments, from event ticketing sites to SaaS products. Django and FastAPI make the web side quick to build, and Python's standard library covers the two things payment code can't get wrong: exact decimal money and secure message signatures.

This page explains how to create a payment gateway in Python in the sense most projects mean it, integrating payment providers into your own application, and then what changes if you want a gateway that other businesses pay through.

Two different Python projects

The same question covers two very different builds. It helps to name which one you're doing.

Project What Python does What sits outside Python
Take payments on your platform Checkout flow, order states, webhook handling, refunds A PSP's hosted checkout or fields and its account
Run a gateway for other merchants Merchant API, vault, routing, connectors, back office Acquirer contracts, PCI DSS validation, usually a license

An events ticketing site, for example, belongs in the first row, and a Python payment gateway for it is mostly integration work. It needs tickets reserved, paid and issued reliably, not a gateway of its own.

Exact money and verified webhooks in Python

Two standard-library modules do the heavy lifting. decimal keeps amounts exact, and hmac checks that a webhook really came from your provider. The example is illustrative and generic; your provider's documentation sets the actual header and signing scheme.

import hashlib
import hmac
from decimal import Decimal, ROUND_HALF_UP

def is_valid_signature(payload: bytes, signature: str, secret: bytes) -> bool:
    """Illustrative only: verify an HMAC-SHA256 webhook signature."""
    expected = hmac.new(secret, payload, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature)

amount = Decimal("19.99")
fee = (amount * Decimal("0.029")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)  # Decimal('0.58')

hmac.compare_digest compares in constant time, which a plain == doesn't. Create Decimal values from strings, as above, because Decimal(19.99) brings the float's binary error with it.

Background jobs and idempotency

Webhooks should be acknowledged quickly and processed in the background, for example with a task queue such as Celery or a cloud queue. That keeps the provider from timing out and retrying while your code issues tickets or sends emails.

Retries will still happen, so make every handler idempotent. Store each provider event ID once and skip any event you've already processed, so a resent "payment succeeded" never issues a second ticket.

How to make a payment gateway for an events ticketing website

A ticketing site has one payment problem that ordinary shops don't: inventory that sells out. Reserve the seats when checkout starts, hold them for a few minutes, and release them if payment fails or the timer runs out.

Use the provider's hosted checkout or embedded fields so card numbers never reach your Python servers, which keeps your PCI DSS scope small. Confirm the order only from a verified webhook, not from the shopper's redirect back to your site, since a redirect can be skipped or faked.

How to create a payment gateway in Python for merchants

A gateway for other businesses is a much larger project. Python can run the application layer, but the work that decides success is the same in every language.

The list runs from a vault with key management, acquirer connectors, routing and 3-D Secure to a merchant back office, with a PCI DSS Level 1 assessment over all of it. Staff is the largest share of the cost to build. Then come compliance, infrastructure and the months before revenue.

When to license instead

If you're building a payment business rather than a platform that takes payments, licensing the gateway usually gets you to market sooner. Licensing gives you 700+ connectors and a merchant back office that already exist, under our PCI DSS Level 1 certification on SaaS.

Your Python team then builds what's specific to your business on top, through the platform's APIs and webhooks.

Questions

How can I develop a payment gateway in Python?

For your own platform, integrate a provider's hosted checkout, keep money in Decimal, verify webhooks with hmac.compare_digest and process them idempotently in the background. For a gateway serving other merchants, add a vault, acquirer connectors, routing, a back office and PCI DSS validation.

Which Python framework is best for payments?

Django and FastAPI both work well. The patterns matter more than the framework: exact decimals, verified webhooks, idempotent handlers and a small PCI DSS scope.

How do I make a payment gateway for an events ticketing website?

Reserve seats at checkout, use a provider's hosted checkout, confirm orders only from verified webhooks and release seats when payment fails or times out.

Is Python fast enough for a payment gateway?

For most gateways, yes. Payment latency is usually dominated by network calls to acquirers and issuers, not by the language running your code.

Build on a ready gateway

Tell us what your Python platform does and who it serves, and we'll show where our APIs and webhooks fit.