Auto-refill
Charge a customer's saved card automatically when their balance runs low.
Auto-refill charges the card a customer saved in a top-up when their balance falls below a threshold, on your Stripe account, and credits the wallet when Stripe confirms the payment. The customer is not present, so the card is charged off-session.
Turn it on for a customer#
- Create a top-up with
"save_payment_method": trueand have the customer pay it. - Set the customer's auto-refill settings:
curl -X PUT https://api.hixel.dev/v1/customers/org_42/auto_refill \
-H "Authorization: Bearer $HIXEL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"enabled": true,
"threshold_micros": "5000000",
"amount_micros": "20000000",
"monthly_cap_micros": "100000000"
}'const settings = await hixel.autoRefills.putSettings("org_42", {
enabled: true,
threshold_micros: "5000000",
amount_micros: "20000000",
monthly_cap_micros: "100000000",
});{
"customer": "org_42",
"enabled": true,
"threshold_micros": "5000000",
"amount_micros": "20000000",
"monthly_cap_micros": "100000000",
"payment_method": { "id": "pm_…", "brand": "visa", "last_four": "4242", "saved_at": "…" },
"refilled_this_month_micros": "0",
"consecutive_failures": 0,
"retry_after": null,
"disabled_reason": null,
…
}| Field | Rules |
|---|---|
enabled | Turning it on needs a connected Stripe account and a card the customer saved on it |
threshold_micros | A refill starts when the balance falls below this. Positive, at most $10,000 |
amount_micros | What one refill charges and credits: whole cents, $1 to $10,000 |
monthly_cap_micros | The most refills may charge in a calendar month (UTC): at least amount_micros, at most $100,000 |
PUT sets all four fields. GET /v1/customers/{external_id}/auto_refill reads them (a customer
never set reads enabled: false), and DELETE forgets them.
How refills run#
- Any change of the wallet's balance checks whether a refill is due. A due refill is charged within about a minute.
- A wallet has at most one refill in flight. If one refill does not bring the balance back above the threshold, the next one follows once the first is final, until the threshold or the monthly cap is reached.
- Each refill is charged once, under an idempotency key of its own with Stripe, however often the charge is retried.
- A refund or dispute that lowers the balance never starts a refill.
- A refill succeeds with a
wallet.creditedwebhook of kindauto_refill.
List a customer's refills with GET /v1/customers/{external_id}/auto_refills and the
environment's with GET /v1/auto_refills (filter by status).
When it fails#
- A decline (or a bank asking for authentication, which cannot happen off-session) fails the
refill and sends
wallet.auto_refill_failed. The next refill waits an hour after the first failure and a day after the second. After three failures in a row, auto-refill turns itself off and sendswallet.auto_refill_disabledwithreason: "failures". - A card that cannot be charged again (expired, lost, removed) turns auto-refill off at once
(
reason: "payment_method"). - A dispute of any of the customer's payments turns it off (
reason: "dispute") and clears the saved cards. Turning it on again needs a newly saved card. - The monthly cap: a refill that would pass it is skipped (
skipped_cap) and you getwallet.auto_refill_cap_reached, once per month.
Turn it back on with PUT and "enabled": true once the customer has fixed the cause.
A refill that needs attention#
Rarely, Hixel cannot tell whether Stripe charged a refill (Stripe's answer was lost
and cannot be read back). Such a refill is needs_attention and blocks the wallet's next refill
until it is resolved. It is re-read daily, and you can act on it:
POST /v1/auto_refills/{id}/recheckreads Stripe again now (at most every 10 minutes per refill; sooner is429auto_refill_too_soon). A payment found is credited once.POST /v1/auto_refills/{id}/resolvewith{"note": "…"}marks it as not charged, after Stripe is read once more and shows no payment that succeeded or may still succeed (otherwise409auto_refill_may_be_charged).
Whatever ended a refill, a payment Stripe shows later is still credited, once.
GET /v1/auto_refills/attention lists the customers whose auto-refill needs a look (state:
disabled, turned off by a failure; retrying, waiting after a decline), the most recent first;
GET /v1/auto_refill_attention_counts answers how many customers are in each state. A refill in
needs_attention is listed by
GET /v1/auto_refills?status=needs_attention.