@kitiumai/auth-stripe
v1.0.1
Published
Stripe billing adapter for Kitium Auth
Maintainers
Readme
@kitium/auth-stripe (Stripe Billing Adapter)
Stripe billing adapter for Kitium Auth core.
- Create/update customers
- Create/update/cancel subscriptions
- Create checkout/billing portal sessions
- Process webhooks (with signature verification)
Install
npm install @kitium/auth-stripe stripeUsage
import { AuthCore } from '@kitium/auth'
import { StripeBillingAdapter } from '@kitium/auth-stripe'
import { PostgresStorageAdapter } from '@kitium/auth-postgres'
const storage = new PostgresStorageAdapter(process.env.DATABASE_URL!)
const billing = new StripeBillingAdapter(
process.env.STRIPE_SECRET_KEY!,
process.env.STRIPE_WEBHOOK_SECRET // optional if you process webhooks
)
const auth = new AuthCore(storage, {
jwtSecret: process.env.JWT_SECRET!,
billing
})
await auth.initialize()Customers
const customer = await billing.createCustomer({ email: '[email protected]', name: 'Alice' })
const fetched = await billing.getCustomer(customer.id)
await billing.updateCustomer(customer.id, { name: 'Alice B' })Subscriptions
const sub = await billing.createSubscription({ customerId: customer.id, productId: 'prod_123' })
const s = await billing.getSubscription(sub.id)
await billing.updateSubscription(sub.id, { metadata: { plan: 'pro' } })
await billing.cancelSubscription(sub.id)Checkout & Billing Portal
const checkout = await billing.createCheckoutSession({
customerId: customer.id,
productId: 'prod_123',
successUrl: 'https://your.app/billing/success',
cancelUrl: 'https://your.app/billing/cancel'
})
// redirect to checkout.url
const portal = await billing.createBillingPortalSession(customer.id, 'https://your.app/account')
// redirect to portal.urlWebhooks
// Express example
app.post('/api/stripe/webhook', express.raw({ type: 'application/json' }), async (req, res) => {
try {
const event = await billing.processWebhook(req.body, req.headers['stripe-signature'] as string)
// handle event.type
res.sendStatus(200)
} catch (e) {
res.status(400).send('Invalid webhook')
}
})License
MIT
