Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export class StripeSubscriptions {
subscriptionMetadata?: Record<string, string>;
/** Metadata to attach to the payment intent (only for mode: "payment") */
paymentIntentMetadata?: Record<string, string>;
/** 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<string, string>;
},
) {
const stripe = new StripeSDK(this.apiKey);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -570,6 +587,7 @@ async function processEvent(
amountDue: invoice.amount_due,
amountPaid: invoice.amount_paid,
created: invoice.created,
metadata: invoice.metadata || {},
});
break;
}
Expand Down
7 changes: 4 additions & 3 deletions src/component/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) =>
Expand Down