Charge
Take credits right away, or hold them first for long jobs.
Charge
await credits.charge({ user, credits: 5 });Or charge an action from your price list, which you set in the dashboard:
await credits.charge({ user, action: "chat.message" });
await credits.charge({ user, action: "video.render", quantity: 12 }); // 12 secondsThe charge settles on Whitechain before the call returns.
Hold, then charge
For long or uncertain work, hold the most it could cost. Then capture what it actually cost, or release it if the work fails.
const hold = await credits.hold({ user, credits: 100 });
try {
const cost = await render(job);
await credits.capture(hold.holdId, { credits: cost }); // the rest goes back
} catch {
await credits.release(hold.holdId); // the user keeps everything
}Holds release on their own after an hour.
Refund
await credits.refund({ user, credits: 5 });You can refund up to what the user spent with your app. You can also refund any charge from the dashboard.
No double charges
Pass your own ref, such as a message or order id. The same ref always returns the first result, so a retry can never charge twice.
await credits.charge({ user, credits: 5, ref: orderId });