Skip to content

Commit c92122e

Browse files
feat(payment): PI-4031 Change how payment_method_category parameter is passed to Klarna
1 parent 949f793 commit c92122e

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

packages/klarna-integration/src/klarnav2/klarnav2-payment-strategy.spec.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
NotInitializedErrorType,
1313
OrderFinalizationNotRequiredError,
1414
OrderRequestBody,
15+
PaymentIntegrationSelectors,
1516
PaymentIntegrationService,
1617
PaymentMethod,
1718
PaymentMethodCancelledError,
@@ -193,7 +194,7 @@ describe('KlarnaV2PaymentStrategy', () => {
193194
it('loads payments widget', () => {
194195
expect(klarnaPayments.init).toHaveBeenCalledWith({ client_token: 'foo' });
195196
expect(klarnaPayments.load).toHaveBeenCalledWith(
196-
{ container: '#container', payment_method_category: paymentMethod.id },
197+
{ container: '#container', payment_method_category: paymentMethod.gateway },
197198
expect.any(Function),
198199
);
199200
expect(klarnaPayments.load).toHaveBeenCalledTimes(1);
@@ -202,6 +203,35 @@ describe('KlarnaV2PaymentStrategy', () => {
202203
it('triggers callback with response', () => {
203204
expect(onLoad).toHaveBeenCalledWith({ show_form: true });
204205
});
206+
207+
it('calls loadPaymentsWidget when subscription is triggered and isPaymentMethodInitialized is true', async () => {
208+
const loadPaymentsWidgetMock = jest
209+
.spyOn(
210+
strategy as unknown as { loadPaymentsWidget: jest.Mock },
211+
'loadPaymentsWidget',
212+
)
213+
.mockImplementation(jest.fn());
214+
215+
const subscribeMock = jest.spyOn(paymentIntegrationService, 'subscribe');
216+
217+
await strategy.initialize({
218+
methodId: paymentMethod.id,
219+
gatewayId: paymentMethod.gateway,
220+
klarnav2: { container: '#container' },
221+
});
222+
223+
const subscriber = subscribeMock.mock.calls[0][0];
224+
225+
subscriber({
226+
isPaymentMethodInitialized: () => true,
227+
} as unknown as PaymentIntegrationSelectors);
228+
229+
expect(loadPaymentsWidgetMock).toHaveBeenCalledWith({
230+
methodId: paymentMethod.id,
231+
gatewayId: paymentMethod.gateway,
232+
klarnav2: { container: '#container' },
233+
});
234+
});
205235
});
206236

207237
describe('#execute()', () => {
@@ -221,7 +251,7 @@ describe('KlarnaV2PaymentStrategy', () => {
221251
await strategy.execute(payload);
222252

223253
expect(klarnaPayments.authorize).toHaveBeenCalledWith(
224-
{ payment_method_category: paymentMethod.id },
254+
{ payment_method_category: paymentMethod.gateway },
225255
getKlarnaV2UpdateSessionParamsPhone(),
226256
expect.any(Function),
227257
);
@@ -292,7 +322,7 @@ describe('KlarnaV2PaymentStrategy', () => {
292322
await strategy.execute(payload);
293323

294324
expect(klarnaPayments.authorize).toHaveBeenCalledWith(
295-
{ payment_method_category: paymentMethod.id },
325+
{ payment_method_category: paymentMethod.gateway },
296326
getKlarnaV2UpdateSessionParamsPhone(),
297327
expect.any(Function),
298328
);
@@ -328,7 +358,7 @@ describe('KlarnaV2PaymentStrategy', () => {
328358
await strategy.execute(payload);
329359

330360
expect(klarnaPayments.authorize).toHaveBeenCalledWith(
331-
{ payment_method_category: paymentMethod.id },
361+
{ payment_method_category: paymentMethod.gateway },
332362
getKlarnaV2UpdateSessionParamsForOC(),
333363
expect.any(Function),
334364
);
@@ -370,7 +400,7 @@ describe('KlarnaV2PaymentStrategy', () => {
370400
await strategy.execute(payload);
371401

372402
expect(klarnaPayments.authorize).toHaveBeenCalledWith(
373-
{ payment_method_category: paymentMethod.id },
403+
{ payment_method_category: paymentMethod.gateway },
374404
getKlarnaV2UpdateSessionParams(),
375405
expect.any(Function),
376406
);
@@ -422,7 +452,7 @@ describe('KlarnaV2PaymentStrategy', () => {
422452
await strategy.execute(payload);
423453

424454
expect(klarnaPayments.authorize).toHaveBeenCalledWith(
425-
{ payment_method_category: paymentMethod.id },
455+
{ payment_method_category: paymentMethod.gateway },
426456
{},
427457
expect.any(Function),
428458
);

packages/klarna-integration/src/klarnav2/klarnav2-payment-strategy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export default class KlarnaV2PaymentStrategy {
105105

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

108-
const { authorization_token: authorizationToken } = await this.authorizeOrThrow(methodId);
108+
const { authorization_token: authorizationToken } = await this.authorizeOrThrow(gatewayId);
109109

110110
await this.paymentIntegrationService.initializePayment(gatewayId, {
111111
authorizationToken,
@@ -163,7 +163,7 @@ export default class KlarnaV2PaymentStrategy {
163163

164164
this.klarnaPayments.init({ client_token: paymentMethod.clientToken });
165165
this.klarnaPayments.load(
166-
{ container, payment_method_category: paymentMethod.id },
166+
{ container, payment_method_category: paymentMethod.gateway },
167167
(response) => {
168168
if (onLoad) {
169169
onLoad(response);
@@ -228,7 +228,7 @@ export default class KlarnaV2PaymentStrategy {
228228
return klarnaAddress;
229229
}
230230

231-
private async authorizeOrThrow(methodId: string): Promise<KlarnaAuthorizationResponse> {
231+
private async authorizeOrThrow(gatewayId: string): Promise<KlarnaAuthorizationResponse> {
232232
await this.paymentIntegrationService.loadCheckout();
233233

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

247247
this.klarnaPayments.authorize(
248-
{ payment_method_category: methodId },
248+
{ payment_method_category: gatewayId },
249249
updateSessionData,
250250
(res) => {
251251
if (res.approved) {

0 commit comments

Comments
 (0)