Fix payment gateway suggestions display logic (#38658)

* add: updated payment gateways for 2023 Q3

* Update payment gateway suggestions display logic, better comments and variable names, added core onboarding profiler option to support offline venue, added IN to paypal visibility rule

* Lint

* Update for better WCPay logic

* Remove accidental imports

* Added return object comment

* More lint

* Changelog

---------

Co-authored-by: rjchow <me@rjchow.com>
This commit is contained in:
Ilyas Foo 2023-06-16 23:46:37 +08:00 committed by GitHub
parent 9a49ff72da
commit 71a039852f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 36 deletions

View File

@ -97,6 +97,7 @@ describe( 'PaymentGatewaySuggestions', () => {
isResolving: false, isResolving: false,
getPaymentGateway: jest.fn(), getPaymentGateway: jest.fn(),
paymentGatewaySuggestions, paymentGatewaySuggestions,
countryCode: 'US',
installedPaymentGateways: [], installedPaymentGateways: [],
} ) ); } ) );
@ -116,6 +117,9 @@ describe( 'PaymentGatewaySuggestions', () => {
); );
expect( paymentTitles ).toEqual( [ expect( paymentTitles ).toEqual( [
'Stripe',
'PayPal Payments',
'Eway',
'Cash on delivery', 'Cash on delivery',
'Direct bank transfer', 'Direct bank transfer',
] ); ] );
@ -136,6 +140,7 @@ describe( 'PaymentGatewaySuggestions', () => {
isResolving: false, isResolving: false,
getPaymentGateway: jest.fn(), getPaymentGateway: jest.fn(),
paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay, paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay,
countryCode: 'US',
installedPaymentGateways: [], installedPaymentGateways: [],
} ) ); } ) );
@ -200,6 +205,7 @@ describe( 'PaymentGatewaySuggestions', () => {
isResolving: false, isResolving: false,
getPaymentGateway: jest.fn(), getPaymentGateway: jest.fn(),
paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay, paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay,
countryCode: 'US',
installedPaymentGateways: [ installedPaymentGateways: [
{ {
id: 'ppcp-gateway', id: 'ppcp-gateway',
@ -324,6 +330,7 @@ describe( 'PaymentGatewaySuggestions', () => {
isResolving: false, isResolving: false,
getPaymentGateway: jest.fn(), getPaymentGateway: jest.fn(),
paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay, paymentGatewaySuggestions: paymentGatewaySuggestionsWithoutWCPay,
countryCode: 'US',
installedPaymentGateways: [ installedPaymentGateways: [
{ {
id: 'ppcp-gateway', id: 'ppcp-gateway',

View File

@ -47,7 +47,11 @@ const amazonPay = {
describe( 'getSplitGateways()', () => { describe( 'getSplitGateways()', () => {
it( 'Returns WCPay gateways', () => { it( 'Returns WCPay gateways', () => {
const [ wcpayGateways ] = getSplitGateways( [ wcpay, cod, paypal ] ); const [ wcpayGateways ] = getSplitGateways(
[ wcpay, cod, paypal ],
'US',
true
);
expect( wcpayGateways ).toEqual( [ wcpay ] ); expect( wcpayGateways ).toEqual( [ wcpay ] );
} ); } );
@ -62,7 +66,7 @@ describe( 'getSplitGateways()', () => {
} ); } );
it( 'Excludes enabled gateways', () => { it( 'Excludes enabled gateways', () => {
const [ , , additionalGateways ] = getSplitGateways( [ const [ , , mainList ] = getSplitGateways( [
wcpay, wcpay,
cod, cod,
bacs, bacs,
@ -71,61 +75,57 @@ describe( 'getSplitGateways()', () => {
enabled: true, 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', () => { 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 ], [ wcpay, cod, bacs, paypal, stripe, klarna ],
'US', 'US',
true, true,
false false
); );
expect( additionalGateways ).toEqual( [ stripe ] ); expect( mainList ).toEqual( [ stripe ] );
} ); } );
it( 'Returns only "additional" category gateways when WCPay or "other" category gateway is set up', () => { 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 ], [ wcpay, cod, bacs, paypal, stripe, klarna ],
'US', 'US',
true, true,
true true
); );
expect( additionalGateways ).toEqual( [ stripe, klarna ] ); expect( mainList ).toEqual( [ stripe, klarna ] );
} ); } );
it( 'Returns "additional" category gateways in recommended order', () => { it( 'Returns "additional" category gateways in recommended order', () => {
const [ , , additionalGateways ] = getSplitGateways( const [ , , mainList ] = getSplitGateways(
[ wcpay, cod, bacs, amazonPay, paypal, stripe, klarna ], [ wcpay, cod, bacs, amazonPay, paypal, stripe, klarna ],
'US', 'US',
true, true,
true true
); );
expect( additionalGateways ).toEqual( [ expect( mainList ).toEqual( [ stripe, klarna, amazonPay ] );
stripe,
klarna,
amazonPay,
] );
} ); } );
} ); } );
describe( 'Additional gateways with ineligible WCPay', () => { describe( 'Main gateway list with ineligible WCPay', () => {
it( 'Returns all gateways when "other" gateways isnt set up', () => { it( 'Returns "other" gateways when no "other" gateway is setup', () => {
const [ , , additionalGateways ] = getSplitGateways( const [ , , mainList ] = getSplitGateways(
[ wcpay, cod, bacs, paypal, stripe, klarna ], [ wcpay, cod, bacs, paypal, stripe, klarna ],
'US', 'US',
false, false,
false false
); );
expect( additionalGateways ).toEqual( [ stripe, paypal, klarna ] ); expect( mainList ).toEqual( [ stripe ] );
} ); } );
it( 'Returns only "additional" category gateways when "other" gateways is set up', () => { it( 'Returns only "additional" category gateways when "other" gateways is set up', () => {
const [ , , additionalGateways ] = getSplitGateways( const [ , , mainList ] = getSplitGateways(
[ wcpay, cod, bacs, paypal, stripe, klarna ], [ wcpay, cod, bacs, paypal, stripe, klarna ],
'US', 'US',
false, false,
true true
); );
expect( additionalGateways ).toEqual( [ stripe, klarna ] ); expect( mainList ).toEqual( [ stripe, klarna ] );
} ); } );
} ); } );
} ); } );

View File

@ -83,6 +83,15 @@ export const getIsWCPayOrOtherCategoryDoneSetup = (
return false; 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 = ( export const getSplitGateways = (
paymentGateways, paymentGateways,
countryCode, countryCode,
@ -93,34 +102,37 @@ export const getSplitGateways = (
.sort( comparePaymentGatewaysByPriority ) .sort( comparePaymentGatewaysByPriority )
.reduce( .reduce(
( all, gateway ) => { ( 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 ) ) {
if ( if (
getIsGatewayWCPay( gateway ) && isWCPaySupported &&
! ( gateway.installed && ! gateway.needsSetup ) ! ( gateway.installed && ! gateway.needsSetup )
) { ) {
// WCPay is always shown when it's installed but not setup.
wcPay.push( gateway ); wcPay.push( gateway );
}
// WCPay is ignored if it reaches here.
} else if ( gateway.is_offline ) { } else if ( gateway.is_offline ) {
// Offline gateways are always shown.
offline.push( gateway ); offline.push( gateway );
} else if ( gateway.enabled ) { } else if ( gateway.enabled ) {
// Enabled gateways should be ignored. // Enabled gateways should be ignored.
} else if ( isWCPayOrOtherCategoryDoneSetup ) { } else if ( isWCPayOrOtherCategoryDoneSetup ) {
// If WCPay or "other" gateway is enabled in an WCPay-eligible country, only
// allow to list "additional" gateways.
if ( if (
getIsGatewayAdditionalCategory( gateway, countryCode ) 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 ) { // "other" gateways would be ignored here since we shouldn't promote competing gateways.
// When WCPay-ineligible, just show all gateways.
additional.push( gateway );
} else if ( } else if (
getIsGatewayOtherCategory( gateway, countryCode ) getIsGatewayOtherCategory( gateway, countryCode )
) { ) {
// When nothing is set up and eligible for WCPay, only show "other" gateways. // When no WCPay or "other" gateway is enabled.
additional.push( gateway ); mainList.push( gateway );
} }
return all; return all;

View File

@ -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

View File

@ -440,6 +440,7 @@ class DefaultPaymentGateways {
'SG', 'SG',
'CN', 'CN',
'ID', 'ID',
'IN',
) )
), ),
self::get_rules_for_cbd( false ), self::get_rules_for_cbd( false ),
@ -595,7 +596,13 @@ class DefaultPaymentGateways {
'JP', 'JP',
) )
), ),
(object) array(
'type' => 'or',
'operands' => (object) array(
self::get_rules_for_selling_venues( array( 'brick-mortar', 'brick-mortar-other' ) ), 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. * Get default rules for CBD based on given argument.
* *