Custom Amounts & Charging Saved Cards
Let customers decide how much to pay, or charge a card they already saved for any amount you choose. HL Hunt supports both, so you can build a "buy more credits" or "top up your balance" flow: the customer picks $10, $25, $50 or types their own amount, and the card on file is charged straight away.
Two ways to do it
Option A: the customer types the amount on the hosted checkout page Best for donations, tips, invoices paid in part, deposits, and first-time top-ups. HL Hunt collects the card, and you get a reusable saved card back.
Option B: your app picks the amount and charges a saved card Best for repeat top-ups and usage purchases. The customer never re-enters their card: you show the amount choices in your own UI, then charge the saved card from your server.
Most products use both: Option A the first time (it saves the card), Option B every time after.
Option A: Checkout Session with an editable amount
Create the session server-side with allow_custom_amount: true. The line item amount becomes the suggested amount shown in the field, and minimum_amount_cents sets the lowest amount the customer may enter.
bash
curl -X POST https://api.hlhunt.org/v1/checkout/sessions \
-H "Authorization: Bearer sk_test_your_secret_key" \
-H "Content-Type: application/json" \
-d '{
"success_url": "https://yoursite.com/billing?session={CHECKOUT_SESSION_ID}",
"cancel_url": "https://yoursite.com/billing",
"line_items": [{ "description": "Account credit", "amount": 2500, "quantity": 1 }],
"allow_custom_amount": true,
"minimum_amount_cents": 500,
"customer_email": "jenny@example.com",
"metadata": { "account_id": "acct_123", "purpose": "top_up" }
}'Redirect the customer to the url in the response. The hosted page shows an Amount to pay field, prefilled with the suggested amount ($25.00 here), and will not accept less than the minimum ($5.00 here).
Rules
minimum_amount_centsmust be at least50($0.50); it defaults to50when omitted.- The highest amount accepted is $999,999.99.
- The editable field appears on the hosted checkout page (the session
url). Embedded and popup checkout charge the fixed line item amount. - After payment,
amount_totalon the session and on thecheckout.session.completedwebhook is the amount the customer actually paid. Credit the account from that value, not from the suggested amount.
Finish the order On checkout.session.completed, read from data.object:
amount_total: what was charged, in cents.transaction_id: the resulting charge.payment_instrument_id: the saved card. Store it against the customer; it is thesourcefor Option B.metadata: what you attached (for exampleaccount_id).
Option B: charge a saved card for any amount
Build the amount picker in your own app (preset buttons plus a custom amount field). When the customer confirms, charge the saved card from your server:
bash
curl -X POST https://api.hlhunt.org/v1/charges \
-H "Authorization: Bearer sk_test_your_secret_key" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: topup-acct_123-7f3a9c" \
-d '{
"amount": 5000,
"currency": "USD",
"source": "PIxxxxxxxxxxxxxxxxxx",
"description": "Account credit",
"metadata": { "account_id": "acct_123", "purpose": "top_up" }
}'sourceis the saved card: thepayment_instrument_idfrom a completed Checkout Session, or theidreturned byPOST /v1/payment-methods.- A card charge answers
201with"status": "succeeded"and the card is charged immediately, or400witherror.type: "card_error"when it is declined. Show the decline message and offer to update the card (send them through Option A again). - Always send an Idempotency-Key, unique per purchase attempt, so a double click or a retried request never charges twice.
- Validate the amount on your server (your own minimum and maximum). The API accepts any amount the card allows; the limits in your UI are yours to enforce.
Example: a "Buy more credit" screen
Show preset amounts ($10, $25, $50, $100) and a Custom amount field.
If the customer has a saved card, show "Visa •••• 4242" with a Pay button: your server calls POST /v1/charges with that source (Option B).
If they have no saved card, or tap Use a different card: your server creates a Checkout Session with allow_custom_amount: true and the chosen amount as the line item (Option A), then redirects them.
Add the credit when the charge succeeds: from the 201 response for Option B, or from checkout.session.completed for Option A.
Listen to charge.succeeded and charge.failed webhooks as your source of truth if your server can be interrupted between charging and crediting.
Related
- Checkout Sessions: every session field, embedded and popup delivery.
- Charges API: full request and response reference for
POST /v1/charges. - Payment Methods API: saving a card with hlhunt.js and
POST /v1/payment-methods. - Webhooks: verifying signatures and reading
data.object.