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:
parent
345cfd6f36
commit
46ac047772
2 changed files with 69 additions and 5 deletions
|
@ -52,7 +52,7 @@
|
|||
|
||||
{{else}}
|
||||
|
||||
{{#if this.stripeConnectAccountId}}
|
||||
{{#if (and this.stripeConnectAccountId (not this.saveStripeSettings.isRunning))}}
|
||||
<div class="gh-stripe-connected-container">
|
||||
{{svg-jar "check-circle-stroke" class="check-circle"}}
|
||||
<h1>You are connected to Stripe</h1>
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue