/** * @dotdo/oauth - Simple Stripe Integration * * Minimal Stripe integration for customer management and webhooks. * This module provides helpers for linking OAuth users to Stripe customers. */ import type { OAuthUser } from './types.js'; /** * Stripe customer data */ export interface StripeCustomer { id: string; email: string | null; name: string | null; metadata: Record; } /** * Stripe subscription data */ export interface StripeSubscription { id: string; customerId: string; status: 'active' | 'past_due' | 'canceled' | 'trialing' | 'unpaid' | 'incomplete' | 'incomplete_expired' | 'paused'; priceId: string; currentPeriodEnd: number; } /** * Stripe webhook event types we handle */ export type StripeWebhookEventType = 'customer.created' | 'customer.updated' | 'customer.deleted' | 'customer.subscription.created' | 'customer.subscription.updated' | 'customer.subscription.deleted' | 'invoice.paid' | 'invoice.payment_failed'; /** * Stripe webhook event */ export interface StripeWebhookEvent { id: string; type: StripeWebhookEventType; data: { object: Record; }; } /** * Storage interface for Stripe-related operations */ export interface StripeStorage { getUserByStripeCustomerId(stripeCustomerId: string): Promise; updateUserStripeCustomerId(userId: string, stripeCustomerId: string): Promise; getUser(id: string): Promise; saveUser(user: OAuthUser): Promise; } /** * Extended user type with Stripe fields */ export interface OAuthUserWithStripe extends OAuthUser { stripeCustomerId?: string; stripeSubscriptionId?: string; stripeSubscriptionStatus?: StripeSubscription['status']; } /** * Stripe client interface (compatible with stripe npm package) */ export interface StripeClient { customers: { create(params: { email?: string; name?: string; metadata?: Record; }): Promise<{ id: string; }>; retrieve(id: string): Promise; update(id: string, params: { metadata?: Record; }): Promise; }; subscriptions: { retrieve(id: string): Promise<{ id: string; customer: string; status: StripeSubscription['status']; items: { data: Array<{ price: { id: string; }; }>; }; current_period_end: number; }>; }; webhooks: { constructEvent(payload: string, signature: string, secret: string): StripeWebhookEvent; }; } /** * Ensure a user has a Stripe customer ID, creating one if needed */ export declare function ensureStripeCustomer(user: OAuthUserWithStripe, stripe: StripeClient, storage: StripeStorage): Promise; /** * Get Stripe customer for a user, or null if not linked */ export declare function getStripeCustomer(user: OAuthUserWithStripe, stripe: StripeClient): Promise; /** * Link an existing Stripe customer to a user */ export declare function linkStripeCustomer(userId: string, stripeCustomerId: string, stripe: StripeClient, storage: StripeStorage): Promise; /** * Handle Stripe webhook events */ export declare function handleStripeWebhook(event: StripeWebhookEvent, storage: StripeStorage): Promise<{ handled: boolean; action?: string; }>; /** * Verify Stripe webhook signature */ export declare function verifyStripeWebhook(payload: string, signature: string, webhookSecret: string, stripe: StripeClient): StripeWebhookEvent; /** * Create a minimal Stripe client for Cloudflare Workers * (alternative to the full stripe npm package) */ export declare function createStripeClient(secretKey: string): StripeClient; /** * Verify Stripe webhook with async signature verification * Use this for proper cryptographic verification */ export declare function verifyStripeWebhookAsync(payload: string, signature: string, webhookSecret: string): Promise; //# sourceMappingURL=stripe.d.ts.map