Skip to content

feat(payment): PI-4031 Change how payment_method_category parameter i… #2890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -12,14 +12,17 @@ import {
NotInitializedErrorType,
OrderFinalizationNotRequiredError,
OrderRequestBody,
PaymentIntegrationSelectors,
PaymentIntegrationService,
PaymentMethod,
PaymentMethodCancelledError,
PaymentMethodInvalidError,
StoreConfig,
} from '@bigcommerce/checkout-sdk/payment-integration-api';
import {
getAddress,
getCheckout,
getConfig,
getOrderRequestBody,
getResponse,
PaymentIntegrationServiceMock,
Expand Down Expand Up @@ -51,6 +54,7 @@ describe('KlarnaV2PaymentStrategy', () => {
let paymentMethodMock: PaymentMethod;
let klarnav2TokenUpdater: KlarnaV2TokenUpdater;
let paymentIntegrationService: PaymentIntegrationService;
let storeConfigMock: StoreConfig;

beforeEach(() => {
paymentIntegrationService = new PaymentIntegrationServiceMock();
Expand Down Expand Up @@ -89,6 +93,10 @@ describe('KlarnaV2PaymentStrategy', () => {
};

checkoutMock = getCheckout();
storeConfigMock = getConfig().storeConfig;
storeConfigMock.checkoutSettings.features = {
'PI-4025.klarna_single_radio_button': false,
};

jest.spyOn(paymentIntegrationService.getState(), 'getPaymentMethodOrThrow').mockReturnValue(
paymentMethodMock,
Expand All @@ -112,6 +120,10 @@ describe('KlarnaV2PaymentStrategy', () => {
jest.spyOn(paymentIntegrationService.getState(), 'getCheckoutOrThrow').mockReturnValue(
checkoutMock,
);

jest.spyOn(paymentIntegrationService.getState(), 'getStoreConfigOrThrow').mockReturnValue(
storeConfigMock,
);
});

afterEach(() => {
Expand Down Expand Up @@ -199,9 +211,61 @@ describe('KlarnaV2PaymentStrategy', () => {
expect(klarnaPayments.load).toHaveBeenCalledTimes(1);
});

it('loads payments widget when PI-4025.klarna_single_radio_button experiment is enabled', async () => {
storeConfigMock.checkoutSettings.features = {
'PI-4025.klarna_single_radio_button': true,
};

jest.spyOn(paymentIntegrationService.getState(), 'getStoreConfigOrThrow').mockReturnValue(
storeConfigMock,
);

await strategy.initialize({
methodId: paymentMethod.id,
gatewayId: paymentMethod.gateway,
klarnav2: { container: '#container', onLoad },
});


expect(klarnaPayments.init).toHaveBeenCalledWith({ client_token: 'foo' });
expect(klarnaPayments.load).toHaveBeenCalledWith(
{ container: '#container', payment_method_category: paymentMethod.gateway },
expect.any(Function),
);
});

it('triggers callback with response', () => {
expect(onLoad).toHaveBeenCalledWith({ show_form: true });
});

it('calls loadPaymentsWidget when subscription is triggered and isPaymentMethodInitialized is true', async () => {
const loadPaymentsWidgetMock = jest
.spyOn(
strategy as unknown as { loadPaymentsWidget: jest.Mock },
'loadPaymentsWidget',
)
.mockImplementation(jest.fn());

const subscribeMock = jest.spyOn(paymentIntegrationService, 'subscribe');

await strategy.initialize({
methodId: paymentMethod.id,
gatewayId: paymentMethod.gateway,
klarnav2: { container: '#container' },
});

const subscriber = subscribeMock.mock.calls[0][0];

subscriber({
isPaymentMethodInitialized: () => true,
} as unknown as PaymentIntegrationSelectors);

expect(loadPaymentsWidgetMock).toHaveBeenCalledWith({
methodId: paymentMethod.id,
gatewayId: paymentMethod.gateway,
klarnav2: { container: '#container' },
});
});
});

describe('#execute()', () => {
Expand Down Expand Up @@ -231,6 +295,32 @@ describe('KlarnaV2PaymentStrategy', () => {
);
});

it('authorizes against klarnav2 when PI-4025.klarna_single_radio_button experiment is enabled ', async () => {
storeConfigMock.checkoutSettings.features = {
'PI-4025.klarna_single_radio_button': true,
};

jest.spyOn(paymentIntegrationService.getState(), 'getStoreConfigOrThrow').mockReturnValue(
storeConfigMock,
);

const loadCheckoutMock = jest.spyOn(paymentIntegrationService, 'loadCheckout');

loadCheckoutMock.mockImplementation(jest.fn());

await strategy.execute(payload);

expect(klarnaPayments.authorize).toHaveBeenCalledWith(
{ payment_method_category: paymentMethod.gateway },
getKlarnaV2UpdateSessionParamsPhone(),
expect.any(Function),
);
expect(klarnav2TokenUpdater.updateClientToken).toHaveBeenCalledWith(
paymentMethod.gateway,
{ params: { params: 'b20deef40f9699e48671bbc3fef6ca44dc80e3c7' } },
);
});

it('executes with no payment argument', async () => {
try {
await strategy.execute({ ...payload, payment: undefined });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class KlarnaV2PaymentStrategy {

await this.klarnav2TokenUpdater.klarnaOrderInitialization(cartId, clientToken);

const { authorization_token: authorizationToken } = await this.authorizeOrThrow(methodId);
const { authorization_token: authorizationToken } = await this.authorizeOrThrow(methodId, gatewayId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { authorization_token: authorizationToken } = await this.authorizeOrThrow(methodId, gatewayId);
const paymentMethodСategory = this.isKlarnaSingleRadioButtonEnabled() ? gatewayId : methodId;
const { authorization_token: authorizationToken } = await this.authorizeOrThrow(paymentMethodСategory);


await this.paymentIntegrationService.initializePayment(gatewayId, {
authorizationToken,
Expand Down Expand Up @@ -163,7 +163,7 @@ export default class KlarnaV2PaymentStrategy {

this.klarnaPayments.init({ client_token: paymentMethod.clientToken });
this.klarnaPayments.load(
{ container, payment_method_category: paymentMethod.id },
{ container, payment_method_category: this.isKlarnaSingleRadioButtonEnabled() ? paymentMethod.gateway : methodId},
(response) => {
if (onLoad) {
onLoad(response);
Expand Down Expand Up @@ -228,7 +228,7 @@ export default class KlarnaV2PaymentStrategy {
return klarnaAddress;
}

private async authorizeOrThrow(methodId: string): Promise<KlarnaAuthorizationResponse> {
private async authorizeOrThrow(methodId: string, gatewayId: string): Promise<KlarnaAuthorizationResponse> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private async authorizeOrThrow(methodId: string, gatewayId: string): Promise<KlarnaAuthorizationResponse> {
private async authorizeOrThrow(paymentMethodСategory: string): Promise<KlarnaAuthorizationResponse> {

await this.paymentIntegrationService.loadCheckout();

const state = this.paymentIntegrationService.getState();
Expand All @@ -245,7 +245,7 @@ export default class KlarnaV2PaymentStrategy {
}

this.klarnaPayments.authorize(
{ payment_method_category: methodId },
{ payment_method_category: this.isKlarnaSingleRadioButtonEnabled() ? gatewayId : methodId },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ payment_method_category: this.isKlarnaSingleRadioButtonEnabled() ? gatewayId : methodId },
{ payment_method_category: paymentMethodСategory },

updateSessionData,
(res) => {
if (res.approved) {
Expand All @@ -261,4 +261,12 @@ export default class KlarnaV2PaymentStrategy {
);
});
}

private isKlarnaSingleRadioButtonEnabled(): boolean {
const { features } = this.paymentIntegrationService
.getState()
.getStoreConfigOrThrow().checkoutSettings;

return features['PI-4025.klarna_single_radio_button'];
}
}