The five services inside a gateway
A self-hosted gateway is a small set of services with clear boundaries:
- Invoice API — creates a payment request and returns an address and an amount.
- Address service — issues a unique deposit address for each invoice or customer.
- Chain watchers — one per network, detecting transactions and counting confirmations.
- Ledger and reconciliation — the record of what you believe happened, checked against the chain.
- Notifier — tells your application, through webhooks, that a payment changed state.
Withdrawals and sweeping are a sixth service, and the only one that needs signing keys.
One address per payment
Giving every invoice its own address is what lets you match an incoming transfer to an order without asking the customer for a reference. With hierarchical deterministic wallets, addresses can be derived from an extended public key, so the public-facing service can issue addresses while holding no private keys at all. The signing keys stay on a separate system that only the sweep and withdrawal service can reach.
A payment is a state machine
Treat each payment as moving through explicit states: created, detected, confirmed, settled, and the exits — expired and refunded. Every transition is recorded with its cause. This is what makes awkward cases manageable:
- Underpayment. The customer sent less than the invoice. Wait for a top-up, accept within a tolerance, or refund — but decide in advance.
- Overpayment. Credit the excess or return it.
- Late payment. Funds arrive after the invoice expired and the quoted rate has moved.
- Several transactions for one invoice. Sum them, and confirm each separately.
When is a payment final?
A transaction that has been broadcast but not yet included in a block can still be replaced or dropped. Once it is in a block, each further block makes reversal less likely, but short chain reorganisations do happen. A gateway therefore needs a confirmation policy per network — and often per amount — and a watcher that can roll a payment back from detected if the block it was in disappears.
Show the customer "payment seen" early if you like, but release goods or credit a balance only on confirmed.
Token deposits are not ordinary transfers
Bitcoin payments are outputs to an address. USDT on Ethereum or Tron is different: the transfer is a call to the token's contract, and the deposit shows up as a transfer event emitted by that contract rather than as a native payment to your address. The watcher has to read contract events, check the contract address against an allow-list so that look-alike tokens are ignored, and handle the token's decimals correctly.
Sweeping and the fee problem
With one address per invoice, funds end up spread across thousands of addresses. A sweep service consolidates them into a treasury wallet. On Bitcoin that means choosing when fees are low enough to be worth it. On Ethereum and Tron there is a catch: a deposit address holding only tokens cannot pay for its own outgoing transfer, because network fees are charged in the native coin. The sweep service has to fund each address first, or use an arrangement that pays fees on its behalf. This cost belongs in your pricing from day one.
Webhooks that can be trusted
Your application learns about state changes through webhooks, and webhooks fail. Three rules keep them safe:
- Sign every callback so the receiver can verify it came from the gateway.
- Retry with backoff until the receiver acknowledges.
- Make handling idempotent. The same event delivered twice must not credit an account twice. Use the event identifier as the key.
The receiver should also be able to ask the gateway for the current state of any payment, so a missed callback is an inconvenience rather than a loss.
Reconciliation is the real product
A scheduled job compares the ledger with the chain: every address balance, every confirmed transaction, every sweep and every fee. Anything on the chain that the ledger does not know about, or the reverse, is raised for a person to look at. Without this job, errors accumulate quietly. With it, they are found the same day.
Custody and compliance
Self-hosting means the keys are yours, which removes the risk of a processor freezing funds and puts key management on your side instead. Separate hot and cold wallets, limits on automated withdrawals and maker–checker approval for large ones are standard practice. Licensing, KYC and AML obligations depend on where you operate; the gateway should provide the hooks your compliance process needs.
We build gateways like this on Bitcoin, Ethereum and Tron, deployed in the client's own infrastructure. See crypto payment gateway development for what a build includes.