0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

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
This commit is contained in:
Rishabh 2021-05-20 20:47:23 +05:30
parent 345cfd6f36
commit 46ac047772
2 changed files with 69 additions and 5 deletions

View file

@ -52,7 +52,7 @@
{{else}} {{else}}
{{#if this.stripeConnectAccountId}} {{#if (and this.stripeConnectAccountId (not this.saveStripeSettings.isRunning))}}
<div class="gh-stripe-connected-container"> <div class="gh-stripe-connected-container">
{{svg-jar "check-circle-stroke" class="check-circle"}} {{svg-jar "check-circle-stroke" class="check-circle"}}
<h1>You are connected to Stripe</h1> <h1>You are connected to Stripe</h1>

View file

@ -3,13 +3,14 @@ import {computed} from '@ember/object';
import {currencies} from 'ghost-admin/utils/currency'; import {currencies} from 'ghost-admin/utils/currency';
import {reads} from '@ember/object/computed'; import {reads} from '@ember/object/computed';
import {inject as service} from '@ember/service'; import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency'; import {task, timeout} from 'ember-concurrency';
export default Component.extend({ export default Component.extend({
config: service(), config: service(),
ghostPaths: service(), ghostPaths: service(),
ajax: service(), ajax: service(),
settings: service(), settings: service(),
store: service(),
topCurrencies: null, topCurrencies: null,
currencies: null, currencies: null,
@ -227,14 +228,77 @@ export default Component.extend({
yield this.settings.reload(); 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* () { saveStripeSettings: task(function* () {
this.set('stripeConnectError', null); this.set('stripeConnectError', null);
this.set('stripeConnectSuccess', null); this.set('stripeConnectSuccess', null);
if (this.get('settings.stripeConnectIntegrationToken')) { if (this.get('settings.stripeConnectIntegrationToken')) {
try { try {
const response = yield this.settings.save(); let response = yield this.settings.save();
this.set('membersStripeOpen', false); const products = yield this.store.query('product', {include: 'stripe_prices'});
this.set('stripeConnectSuccess', true); 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; return response;
} catch (error) { } catch (error) {
if (error.payload && error.payload.errors) { if (error.payload && error.payload.errors) {