Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 3cee644

Browse files
oprwavvves
authored andcommitted
Check that the callback for filtering payment methods is available and is a function before trying to run it (#7377)
* Check callback for payment method is available before trying to run it * Check if callback is a function before trying to run it * Update tests to ensure callbacks only run if they are registered
1 parent d246ff3 commit 3cee644

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

assets/js/blocks-registry/payment-methods/payment-method-config-helper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export const canMakePaymentWithExtensions =
5050

5151
Object.entries( extensionsCallbacks ).forEach(
5252
( [ namespace, callbacks ] ) => {
53+
if (
54+
! ( paymentMethodName in callbacks ) ||
55+
typeof callbacks[ paymentMethodName ] !== 'function'
56+
) {
57+
return;
58+
}
5359
namespacedCallbacks[ namespace ] =
5460
callbacks[ paymentMethodName ];
5561
}

assets/js/blocks-registry/payment-methods/test/payment-method-config-helper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ describe( 'payment-method-config-helper', () => {
8888
woopay: trueCallback,
8989
// testpay: one callback errors, one returns true
9090
testpay: throwsCallback,
91+
// Used to check that only valid callbacks run in each namespace. It is not present in
92+
// 'other-woocommerce-marketplace-extension'.
93+
blocks_pay: trueCallback,
9194
}
9295
);
9396
registerPaymentMethodExtensionCallbacks(
@@ -202,5 +205,14 @@ describe( 'payment-method-config-helper', () => {
202205
expect( throwsCallback ).toHaveBeenCalledTimes( 1 );
203206
expect( trueCallback ).toHaveBeenCalledTimes( 1 );
204207
} );
208+
209+
it( 'Does not error when a callback for a payment method is in one namespace but not another', () => {
210+
helpers.canMakePaymentWithExtensions(
211+
() => true,
212+
canMakePaymentExtensionsCallbacks,
213+
'blocks_pay'
214+
)( canMakePaymentArgument );
215+
expect( console ).not.toHaveErrored();
216+
} );
205217
} );
206218
} );

0 commit comments

Comments
 (0)