Skip to content

Commit 6627fdf

Browse files
klarna draft
1 parent 78d7539 commit 6627fdf

File tree

4 files changed

+105
-21
lines changed

4 files changed

+105
-21
lines changed

packages/bigcommerce-payments-integration/src/bigcommerce-payments-alternative-methods/bigcommerce-payments-alternative-methods-payment-strategy.ts

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ import BigCommercePaymentsAlternativeMethodsPaymentInitializeOptions, {
3737
const POLLING_INTERVAL = 3000;
3838
const MAX_POLLING_TIME = 300000;
3939

40+
export interface RedirectActionBody {
41+
body: {
42+
additional_action_required: {
43+
type: 'offsite_redirect';
44+
data: {
45+
redirect_url: string;
46+
};
47+
};
48+
};
49+
}
50+
51+
export interface RedirectError {
52+
body: {
53+
additional_action_required: {
54+
data: {
55+
redirect_url: string;
56+
};
57+
};
58+
};
59+
}
60+
4061
export default class BigCommercePaymentsAlternativeMethodsPaymentStrategy
4162
implements PaymentStrategy
4263
{
@@ -56,14 +77,20 @@ export default class BigCommercePaymentsAlternativeMethodsPaymentStrategy
5677
private loadingIndicator: LoadingIndicator,
5778
private pollingInterval: number = POLLING_INTERVAL,
5879
private maxPollingIntervalTime: number = MAX_POLLING_TIME,
59-
) {}
80+
) {
81+
console.log('BigCommercePaymentsAlternativeMethodsPaymentStrategy constructor test');
82+
}
6083

6184
async initialize(
6285
options: PaymentInitializeOptions &
6386
WithBigCommercePaymentsAlternativeMethodsPaymentInitializeOptions,
6487
): Promise<void> {
6588
const { gatewayId, methodId, bigcommerce_payments_apms } = options;
6689

90+
if (methodId === 'klarna') {
91+
return;
92+
}
93+
6794
this.bigCommercePaymentsAlternativeMethods = bigcommerce_payments_apms;
6895

6996
if (!methodId) {
@@ -128,25 +155,61 @@ export default class BigCommercePaymentsAlternativeMethodsPaymentStrategy
128155

129156
const { methodId, gatewayId } = payment;
130157

131-
if (!this.orderId) {
132-
throw new PaymentMethodInvalidError();
133-
}
158+
if (methodId === 'klarna') {
159+
try {
160+
const paymentData = {
161+
formattedPayload: {
162+
vault_payment_instrument: null,
163+
set_as_default_stored_instrument: null,
164+
device_info: null,
165+
method_id: methodId,
166+
},
167+
};
168+
169+
await this.paymentIntegrationService.submitOrder(order, options);
170+
await this.paymentIntegrationService.submitPayment({
171+
methodId,
172+
gatewayId,
173+
paymentData,
174+
});
175+
} catch (error: unknown) {
176+
if (this.isRedirectError(error)) {
177+
const redirectUrl = error.body.additional_action_required.data.redirect_url;
134178

135-
if (this.isPollingEnabled && methodId === 'ideal') {
136-
await new Promise((resolve, reject) => {
137-
void this.initializePollingMechanism(methodId, resolve, reject, gatewayId);
138-
});
139-
}
179+
return new Promise((_, reject) => {
180+
window.location.replace(redirectUrl);
140181

141-
if (!this.isNonInstantPaymentMethod(methodId)) {
142-
await this.paymentIntegrationService.submitOrder(order, options);
143-
}
182+
this.toggleLoadingIndicator(false);
144183

145-
await this.bigCommercePaymentsIntegrationService.submitPayment(
146-
methodId,
147-
this.orderId,
148-
gatewayId,
149-
);
184+
reject();
185+
});
186+
}
187+
188+
this.handleError(error);
189+
190+
return Promise.reject(error);
191+
}
192+
} else {
193+
if (!this.orderId) {
194+
throw new PaymentMethodInvalidError();
195+
}
196+
197+
if (this.isPollingEnabled && methodId === 'ideal') {
198+
await new Promise((resolve, reject) => {
199+
void this.initializePollingMechanism(methodId, resolve, reject, gatewayId);
200+
});
201+
}
202+
203+
if (!this.isNonInstantPaymentMethod(methodId)) {
204+
await this.paymentIntegrationService.submitOrder(order, options);
205+
}
206+
207+
await this.bigCommercePaymentsIntegrationService.submitPayment(
208+
methodId,
209+
this.orderId,
210+
gatewayId,
211+
);
212+
}
150213
}
151214

152215
finalize(): Promise<void> {
@@ -445,4 +508,18 @@ export default class BigCommercePaymentsAlternativeMethodsPaymentStrategy
445508

446509
return this.paypalApms;
447510
}
511+
512+
private isRedirectError(error: unknown): error is RedirectError {
513+
if (typeof error !== 'object' || error === null) {
514+
return false;
515+
}
516+
517+
const { body }: Partial<RedirectActionBody> = error;
518+
519+
if (!body) {
520+
return false;
521+
}
522+
523+
return !!body.additional_action_required?.data.redirect_url;
524+
}
448525
}

packages/bigcommerce-payments-integration/src/bigcommerce-payments-alternative-methods/create-bigcommerce-payments-alternative-methods-button-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ const createBigCommercePaymentsAlternativeMethodsButtonStrategy: CheckoutButtonS
1616
);
1717

1818
export default toResolvableModule(createBigCommercePaymentsAlternativeMethodsButtonStrategy, [
19-
{ id: 'bigcommerce_payments_apms' },
19+
{ gateway: 'bigcommerce_payments_apms' },
2020
]);

packages/bigcommerce-payments-integration/src/bigcommerce-payments-alternative-methods/create-bigcommerce-payments-alternative-methods-payment-strategy.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import BigCommercePaymentsAlternativeMethodsPaymentStrategy from './bigcommerce-
1212

1313
const createBigCommercePaymentsAlternativeMethodsPaymentStrategy: PaymentStrategyFactory<
1414
BigCommercePaymentsAlternativeMethodsPaymentStrategy
15-
> = (paymentIntegrationService) =>
16-
new BigCommercePaymentsAlternativeMethodsPaymentStrategy(
15+
> = (paymentIntegrationService) => {
16+
console.log('createBigCommercePaymentsAlternativeMethodsPaymentStrategy');
17+
18+
return new BigCommercePaymentsAlternativeMethodsPaymentStrategy(
1719
paymentIntegrationService,
1820
createBigCommercePaymentsIntegrationService(paymentIntegrationService),
1921
createBigCommercePaymentsSdk(),
2022
new LoadingIndicator({
2123
containerStyles: LOADING_INDICATOR_STYLES,
2224
}),
2325
);
26+
}
27+
2428

2529
export default toResolvableModule(createBigCommercePaymentsAlternativeMethodsPaymentStrategy, [
26-
{ gateway: 'bigcommerce_payments_apms' },
30+
// { gateway: 'bigcommerce_payments_apms' },
31+
{ gateway: 'bigcommerce_payments_apms', id: 'klarna' },
2732
]);

packages/payment-integration-api/src/to-resolvable-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ export default function toResolvableModule<TModule, TIdentifier>(
44
module: TModule,
55
resolveIds: TIdentifier[],
66
): ResolvableModule<TModule, TIdentifier> {
7+
console.log('resolveIds', resolveIds);
8+
79
return Object.assign(module, { resolveIds });
810
}

0 commit comments

Comments
 (0)