diff --git a/src/client/index.ts b/src/client/index.ts index b60bdfb..d41564c 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -166,6 +166,10 @@ export class StripeSubscriptions { subscriptionMetadata?: Record; /** Metadata to attach to the payment intent (only for mode: "payment") */ paymentIntentMetadata?: Record; + /** Whether to create an invoice for the payment (only for mode: "payment") */ + invoiceCreation?: boolean; + /** Metadata to attach to the invoice (only for mode: "payment" and invoiceCreation: true) */ + invoiceMetadata?: Record; }, ) { const stripe = new StripeSDK(this.apiKey); @@ -201,6 +205,19 @@ export class StripeSubscriptions { }; } + // Add invoice creation metadata for linking userId/orgId + if (args.mode === "payment" && args.invoiceCreation) { + sessionParams.invoice_creation = { + enabled: true, + }; + + if (args.invoiceMetadata) { + sessionParams.invoice_data = { + metadata: args.invoiceMetadata, + }; + } + } + const session = await stripe.checkout.sessions.create(sessionParams); return { @@ -570,6 +587,7 @@ async function processEvent( amountDue: invoice.amount_due, amountPaid: invoice.amount_paid, created: invoice.created, + metadata: invoice.metadata || {}, }); break; } diff --git a/src/component/private.ts b/src/component/private.ts index e95926f..671b6d5 100644 --- a/src/component/private.ts +++ b/src/component/private.ts @@ -280,6 +280,7 @@ export const handleInvoiceCreated = mutation({ amountDue: v.number(), amountPaid: v.number(), created: v.number(), + metadata: v.optional(v.any()), }, returns: v.null(), handler: async (ctx, args) => { @@ -292,10 +293,10 @@ export const handleInvoiceCreated = mutation({ if (!existing) { // Look up orgId/userId from the subscription if available - let orgId: string | undefined; - let userId: string | undefined; + let orgId: string | undefined = args.metadata?.orgId as string | undefined; + let userId: string | undefined = args.metadata?.userId as string | undefined; - if (args.stripeSubscriptionId) { + if ((!orgId || !userId) && args.stripeSubscriptionId) { const subscription = await ctx.db .query("subscriptions") .withIndex("by_stripe_subscription_id", (q) =>