From e5b0cf86865adcc9786a92b125cb3edd3b7f8d9f Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 28 Sep 2021 13:35:23 +0200 Subject: [PATCH] Added basic support for coupons refs https://github.com/TryGhost/Team/issues/1090 This allows us to create Stripe Coupons and use them with Stripe Checkout from the members-api module whilst we develop the Offers feature. --- .../members-stripe-service/lib/StripeService.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ghost/members-stripe-service/lib/StripeService.js b/ghost/members-stripe-service/lib/StripeService.js index 7e62496e9e..2966e5f060 100644 --- a/ghost/members-stripe-service/lib/StripeService.js +++ b/ghost/members-stripe-service/lib/StripeService.js @@ -78,6 +78,16 @@ module.exports = class StripeService { this._configured = true; } + /** + * @param {object} options + */ + async createCoupon(options) { + await this._rateLimitBucket.throttle(); + const coupon = await this._stripe.coupons.create(options); + + return coupon; + } + /** * @param {object} options * @param {string} options.name @@ -348,14 +358,19 @@ module.exports = class StripeService { const metadata = options.metadata || undefined; const customerEmail = customer ? customer.email : options.customerEmail; await this._rateLimitBucket.throttle(); + let discounts; + if (options.coupon) { + discounts = [{coupon: options.coupon.id}]; + } const session = await this._stripe.checkout.sessions.create({ payment_method_types: ['card'], success_url: options.successUrl, cancel_url: options.cancelUrl, customer_email: customerEmail, // @ts-ignore - we need to update to latest stripe library to correctly use newer features - allow_promotion_codes: this._config.enablePromoCodes, + allow_promotion_codes: discounts ? undefined : this._config.enablePromoCodes, metadata, + discounts, /* line_items: [{ price: priceId