From b5b6111e8388d19a394c5e19f7ab2cfb77112b41 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 31 Jul 2021 17:05:39 +0100 Subject: [PATCH] Server: Allows providing a coupon when creating the Stripe checkout session --- Assets/WebsiteAssets/templates/plans.mustache | 17 +++++++++++++---- packages/server/src/routes/index/stripe.ts | 19 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Assets/WebsiteAssets/templates/plans.mustache b/Assets/WebsiteAssets/templates/plans.mustache index 84d0513dc..00383d37d 100644 --- a/Assets/WebsiteAssets/templates/plans.mustache +++ b/Assets/WebsiteAssets/templates/plans.mustache @@ -53,7 +53,10 @@ var stripe = Stripe('{{{stripeConfig.publishableKey}}}'); var createCheckoutSession = function(priceId) { - console.info('Creating Stripe session for price:', priceId); + const urlQuery = new URLSearchParams(location.search); + const coupon = urlQuery.get('coupon') || ''; + + console.info('Creating Stripe session for price:', priceId, 'Coupon:', coupon); return fetch("{{{stripeConfig.webhookBaseUrl}}}/stripe/createCheckoutSession", { method: "POST", @@ -61,10 +64,16 @@ "Content-Type": "application/json" }, body: JSON.stringify({ - priceId: priceId + priceId: priceId, + coupon: coupon, }) - }).then(function(result) { - return result.json(); + }).then(async function(result) { + if (!result.ok) { + console.error('Could not create Stripe checkout session', await result.text()); + alert('The checkout session could not be created. Please contact support@joplincloud.com for support.'); + } else { + return result.json(); + } }); }; diff --git a/packages/server/src/routes/index/stripe.ts b/packages/server/src/routes/index/stripe.ts index cfc2fca9b..f7b0d6118 100644 --- a/packages/server/src/routes/index/stripe.ts +++ b/packages/server/src/routes/index/stripe.ts @@ -33,6 +33,7 @@ async function stripeEvent(stripe: Stripe, req: any): Promise { interface CreateCheckoutSessionFields { priceId: string; + coupon: string; } type StripeRouteHandler = (stripe: Stripe, path: SubPath, ctx: AppContext)=> Promise; @@ -60,9 +61,7 @@ export const postHandlers: PostHandlers = { const fields = await bodyFields(ctx.req); const priceId = fields.priceId; - // See https://stripe.com/docs/api/checkout/sessions/create - // for additional parameters to pass. - const session = await stripe.checkout.sessions.create({ + const checkoutSession: Stripe.Checkout.SessionCreateParams = { mode: 'subscription', payment_method_types: ['card'], line_items: [ @@ -80,7 +79,19 @@ export const postHandlers: PostHandlers = { // is redirected to the success page. success_url: `${globalConfig().baseUrl}/stripe/success?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${globalConfig().baseUrl}/stripe/cancel`, - }); + }; + + if (fields.coupon) { + checkoutSession.discounts = [ + { + coupon: fields.coupon.trim(), + }, + ]; + } + + // See https://stripe.com/docs/api/checkout/sessions/create + // for additional parameters to pass. + const session = await stripe.checkout.sessions.create(checkoutSession); logger.info('Created checkout session', session.id);