BetterAuth
Payments and Checkouts made dead simple with BetterAuth
A Better Auth plugin for integrating Polar payments and subscriptions into your authentication flow.
Features
- Checkout Integration
- Customer Portal
- Webhook handling
- Automatic Customer creation on signup
Installation
Preparation
Go to your Polar Organization Settings, and create an Organization Access Token. Add it to your environment.
Configuration Options
Required Options
client
: Polar SDK client instance
Optional Options
createCustomerOnSignUp
: Automatically create a Polar customer when a user signs upgetCustomerCreateParams
: Custom function to provide additional customer creation parametersenableCustomerPortal
: Enable the customer portal functionality. Deployed as GET endpoint at /portalcheckout.enabled
: Enable checkout functionality. Deployed as GET endpoint at /checkoutcheckout.products
: Array of products or function returning products. Slug passed will then be passable as route param.checkout.successUrl
: URL to redirect to after successful checkout
Webhook Handlers
Configure a Webhook endpoint in your Polar Organization Settings page. Webhook endpoint is configured at /polar/webhooks.
Add the secret to your environment.
The plugin supports handlers for all Polar webhook events:
onPayload
- Catch-all handler for any incoming Webhook eventonCheckoutCreated
- Triggered when a checkout is createdonCheckoutUpdated
- Triggered when a checkout is updatedonOrderCreated
- Triggered when an order is createdonOrderRefunded
- Triggered when an order is refundedonRefundCreated
- Triggered when a refund is createdonRefundUpdated
- Triggered when a refund is updatedonSubscriptionCreated
- Triggered when a subscription is createdonSubscriptionUpdated
- Triggered when a subscription is updatedonSubscriptionActive
- Triggered when a subscription becomes activeonSubscriptionCanceled
- Triggered when a subscription is canceledonSubscriptionRevoked
- Triggered when a subscription is revokedonSubscriptionUncanceled
- Triggered when a subscription cancellation is reversedonProductCreated
- Triggered when a product is createdonProductUpdated
- Triggered when a product is updatedonOrganizationUpdated
- Triggered when an organization is updatedonBenefitCreated
- Triggered when a benefit is createdonBenefitUpdated
- Triggered when a benefit is updatedonBenefitGrantCreated
- Triggered when a benefit grant is createdonBenefitGrantUpdated
- Triggered when a benefit grant is updatedonBenefitGrantRevoked
- Triggered when a benefit grant is revokedonCustomerCreated
- Triggered when a customer is createdonCustomerUpdated
- Triggered when a customer is updatedonCustomerDeleted
- Triggered when a customer is deletedonCustomerStateChanged
- Triggered when a customer is created
API Routes
The plugin adds the following API routes:
GET /checkout/:slug
- Redirect to Polar checkoutGET /state
- Customer state (Customer Data, Active Subscriptions, Entitlements, etc.) for the authenticated userGET /customer-portal
- Redirects to Polar Customer Portal for authenticated userPOST /polar/webhooks
- Incoming webhooks are automatically parsed & validated
Customers
When createCustomerOnSignUp
is enabled, a new Polar Customer is automatically created when a new User is added in the Better-Auth Database.
All new customers are created with an associated externalId
, which is the ID of your User in the Database. This allows us to skip any Polar Customer to User mapping in your Database.
Checkouts
When checkouts are enabled, you’re able to initialize Checkout Sessions on the /checkout/:slug
route.
Checkouts with slug
If you pass an array of products to the configuration, you’re able to use the slug as a reference instead of using the product id.
Checkouts with product id
All your organization products are eligible for checkouts, even if they’re not passed to the products-configuration.
You can initialize a checkout session like following - /checkout?productId=123-456-789
Was this page helpful?