Skip to content

Commit 6011d6b

Browse files
Merge pull request #20075 from mozilla/PAY-3093
fix(payments-next): Coupon updates cause Stripe Element state loss
2 parents 7495875 + 9d77555 commit 6011d6b

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

libs/payments/ui/src/lib/client/components/StripeWrapper.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ interface StripeWrapperProps {
106106
amount: number;
107107
currency: string;
108108
cart: {
109+
id: string;
109110
paymentInfo?: {
110111
type:
111112
| Stripe.PaymentMethod.Type
@@ -133,6 +134,11 @@ export function StripeWrapper({
133134
const config = useContext(ConfigContext);
134135
const [stripePromise] = useState(() => loadStripe(config.stripePublicApiKey));
135136

137+
// Re-use initial session secret to prevent unnecessary Stripe Elements remounts
138+
const [initialSessionSecret] = useState(
139+
cart.paymentInfo?.customerSessionClientSecret
140+
);
141+
136142
const normalizedCurrency = currency.toLowerCase();
137143
const minCharge = STRIPE_MINIMUM_CHARGE_AMOUNTS[normalizedCurrency];
138144
const isBelowMin = amount < minCharge;
@@ -144,7 +150,7 @@ export function StripeWrapper({
144150
amount: isBelowMin ? undefined : amount,
145151
currency: normalizedCurrency,
146152
externalPaymentMethodTypes: ['external_paypal'],
147-
customerSessionClientSecret: cart.paymentInfo?.customerSessionClientSecret,
153+
customerSessionClientSecret: initialSessionSecret,
148154
};
149155

150156
// Remove external_paypal if the customer has an active subscription
@@ -158,7 +164,7 @@ export function StripeWrapper({
158164
}
159165

160166
return (
161-
<Elements stripe={stripePromise} options={options}>
167+
<Elements stripe={stripePromise} options={options} key={cart.id}>
162168
{children}
163169
</Elements>
164170
);

0 commit comments

Comments
 (0)