Top-ups
Let customers buy credit with Stripe Checkout on your own Stripe account.
A top-up is a customer buying credit. You connect your own Stripe account to an environment, create a top-up with one call, and send the customer to the Stripe Checkout page it returns. When Stripe confirms the payment, Hixel credits the customer's wallet. The money goes to your Stripe account; Hixel never holds it. Stripe produces an invoice and a receipt for each payment.
Connect Stripe#
Each environment has its own connection. Production accepts only a live Stripe key, and a test environment only a test key, so a test payment can never credit a live wallet.
Create a restricted key in Stripe#
In Stripe, under Developers → API keys, create a restricted key in the environment's mode with these permissions and no others:
| Resource | Permission |
|---|---|
| Checkout Sessions | Write |
| Customers | Write |
| Payment Intents | Write |
| Payment Methods | Read |
| Charges | Read |
| Disputes | Read |
| Invoices | Read |
| Connected accounts / Account | Read |
A standard secret key works too, but it can do everything in your account.
Save the key#
In the dashboard under Credits → Stripe, or:
curl -X PUT https://api.hixel.dev/v1/stripe_connection \
-H "Authorization: Bearer $HIXEL_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "api_key": "rk_test_…" }'const connection = await hixel.stripeConnection.connect({ api_key: "rk_test_…" });
console.log(connection.webhook_url, connection.webhook_events);Hixel checks the key with Stripe before storing it, encrypted. It is never shown again:
reads give its kind, mode and last four characters. The answer includes webhook_url and
webhook_events, for the next step.
Add the webhook endpoint in Stripe#
In Stripe, under Developers → Webhooks, add an endpoint for the connection's webhook_url
(https://api.hixel.dev/webhooks/stripe/…) and subscribe it to every event in
webhook_events.
Save the endpoint's signing secret#
curl -X PUT https://api.hixel.dev/v1/stripe_connection/webhook_secret \
-H "Authorization: Bearer $HIXEL_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "webhook_secret": "whsec_…" }'await hixel.stripeConnection.setWebhookSecret({ webhook_secret: "whsec_…" });Top-ups can be created from now on.
GET /v1/stripe_connection shows the connection, including has_webhook_secret,
last_event_at and last_error. PATCH /v1/stripe_connection with {"enabled": false} stops
new top-ups and refills while refunds and disputes of earlier payments are still applied.
Create a top-up#
curl https://api.hixel.dev/v1/customers/org_42/top_ups \
-H "Authorization: Bearer $HIXEL_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: top-up-org_42-7f3a" \
-d '{
"amount_micros": "25000000",
"success_url": "https://app.example.com/billing/paid",
"cancel_url": "https://app.example.com/billing",
"member": "user_7",
"metadata": { "order": "42" }
}'const topUp = await hixel.topUps.create(
"org_42",
{
amount_micros: "25000000",
success_url: "https://app.example.com/billing/paid",
cancel_url: "https://app.example.com/billing",
member: "user_7",
metadata: { order: "42" },
},
{ idempotencyKey: "top-up-org_42-7f3a" },
);
// Redirect the customer to topUp.checkout_url.{
"id": "tup_…",
"customer": "org_42",
"member": "user_7",
"amount_micros": "25000000",
"currency": "usd",
"mode": "test",
"status": "pending",
"checkout_url": "https://checkout.stripe.com/c/pay/cs_test_…",
"expires_at": "2026-11-26T10:00:00.000Z",
"metadata": { "order": "42" },
"ledger_entry_id": null,
…
}Redirect the customer to checkout_url. Send Idempotency-Key so a retried call answers the
same top-up instead of opening a second Checkout session.
| Field | Rules |
|---|---|
amount_micros | Whole cents (a multiple of "10000"), from $1 to $10,000 |
success_url | Your https:// page Checkout returns to after paying |
cancel_url | Your https:// page Checkout's back button goes to |
member | Optional. Who asked for it |
metadata | Optional. Up to 20 keys; kept by Hixel and shown on reads and events, not sent to Stripe |
save_payment_method | Optional. Ask Stripe to save the card for auto-refill. Getting the customer's consent to be charged later is your responsibility. |
The customer pays in US dollars, the amount credited.
What happens next#
status | Meaning |
|---|---|
creating | Stripe has not answered the session's creation yet |
pending | The Checkout session is open, waiting for the customer |
processing | Paid with a method that clears later, such as a bank debit |
succeeded | Paid and credited |
failed | The payment failed; nothing was credited |
expired | The session expired unpaid; nothing was credited |
When Stripe confirms the payment, the wallet is credited once, as a top_up ledger entry, and
you get wallet.top_up_succeeded and
wallet.credited. A paid top-up shows invoice_url and invoice_pdf_url. Hixel never
trusts the content of a Stripe event: it reads the payment back from Stripe with your key before
crediting, and credits each top-up once however often Stripe sends its events.
Read top-ups with GET /v1/top_ups/{id}, GET /v1/customers/{external_id}/top_ups and
GET /v1/top_ups (newest first; filter by status).
Refunds and disputes#
Refund a payment in Stripe as usual. Its amount is taken back from the wallet once
(wallet.top_up_refunded), and given back if the refund fails. A dispute takes the amount once
Stripe withdraws the funds and gives it back if you win (wallet.top_up_disputed); it also turns
the customer's auto-refill off. If the credit was already spent, the wallet goes below zero and
the customer's paid requests are refused until credit is added again.
Payment reports#
GET /v1/payments/summary?from=…&to=… counts the environment's top-ups and auto-refills created in
a range, by status, with their amounts and what refunds and disputes took back.
GET /v1/payments/reversals lists the top-ups and auto-refills with a refund or a dispute, newest
first; each item's kind says which (top_up or auto_refill).
Disconnecting#
A card payment can be disputed for about 120 days. Once a connection is deleted, refunds and
disputes of its payments are no longer taken from wallets, so DELETE /v1/stripe_connection is
refused while payments may still be completing (stripe_connection_in_use), and while payments
were credited in the last 120 days (stripe_connection_has_recent_payments) unless you confirm
with ?confirm_recent_payments=true. To only stop new top-ups, disable the connection instead.
Failures#
| Status | code | When |
|---|---|---|
409 | stripe_not_connected | Creating a top-up without an enabled connection and its signing secret |
400 | stripe_mode_mismatch | A live key on a test environment, or a test key on Production |
409 | stripe_account_changed | A key of another Stripe account: disconnect first |
409 | stripe_connection_in_use | Deleting while a payment may still complete |
409 | stripe_connection_has_recent_payments | Deleting with payments credited in the last 120 days |