Skip to content

Commit 01b4fbb

Browse files
authored
feat(payment): add checkout locale to Stripe elements (#3088)
* feat(payment): add checkout locale to Stripe elements
1 parent 1802a13 commit 01b4fbb

24 files changed

+100
-20
lines changed

packages/core/src/cart/cart-selector.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default interface CartSelector {
1212
getCartOrThrow(): Cart;
1313
getLoadError(): Error | undefined;
1414
isLoading(): boolean;
15+
getLocale(): string | undefined;
1516
}
1617

1718
export type CartSelectorFactory = (state: CartState) => CartSelector;
@@ -36,12 +37,18 @@ export function createCartSelectorFactory() {
3637
(status) => () => status,
3738
);
3839

40+
const getLocale = createSelector(
41+
(state: CartState) => state.data?.locale,
42+
(data) => () => data,
43+
);
44+
3945
return memoizeOne((state: CartState = DEFAULT_STATE): CartSelector => {
4046
return {
4147
getCart: getCart(state),
4248
getCartOrThrow: getCartOrThrow(state),
4349
getLoadError: getLoadError(state),
4450
isLoading: isLoading(state),
51+
getLocale: getLocale(state),
4552
};
4653
});
4754
}

packages/core/src/cart/cart.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export default interface Cart {
3535
createdTime: string;
3636
updatedTime: string;
3737
source?: CartSource;
38+
locale: string;
3839
}

packages/core/src/cart/carts.mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function getCart(): Cart {
2626
},
2727
createdTime: '2018-03-06T04:41:49+00:00',
2828
updatedTime: '2018-03-07T03:44:51+00:00',
29+
locale: 'en',
2930
};
3031
}
3132

packages/core/src/payment-integration/create-payment-integration-selectors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { cloneResult as clone } from '../common/utility';
55

66
export default function createPaymentIntegrationSelectors({
77
billingAddress: { getBillingAddress, getBillingAddressOrThrow },
8-
cart: { getCart, getCartOrThrow },
8+
cart: { getCart, getCartOrThrow, getLocale: getCartLocale },
99
checkout: { getCheckout, getCheckoutOrThrow, getOutstandingBalance },
1010
config: {
1111
getContextConfig,
@@ -50,6 +50,7 @@ export default function createPaymentIntegrationSelectors({
5050
return {
5151
getHost: clone(getHost),
5252
getLocale: clone(getLocale),
53+
getCartLocale: clone(getCartLocale),
5354
getBillingAddress: clone(getBillingAddress),
5455
getBillingAddressOrThrow: clone(getBillingAddressOrThrow),
5556
getCart: clone(getCart),

packages/core/src/shipping/strategies/stripe-upe/stripe-upe-shipping-strategy.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { createScriptLoader } from '@bigcommerce/script-loader';
44
import { Observable, of } from 'rxjs';
55

66
import {
7+
STRIPE_UPE_CLIENT_API_VERSION,
8+
STRIPE_UPE_CLIENT_BETAS,
79
StripeClient,
810
StripeDisplayName,
911
StripeElement,
@@ -139,6 +141,8 @@ describe('StripeUPEShippingStrategy', () => {
139141
jest.spyOn(store.getState().paymentMethods, 'getPaymentMethodOrThrow').mockReturnValue(
140142
getStripeUPE(),
141143
);
144+
145+
jest.spyOn(store.getState().cart, 'getLocale').mockReturnValue('en');
142146
});
143147

144148
afterEach(() => {
@@ -152,6 +156,12 @@ describe('StripeUPEShippingStrategy', () => {
152156
);
153157

154158
expect(stripeScriptLoader.getStripeClient).toHaveBeenCalledTimes(1);
159+
expect(stripeScriptLoader.getStripeClient).toHaveBeenCalledWith(
160+
paymentMethodMock.initializationData,
161+
'en',
162+
STRIPE_UPE_CLIENT_BETAS,
163+
STRIPE_UPE_CLIENT_API_VERSION,
164+
);
155165
expect(stripeUPEJsMock.elements).toHaveBeenCalledTimes(1);
156166
});
157167

packages/core/src/shipping/strategies/stripe-upe/stripe-upe-shipping-strategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export default class StripeUPEShippingStrategy implements ShippingStrategy {
9696

9797
this._stripeUPEClient = await this._stripeUPEScriptLoader.getStripeClient(
9898
initializationData,
99+
state.cart.getLocale(),
99100
STRIPE_UPE_CLIENT_BETAS,
100101
STRIPE_UPE_CLIENT_API_VERSION,
101102
);

packages/payment-integration-api/src/cart/cart.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export default interface Cart {
2020
createdTime: string;
2121
updatedTime: string;
2222
source?: CartSource;
23+
locale: string;
2324
}

packages/payment-integration-api/src/mocks/carts.mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default function getCart(): Cart {
3030
},
3131
createdTime: '2018-03-06T04:41:49+00:00',
3232
updatedTime: '2018-03-07T03:44:51+00:00',
33+
locale: 'en',
3334
};
3435
}
3536

packages/payment-integration-api/src/payment-integration-selectors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Consignment, ShippingAddress } from './shipping';
1717
export default interface PaymentIntegrationSelectors {
1818
getHost(): string | undefined;
1919
getLocale(): string | undefined;
20+
getCartLocale(): string | undefined;
2021

2122
getBillingAddress(): BillingAddress | undefined;
2223
getBillingAddressOrThrow(): BillingAddress;

packages/payment-integrations-test-utils/src/test-utils/carts.mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default function getCart(): Cart {
3030
},
3131
createdTime: '2018-03-06T04:41:49+00:00',
3232
updatedTime: '2018-03-07T03:44:51+00:00',
33+
locale: 'en',
3334
};
3435
}
3536

0 commit comments

Comments
 (0)