Skip to content

Commit

Permalink
feature(website): campaign metadata in stripe checkout (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrashee authored Jan 26, 2024
1 parent 774feeb commit fa901ee
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions website/src/app/api/stripe/checkout-session/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type CreateCheckoutSessionData = {
currency?: WebsiteCurrency;
intervalCount?: number;
firebaseAuthToken?: string;
campaignId?: string; // to optionally associate a payment to a fundraising campaign.
};

type CreateCheckoutSessionRequest = { json(): Promise<CreateCheckoutSessionData> } & Request;
Expand All @@ -22,6 +23,7 @@ export async function POST(request: CreateCheckoutSessionRequest) {
successUrl,
recurring = false,
firebaseAuthToken,
campaignId,
} = await request.json();
const stripe = initializeStripe(process.env.STRIPE_SECRET_KEY!);
const userDoc = await getUserDocFromAuthToken(firebaseAuthToken);
Expand All @@ -33,6 +35,12 @@ export async function POST(request: CreateCheckoutSessionRequest) {
product: recurring ? process.env.STRIPE_PRODUCT_RECURRING : process.env.STRIPE_PRODUCT_ONETIME,
recurring: recurring ? { interval: 'month', interval_count: intervalCount } : undefined,
});
const metadata = campaignId
? {
campaignId: campaignId,
}
: undefined;

const session = await stripe.checkout.sessions.create({
mode: recurring ? 'subscription' : 'payment',
customer: customerId,
Expand All @@ -45,6 +53,7 @@ export async function POST(request: CreateCheckoutSessionRequest) {
],
success_url: successUrl,
locale: 'auto',
metadata: metadata,
});
return NextResponse.json(session);
}

0 comments on commit fa901ee

Please sign in to comment.