0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Updated data-members-plan handling to send Tier & Cadence

refs https://github.com/TryGhost/Team/issues/2199

Sending a price id was supposed to have been cleaned up but the
data-attributes file was missed.
This commit is contained in:
Fabien "egg" O'Carroll 2022-11-02 15:17:59 +07:00 committed by Fabien 'egg' O'Carroll
parent 876100e80b
commit 592e282f5b
3 changed files with 27 additions and 11 deletions

View file

@ -1,5 +1,5 @@
/* eslint-disable no-console */
import {getQueryPrice, getUrlHistory} from './utils/helpers';
import {getCheckoutSessionDataFromPlanAttribute, getUrlHistory} from './utils/helpers';
import {HumanReadableError} from './utils/errors';
export function formSubmitHandler({event, form, errorEl, siteUrl, submitHandler}) {
@ -68,11 +68,7 @@ export function planClickHandler({event, el, errorEl, siteUrl, site, member, cli
el.removeEventListener('click', clickHandler);
event.preventDefault();
let plan = el.dataset.membersPlan;
let priceId = '';
if (plan) {
const price = getQueryPrice({site, priceId: plan.toLowerCase()});
priceId = price ? price.id : plan;
}
let requestData = getCheckoutSessionDataFromPlanAttribute(site, plan);
let successUrl = el.dataset.membersSuccess;
let cancelUrl = el.dataset.membersCancel;
let checkoutSuccessUrl;
@ -113,7 +109,7 @@ export function planClickHandler({event, el, errorEl, siteUrl, site, member, cli
'Content-Type': 'application/json'
},
body: JSON.stringify({
priceId: priceId,
...requestData,
identity: identity,
successUrl: checkoutSuccessUrl,
cancelUrl: checkoutCancelUrl,

View file

@ -165,7 +165,6 @@ describe('Member Data attributes:', () => {
const {event, errorEl, siteUrl, clickHandler, site, member, element} = getMockData();
const paidTier = site.products.find(p => p.type === 'paid');
const plan = paidTier.monthlyPrice.id;
await planClickHandler({event, errorEl, siteUrl, clickHandler, site, member, el: element});
expect(window.fetch).toHaveBeenNthCalledWith(1,
@ -174,7 +173,8 @@ describe('Member Data attributes:', () => {
}
);
const expectedBody = {
priceId: plan,
cadence: 'month',
tierId: paidTier.id,
identity: 'session-identity',
successUrl: 'https://portal.localhost/success',
cancelUrl: 'https://portal.localhost/cancel',
@ -205,14 +205,14 @@ describe('Member Data attributes:', () => {
let {event, errorEl, siteUrl, clickHandler, site, member, element} = getMockData();
member = FixtureMember.free;
const paidTier = site.products.find(p => p.type === 'paid');
const plan = paidTier.monthlyPrice.id;
await planClickHandler({event, errorEl, siteUrl, clickHandler, site, member, el: element});
expect(window.fetch).toHaveBeenNthCalledWith(1, 'https://portal.localhost/members/api/session', {
credentials: 'same-origin'
});
const expectedBody = {
priceId: plan,
cadence: 'month',
tierId: paidTier.id,
identity: 'session-identity',
successUrl: 'https://portal.localhost/success',
cancelUrl: 'https://portal.localhost/cancel',

View file

@ -179,6 +179,26 @@ export function hasPrice({site = {}, plan}) {
return false;
}
export function getCheckoutSessionDataFromPlanAttribute(site, plan) {
const products = getAvailableProducts({site});
const defaultTier = products.find(p => p.type === 'paid');
if (plan === 'monthly') {
return {
cadence: 'month',
tierId: defaultTier.id
};
}
if (plan === 'yearly') {
return {
cadence: 'year',
tierId: defaultTier.id
};
}
return {
priceId: plan
};
}
export function getQueryPrice({site = {}, priceId}) {
const prices = getAvailablePrices({site});
if (priceId === 'free') {