Learn how users can purchase Payment Plans to access AI services
With the Payments Library, users/subscribers can order Payment Plans by paying. The process is simple and secure. The Subscriber needs to have enough funds in their wallet to pay for the Plan in the token selected by the creator of the Plan. In Nevermined, the Payment Plan creators can request the payments in any valid ERC20 or Native token (depending on the network where the Plan is created).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 (Stripe integration) and will return the user to the application with the Payment Plan ordered.
// Here we are ordering the plan created in the previous stepsconst orderResult = await payments.plans.orderPlan(planId)// OUTPUT: orderResult: // {// txHash: '0x5b95ebaec594b6d87e688faddf85eec3d708e6a06e61864699e5a366af1343f6',// success: true// }
Copy
# Here we are ordering the Plan created in the previous stepsorder_result = payments.order_plan(plan_DID) # OUTPUT: orderResult: # { success: True, agreementId: '0xaabbcc' }
For fiat payments, the process involves redirecting users to a Stripe checkout session:
TypeScript
Python
Copy
// 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
# 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 orders 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.
try { const orderResult = await payments.plans.orderPlan(planId) if (orderResult.success) { console.log('Plan purchased successfully!') console.log('Transaction hash:', orderResult.txHash) // Check balance to confirm const balance = await payments.plans.getPlanBalance(planId) console.log('Available credits:', balance.balance) }} catch (error) { console.error('Purchase failed:', error.message) if (error.message.includes('insufficient funds')) { console.log('Please ensure you have enough tokens in your wallet') } else if (error.message.includes('user rejected')) { console.log('Transaction was cancelled by user') }}
Copy
try: order_result = payments.order_plan(plan_id) if order_result.get('success'): print('Plan purchased successfully!') print(f'Agreement ID: {order_result.get("agreementId")}') # Check balance to confirm balance = payments.get_plan_balance(plan_id) print(f'Available credits: {balance.get("balance")}')except Exception as error: print(f'Purchase failed: {error}') if 'insufficient funds' in str(error): print('Please ensure you have enough tokens in your wallet') elif 'user rejected' in str(error): print('Transaction was cancelled by user')