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,
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',

View File

@ -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 ] );
} );
} );
} );

View File

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

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',
'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.
*