Skip to content

Commit 3f76e30

Browse files
committed
feat(core): CHECKOUT-9450 Lazy load payment strategies through essential build
1 parent f229751 commit 3f76e30

27 files changed

+665
-57
lines changed

packages/bigcommerce-payments-integration/src/bigcommerce-payments-fastlane/bigcommerce-payments-fastlane-payment-strategy.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ describe('BigCommercePaymentsFastlanePaymentStrategy', () => {
750750
onError: jest.fn(),
751751
},
752752
};
753+
753754
jest.spyOn(paymentIntegrationService, 'submitPayment').mockRejectedValue({
754755
name: 'Error',
755756
message: 'Payment request failed',

packages/bigcommerce-payments-integration/src/bigcommerce-payments-fastlane/bigcommerce-payments-fastlane-payment-strategy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import {
3030
PaymentStrategy,
3131
VaultedInstrument,
3232
} from '@bigcommerce/checkout-sdk/payment-integration-api';
33+
import { isExperimentEnabled } from '@bigcommerce/checkout-sdk/utility';
3334

3435
import BigCommercePaymentsRequestSender from '../bigcommerce-payments-request-sender';
36+
import { LiabilityShiftEnum } from '../bigcommerce-payments-types';
3537

3638
import BigCommercePaymentsFastlanePaymentInitializeOptions, {
3739
WithBigCommercePaymentsFastlanePaymentInitializeOptions,
3840
} from './bigcommerce-payments-fastlane-payment-initialize-options';
39-
import { LiabilityShiftEnum } from '../bigcommerce-payments-types';
40-
import { isExperimentEnabled } from '@bigcommerce/checkout-sdk/utility';
4141

4242
export default class BigCommercePaymentsFastlanePaymentStrategy implements PaymentStrategy {
4343
private paypalComponentMethods?: PayPalFastlaneCardComponentMethods;
@@ -158,6 +158,7 @@ export default class BigCommercePaymentsFastlanePaymentStrategy implements Payme
158158

159159
try {
160160
await this.paymentIntegrationService.submitOrder(order, options);
161+
161162
const paymentPayload = isVaultedFlow
162163
? await this.prepareVaultedInstrumentPaymentPayload(methodId, paymentData)
163164
: await this.preparePaymentPayload(methodId, paymentData);
@@ -406,6 +407,7 @@ export default class BigCommercePaymentsFastlanePaymentStrategy implements Payme
406407
private async createOrder(id: string): Promise<void> {
407408
const state = this.paymentIntegrationService.getState();
408409
const cartId = state.getCartOrThrow().id;
410+
409411
if (this.methodId) {
410412
const { orderId } = await this.bigCommercePaymentsRequestSender.createOrder(
411413
this.methodId,

packages/core/src/checkout-buttons/checkout-button-initializer.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('CheckoutButtonInitializer', () => {
2626

2727
buttonActionCreator = new CheckoutButtonStrategyActionCreator(
2828
createCheckoutButtonRegistry(store, createRequestSender(), createFormPoster(), 'en'),
29-
createCheckoutButtonRegistryV2(createPaymentIntegrationService(store)),
29+
createCheckoutButtonRegistryV2(createPaymentIntegrationService(store), {}),
3030
new PaymentMethodActionCreator(new PaymentMethodRequestSender(createRequestSender())),
3131
);
3232

packages/core/src/checkout-buttons/checkout-button-strategy-action-creator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('CheckoutButtonStrategyActionCreator', () => {
4848
);
4949
strategy = new MockButtonStrategy();
5050
store = createCheckoutStore();
51-
registryV2 = createCheckoutButtonRegistryV2(createPaymentIntegrationService(store));
51+
registryV2 = createCheckoutButtonRegistryV2(createPaymentIntegrationService(store), {});
5252
registry.register(CheckoutButtonMethodType.MASTERPASS, () => strategy);
5353

5454
jest.spyOn(paymentMethodActionCreator, 'loadPaymentMethod').mockReturnValue(() =>

packages/core/src/checkout-buttons/create-checkout-button-initializer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createRequestSender } from '@bigcommerce/request-sender';
33

44
import { createCheckoutStore } from '../checkout';
55
import { ConfigState } from '../config';
6+
import * as defaultCheckoutButtonStrategyFactories from '../generated/checkout-button-strategies';
67
import { PaymentMethodActionCreator, PaymentMethodRequestSender } from '../payment';
78
import { createPaymentIntegrationService } from '../payment-integration';
89

@@ -53,7 +54,12 @@ export default function createCheckoutButtonInitializer(
5354
const requestSender = createRequestSender({ host });
5455
const formPoster = createFormPoster({ host });
5556
const paymentIntegrationService = createPaymentIntegrationService(store);
56-
const registryV2 = createCheckoutButtonRegistryV2(paymentIntegrationService);
57+
const registryV2 = createCheckoutButtonRegistryV2(
58+
paymentIntegrationService,
59+
defaultCheckoutButtonStrategyFactories,
60+
// TODO: Replace once CHECKOUT-9450.lazy_load_payment_strategies experiment is rolled out
61+
// process.env.ESSENTIAL_BUILD ? {} : defaultCheckoutButtonStrategyFactories,
62+
);
5763

5864
return new CheckoutButtonInitializer(
5965
store,

packages/core/src/checkout-buttons/create-checkout-button-registry-v2.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import {
77
} from '@bigcommerce/checkout-sdk/payment-integration-api';
88

99
import { ResolveIdRegistry } from '../common/registry';
10-
import * as defaultCheckoutButtonStrategyFactories from '../generated/checkout-button-strategies';
1110

1211
export interface CheckoutButtonStrategyFactories {
1312
[key: string]: CheckoutButtonStrategyFactory<CheckoutButtonStrategy>;
1413
}
1514

1615
export default function createCheckoutButtonStrategyRegistry(
1716
paymentIntegrationService: PaymentIntegrationService,
18-
checkoutButtonStrategyFactories: CheckoutButtonStrategyFactories = defaultCheckoutButtonStrategyFactories,
17+
checkoutButtonStrategyFactories: CheckoutButtonStrategyFactories,
1918
): ResolveIdRegistry<CheckoutButtonStrategy, CheckoutButtonStrategyResolveId> {
2019
const registry = new ResolveIdRegistry<
2120
CheckoutButtonStrategy,

packages/core/src/checkout/checkout-service.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '../billing';
1919
import { getBillingAddress } from '../billing/billing-addresses.mock';
2020
import { createDataStoreProjection, DataStoreProjection } from '../common/data-store';
21-
import { ErrorActionCreator } from '../common/error';
21+
import { ErrorActionCreator, ErrorLogger } from '../common/error';
2222
import { getResponse } from '../common/http-request/responses.mock';
2323
import { ResolveIdRegistry } from '../common/registry';
2424
import { ConfigActionCreator, ConfigRequestSender } from '../config';
@@ -253,8 +253,8 @@ describe('CheckoutService', () => {
253253
jest.spyOn(paymentStrategy, 'deinitialize').mockResolvedValue(Promise.resolve());
254254

255255
paymentStrategyRegistry = new PaymentStrategyRegistry();
256-
paymentStrategyRegistryV2 = createPaymentStrategyRegistryV2(paymentIntegrationService);
257-
customerRegistryV2 = createCustomerStrategyRegistryV2(paymentIntegrationService);
256+
paymentStrategyRegistryV2 = createPaymentStrategyRegistryV2(paymentIntegrationService, {});
257+
customerRegistryV2 = createCustomerStrategyRegistryV2(paymentIntegrationService, {});
258258

259259
// This can't be fixed until we have differences between core and payment integration api payment strategy types
260260
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
@@ -376,9 +376,13 @@ describe('CheckoutService', () => {
376376
checkoutRequestSender,
377377
);
378378

379+
const errorLogger: ErrorLogger = { log: jest.fn() };
380+
379381
customerStrategyActionCreator = new CustomerStrategyActionCreator(
380382
createCustomerStrategyRegistry(store, requestSender, locale),
381383
customerRegistryV2,
384+
paymentIntegrationService,
385+
errorLogger,
382386
);
383387

384388
instrumentActionCreator = new InstrumentActionCreator(instrumentRequestSender);
@@ -397,6 +401,8 @@ describe('CheckoutService', () => {
397401
paymentStrategyRegistryV2,
398402
orderActionCreator,
399403
spamProtectionActionCreator,
404+
paymentIntegrationService,
405+
errorLogger,
400406
);
401407

402408
shippingStrategyActionCreator = new ShippingStrategyActionCreator(

packages/core/src/checkout/create-checkout-service.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createScriptLoader } from '@bigcommerce/script-loader';
33

44
import { BillingAddressActionCreator, BillingAddressRequestSender } from '../billing';
55
import { createDataStoreProjection } from '../common/data-store';
6-
import { ErrorActionCreator } from '../common/error';
6+
import { DefaultErrorLogger, ErrorActionCreator, ErrorLogger } from '../common/error';
77
import { getDefaultLogger } from '../common/log';
88
import { getEnvironment } from '../common/utility';
99
import { ConfigActionCreator, ConfigRequestSender, ConfigState, ConfigWindow } from '../config';
@@ -28,7 +28,8 @@ import {
2828
WorkerExtensionMessenger,
2929
} from '../extension';
3030
import { FormFieldsActionCreator, FormFieldsRequestSender } from '../form';
31-
import * as defaultPaymentStrategyFactories from '../generated/payment-strategies';
31+
import * as customerStrategyFactories from '../generated/customer-strategies';
32+
import * as paymentStrategyFactories from '../generated/payment-strategies';
3233
import { CountryActionCreator, CountryRequestSender } from '../geography';
3334
import { OrderActionCreator, OrderRequestSender } from '../order';
3435
import {
@@ -107,7 +108,11 @@ export default function createCheckoutService(options?: CheckoutServiceOptions):
107108
errors: {},
108109
statuses: {},
109110
};
110-
const { locale = '', shouldWarnMutation = true } = options || {};
111+
const {
112+
locale = '',
113+
shouldWarnMutation = true,
114+
errorLogger = new DefaultErrorLogger(),
115+
} = options || {};
111116
const requestSender = createRequestSender({ host: options && options.host });
112117
const store = createCheckoutStore({ config }, { shouldWarnMutation });
113118
const paymentClient = createPaymentClient(store);
@@ -136,12 +141,20 @@ export default function createCheckoutService(options?: CheckoutServiceOptions):
136141
formFieldsActionCreator,
137142
);
138143
const paymentIntegrationService = createPaymentIntegrationService(store);
144+
139145
const registryV2 = createPaymentStrategyRegistryV2(
140146
paymentIntegrationService,
141-
defaultPaymentStrategyFactories,
147+
paymentStrategyFactories,
148+
// TODO: Replace once CHECKOUT-9450.lazy_load_payment_strategies experiment is rolled out
149+
// process.env.ESSENTIAL_BUILD ? {} : paymentStrategyFactories,
142150
{ useFallback: true },
143151
);
144-
const customerRegistryV2 = createCustomerStrategyRegistryV2(paymentIntegrationService);
152+
const customerRegistryV2 = createCustomerStrategyRegistryV2(
153+
paymentIntegrationService,
154+
customerStrategyFactories,
155+
// TODO: Replace once CHECKOUT-9450.lazy_load_payment_strategies experiment is rolled out
156+
// process.env.ESSENTIAL_BUILD ? {} : customerStrategyFactories,
157+
);
145158
const extensionActionCreator = new ExtensionActionCreator(
146159
new ExtensionRequestSender(requestSender),
147160
);
@@ -174,6 +187,8 @@ export default function createCheckoutService(options?: CheckoutServiceOptions):
174187
new CustomerStrategyActionCreator(
175188
createCustomerStrategyRegistry(store, requestSender, locale),
176189
customerRegistryV2,
190+
paymentIntegrationService,
191+
errorLogger,
177192
),
178193
new ErrorActionCreator(),
179194
new GiftCertificateActionCreator(new GiftCertificateRequestSender(requestSender)),
@@ -187,10 +202,13 @@ export default function createCheckoutService(options?: CheckoutServiceOptions):
187202
requestSender,
188203
spamProtection,
189204
locale,
205+
errorLogger,
190206
),
191207
registryV2,
192208
orderActionCreator,
193209
spamProtectionActionCreator,
210+
paymentIntegrationService,
211+
errorLogger,
194212
),
195213
new PickupOptionActionCreator(new PickupOptionRequestSender(requestSender)),
196214
new ShippingCountryActionCreator(
@@ -213,4 +231,5 @@ export interface CheckoutServiceOptions {
213231
host?: string;
214232
shouldWarnMutation?: boolean;
215233
externalSource?: string;
234+
errorLogger?: ErrorLogger;
216235
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface ErrorLogger {
2+
log(error: Error): void;
3+
}
4+
5+
export class DefaultErrorLogger implements ErrorLogger {
6+
log(error: Error): void {
7+
// eslint-disable-next-line no-console
8+
console.error(error);
9+
}
10+
}

packages/core/src/common/error/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './error-actions';
2+
export * from './error-logger';
23

34
export { default as clearErrorReducer } from './clear-error-reducer';
45
export { default as createRequestErrorFactory } from './create-request-error-factory';

0 commit comments

Comments
 (0)