1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Server: Allows providing a coupon when creating the Stripe checkout session

This commit is contained in:
Laurent Cozic 2021-07-31 17:05:39 +01:00
parent 23553b70e0
commit b5b6111e83
2 changed files with 28 additions and 8 deletions

View File

@ -53,7 +53,10 @@
var stripe = Stripe('{{{stripeConfig.publishableKey}}}'); var stripe = Stripe('{{{stripeConfig.publishableKey}}}');
var createCheckoutSession = function(priceId) { 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", { return fetch("{{{stripeConfig.webhookBaseUrl}}}/stripe/createCheckoutSession", {
method: "POST", method: "POST",
@ -61,10 +64,16 @@
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
body: JSON.stringify({ body: JSON.stringify({
priceId: priceId priceId: priceId,
coupon: coupon,
}) })
}).then(function(result) { }).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(); return result.json();
}
}); });
}; };

View File

@ -33,6 +33,7 @@ async function stripeEvent(stripe: Stripe, req: any): Promise<Stripe.Event> {
interface CreateCheckoutSessionFields { interface CreateCheckoutSessionFields {
priceId: string; priceId: string;
coupon: string;
} }
type StripeRouteHandler = (stripe: Stripe, path: SubPath, ctx: AppContext)=> Promise<any>; type StripeRouteHandler = (stripe: Stripe, path: SubPath, ctx: AppContext)=> Promise<any>;
@ -60,9 +61,7 @@ export const postHandlers: PostHandlers = {
const fields = await bodyFields<CreateCheckoutSessionFields>(ctx.req); const fields = await bodyFields<CreateCheckoutSessionFields>(ctx.req);
const priceId = fields.priceId; const priceId = fields.priceId;
// See https://stripe.com/docs/api/checkout/sessions/create const checkoutSession: Stripe.Checkout.SessionCreateParams = {
// for additional parameters to pass.
const session = await stripe.checkout.sessions.create({
mode: 'subscription', mode: 'subscription',
payment_method_types: ['card'], payment_method_types: ['card'],
line_items: [ line_items: [
@ -80,7 +79,19 @@ export const postHandlers: PostHandlers = {
// is redirected to the success page. // is redirected to the success page.
success_url: `${globalConfig().baseUrl}/stripe/success?session_id={CHECKOUT_SESSION_ID}`, success_url: `${globalConfig().baseUrl}/stripe/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${globalConfig().baseUrl}/stripe/cancel`, 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); logger.info('Created checkout session', session.id);