Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
toResolvableModule,
} from '@bigcommerce/checkout-sdk/payment-integration-api';
import {
createPayPalCommerceFastlaneUtils,
createPayPalCommerceSdk,
} from '@bigcommerce/checkout-sdk/paypal-commerce-utils';
createPayPalFastlaneUtils,
createPayPalSdkScriptLoader,
} from '@bigcommerce/checkout-sdk/paypal-utils';

import PayPalCommerceFastlaneCustomerStrategy from './paypal-commerce-fastlane-customer-strategy';

Expand All @@ -14,8 +14,8 @@ const createPayPalCommerceFastlaneCustomerStrategy: CustomerStrategyFactory<
> = (paymentIntegrationService) =>
new PayPalCommerceFastlaneCustomerStrategy(
paymentIntegrationService,
createPayPalCommerceSdk(),
createPayPalCommerceFastlaneUtils(),
createPayPalSdkScriptLoader(),
createPayPalFastlaneUtils(),
);

export default toResolvableModule(createPayPalCommerceFastlaneCustomerStrategy, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ import {
PaymentIntegrationServiceMock,
} from '@bigcommerce/checkout-sdk/payment-integrations-test-utils';
import {
createPayPalCommerceFastlaneUtils,
createPayPalCommerceSdk,
getPayPalCommerceAcceleratedCheckoutPaymentMethod,
createPayPalFastlaneUtils,
createPayPalSdkScriptLoader,
getPayPalAcceleratedCheckoutPaymentMethod,
getPayPalFastlaneSdk,
PayPalCommerceFastlaneUtils,
PayPalCommerceSdk,
PayPalFastlaneAuthenticationState,
PayPalFastlaneSdk,
} from '@bigcommerce/checkout-sdk/paypal-commerce-utils';
PayPalFastlaneUtils,
PayPalSdkScriptLoader,
} from '@bigcommerce/checkout-sdk/paypal-utils';

import PayPalCommerceFastlaneCustomerStrategy from './paypal-commerce-fastlane-customer-strategy';

