comecerodocs
Tutorials

Sell prepaid credit

Let customers buy prepaid usage credit with an invoice, add it once payment completes, and follow the balance, credit history, refunds and spending limits.

This tutorial creates an invoice for a 20 USD usage credit purchase. The customer pays it through the invoice payment page. Comecero adds the credit once the invoice is paid and uses it before charging cash for usage.

Before you start

  • Usage billing is on for your account.
  • The customer has a test subscription with one active USD usage agreement and metered grants, such as Bill for API calls.
  • Credit sales are enabled in usage_customer_settings under Subscription settings. A purchase is refused while the shop does not sell credit.
  • Your server has a test secret token in COMECERO_SECRET_TOKEN. Keep it out of the browser.

Create the purchase invoice

Call Buy prepaid credit with the Comecero customer id and the subscription id. These are not your own customer id from usage events. Give each purchase its own Idempotency-Key and keep it for a retry of the same request.

curl -X POST https://api.comecero.com/api/v1/customers/0106784119870CueWmZ9P7/credit_purchases \
  -H "Authorization: Bearer $COMECERO_SECRET_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: credit_purchase_5501" \
  -d '{ "subscription_id": "SB0123456789", "amount": "20" }'

The first request returns 201 with an open invoice and its invoice_id. A repeat with the same key and body returns that invoice with 200 for a day. Reusing the key with another body is refused. A plan can have at most 20 unpaid credit invoices.

The amount must fit the shop's minimum and maximum and the currency's decimal places. The invoice has one Prepaid credit line. Its credit_purchase.credit_granted is the credit before tax, so a total of 20 USD can give less than 20 USD of credit when prices include tax. Show the customer the invoice's credit, tax and total.

A customer's own login can call the same route using customers/me.

Let the customer pay

Use Create an invoice payment link for the returned invoice and send the customer to link_url. The normal invoice page handles their payment. See Invoices.

If you take payment through your own server, follow Pay an invoice. Never automatically retry a payment after a decline, timeout or unknown result. Read its state and let the customer choose what to do next.

Follow the credit

Once the invoice is paid, Comecero creates or uses the subscription's credit and connects its metered grants to it. The credit is added once, even if payment notifications repeat. Do not add it again from your own invoice:paid webhook.

Read the customer's usage for credit.balance, the projected balance after current unbilled usage, and amount_so_far, the estimated cash charge. Credit history shows settled additions and use. Those values can differ before the period is billed.

For an entitlement's settled ledger balance, use Get an entitlement balance from your server. Manual credits added with Add prepaid credit are separate from paid credit purchases.

What happens when credit runs out

The included allowance is free. Usage above it is priced, linked credit pays first, and any cash left goes on the invoice with overage_policy: allow. With block, the cash part is capped and recorded in capped_amount. A fully capped run can complete with no separate usage invoice.

For bought credit, at_zero: stop also caps uncovered usage; charge permits cash billing. A customer's spending limit is applied before credit is drawn, and counts both credit-funded and cash usage. These rules cap billing, while the meter keeps counting. Your app must stop access itself if that is what you offer.

Refunds and chargebacks

Comecero tracks each paid credit purchase. A refund takes back that purchase's unused credit. Refunding more than is unused is refused with 422 (UbV149); only Comecero staff can override it. A chargeback freezes credit under the purchase's dispute rules. It is not the same as deleting a manual credit grant. See Credits and burndown.

Next steps

On this page