Skip to content

Commit 53676a7

Browse files
committed
feat(payment): PI-4492 google pay deinitialize block during paying
1 parent 45fa2f5 commit 53676a7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/google-pay-integration/src/google-pay-payment-strategy.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
OrderRequestBody,
1212
PaymentArgumentInvalidError,
1313
PaymentInitializeOptions,
14+
PaymentIntegrationSelectors,
1415
PaymentIntegrationService,
1516
PaymentMethod,
1617
} from '@bigcommerce/checkout-sdk/payment-integration-api';
@@ -581,6 +582,30 @@ describe('GooglePayPaymentStrategy', () => {
581582
await expect(deinitialize).resolves.toBeUndefined();
582583
});
583584

585+
it('should NOT deinitialize the strategy if deinitialization is blocked', async () => {
586+
const delayedPromise = new Promise((r) => {
587+
setTimeout(r, 100);
588+
});
589+
590+
jest.spyOn(processor, 'showPaymentSheet').mockResolvedValue(
591+
Promise.resolve(getCardDataResponse()),
592+
);
593+
594+
jest.spyOn(paymentIntegrationService, 'updateBillingAddress').mockReturnValue(
595+
delayedPromise as Promise<PaymentIntegrationSelectors>,
596+
);
597+
598+
jest.spyOn(button, 'removeEventListener');
599+
600+
await strategy.initialize(options);
601+
602+
button.click();
603+
await new Promise((resolve) => process.nextTick(resolve));
604+
await strategy.deinitialize();
605+
606+
expect(button.removeEventListener).not.toHaveBeenCalled();
607+
});
608+
584609
it('should unbind payment button', async () => {
585610
await strategy.initialize(options);
586611
await strategy.deinitialize();

packages/google-pay-integration/src/google-pay-payment-strategy.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default class GooglePayPaymentStrategy implements PaymentStrategy {
4242
private _paymentButton?: HTMLElement;
4343
private _clickListener?: (event: MouseEvent) => unknown;
4444
private _methodId?: keyof WithGooglePayPaymentInitializeOptions;
45+
private _isDeinitializationBlocked = false;
4546

4647
constructor(
4748
protected _paymentIntegrationService: PaymentIntegrationService,
@@ -112,6 +113,10 @@ export default class GooglePayPaymentStrategy implements PaymentStrategy {
112113
}
113114

114115
deinitialize(): Promise<void> {
116+
if (this._isDeinitializationBlocked) {
117+
return Promise.resolve();
118+
}
119+
115120
if (this._clickListener) {
116121
this._paymentButton?.removeEventListener('click', this._clickListener);
117122
}
@@ -174,6 +179,8 @@ export default class GooglePayPaymentStrategy implements PaymentStrategy {
174179
);
175180

176181
throw err;
182+
} finally {
183+
this._toggleBlockDeinitialization(false);
177184
}
178185

179186
onPaymentSelect?.();
@@ -183,6 +190,7 @@ export default class GooglePayPaymentStrategy implements PaymentStrategy {
183190
protected async _interactWithPaymentSheet(): Promise<void> {
184191
const response = await this._googlePayPaymentProcessor.showPaymentSheet();
185192

193+
this._toggleBlockDeinitialization(true);
186194
this._toggleLoadingIndicator(true);
187195

188196
const billingAddress =
@@ -197,6 +205,7 @@ export default class GooglePayPaymentStrategy implements PaymentStrategy {
197205
await this._paymentIntegrationService.loadCheckout();
198206
await this._paymentIntegrationService.loadPaymentMethod(this._getMethodId());
199207
this._toggleLoadingIndicator(false);
208+
this._toggleBlockDeinitialization(false);
200209
}
201210

202211
protected _getMethodId(): keyof WithGooglePayPaymentInitializeOptions {
@@ -315,6 +324,10 @@ export default class GooglePayPaymentStrategy implements PaymentStrategy {
315324
};
316325
}
317326

327+
private _toggleBlockDeinitialization(isBlocked: boolean) {
328+
this._isDeinitializationBlocked = isBlocked;
329+
}
330+
318331
private _toggleLoadingIndicator(isLoading: boolean): void {
319332
if (isLoading && this._loadingIndicatorContainer) {
320333
this._loadingIndicator.show(this._loadingIndicatorContainer);

0 commit comments

Comments
 (0)