Learn how users can purchase Payment Plans to access AI services
With the Payments Library it’s easy for users and AI agents to buy Payment Plans. The user only needs to hold sufficient balance in the plan currency (set on plan creation). With Nevermined, Plan creators can set payments currency to any valid ERC-20 or native token.Payment Plans can also be purchased in fiat (via Stripe). The Stripe Checkout flow requires the use of credit cards and is fully integrated into the Nevermined Web App. Nevermined likes to recommend stablecoin payments. That way, the payment and fetching of credits associated with the Payment Plan can be done in a single step.
// Here we are ordering the plan created in the previous stepsconst orderResult = await payments.plans.orderPlan(planId)// OUTPUT: orderResult:// {// txHash: '0x5b95ebaec594b6d87e688faddf85eec3d708e6a06e61864699e5a366af1343f6',// success: true// }
Copy
Ask AI
# Here we are ordering the Plan created in the previous stepsorder_result = payments.plans.order_plan(plan_id)# OUTPUT: orderResult:# { success: True, agreementId: '0xaabbcc' }
When the Payment Plan requires a payment in fiat, the payment can be initiated with the libraries, but the final step needs to be done in the Nevermined App. The app will handle the payment in fiat (via Stripe Checkout) and will return the user to the application that initiated the purchase flow.For fiat payments, the process involves redirecting users to a Stripe Checkout session:
TypeScript
Python
Copy
Ask AI
// Get fiat payment configurationconst fiatConfig = await payments.plans.getFiatPriceConfig(planId)// Order plan with fiat paymentconst { sessionId, url } = await payments.plans.orderFiatPlan(planId)// Redirect user to Stripe checkoutwindow.location.href = url// After successful payment, user will be redirected back with the plan active
Copy
Ask AI
# Get fiat payment configurationfiat_config = payments.plans.get_fiat_price_config(plan_id)# Order plan with fiat paymentfiat_result = payments.plans.order_fiat_plan(plan_id)# Redirect user to Stripe checkoutprint(f'Stripe checkout URL: {fiat_result["url"]}')# User completes payment on Stripe and returns to your application
After a user purchases a plan, they can check their balance for that plan. The balance represents the number of credits the user has available to use within the plan.
Time-based plans provide a balance of 1 credit for subscribers. When the plan expires, this balance will be zero.
Users browse available payment plans and select one that fits their needs
2
Initiate Purchase
Call payments.plans.orderPlan() or payments.plans.orderFiatPlan() depending on payment method
3
Complete Payment
For crypto: transaction is processed on-chain and happens in one transaction.
For fiat: user completes Stripe checkout, and the credits will be distributed automatically after the payment.
4
Verify Purchase
Check balance to confirm the plan was successfully purchased and get the balance. Call payments.plans.getPlanBalance().
5
Access Services
Use the plan to access AI agents and services. Call payments.x402.getX402AccessToken().