2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2022-01-21 22:25:47 +03:00
|
|
|
|
2020-06-11 12:05:40 +03:00
|
|
|
export default class MembersUtilsService extends Service {
|
2021-01-28 21:41:03 +03:00
|
|
|
@service settings;
|
2021-06-29 09:49:20 +03:00
|
|
|
@service feature;
|
2022-10-31 15:01:39 +03:00
|
|
|
@service session;
|
2022-03-08 10:30:31 +03:00
|
|
|
@service store;
|
2020-06-11 12:05:40 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2022-10-27 12:44:19 +03:00
|
|
|
paidTiers = null;
|
|
|
|
|
2021-05-21 20:22:01 +03:00
|
|
|
get isMembersEnabled() {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settings.membersEnabled;
|
2021-05-21 20:22:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-24 17:53:03 +03:00
|
|
|
get paidMembersEnabled() {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settings.paidMembersEnabled;
|
2022-05-24 17:53:03 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:56:38 +03:00
|
|
|
get isMembersInviteOnly() {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settings.membersInviteOnly;
|
2022-09-28 14:56:38 +03:00
|
|
|
}
|
|
|
|
|
2022-10-27 12:44:19 +03:00
|
|
|
get hasMultipleTiers() {
|
|
|
|
return this.paidMembersEnabled && this.paidTiers && this.paidTiers.length > 1;
|
|
|
|
}
|
|
|
|
|
2022-11-11 12:11:34 +03:00
|
|
|
get hasActiveTiers() {
|
|
|
|
return this.paidMembersEnabled && this.paidTiers && this.paidTiers.length > 0;
|
|
|
|
}
|
|
|
|
|
2022-10-27 12:44:19 +03:00
|
|
|
async fetch() {
|
|
|
|
if (this.paidTiers !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:01:39 +03:00
|
|
|
// contributors don't have permissions to fetch tiers
|
|
|
|
if (this.session.user && !this.session.user.isContributor) {
|
|
|
|
return this.store.query('tier', {filter: 'type:paid+active:true', limit: 'all'}).then((tiers) => {
|
|
|
|
this.paidTiers = tiers;
|
|
|
|
});
|
|
|
|
}
|
2022-10-27 12:44:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async reload() {
|
2022-10-31 15:01:39 +03:00
|
|
|
// contributors don't have permissions to fetch tiers
|
|
|
|
if (this.session.user && !this.session.user.isContributor) {
|
|
|
|
return this.store.query('tier', {filter: 'type:paid+active:true', limit: 'all'}).then((tiers) => {
|
|
|
|
this.paidTiers = tiers;
|
|
|
|
});
|
|
|
|
}
|
2022-10-27 12:44:19 +03:00
|
|
|
}
|
|
|
|
|
2022-05-24 17:53:03 +03:00
|
|
|
/**
|
|
|
|
* Note: always use paidMembersEnabled! Only use this getter for the Stripe Connection UI.
|
|
|
|
*/
|
2020-06-11 15:46:04 +03:00
|
|
|
get isStripeEnabled() {
|
2022-10-07 17:24:03 +03:00
|
|
|
const stripeDirect = this.config.stripeDirect;
|
2020-06-11 17:45:45 +03:00
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
const hasDirectKeys = !!this.settings.stripeSecretKey && !!this.settings.stripePublishableKey;
|
|
|
|
const hasConnectKeys = !!this.settings.stripeConnectSecretKey && !!this.settings.stripeConnectPublishableKey;
|
2020-06-11 17:45:45 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
if (stripeDirect) {
|
|
|
|
return hasDirectKeys;
|
|
|
|
}
|
2020-06-11 17:45:45 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
return hasConnectKeys || hasDirectKeys;
|
2020-06-11 12:05:40 +03:00
|
|
|
}
|
2021-01-28 21:41:03 +03:00
|
|
|
|
2021-05-18 15:59:47 +03:00
|
|
|
// Button / Icon helpers ---------------------------------------------------
|
|
|
|
|
|
|
|
get defaultButtonIcons() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
icon: 'portal-icon-1',
|
|
|
|
value: 'icon-1'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: 'portal-icon-2',
|
|
|
|
value: 'icon-2'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: 'portal-icon-3',
|
|
|
|
value: 'icon-3'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: 'portal-icon-4',
|
|
|
|
value: 'icon-4'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: 'portal-icon-5',
|
|
|
|
value: 'icon-5'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
get defaultIconKeys() {
|
|
|
|
return this.defaultButtonIcons.map(buttonIcon => buttonIcon.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
get buttonIcon() {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settings.portalButtonIcon || this.defaultIconKeys[0];
|
2021-05-18 15:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Plan helpers ------------------------------------------------------------
|
|
|
|
|
|
|
|
get isFreeChecked() {
|
2022-10-07 16:23:21 +03:00
|
|
|
const allowedPlans = this.settings.portalPlans || [];
|
|
|
|
return !!(this.settings.membersSignupAccess === 'all' && allowedPlans.includes('free'));
|
2021-05-18 15:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get isMonthlyChecked() {
|
2022-10-07 16:23:21 +03:00
|
|
|
const allowedPlans = this.settings.portalPlans || [];
|
2021-05-18 15:59:47 +03:00
|
|
|
return !!(this.isStripeConfigured && allowedPlans.includes('monthly'));
|
|
|
|
}
|
|
|
|
|
|
|
|
get isYearlyChecked() {
|
2022-10-07 16:23:21 +03:00
|
|
|
const allowedPlans = this.settings.portalPlans || [];
|
2021-05-18 15:59:47 +03:00
|
|
|
return !!(this.isStripeConfigured && allowedPlans.includes('yearly'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Portal preview ----------------------------------------------------------
|
|
|
|
|
|
|
|
getPortalPreviewUrl(overrides) {
|
2022-03-08 10:30:31 +03:00
|
|
|
let {
|
2021-05-18 15:59:47 +03:00
|
|
|
disableBackground = false,
|
2021-01-28 21:41:03 +03:00
|
|
|
page = 'signup',
|
2022-10-07 16:23:21 +03:00
|
|
|
button = this.settings.portalButton,
|
2021-05-18 15:59:47 +03:00
|
|
|
buttonIcon = this.buttonIcon,
|
|
|
|
isFreeChecked = this.isFreeChecked,
|
|
|
|
isMonthlyChecked = this.isMonthlyChecked,
|
|
|
|
isYearlyChecked = this.isYearlyChecked,
|
2021-01-28 21:41:03 +03:00
|
|
|
monthlyPrice,
|
|
|
|
yearlyPrice,
|
2022-10-07 16:23:21 +03:00
|
|
|
portalPlans = this.settings.portalPlans,
|
2022-05-11 20:11:54 +03:00
|
|
|
portalTiers,
|
2021-05-18 15:59:47 +03:00
|
|
|
currency,
|
2022-10-07 16:23:21 +03:00
|
|
|
membersSignupAccess = this.settings.membersSignupAccess
|
2021-05-18 15:59:47 +03:00
|
|
|
} = overrides;
|
2021-01-28 21:41:03 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
const tiers = this.store.peekAll('tier') || [];
|
2022-03-08 10:30:31 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
portalTiers = portalTiers || tiers.filter((t) => {
|
2022-03-08 10:30:31 +03:00
|
|
|
return t.visibility === 'public' && t.type === 'paid';
|
|
|
|
}).map(t => t.id);
|
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
const baseUrl = this.config.blogUrl;
|
2021-01-28 21:41:03 +03:00
|
|
|
const portalBase = '/#/portal/preview';
|
|
|
|
const settingsParam = new URLSearchParams();
|
2022-10-07 16:23:21 +03:00
|
|
|
const signupButtonText = this.settings.portalButtonSignupText || '';
|
2021-05-18 15:59:47 +03:00
|
|
|
const allowSelfSignup = membersSignupAccess === 'all' && (!this.isStripeEnabled || isFreeChecked);
|
2021-01-28 21:41:03 +03:00
|
|
|
|
2021-05-18 16:34:46 +03:00
|
|
|
settingsParam.append('button', button);
|
2022-10-07 16:23:21 +03:00
|
|
|
settingsParam.append('name', this.settings.portalName);
|
2021-02-10 21:40:27 +03:00
|
|
|
settingsParam.append('isFree', isFreeChecked);
|
|
|
|
settingsParam.append('isMonthly', isMonthlyChecked);
|
|
|
|
settingsParam.append('isYearly', isYearlyChecked);
|
2021-01-28 21:41:03 +03:00
|
|
|
settingsParam.append('page', page);
|
|
|
|
settingsParam.append('buttonIcon', encodeURIComponent(buttonIcon));
|
|
|
|
settingsParam.append('signupButtonText', encodeURIComponent(signupButtonText));
|
2021-05-18 15:59:47 +03:00
|
|
|
settingsParam.append('membersSignupAccess', membersSignupAccess);
|
2021-04-27 20:35:05 +03:00
|
|
|
settingsParam.append('allowSelfSignup', allowSelfSignup);
|
2021-01-28 21:41:03 +03:00
|
|
|
|
2021-05-04 18:43:31 +03:00
|
|
|
if (portalPlans) {
|
|
|
|
settingsParam.append('portalPrices', encodeURIComponent(portalPlans));
|
|
|
|
}
|
|
|
|
|
2022-05-16 18:25:03 +03:00
|
|
|
if (portalTiers) {
|
2022-05-11 20:11:54 +03:00
|
|
|
settingsParam.append('portalProducts', encodeURIComponent(portalTiers));
|
2021-06-21 11:51:48 +03:00
|
|
|
}
|
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
if (this.settings.accentColor === '' || this.settings.accentColor) {
|
|
|
|
settingsParam.append('accentColor', encodeURIComponent(`${this.settings.accentColor}`));
|
2021-01-28 21:41:03 +03:00
|
|
|
}
|
2022-10-07 16:23:21 +03:00
|
|
|
if (this.settings.portalButtonStyle) {
|
|
|
|
settingsParam.append('buttonStyle', encodeURIComponent(this.settings.portalButtonStyle));
|
2021-01-28 21:41:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (monthlyPrice) {
|
|
|
|
settingsParam.append('monthlyPrice', monthlyPrice);
|
|
|
|
}
|
|
|
|
if (yearlyPrice) {
|
2021-02-09 11:52:14 +03:00
|
|
|
settingsParam.append('yearlyPrice', yearlyPrice);
|
2021-01-28 21:41:03 +03:00
|
|
|
}
|
|
|
|
if (currency) {
|
|
|
|
settingsParam.append('currency', currency);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disableBackground) {
|
|
|
|
settingsParam.append('disableBackground', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${baseUrl}${portalBase}?${settingsParam.toString()}`;
|
|
|
|
}
|
2021-10-18 12:01:29 +03:00
|
|
|
|
|
|
|
getOfferPortalPreviewUrl(overrides) {
|
|
|
|
const {
|
|
|
|
disableBackground = false,
|
|
|
|
name,
|
|
|
|
code,
|
2022-03-15 14:31:27 +03:00
|
|
|
displayTitle = '',
|
2021-10-19 10:38:31 +03:00
|
|
|
displayDescription = '',
|
2021-10-18 12:01:29 +03:00
|
|
|
type,
|
|
|
|
cadence,
|
|
|
|
amount = 0,
|
|
|
|
duration,
|
|
|
|
durationInMonths,
|
|
|
|
currency = 'usd',
|
|
|
|
status,
|
|
|
|
tierId
|
|
|
|
} = overrides;
|
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
const baseUrl = this.config.blogUrl;
|
2021-10-18 12:01:29 +03:00
|
|
|
const portalBase = '/#/portal/preview/offer';
|
|
|
|
const settingsParam = new URLSearchParams();
|
|
|
|
|
|
|
|
settingsParam.append('name', encodeURIComponent(name));
|
|
|
|
settingsParam.append('code', encodeURIComponent(code));
|
|
|
|
settingsParam.append('display_title', encodeURIComponent(displayTitle));
|
|
|
|
settingsParam.append('display_description', encodeURIComponent(displayDescription));
|
|
|
|
settingsParam.append('type', encodeURIComponent(type));
|
|
|
|
settingsParam.append('cadence', encodeURIComponent(cadence));
|
|
|
|
settingsParam.append('amount', encodeURIComponent(amount));
|
|
|
|
settingsParam.append('duration', encodeURIComponent(duration));
|
|
|
|
settingsParam.append('duration_in_months', encodeURIComponent(durationInMonths));
|
|
|
|
settingsParam.append('currency', encodeURIComponent(currency));
|
|
|
|
settingsParam.append('status', encodeURIComponent(status));
|
|
|
|
settingsParam.append('tier_id', encodeURIComponent(tierId));
|
|
|
|
|
|
|
|
if (disableBackground) {
|
|
|
|
settingsParam.append('disableBackground', 'true');
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${baseUrl}${portalBase}?${settingsParam.toString()}`;
|
|
|
|
}
|
2020-06-11 12:05:40 +03:00
|
|
|
}
|