diff --git a/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/index.js b/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/index.js index 74b3278effa..7cf425864e6 100644 --- a/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/index.js +++ b/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/index.js @@ -97,6 +97,7 @@ describe( 'PaymentGatewaySuggestions', () => { isResolving: false, getPaymentGateway: jest.fn(), paymentGatewaySuggestions, + countryCode: 'US', installedPaymentGateways: [], } ) ); @@ -116,6 +117,9 @@ describe( 'PaymentGatewaySuggestions', () => { ); expect( paymentTitles ).toEqual( [ + 'Stripe', + 'PayPal Payments', + 'Eway', 'Cash on delivery', 'Direct bank transfer', ] ); @@ -136,6 +140,7 @@ describe( 'PaymentGatewaySuggestions', () => { isResolving: false, getPaymentGateway: jest.fn(), paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay, + countryCode: 'US', installedPaymentGateways: [], } ) ); @@ -200,6 +205,7 @@ describe( 'PaymentGatewaySuggestions', () => { isResolving: false, getPaymentGateway: jest.fn(), paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay, + countryCode: 'US', installedPaymentGateways: [ { id: 'ppcp-gateway', @@ -324,6 +330,7 @@ describe( 'PaymentGatewaySuggestions', () => { isResolving: false, getPaymentGateway: jest.fn(), paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay, + countryCode: 'US', installedPaymentGateways: [ { id: 'ppcp-gateway', diff --git a/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/utils.js b/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/utils.js index 1e4218d9308..3db3b1fa663 100644 --- a/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/utils.js +++ b/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/test/utils.js @@ -47,7 +47,11 @@ const amazonPay = { describe( 'getSplitGateways()', () => { it( 'Returns WCPay gateways', () => { - const [ wcpayGateways ] = getSplitGateways( [ wcpay, cod, paypal ] ); + const [ wcpayGateways ] = getSplitGateways( + [ wcpay, cod, paypal ], + 'US', + true + ); expect( wcpayGateways ).toEqual( [ wcpay ] ); } ); @@ -62,7 +66,7 @@ describe( 'getSplitGateways()', () => { } ); it( 'Excludes enabled gateways', () => { - const [ , , additionalGateways ] = getSplitGateways( [ + const [ , , mainList ] = getSplitGateways( [ wcpay, cod, bacs, @@ -71,61 +75,57 @@ describe( 'getSplitGateways()', () => { enabled: true, }, ] ); - expect( additionalGateways ).toEqual( [] ); + expect( mainList ).toEqual( [] ); } ); - describe( 'Additional gateways with eligible WCPay', () => { + describe( 'Main gateway list with eligible WCPay', () => { it( 'Returns only "other" category gateways when WCPay or "other" category gateway isnt set up', () => { - const [ , , additionalGateways ] = getSplitGateways( + const [ , , mainList ] = getSplitGateways( [ wcpay, cod, bacs, paypal, stripe, klarna ], 'US', true, false ); - expect( additionalGateways ).toEqual( [ stripe ] ); + expect( mainList ).toEqual( [ stripe ] ); } ); it( 'Returns only "additional" category gateways when WCPay or "other" category gateway is set up', () => { - const [ , , additionalGateways ] = getSplitGateways( + const [ , , mainList ] = getSplitGateways( [ wcpay, cod, bacs, paypal, stripe, klarna ], 'US', true, true ); - expect( additionalGateways ).toEqual( [ stripe, klarna ] ); + expect( mainList ).toEqual( [ stripe, klarna ] ); } ); it( 'Returns "additional" category gateways in recommended order', () => { - const [ , , additionalGateways ] = getSplitGateways( + const [ , , mainList ] = getSplitGateways( [ wcpay, cod, bacs, amazonPay, paypal, stripe, klarna ], 'US', true, true ); - expect( additionalGateways ).toEqual( [ - stripe, - klarna, - amazonPay, - ] ); + expect( mainList ).toEqual( [ stripe, klarna, amazonPay ] ); } ); } ); - describe( 'Additional gateways with ineligible WCPay', () => { - it( 'Returns all gateways when "other" gateways isnt set up', () => { - const [ , , additionalGateways ] = getSplitGateways( + describe( 'Main gateway list with ineligible WCPay', () => { + it( 'Returns "other" gateways when no "other" gateway is setup', () => { + const [ , , mainList ] = getSplitGateways( [ wcpay, cod, bacs, paypal, stripe, klarna ], 'US', false, false ); - expect( additionalGateways ).toEqual( [ stripe, paypal, klarna ] ); + expect( mainList ).toEqual( [ stripe ] ); } ); it( 'Returns only "additional" category gateways when "other" gateways is set up', () => { - const [ , , additionalGateways ] = getSplitGateways( + const [ , , mainList ] = getSplitGateways( [ wcpay, cod, bacs, paypal, stripe, klarna ], 'US', false, true ); - expect( additionalGateways ).toEqual( [ stripe, klarna ] ); + expect( mainList ).toEqual( [ stripe, klarna ] ); } ); } ); } ); diff --git a/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/utils.js b/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/utils.js index 20cd338a4fa..0380a7270bc 100644 --- a/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/utils.js +++ b/plugins/woocommerce-admin/client/task-lists/fills/PaymentGatewaySuggestions/utils.js @@ -83,6 +83,15 @@ export const getIsWCPayOrOtherCategoryDoneSetup = ( return false; }; +/** + * Splits up gateways to WCPay, offline and main list. + * + * @param {Array} paymentGateways Payment gateway list. + * @param {string} countryCode Store country code. + * @param {boolean} isWCPaySupported Whether WCPay is supported in the store. + * @param {boolean} isWCPayOrOtherCategoryDoneSetup Whether WCPay or "other" category gateway is done setup. + * @return {Array} Array of [ WCPay, offline, main list ]. + */ export const getSplitGateways = ( paymentGateways, countryCode, @@ -93,34 +102,37 @@ export const getSplitGateways = ( .sort( comparePaymentGatewaysByPriority ) .reduce( ( all, gateway ) => { - const [ wcPay, offline, additional ] = all; + // mainList is the list of gateways that is shown in the payments task. + const [ wcPay, offline, mainList ] = all; - // WCPay is handled separately when not installed and configured - if ( - getIsGatewayWCPay( gateway ) && - ! ( gateway.installed && ! gateway.needsSetup ) - ) { - wcPay.push( gateway ); + if ( getIsGatewayWCPay( gateway ) ) { + if ( + isWCPaySupported && + ! ( gateway.installed && ! gateway.needsSetup ) + ) { + // WCPay is always shown when it's installed but not setup. + wcPay.push( gateway ); + } + // WCPay is ignored if it reaches here. } else if ( gateway.is_offline ) { + // Offline gateways are always shown. offline.push( gateway ); } else if ( gateway.enabled ) { // Enabled gateways should be ignored. } else if ( isWCPayOrOtherCategoryDoneSetup ) { - // If WCPay or "other" gateway is enabled in an WCPay-eligible country, only - // allow to list "additional" gateways. if ( getIsGatewayAdditionalCategory( gateway, countryCode ) ) { - additional.push( gateway ); + // If WCPay or "other" gateway is enabled, only + // allow to list "additional" gateways. + mainList.push( gateway ); } - } else if ( ! isWCPaySupported ) { - // When WCPay-ineligible, just show all gateways. - additional.push( gateway ); + // "other" gateways would be ignored here since we shouldn't promote competing gateways. } else if ( getIsGatewayOtherCategory( gateway, countryCode ) ) { - // When nothing is set up and eligible for WCPay, only show "other" gateways. - additional.push( gateway ); + // When no WCPay or "other" gateway is enabled. + mainList.push( gateway ); } return all; diff --git a/plugins/woocommerce/changelog/fix-payment-gateway-suggestions-logic b/plugins/woocommerce/changelog/fix-payment-gateway-suggestions-logic new file mode 100644 index 00000000000..676910d2d67 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-payment-gateway-suggestions-logic @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix payment gateway suggestions display logic, added IN to paypal gateway visible rule, updated tests, improved comments diff --git a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php index 0cb7149a86a..d8e2dbeb4b6 100644 --- a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php +++ b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php @@ -440,6 +440,7 @@ class DefaultPaymentGateways { 'SG', 'CN', 'ID', + 'IN', ) ), self::get_rules_for_cbd( false ), @@ -595,7 +596,13 @@ class DefaultPaymentGateways { 'JP', ) ), - self::get_rules_for_selling_venues( array( 'brick-mortar', 'brick-mortar-other' ) ), + (object) array( + 'type' => 'or', + 'operands' => (object) array( + self::get_rules_for_selling_venues( array( 'brick-mortar', 'brick-mortar-other' ) ), + self::get_rules_selling_offline(), + ), + ), ), ), ), @@ -956,6 +963,29 @@ class DefaultPaymentGateways { ); } + /** + * Get rules for when selling offline for core profiler. + * + * @return object Rules to match. + */ + public static function get_rules_selling_offline() { + return (object) array( + 'type' => 'option', + 'transformers' => array( + (object) array( + 'use' => 'dot_notation', + 'arguments' => (object) array( + 'path' => 'selling_online_answer', + ), + ), + ), + 'option_name' => 'woocommerce_onboarding_profile', + 'operation' => 'contains', + 'value' => 'no_im_selling_offline', + 'default' => array(), + ); + } + /** * Get default rules for CBD based on given argument. *