From 75169b705be07a7bc5c7d59d0f0c971b1d18d2ca Mon Sep 17 00:00:00 2001 From: Rishabh Garg Date: Tue, 4 May 2021 21:02:20 +0530 Subject: [PATCH] Added custom prices list to portal settings (#12912) refs https://github.com/TryGhost/Team/issues/637 With custom prices, Portal now needs to show all available custom prices in the UI instead of just `monthly` and `yearly` prices. This change adds a list of all custom prices to Portal site settings for the default product which Portal will use to show the available prices in UI. Note: As part of cleanup, the stripe price ids will be removed from the prices list. Also: - Fixes product name in serialised subscriptions - Adds `type` value in serialised price object --- .../server/models/stripe-customer-subscription.js | 4 +++- core/server/services/members/middleware.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/server/models/stripe-customer-subscription.js b/core/server/models/stripe-customer-subscription.js index fd9265f7aa..8b3782c108 100644 --- a/core/server/models/stripe-customer-subscription.js +++ b/core/server/models/stripe-customer-subscription.js @@ -43,13 +43,15 @@ const StripeCustomerSubscription = ghostBookshelf.Model.extend({ nickname: defaultSerializedObject.stripePrice.nickname, amount: defaultSerializedObject.stripePrice.amount, interval: defaultSerializedObject.stripePrice.interval, + type: defaultSerializedObject.stripePrice.type, currency: String.prototype.toUpperCase.call(defaultSerializedObject.stripePrice.currency) }; if (defaultSerializedObject.stripePrice.stripeProduct) { + const productData = defaultSerializedObject.stripePrice.stripeProduct.product || {}; serialized.price.product = { id: defaultSerializedObject.stripePrice.stripeProduct.stripe_product_id, - name: defaultSerializedObject.stripePrice.stripeProduct.name, + name: productData.name, product_id: defaultSerializedObject.stripePrice.stripeProduct.product_id }; } diff --git a/core/server/services/members/middleware.js b/core/server/services/members/middleware.js index b4b1506629..7c2917f1c0 100644 --- a/core/server/services/members/middleware.js +++ b/core/server/services/members/middleware.js @@ -77,6 +77,19 @@ const updateMemberData = async function (req, res) { } }; +const getDefaultProductPrices = async function () { + const page = await membersService.api.productRepository.list({ + limit: 1 + }); + const [product] = page.data; + if (product) { + const model = await membersService.api.productRepository.get({id: product.get('id')}, {withRelated: ['stripePrices']}); + const productData = model.toJSON(); + return productData.stripePrices || []; + } + return []; +}; + const getMemberSiteData = async function (req, res) { const isStripeConfigured = membersService.config.isStripeConnected(); const domain = urlUtils.urlFor('home', true).match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i')); @@ -86,6 +99,7 @@ const getMemberSiteData = async function (req, res) { if (!supportAddress.includes('@')) { supportAddress = `${supportAddress}@${blogDomain}`; } + const defaultPrices = await getDefaultProductPrices(); const response = { title: settingsCache.get('title'), description: settingsCache.get('description'), @@ -95,6 +109,7 @@ const getMemberSiteData = async function (req, res) { url: urlUtils.urlFor('home', true), version: ghostVersion.safe, plans: membersService.config.getPublicPlans(), + prices: defaultPrices, allow_self_signup: membersService.config.getAllowSelfSignup(), members_signup_access: settingsCache.get('members_signup_access'), is_stripe_configured: isStripeConfigured,