describe('PayPalCommerceFastlaneCustomerStrategy', () => {
let paymentIntegrationService: PaymentIntegrationService;
let paymentMethod: PaymentMethod;
let paypalCommerceFastlaneUtils: PayPalCommerceFastlaneUtils;
let paypalCommerceSdk: PayPalCommerceSdk;
let paypalCommerceFastlaneUtils: PayPalFastlaneUtils;
let paypalSdkScriptLoader: PayPalSdkScriptLoader;
let paypalFastlaneSdk: PayPalFastlaneSdk;
let strategy: PayPalCommerceFastlaneCustomerStrategy;

Expand Down Expand Up @@ -155,16 +155,16 @@ describe('PayPalCommerceFastlaneCustomerStrategy', () => {
};

beforeEach(() => {
paymentMethod = getPayPalCommerceAcceleratedCheckoutPaymentMethod();
paymentMethod = getPayPalAcceleratedCheckoutPaymentMethod();
paypalFastlaneSdk = getPayPalFastlaneSdk();

paymentIntegrationService = new PaymentIntegrationServiceMock();
paypalCommerceSdk = createPayPalCommerceSdk();
paypalCommerceFastlaneUtils = createPayPalCommerceFastlaneUtils();
paypalSdkScriptLoader = createPayPalSdkScriptLoader();
paypalCommerceFastlaneUtils = createPayPalFastlaneUtils();

strategy = new PayPalCommerceFastlaneCustomerStrategy(
paymentIntegrationService,
paypalCommerceSdk,
paypalSdkScriptLoader,
paypalCommerceFastlaneUtils,
);

Expand All @@ -183,7 +183,7 @@ describe('PayPalCommerceFastlaneCustomerStrategy', () => {
jest.spyOn(state, 'getBillingAddress').mockReturnValue(getBillingAddress());
jest.spyOn(state, 'getStoreConfigOrThrow').mockReturnValue(storeConfig);

jest.spyOn(paypalCommerceSdk, 'getPayPalFastlaneSdk').mockImplementation(() =>
jest.spyOn(paypalSdkScriptLoader, 'getPayPalFastlaneSdk').mockImplementation(() =>
Promise.resolve(paypalFastlaneSdk),
);
jest.spyOn(paypalCommerceFastlaneUtils, 'initializePayPalFastlane');
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('PayPalCommerceFastlaneCustomerStrategy', () => {
it('loads paypal sdk and initialises paypal fastlane in production mode', async () => {
await strategy.initialize(initializationOptions);

expect(paypalCommerceSdk.getPayPalFastlaneSdk).toHaveBeenCalledWith(
expect(paypalSdkScriptLoader.getPayPalFastlaneSdk).toHaveBeenCalledWith(
paymentMethod,
cart.currency.code,
cart.id,
Expand All @@ -320,7 +320,7 @@ describe('PayPalCommerceFastlaneCustomerStrategy', () => {

await strategy.initialize(initializationOptions);

expect(paypalCommerceSdk.getPayPalFastlaneSdk).toHaveBeenCalledWith(
expect(paypalSdkScriptLoader.getPayPalFastlaneSdk).toHaveBeenCalledWith(
paymentMethod,
cart.currency.code,
cart.id,
Expand All @@ -342,13 +342,13 @@ describe('PayPalCommerceFastlaneCustomerStrategy', () => {
'getPaymentMethodOrThrow',
).mockReturnValue(paymentMethod);

jest.spyOn(paypalCommerceSdk, 'getPayPalFastlaneSdk').mockImplementation(() =>
jest.spyOn(paypalSdkScriptLoader, 'getPayPalFastlaneSdk').mockImplementation(() =>
Promise.reject(),
);

await strategy.initialize(initializationOptions);

expect(paypalCommerceSdk.getPayPalFastlaneSdk).toHaveBeenCalled();
expect(paypalSdkScriptLoader.getPayPalFastlaneSdk).toHaveBeenCalled();
expect(paypalCommerceFastlaneUtils.initializePayPalFastlane).not.toHaveBeenCalled();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
} from '@bigcommerce/checkout-sdk/payment-integration-api';
import {
getFastlaneStyles,
PayPalCommerceFastlaneUtils,
PayPalCommerceInitializationData,
PayPalCommerceSdk,
PayPalFastlaneAuthenticationResult,
PayPalFastlaneAuthenticationState,
PayPalFastlaneStylesOption,
} from '@bigcommerce/checkout-sdk/paypal-commerce-utils';
PayPalFastlaneUtils,
PayPalInitializationData,
PayPalSdkScriptLoader,
} from '@bigcommerce/checkout-sdk/paypal-utils';

import PayPalCommerceFastlaneCustomerInitializeOptions, {
WithPayPalCommerceFastlaneCustomerInitializeOptions,
Expand All @@ -24,8 +24,8 @@ import PayPalCommerceFastlaneCustomerInitializeOptions, {
export default class PayPalCommerceFastlaneCustomerStrategy implements CustomerStrategy {
constructor(
private paymentIntegrationService: PaymentIntegrationService,
private paypalCommerceSdk: PayPalCommerceSdk,
private paypalCommerceFastlaneUtils: PayPalCommerceFastlaneUtils,
private paypalSdkScriptLoader: PayPalSdkScriptLoader,
private paypalFastlaneUtils: PayPalFastlaneUtils,
) {}

async initialize(
Expand All @@ -44,18 +44,17 @@ export default class PayPalCommerceFastlaneCustomerStrategy implements CustomerS

const state = this.paymentIntegrationService.getState();
const cart = state.getCartOrThrow();
const paymentMethod =
state.getPaymentMethodOrThrow<PayPalCommerceInitializationData>(methodId);
const paymentMethod = state.getPaymentMethodOrThrow<PayPalInitializationData>(methodId);

const isTestModeEnabled = !!paymentMethod.initializationData?.isDeveloperModeApplicable;

const paypalFastlaneSdk = await this.paypalCommerceSdk.getPayPalFastlaneSdk(
const paypalFastlaneSdk = await this.paypalSdkScriptLoader.getPayPalFastlaneSdk(
paymentMethod,
cart.currency.code,
cart.id,
);

await this.paypalCommerceFastlaneUtils.initializePayPalFastlane(
await this.paypalFastlaneUtils.initializePayPalFastlane(
paypalFastlaneSdk,
isTestModeEnabled,
this.getFastlaneStyles(methodId, paypalcommercefastlane),
Expand Down Expand Up @@ -132,24 +131,22 @@ export default class PayPalCommerceFastlaneCustomerStrategy implements CustomerS
const billingAddress = state.getBillingAddress();
const customerEmail = customer?.email || billingAddress?.email || '';

const { customerContextId } = await this.paypalCommerceFastlaneUtils.lookupCustomerOrThrow(
const { customerContextId } = await this.paypalFastlaneUtils.lookupCustomerOrThrow(
customerEmail,
);

const authenticationResult =
await this.paypalCommerceFastlaneUtils.triggerAuthenticationFlowOrThrow(
customerContextId,
);
await this.paypalFastlaneUtils.triggerAuthenticationFlowOrThrow(customerContextId);

const isAuthenticationFlowCanceled =
authenticationResult.authenticationState === PayPalFastlaneAuthenticationState.CANCELED;

await this.updateCustomerDataState(methodId, authenticationResult);

if (isAuthenticationFlowCanceled) {
this.paypalCommerceFastlaneUtils.removeStorageSessionId();
this.paypalFastlaneUtils.removeStorageSessionId();
} else {
this.paypalCommerceFastlaneUtils.updateStorageSessionId(cartId);
this.paypalFastlaneUtils.updateStorageSessionId(cartId);
}
}

Expand All @@ -161,7 +158,7 @@ export default class PayPalCommerceFastlaneCustomerStrategy implements CustomerS
const cart = state.getCartOrThrow();

const { authenticationState, addresses, billingAddress, shippingAddress, instruments } =
this.paypalCommerceFastlaneUtils.mapPayPalFastlaneProfileToBcCustomerData(
this.paypalFastlaneUtils.mapPayPalFastlaneProfileToBcCustomerData(
methodId,
authenticationResult,
);
Expand All @@ -182,7 +179,7 @@ export default class PayPalCommerceFastlaneCustomerStrategy implements CustomerS
);

const paymentMethod =
updatedState.getPaymentMethodOrThrow<PayPalCommerceInitializationData>(methodId);
updatedState.getPaymentMethodOrThrow<PayPalInitializationData>(methodId);
const { isFastlaneShippingOptionAutoSelectEnabled } =
paymentMethod.initializationData || {};
const consignments = updatedState.getConsignments() || [];
Expand Down Expand Up @@ -213,8 +210,7 @@ export default class PayPalCommerceFastlaneCustomerStrategy implements CustomerS
paypalcommercefastlane: PayPalCommerceFastlaneCustomerInitializeOptions | undefined,
): PayPalFastlaneStylesOption | undefined {
const state = this.paymentIntegrationService.getState();
const paymentMethod =
state.getPaymentMethodOrThrow<PayPalCommerceInitializationData>(methodId);
const paymentMethod = state.getPaymentMethodOrThrow<PayPalInitializationData>(methodId);

const { fastlaneStyles, isFastlaneStylingEnabled } = paymentMethod.initializationData || {};

Expand Down