Skip to content

Commit 3be2974

Browse files
Merge pull request #18943 from mozilla/FXA-11344-applied-coupon-code-disappears
fix(payments-next):Applied coupon code before sign-in does not remain on checkout page after signing in
2 parents dd218a8 + 03452c3 commit 3be2974

4 files changed

Lines changed: 62 additions & 25 deletions

File tree

apps/payments/next/app/[locale]/[offeringId]/[interval]/checkout/[cartId]/start/page.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ export default async function Checkout({
8181
]);
8282

8383
const redirectSearchParams: Record<string, string> = searchParams || {};
84-
if (cart.taxAddress) {
85-
redirectSearchParams.countryCode = cart.taxAddress.countryCode;
86-
redirectSearchParams.postalCode = cart.taxAddress.postalCode;
87-
}
84+
redirectSearchParams.cartId = cart.id;
85+
redirectSearchParams.cartVersion = cart.version.toString();
8886

8987
const redirectTo = buildRedirectUrl(
9088
params.offeringId,

apps/payments/next/app/[locale]/[offeringId]/[interval]/new/page.tsx

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
validateLocationAction,
99
getTaxAddressAction,
1010
setupCartAction,
11+
updateCartUidAction,
1112
} from '@fxa/payments/ui/actions';
1213
import { CartEligibilityStatus, CartState } from '@fxa/shared/db/mysql/account';
1314
import { BaseParams, buildRedirectUrl } from '@fxa/payments/ui';
@@ -96,41 +97,53 @@ export default async function New({
9697
}
9798

9899
let redirectToUrl: URL;
99-
try {
100-
const cart = await setupCartAction(
101-
interval as SubplatInterval,
102-
offeringId,
103-
taxAddress,
104-
undefined,
105-
coupon,
106-
fxaUid
100+
let cart: ResultCart;
101+
102+
if (searchParams.cartId && fxaUid && searchParams.cartVersion) {
103+
cart = await updateCartUidAction(
104+
searchParams.cartId,
105+
Number(searchParams.cartVersion),
106+
fxaUid,
107107
);
108108

109109
redirectToUrl = getRedirectToUrl(cart, params, searchParams);
110-
} catch (error) {
111-
if (error.name === 'CartInvalidPromoCodeError') {
112-
const cart = await setupCartAction(
110+
} else {
111+
try {
112+
cart = await setupCartAction(
113113
interval as SubplatInterval,
114114
offeringId,
115115
taxAddress,
116116
undefined,
117-
undefined,
117+
coupon,
118118
fxaUid
119119
);
120120

121121
redirectToUrl = getRedirectToUrl(cart, params, searchParams);
122-
} else if (
123-
error.name === 'RetrieveStripePriceInvalidOfferingError' ||
124-
error.name === 'RetrieveStripePriceNotFoundError'
125-
) {
126-
notFound();
127-
} else {
128-
throw error;
122+
} catch (error) {
123+
if (error.name === 'CartInvalidPromoCodeError') {
124+
cart = await setupCartAction(
125+
interval as SubplatInterval,
126+
offeringId,
127+
taxAddress,
128+
undefined,
129+
undefined,
130+
fxaUid
131+
);
132+
133+
redirectToUrl = getRedirectToUrl(cart, params, searchParams);
134+
} else if (
135+
error.name === 'RetrieveStripePriceInvalidOfferingError' ||
136+
error.name === 'RetrieveStripePriceNotFoundError'
137+
) {
138+
notFound();
139+
} else {
140+
throw error;
141+
}
129142
}
130143
}
131144

132-
redirectToUrl.searchParams.delete('countryCode');
133-
redirectToUrl.searchParams.delete('postalCode');
145+
redirectToUrl.searchParams.delete('cartId');
146+
redirectToUrl.searchParams.delete('cartVersion');
134147

135148
redirect(redirectToUrl.href);
136149
}

libs/payments/ui/src/lib/actions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ export { submitNeedsInputAndRedirectAction } from './submitNeedsInputAndRedirect
2424
export { validateAndFormatPostalCode } from './validateAndFormatPostalCode';
2525
export { validateCartStateAndRedirectAction } from './validateCartStateAndRedirect';
2626
export { validateLocationAction } from './validateLocation';
27+
export { updateCartUidAction } from './updateCartUid';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
'use server';
6+
7+
import { getApp } from '../nestapp/app';
8+
9+
export const updateCartUidAction = async (
10+
cartId: string,
11+
version: number,
12+
uid: string
13+
) => {
14+
const actionsService = getApp().getActionsService();
15+
16+
const result = await actionsService.updateCart({
17+
cartId,
18+
version,
19+
cartDetails: {
20+
uid,
21+
},
22+
});
23+
24+
return result;
25+
};

0 commit comments

Comments
 (0)