From 46ac047772ede0ae803476bf74e2f95c84c55dfe Mon Sep 17 00:00:00 2001 From: Rishabh Date: Thu, 20 May 2021 20:47:23 +0530 Subject: [PATCH] Added default price creation on stripe connect no refs On Stripe connect, we add the default prices on the new Stripe account in the DB so the site has prices in Portal checked from the start --- .../gh-members-payments-setting.hbs | 2 +- .../components/gh-members-payments-setting.js | 72 +++++++++++++++++-- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/ghost/admin/app/components/gh-members-payments-setting.hbs b/ghost/admin/app/components/gh-members-payments-setting.hbs index 4391f1e4ce..9235779ffc 100644 --- a/ghost/admin/app/components/gh-members-payments-setting.hbs +++ b/ghost/admin/app/components/gh-members-payments-setting.hbs @@ -52,7 +52,7 @@ {{else}} - {{#if this.stripeConnectAccountId}} + {{#if (and this.stripeConnectAccountId (not this.saveStripeSettings.isRunning))}}
{{svg-jar "check-circle-stroke" class="check-circle"}}

You are connected to Stripe

diff --git a/ghost/admin/app/components/gh-members-payments-setting.js b/ghost/admin/app/components/gh-members-payments-setting.js index 3b54b5eb29..25841b8a3e 100644 --- a/ghost/admin/app/components/gh-members-payments-setting.js +++ b/ghost/admin/app/components/gh-members-payments-setting.js @@ -3,13 +3,14 @@ import {computed} from '@ember/object'; import {currencies} from 'ghost-admin/utils/currency'; import {reads} from '@ember/object/computed'; import {inject as service} from '@ember/service'; -import {task} from 'ember-concurrency'; +import {task, timeout} from 'ember-concurrency'; export default Component.extend({ config: service(), ghostPaths: service(), ajax: service(), settings: service(), + store: service(), topCurrencies: null, currencies: null, @@ -227,14 +228,77 @@ export default Component.extend({ yield this.settings.reload(); }), + calculateDiscount(monthly, yearly) { + if (isNaN(monthly) || isNaN(yearly)) { + return 0; + } + + return monthly ? 100 - Math.floor((yearly / 12 * 100) / monthly) : 0; + }, + + getActivePrice(prices, interval, amount, currency) { + return prices.find((price) => { + return ( + price.active && price.amount === amount && price.type === 'recurring' && + price.interval === interval && price.currency.toLowerCase() === currency.toLowerCase() + ); + }); + }, + + updatePortalPlans(monthlyPriceId, yearlyPriceId) { + let portalPlans = ['free']; + if (monthlyPriceId) { + portalPlans.push(monthlyPriceId); + } + if (yearlyPriceId) { + portalPlans.push(yearlyPriceId); + } + this.settings.set('portalPlans', portalPlans); + }, + saveStripeSettings: task(function* () { this.set('stripeConnectError', null); this.set('stripeConnectSuccess', null); if (this.get('settings.stripeConnectIntegrationToken')) { try { - const response = yield this.settings.save(); - this.set('membersStripeOpen', false); - this.set('stripeConnectSuccess', true); + let response = yield this.settings.save(); + const products = yield this.store.query('product', {include: 'stripe_prices'}); + this.product = products.firstObject; + if (this.product) { + const stripePrices = this.product.stripePrices || []; + const yearlyDiscount = this.calculateDiscount(5, 50); + stripePrices.push( + { + nickname: 'Monthly', + amount: 500, + active: 1, + description: 'Full access', + currency: 'usd', + interval: 'month', + type: 'recurring' + }, + { + nickname: 'Yearly', + amount: 5000, + active: 1, + currency: 'usd', + description: yearlyDiscount > 0 ? `${yearlyDiscount}% discount` : 'Full access', + interval: 'year', + type: 'recurring' + } + ); + this.product.set('stripePrices', stripePrices); + yield timeout(1000); + const updatedProduct = yield this.product.save(); + const monthlyPrice = this.getActivePrice(updatedProduct.stripePrices, 'month', 500, 'usd'); + const yearlyPrice = this.getActivePrice(updatedProduct.stripePrices, 'year', 5000, 'usd'); + this.updatePortalPlans(monthlyPrice.id, yearlyPrice.id); + this.settings.set('membersMonthlyPriceId', monthlyPrice.id); + this.settings.set('membersYearlyPriceId', yearlyPrice.id); + response = yield this.settings.save(); + this.set('membersStripeOpen', false); + this.set('stripeConnectSuccess', true); + } return response; } catch (error) { if (error.payload && error.payload.errors) {