Skip to content

Commit

Permalink
chore(common): CHECKOUT-6855 Load payment strategies from V2 as prior…
Browse files Browse the repository at this point in the history
  • Loading branch information
animesh1987 authored and AlanMunozN committed Aug 5, 2022
1 parent 3fcd6d5 commit 7d13250
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 56 deletions.
4 changes: 4 additions & 0 deletions packages/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"command": "webpack --config webpack.config.js --config-name cjs --watch --progress"
},
"dependsOn": [
{
"target": "generate",
"projects": "self"
},
{
"target": "build",
"projects": "dependencies"
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/checkout/checkout-service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PaymentStrategy as PaymentStrategyV2, PaymentStrategyResolveId } from '@bigcommerce/checkout-sdk/payment-integration';
import { createAction } from '@bigcommerce/data-store';
import { createRequestSender, createTimeout } from '@bigcommerce/request-sender';
import { createScriptLoader } from '@bigcommerce/script-loader';
Expand All @@ -8,6 +9,7 @@ import { BillingAddressActionCreator, BillingAddressRequestSender } from '../bil
import { getBillingAddress } from '../billing/billing-addresses.mock';
import { ErrorActionCreator } from '../common/error';
import { getResponse } from '../common/http-request/responses.mock';
import { ResolveIdRegistry } from '../common/registry';
import { ConfigActionCreator, ConfigRequestSender } from '../config';
import { getConfig } from '../config/configs.mock';
import { CouponActionCreator, CouponRequestSender, GiftCertificateActionCreator, GiftCertificateRequestSender } from '../coupon';
Expand All @@ -19,7 +21,8 @@ import { getCountriesResponseBody } from '../geography/countries.mock';
import { OrderActionCreator, OrderRequestSender } from '../order';
import { getCompleteOrderResponseBody, getOrderRequestBody } from '../order/internal-orders.mock';
import { getOrder } from '../order/orders.mock';
import { createPaymentClient, PaymentMethodActionCreator, PaymentMethodRequestSender, PaymentStrategyActionCreator, PaymentStrategyRegistry } from '../payment';
import { createPaymentClient, createPaymentStrategyRegistryV2, PaymentMethodActionCreator, PaymentMethodRequestSender, PaymentStrategyActionCreator, PaymentStrategyRegistry } from '../payment';
import { createPaymentIntegrationService } from '../payment-integration';
import { InstrumentActionCreator, InstrumentRequestSender } from '../payment/instrument';
import { deleteInstrumentResponseBody, getLoadInstrumentsResponseBody, getVaultAccessTokenResponseBody } from '../payment/instrument/instrument.mock';
import { getAuthorizenet, getPaymentMethod, getPaymentMethods } from '../payment/payment-methods.mock';
Expand Down Expand Up @@ -70,6 +73,7 @@ describe('CheckoutService', () => {
let paymentStrategy: PaymentStrategy;
let paymentStrategyActionCreator: PaymentStrategyActionCreator;
let paymentStrategyRegistry: PaymentStrategyRegistry;
let paymentStrategyRegistryV2: ResolveIdRegistry<PaymentStrategyV2, PaymentStrategyResolveId>;
let shippingStrategyActionCreator: ShippingStrategyActionCreator;
let shippingCountryRequestSender: ShippingCountryRequestSender;
let signInEmailActionCreator: SignInEmailActionCreator;
Expand Down Expand Up @@ -145,6 +149,8 @@ describe('CheckoutService', () => {
.mockResolvedValue(store.getState());

paymentStrategyRegistry = new PaymentStrategyRegistry(store);
const paymentIntegrationService = createPaymentIntegrationService(store);
paymentStrategyRegistryV2 = createPaymentStrategyRegistryV2(paymentIntegrationService);

jest.spyOn(paymentStrategyRegistry, 'getByMethod').mockReturnValue(paymentStrategy);

Expand Down Expand Up @@ -239,6 +245,7 @@ describe('CheckoutService', () => {

paymentStrategyActionCreator = new PaymentStrategyActionCreator(
paymentStrategyRegistry,
paymentStrategyRegistryV2,
orderActionCreator,
spamProtectionActionCreator
);
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/checkout/create-checkout-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { createCustomerStrategyRegistry, CustomerActionCreator, CustomerRequestS
import { FormFieldsActionCreator, FormFieldsRequestSender } from '../form';
import { CountryActionCreator, CountryRequestSender } from '../geography';
import { OrderActionCreator, OrderRequestSender } from '../order';
import { createPaymentClient, createPaymentStrategyRegistry, PaymentMethodActionCreator, PaymentMethodRequestSender, PaymentStrategyActionCreator } from '../payment';
import { createPaymentClient, createPaymentStrategyRegistry, createPaymentStrategyRegistryV2, PaymentMethodActionCreator, PaymentMethodRequestSender, PaymentStrategyActionCreator } from '../payment';
import { createPaymentIntegrationService } from '../payment-integration';
import { InstrumentActionCreator, InstrumentRequestSender } from '../payment/instrument';
import { createShippingStrategyRegistry, ConsignmentActionCreator, ConsignmentRequestSender, PickupOptionActionCreator, PickupOptionRequestSender, ShippingCountryActionCreator, ShippingCountryRequestSender, ShippingStrategyActionCreator } from '../shipping';
import { SignInEmailActionCreator, SignInEmailRequestSender } from '../signin-email';
Expand Down Expand Up @@ -78,6 +79,8 @@ export default function createCheckoutService(options?: CheckoutServiceOptions):
const subscriptionsActionCreator = new SubscriptionsActionCreator(new SubscriptionsRequestSender(requestSender));
const formFieldsActionCreator = new FormFieldsActionCreator(new FormFieldsRequestSender(requestSender));
const checkoutActionCreator = new CheckoutActionCreator(checkoutRequestSender, configActionCreator, formFieldsActionCreator);
const paymentIntegrationService = createPaymentIntegrationService(store);
const registryV2 = createPaymentStrategyRegistryV2(paymentIntegrationService);

return new CheckoutService(
store,
Expand All @@ -103,6 +106,7 @@ export default function createCheckoutService(options?: CheckoutServiceOptions):
new PaymentMethodActionCreator(new PaymentMethodRequestSender(requestSender)),
new PaymentStrategyActionCreator(
createPaymentStrategyRegistry(store, paymentClient, requestSender, spamProtection, locale),
registryV2,
orderActionCreator,
spamProtectionActionCreator
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { AdyenV3PaymentStrategy } from './strategies/adyenv3';
import { AffirmPaymentStrategy } from './strategies/affirm';
import { AfterpayPaymentStrategy } from './strategies/afterpay';
import { AmazonPayPaymentStrategy } from './strategies/amazon-pay';
import { ApplePayPaymentStrategy } from './strategies/apple-pay';
import { BarclaysPaymentStrategy } from './strategies/barclays';
import { BlueSnapV2PaymentStrategy } from './strategies/bluesnapv2';
import { BraintreeCreditCardPaymentStrategy, BraintreePaypalPaymentStrategy, BraintreeVisaCheckoutPaymentStrategy } from './strategies/braintree';
Expand Down Expand Up @@ -62,11 +61,6 @@ describe('CreatePaymentStrategyRegistry', () => {
expect(registry).toEqual(expect.any(PaymentStrategyRegistry));
});

it('can instantiate Apple Pay', () => {
const paymentStrategy = registry.get(PaymentStrategyType.APPLEPAY);
expect(paymentStrategy).toBeInstanceOf(ApplePayPaymentStrategy);
});

it('can instantiate adyenv2', () => {
const paymentStrategy = registry.get(PaymentStrategyType.ADYENV2);
expect(paymentStrategy).toBeInstanceOf(AdyenV2PaymentStrategy);
Expand Down
23 changes: 10 additions & 13 deletions packages/core/src/payment/create-payment-strategy-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { ConfigActionCreator, ConfigRequestSender } from '../config';
import { FormFieldsActionCreator, FormFieldsRequestSender } from '../form';
import { HostedFormFactory } from '../hosted-form';
import { OrderActionCreator, OrderRequestSender } from '../order';
import { createPaymentIntegrationService } from '../payment-integration';
import { RemoteCheckoutActionCreator, RemoteCheckoutRequestSender } from '../remote-checkout';
import { createSpamProtection, GoogleRecaptcha, PaymentHumanVerificationHandler, SpamProtectionActionCreator, SpamProtectionRequestSender } from '../spam-protection';
import { StoreCreditActionCreator, StoreCreditRequestSender } from '../store-credit';
import { SubscriptionsActionCreator, SubscriptionsRequestSender } from '../subscription';
import createPaymentStrategyRegistryV2 from './create-payment-strategy-registry-v2';

import PaymentActionCreator from './payment-action-creator';
import PaymentMethodActionCreator from './payment-method-action-creator';
Expand All @@ -30,7 +32,6 @@ import { AffirmPaymentStrategy, AffirmScriptLoader } from './strategies/affirm';
import { AfterpayPaymentStrategy, AfterpayScriptLoader } from './strategies/afterpay';
import { AmazonPayPaymentStrategy, AmazonPayScriptLoader } from './strategies/amazon-pay';
import { createAmazonPayV2PaymentProcessor, AmazonPayV2PaymentStrategy } from './strategies/amazon-pay-v2';
import { ApplePayPaymentStrategy, ApplePaySessionFactory } from './strategies/apple-pay';
import { BarclaysPaymentStrategy } from './strategies/barclays';
import { BlueSnapV2PaymentStrategy } from './strategies/bluesnapv2';
import { BoltPaymentStrategy, BoltScriptLoader } from './strategies/bolt';
Expand Down Expand Up @@ -90,6 +91,8 @@ export default function createPaymentStrategyRegistry(
new BillingAddressRequestSender(requestSender),
new SubscriptionsActionCreator(new SubscriptionsRequestSender(requestSender))
);
const paymentIntegrationService = createPaymentIntegrationService(store);
const registryV2 = createPaymentStrategyRegistryV2(paymentIntegrationService);
const braintreePaymentProcessor = createBraintreePaymentProcessor(scriptLoader);
const checkoutRequestSender = new CheckoutRequestSender(requestSender);
const checkoutValidator = new CheckoutValidator(checkoutRequestSender);
Expand All @@ -104,7 +107,12 @@ export default function createPaymentStrategyRegistry(
const formFieldsActionCreator = new FormFieldsActionCreator(new FormFieldsRequestSender(requestSender));
const checkoutActionCreator = new CheckoutActionCreator(checkoutRequestSender, configActionCreator, formFieldsActionCreator);
const remoteCheckoutActionCreator = new RemoteCheckoutActionCreator(remoteCheckoutRequestSender, checkoutActionCreator);
const paymentStrategyActionCreator = new PaymentStrategyActionCreator(registry, orderActionCreator, spamProtectionActionCreator);
const paymentStrategyActionCreator = new PaymentStrategyActionCreator(
registry,
registryV2,
orderActionCreator,
spamProtectionActionCreator
);
const formPoster = createFormPoster();
const stepHandler = createStepHandler(formPoster, paymentHumanVerificationHandler);
const hostedFormFactory = new HostedFormFactory(store);
Expand Down Expand Up @@ -843,16 +851,5 @@ export default function createPaymentStrategyRegistry(
)
);

registry.register(PaymentStrategyType.APPLEPAY, () =>
new ApplePayPaymentStrategy(
store,
requestSender,
orderActionCreator,
paymentMethodActionCreator,
paymentActionCreator,
new ApplePaySessionFactory()
)
);

return registry;
}
1 change: 1 addition & 0 deletions packages/core/src/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './payment-status-types';
export { default as PaymentAdditionalAction, CardingProtectionActionData } from './payment-additional-action';
export { default as createPaymentClient } from './create-payment-client';
export { default as createPaymentStrategyRegistry } from './create-payment-strategy-registry';
export { default as createPaymentStrategyRegistryV2 } from './create-payment-strategy-registry-v2';
export { default as isHostedInstrumentLike } from './is-hosted-intrument-like';
export { default as isNonceLike } from './is-nonce-like';
export { default as isVaultedInstrument } from './is-vaulted-instrument';
Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/payment/payment-strategy-action-creator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createClient as createPaymentClient } from '@bigcommerce/bigpay-client';
import { PaymentStrategy as PaymentStrategyV2, PaymentStrategyResolveId } from '@bigcommerce/checkout-sdk/payment-integration';
import { createAction } from '@bigcommerce/data-store';
import { createRequestSender, RequestSender } from '@bigcommerce/request-sender';
import { createScriptLoader } from '@bigcommerce/script-loader';
Expand All @@ -9,15 +10,18 @@ import { catchError, toArray } from 'rxjs/operators';
import { createCheckoutStore, CheckoutRequestSender, CheckoutStore, CheckoutStoreState, CheckoutValidator } from '../checkout';
import { getCheckout, getCheckoutStoreState, getCheckoutStoreStateWithOrder } from '../checkout/checkouts.mock';
import { MissingDataError } from '../common/error/errors';
import { ResolveIdRegistry } from '../common/registry';
import { getCustomerState } from '../customer/customers.mock';
import { HostedFormFactory } from '../hosted-form';
import { OrderActionCreator, OrderActionType, OrderRequestSender } from '../order';
import { OrderFinalizationNotRequiredError } from '../order/errors';
import { getOrderRequestBody } from '../order/internal-orders.mock';
import { getOrderState } from '../order/orders.mock';
import { createPaymentIntegrationService } from '../payment-integration';
import { createSpamProtection, GoogleRecaptcha, PaymentHumanVerificationHandler, SpamProtectionActionCreator, SpamProtectionRequestSender } from '../spam-protection';

import createPaymentStrategyRegistry from './create-payment-strategy-registry';
import createPaymentStrategyRegistryV2 from './create-payment-strategy-registry-v2';
import PaymentActionCreator from './payment-action-creator';
import { getPaymentMethod } from './payment-methods.mock';
import PaymentRequestSender from './payment-request-sender';
Expand All @@ -36,6 +40,7 @@ describe('PaymentStrategyActionCreator', () => {
let requestSender: RequestSender;
let spamProtection: GoogleRecaptcha;
let registry: PaymentStrategyRegistry;
let registryV2: ResolveIdRegistry<PaymentStrategyV2, PaymentStrategyResolveId>;
let state: CheckoutStoreState;
let store: CheckoutStore;
let strategy: PaymentStrategy;
Expand All @@ -56,6 +61,8 @@ describe('PaymentStrategyActionCreator', () => {
new OrderRequestSender(requestSender),
new CheckoutValidator(new CheckoutRequestSender(createRequestSender()))
);
const paymentIntegrationService = createPaymentIntegrationService(store);
registryV2 = createPaymentStrategyRegistryV2(paymentIntegrationService);
strategy = new CreditCardPaymentStrategy(
store,
orderActionCreator,
Expand All @@ -75,7 +82,7 @@ describe('PaymentStrategyActionCreator', () => {
spamProtection,
new SpamProtectionRequestSender(requestSender)
);
actionCreator = new PaymentStrategyActionCreator(registry, orderActionCreator, spamProtectionActionCreator);
actionCreator = new PaymentStrategyActionCreator(registry, registryV2, orderActionCreator, spamProtectionActionCreator);

jest.spyOn(registry, 'getByMethod')
.mockReturnValue(strategy);
Expand Down Expand Up @@ -340,7 +347,7 @@ describe('PaymentStrategyActionCreator', () => {
});
registry = createPaymentStrategyRegistry(store, paymentClient, requestSender, spamProtection, 'en_US');

const actionCreator = new PaymentStrategyActionCreator(registry, orderActionCreator, spamProtectionActionCreator);
const actionCreator = new PaymentStrategyActionCreator(registry, registryV2, orderActionCreator, spamProtectionActionCreator);

try {
await from(actionCreator.execute(getOrderRequestBody())(store)).toPromise();
Expand All @@ -364,7 +371,7 @@ describe('PaymentStrategyActionCreator', () => {
jest.spyOn(registry, 'get')
.mockReturnValue(noPaymentDataStrategy);

const actionCreator = new PaymentStrategyActionCreator(registry, orderActionCreator, spamProtectionActionCreator);
const actionCreator = new PaymentStrategyActionCreator(registry, registryV2, orderActionCreator, spamProtectionActionCreator);
const payload = { ...getOrderRequestBody(), useStoreCredit: true };

await from(actionCreator.execute(payload)(store))
Expand Down Expand Up @@ -459,7 +466,7 @@ describe('PaymentStrategyActionCreator', () => {
});
registry = createPaymentStrategyRegistry(store, paymentClient, requestSender, spamProtection, 'en_US');

const actionCreator = new PaymentStrategyActionCreator(registry, orderActionCreator, spamProtectionActionCreator);
const actionCreator = new PaymentStrategyActionCreator(registry, registryV2, orderActionCreator, spamProtectionActionCreator);

try {
await from(actionCreator.finalize()(store)).toPromise();
Expand All @@ -479,7 +486,7 @@ describe('PaymentStrategyActionCreator', () => {
});
registry = createPaymentStrategyRegistry(store, paymentClient, requestSender, spamProtection, 'en_US');

const actionCreator = new PaymentStrategyActionCreator(registry, orderActionCreator, spamProtectionActionCreator);
const actionCreator = new PaymentStrategyActionCreator(registry, registryV2, orderActionCreator, spamProtectionActionCreator);

try {
await from(actionCreator.finalize()(store)).toPromise();
Expand Down
Loading

0 comments on commit 7d13250

Please sign in to comment.