diff --git a/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/payment-method-config-helper.ts b/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/payment-method-config-helper.ts index 3f0868f124d..df56325b76a 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/payment-method-config-helper.ts +++ b/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/payment-method-config-helper.ts @@ -50,6 +50,12 @@ export const canMakePaymentWithExtensions = Object.entries( extensionsCallbacks ).forEach( ( [ namespace, callbacks ] ) => { + if ( + ! ( paymentMethodName in callbacks ) || + typeof callbacks[ paymentMethodName ] !== 'function' + ) { + return; + } namespacedCallbacks[ namespace ] = callbacks[ paymentMethodName ]; } diff --git a/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/test/payment-method-config-helper.ts b/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/test/payment-method-config-helper.ts index 2951725c3d7..4f75083d891 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/test/payment-method-config-helper.ts +++ b/plugins/woocommerce-blocks/assets/js/blocks-registry/payment-methods/test/payment-method-config-helper.ts @@ -88,6 +88,9 @@ describe( 'payment-method-config-helper', () => { woopay: trueCallback, // testpay: one callback errors, one returns true testpay: throwsCallback, + // Used to check that only valid callbacks run in each namespace. It is not present in + // 'other-woocommerce-marketplace-extension'. + blocks_pay: trueCallback, } ); registerPaymentMethodExtensionCallbacks( @@ -202,5 +205,14 @@ describe( 'payment-method-config-helper', () => { expect( throwsCallback ).toHaveBeenCalledTimes( 1 ); expect( trueCallback ).toHaveBeenCalledTimes( 1 ); } ); + + it( 'Does not error when a callback for a payment method is in one namespace but not another', () => { + helpers.canMakePaymentWithExtensions( + () => true, + canMakePaymentExtensionsCallbacks, + 'blocks_pay' + )( canMakePaymentArgument ); + expect( console ).not.toHaveErrored(); + } ); } ); } );