AI assistant applications and SAAS platforms often use prepaid tokens or credits. For example, the user may buy 500 API tokens. If the balance drops below 50 API tokens, another 500 API tokens may be added and billed automatically.
Workflow
The workflow for purchasing api tokens is as follows:
- User registers on your platform
- User buys tokens on Digistore24 checkout page and initializes automatic payments
- Digistore24 notifies your platform via Webhook or IPN.
- User spends tokens on your platform
- Optional: If the balance drops below a certain limit, more tokens are billed and added.
Code examples
In the download package of the Digistore24 api connector, you will find PHP code examples.
1. User registers on your platform
The user's first contact is with your platform. Usually you'll want them to create an account there.
2. User buys tokens on Digistore24 and initializes automatic payments
The api function createBuyUrl allows you (among other things) to
- create an order form link for a specific product
- adjust the pricing/billing
- add custom parameters
The Digistore24 API will provide a checkout url. Direct the user to this url to perform the payment.
Set up a Digistore24 product for buying api tokens.
API request:
The api request creates a checkout url to bill 50 USD as a single payment from the buyer. Buyer name and email adresse are prefilled.
Url: https://www.digistore24.com/api/createBuyUrl:
HTTP header:
- X-DS-API-KEY: Your Digistore24 API key
Post data:
- product_id=123
- buyer[email]=some.name@domain.com (optional)
- buyer[last_name]=John (optional)
- buyer[first_name]=Myer (optional)
- payment_plan[first_amount] =50
- payment_plan[other_amounts]=0
- payment_plan[currency]=USD
- tracking[custom]=your-apps-payment-reference
- urls [thankyou_url]=https://www.you-app.com/thankyou
- settings[force_rebilling]=Y (only needed, if you want to use automatic billing in step 5. This excludes wire transfer and other single payment only payment methods.)
You can see more parameters here.
Return value:
- url: Redirect the user to this url so that they can make the payment.
3. Digistore24 notifies your platform via Webhook or IPN.
Either add a webhook or a generic IPN to securely receive the payment status.
Webhook
For ease of implementation, we recomment this setup.
In the webhook setup form, you should enter the parameter name for tracking[custom] next to Custom parameter.
If the parameter for "Event" is "payment", book the token, because you received the payment.
If you later receive an event refund or chargeback, then the payment has been cancelled.
IPN
The generic IPN integration is slightly more complex, but it does transfer much more data. The value of tracking[custom] is transferred as parameter "custom".
If the parameter "Event" is "on_payment", book the token, because you received the payment.
If you later receive on_refund or on_chargeback, then the payment has been cancelled.
4. User spends tokens on your platform
You are responsible for managing the token balance.
5. Optional: If the balance drops below a certain limit, more tokens are billed and added
In your application, you implement a logic to automatically add new tokens, if the balance drops below a certain limit.
Apply for "billing on demand" permission
By default, billing on demand is disabled for your account. If you want to use billing on demand, contact our support to enable it. Please add a description of your platform and the current development status or the planned release date to your request.
When implementing auto recharging, make sure that:
- the user needs to explicitly turn on automatic billing in your application. And that it is turned off by default.
- the user can enter the limit and the amount to his needs. It's ok to have a minimum recharging amount like 50 USD or so.
API request:
This api request bills for 50 USD from the buyer from step 2.
Url: https://www.digistore24.com/api/createBillingOnDemand
HTTP header:
- X-DS-API-KEY: Your Digistore24 API key
Post data:
- purchase_id=The reference order from step 2.
- product_id=123 (use the same product id as in step 2 or a different one)
- payment_plan[first_amount]=50
- payment_plan[other_amounts]=0
- payment_plan[currency]=USD
- tracking[custom]=your-apps-payment-reference
You can see more parameters here.
Return value:
- created_purchase_id: the recharge purchase gets a new order id
-
payment_status:
- completed → payment was successful. Your app may add the tokens to the user's balance.
- any other value: payment failed. Begin at step 2 again.
- payment_status_msg: a human readable description of payment_status.