Skip to content

Commit

Permalink
Merge pull request #1384 from guardian/supporter-plus-cancellation-of…
Browse files Browse the repository at this point in the history
…fer-pricing

Use discount api response for cancellation offer payment date
  • Loading branch information
rBangay authored Sep 6, 2024
2 parents 46b29cc + 55d3d48 commit e65d310
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
6 changes: 6 additions & 0 deletions client/components/mma/cancel/Cancellation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const Offer: StoryObj<typeof CancellationContainer> = {
upToPeriodsType: 'months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.99 }],
},
},
},
Expand All @@ -116,6 +117,7 @@ export const OfferReview: StoryObj<typeof CancellationContainer> = {
upToPeriodsType: 'months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.99 }],
},
},
msw: [
Expand All @@ -136,6 +138,7 @@ export const OfferConfirmed: StoryObj<typeof CancellationContainer> = {
reactRouter: {
state: {
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.99 }],
},
},
},
Expand All @@ -156,6 +159,9 @@ export const SupportplusCancelConfirm: StoryObj<typeof CancellationContainer> =
upToPeriodsType: 'months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [
{ date: '2024-07-30', amount: 14.99 },
],
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useLocation, useNavigate } from 'react-router-dom';
import { Ribbon } from '@/client/components/shared/Ribbon';
import { measure } from '@/client/styles/typography';
import type { DiscountPreviewResponse } from '@/client/utilities/discountPreview';
import { getMaxNonDiscountedPrice } from '@/client/utilities/discountPreview';
import { DATE_FNS_LONG_OUTPUT_FORMAT, parseDate } from '@/shared/dates';
import { number2words } from '@/shared/numberUtils';
import { getMainPlan, isPaidSubscriptionPlan } from '@/shared/productResponse';
Expand Down Expand Up @@ -175,6 +176,11 @@ export const SupporterPlusOffer = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const humanReadableStrikethroughPrice = getMaxNonDiscountedPrice(
routerState.nonDiscountedPayments,
true,
);

return (
<>
<ProgressStepper
Expand Down Expand Up @@ -220,7 +226,8 @@ export const SupporterPlusOffer = () => {
<p css={strikethroughPriceCss}>
<s>
{mainPlan.currency}
{mainPlan.price / 100}/{mainPlan.billingPeriod}
{humanReadableStrikethroughPrice}/
{mainPlan.billingPeriod}
</s>
</p>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { useContext, useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { measure } from '@/client/styles/typography';
import type { DiscountPreviewResponse } from '@/client/utilities/discountPreview';
import { getMaxNonDiscountedPrice } from '@/client/utilities/discountPreview';
import { DATE_FNS_LONG_OUTPUT_FORMAT, parseDate } from '@/shared/dates';
import type { PaidSubscriptionPlan } from '@/shared/productResponse';
import { getMainPlan } from '@/shared/productResponse';
import { DownloadAppCta } from '../../../shared/DownloadAppCta';
import { Heading } from '../../../shared/Heading';
import type {
Expand Down Expand Up @@ -170,6 +173,9 @@ export const SupporterPlusOfferConfirmed = () => {
) as CancellationContextInterface;

const productDetail = cancellationContext.productDetail;
const mainPlan = getMainPlan(
productDetail.subscription,
) as PaidSubscriptionPlan;

useEffect(() => {
pageTitleContext.setPageTitle('Confirmation');
Expand All @@ -181,6 +187,11 @@ export const SupporterPlusOfferConfirmed = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const humanReadableNextNonDiscountedPrice = getMaxNonDiscountedPrice(
routerState.nonDiscountedPayments,
true,
);

return (
<>
<Heading
Expand Down Expand Up @@ -210,7 +221,10 @@ export const SupporterPlusOfferConfirmed = () => {
</li>
<li>
You will not be billed until{' '}
{nextNonDiscountedPaymentDate}
{nextNonDiscountedPaymentDate} after which you will pay{' '}
{mainPlan.currency}
{humanReadableNextNonDiscountedPrice}/
{mainPlan.billingPeriod}
</li>
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useContext, useState } from 'react';
import { Link, useLocation, useNavigate } from 'react-router-dom';
import { measure } from '@/client/styles/typography';
import type { DiscountPreviewResponse } from '@/client/utilities/discountPreview';
import { getMaxNonDiscountedPrice } from '@/client/utilities/discountPreview';
import { fetchWithDefaultParameters } from '@/client/utilities/fetch';
import { DATE_FNS_LONG_OUTPUT_FORMAT, parseDate } from '@/shared/dates';
import { number2words } from '@/shared/numberUtils';
Expand Down Expand Up @@ -127,6 +128,11 @@ export const SupporterPlusOfferReview = () => {
'yyyy-MM-dd',
).dateStr(DATE_FNS_LONG_OUTPUT_FORMAT);

const humanReadableStrikethroughPrice = getMaxNonDiscountedPrice(
routerState.nonDiscountedPayments,
true,
);

const [performingDiscountStatus, setPerformingDiscountStatus] =
useState<OfferApiCallStatus>('NOT_READY');

Expand Down Expand Up @@ -194,7 +200,8 @@ export const SupporterPlusOfferReview = () => {
<p css={strikethroughPriceCss}>
<s>
{mainPlan.currency}
{mainPlan.price / 100}/{mainPlan.billingPeriod}
{humanReadableStrikethroughPrice}/
{mainPlan.billingPeriod}
</s>
</p>
)}
Expand Down
20 changes: 20 additions & 0 deletions client/utilities/discountPreview.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
interface NonDiscountedPayments {
date: string;
amount: number;
}

export type DiscountPreviewResponse = {
discountedPrice: number;
upToPeriods: number;
upToPeriodsType: string;
firstDiscountedPaymentDate: string;
nextNonDiscountedPaymentDate: string;
nonDiscountedPayments: NonDiscountedPayments[];
};

export const getMaxNonDiscountedPrice = (
nonDiscountedPayments: NonDiscountedPayments[],
asHumanReadable?: boolean,
) => {
const allNonDiscountedAmounts = nonDiscountedPayments.map((p) => p.amount);
const maxNonDiscountedPrice = Math.max(...allNonDiscountedAmounts);
if (!asHumanReadable) {
return maxNonDiscountedPrice;
}
return Number.isInteger(maxNonDiscountedPrice)
? maxNonDiscountedPrice
: maxNonDiscountedPrice.toFixed(2);
};
3 changes: 3 additions & 0 deletions cypress/tests/mocked/parallel-2/cancelSupporterPlus.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ describe('Cancel Supporter Plus', () => {
upToPeriodsType: 'Months',
firstDiscountedPaymentDate: '2024-05-30',
nextNonDiscountedPaymentDate: '2024-07-30',
nonDiscountedPayments: [{ date: '2024-07-30', amount: 14.5 }],
};
it('user accepts offer instead of cancelling', () => {
cy.intercept('GET', '/api/me/mma', {
Expand Down Expand Up @@ -184,6 +185,8 @@ describe('Cancel Supporter Plus', () => {
name: 'Continue to cancellation',
}).click();

cy.findByText('£14.50/month');

cy.findByRole('button', { name: 'Redeem your offer' }).click();

cy.findByRole('button', {
Expand Down

0 comments on commit e65d310

Please sign in to comment.