Add default priority for countries that don't exist in payment recommendation map (#37590)

* Add default priority for countries that are not in the payment recommendation map

* Add changelog

* Add doc

* Fix linting
This commit is contained in:
Chi-Hsuan Huang 2023-04-11 14:13:59 +08:00 committed by GitHub
parent 5d68259480
commit 4cc6644c8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Add default priority for countries that are not in the payment recommendation map

View File

@ -11,6 +11,38 @@ defined( 'ABSPATH' ) || exit;
* Default Payment Gateways * Default Payment Gateways
*/ */
class DefaultPaymentGateways { class DefaultPaymentGateways {
/**
* This is the default priority for countries that are not in the $recommendation_priority_map.
* Priority is used to determine which payment gateway to recommend first.
* The lower the number, the higher the priority.
*
* @var array
*/
private static $recommendation_priority = array(
'woocommerce_payments' => 1,
'woocommerce_payments:with-in-person-payments' => 1,
'woocommerce_payments:without-in-person-payments' => 1,
'stripe' => 2,
'woo-mercado-pago-custom' => 3,
// PayPal Payments.
'ppcp-gateway' => 4,
'mollie_wc_gateway_banktransfer' => 5,
'razorpay' => 5,
'payfast' => 5,
'payubiz' => 6,
'square_credit_card' => 6,
'klarna_payments' => 6,
// Klarna Checkout.
'kco' => 6,
'paystack' => 6,
'eway' => 7,
'amazon_payments_advanced' => 7,
'affirm' => 8,
'afterpay' => 9,
'zipmoney' => 10,
'payoneer-checkout' => 11,
);
/** /**
* Get default specs. * Get default specs.
* *
@ -1126,9 +1158,9 @@ class DefaultPaymentGateways {
'GH' => [ 'paystack', 'ppcp-gateway' ], 'GH' => [ 'paystack', 'ppcp-gateway' ],
); );
// If the country code is not in the list, return null. // If the country code is not in the list, return default priority.
if ( ! isset( $recommendation_priority_map[ $country_code ] ) ) { if ( ! isset( $recommendation_priority_map[ $country_code ] ) ) {
return null; return self::get_default_recommendation_priority( $gateway_id );
} }
$index = array_search( $gateway_id, $recommendation_priority_map[ $country_code ], true ); $index = array_search( $gateway_id, $recommendation_priority_map[ $country_code ], true );
@ -1140,4 +1172,18 @@ class DefaultPaymentGateways {
return $index; return $index;
} }
/**
* Get the default recommendation priority for a payment gateway.
* This is used when a country is not in the $recommendation_priority_map array.
*
* @param string $id Payment gateway id.
* @return int Priority.
*/
private static function get_default_recommendation_priority( $id ) {
if ( ! $id || ! array_key_exists( $id, self::$recommendation_priority ) ) {
return null;
}
return self::$recommendation_priority[ $id ];
}
} }