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 launch wizard

no refs

On Stripe connect in launch wizard, we add the default prices on the new Stripe account in the DB so the site has prices in Portal checked from the start. It behaves the same way as connecting stripe from membership settings.
This commit is contained in:
Rishabh 2021-05-20 21:23:38 +05:30
parent 46ac047772
commit 02a0063444
2 changed files with 71 additions and 1 deletions

View file

@ -102,7 +102,11 @@
@class="w-70 ml4 right gh-btn gh-btn-black gh-btn-large gh-btn-icon-right" @class="w-70 ml4 right gh-btn gh-btn-black gh-btn-large gh-btn-icon-right"
data-test-button="wizard-next" data-test-button="wizard-next"
> >
{{#if this.saveAndContinueTask.isRunning}}
<span>Saving...</span>
{{else}}
<span>{{if this.settings.stripeConnectAccountId "Continue" "Save and continue"}}{{svg-jar "arrow-right-tail"}}</span> <span>{{if this.settings.stripeConnectAccountId "Continue" "Save and continue"}}{{svg-jar "arrow-right-tail"}}</span>
{{/if}}
</GhTaskButton> </GhTaskButton>
</div> </div>
</div> </div>

View file

@ -10,6 +10,7 @@ export default class GhLaunchWizardConnectStripeComponent extends Component {
@service config; @service config;
@service ghostPaths; @service ghostPaths;
@service settings; @service settings;
@service store;
@tracked hasActiveStripeSubscriptions = false; @tracked hasActiveStripeSubscriptions = false;
@tracked showDisconnectStripeConnectModal = false; @tracked showDisconnectStripeConnectModal = false;
@ -59,6 +60,34 @@ export default class GhLaunchWizardConnectStripeComponent extends Component {
this.stripeConnectError = null; this.stripeConnectError = null;
} }
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);
}
@task({drop: true}) @task({drop: true})
*openDisconnectStripeConnectModalTask() { *openDisconnectStripeConnectModalTask() {
this.hasActiveStripeSubscriptions = false; this.hasActiveStripeSubscriptions = false;
@ -114,6 +143,43 @@ export default class GhLaunchWizardConnectStripeComponent extends Component {
try { try {
yield this.settings.save(); 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);
yield this.settings.save();
}
this.pauseAndContinueTask.perform(); this.pauseAndContinueTask.perform();
return true; return true;
} catch (error) { } catch (error) {