Docs : SDK : purchaseSubscription
purchaseSubscription redirects your users to a Stripe Checkout form, a Stripe-hosted payment page designed to reduce friction accepting payment from your users. To call this function, you must first do the following:
-
Include the Stripe.js client in your web app.
-
Connect a Stripe account to your Userbase admin account in your Admin panel.
-
Create a product with a recurring price in your Stripe dashboard.
-
Take note of the price ID in the Stripe dashboard (it starts with 'price_').
And you're good to go!
userbase.purchaseSubscription({
successUrl: 'https://example.com/success',
cancelUrl: 'https://example.com/cancel',
priceId: 'price_1HsI6AE3KJTop1QHR00YQXPr'
}).then(() => {
// user successfully redirected to Stripe Checkout form
}).catch((e) => console.error(e))
Parameters
-
sucessUrl [string] - The URL the user will be redirected to upon successful payment.
-
cancelUrl [string] - The URL the user will be redirected to upon canceling payment.
-
priceId [string] - The price ID the user will purchase. You can find this ID in your Stripe dashboard after creating a product with a recurring price.
Notes
-
You can offer a free trial via your Admin panel. Users who sign up for an app with a free trial set will start out in trial mode. Users have full capabilities until the trial expires.
-
To accept payments in production, you will need to purchase the Prod plan.
-
Upon successful payment, the user's subscriptionStatus will be set to 'active'. This allows the user to successfully call openDatabase on an app that requires payment.
-
The user's credit card will be charged automatically at the start of every billing period. For example, if the user purchases a yearly subscription on May 4th, then the user will be charged on May 4th every year until they cancel.
-
You can customize your Stripe Checkout form in your Stripe dashboard.
Advanced
- You can allow promo codes at checkout. To enable promo codes:
- Add a coupon to your Stripe account under Products > Coupons
- Then add a promotion code for that coupon
- Finally, visit your app's Payments Portal in your Admin panel, click "Show Advanced", and enable the option to "Allow promotion codes in Checkout"
- You can support calculating tax at checkout. To enable automatic tax:
- You need to activate tax collection under Settings > Tax Settings
- Set an origin address via the same link and configure tax settings
- Then you need to set tax behavior on a product
- Finally, visit your app's Payments Portal in your Admin panel, click "Show Advanced", and enable the option to "Collect taxes in Checkout"
Errors
- ParamsMustBeObject
- SuccessUrlMissing
- SuccessUrlMustBeString
- SuccessUrlInvalid
- CancelUrlMissing
- CancelUrlMustBeString
- CancelUrlInvalid
- StripeError
- StripeJsLibraryMissing
- SubscriptionPlanNotSet
- SubscriptionPlanAlreadyPurchased
- StripeAccountNotConnected
- PaymentsDisabled
- UserNotSignedIn
- TooManyRequests
- ServiceUnavailable