From 4cc6644c8badfa07436016bb5ff00aff10225370 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Tue, 11 Apr 2023 14:13:59 +0800 Subject: [PATCH] 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 --- .../update-default-payment-gateway-priority | 4 ++ .../DefaultPaymentGateways.php | 50 ++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce/changelog/update-default-payment-gateway-priority diff --git a/plugins/woocommerce/changelog/update-default-payment-gateway-priority b/plugins/woocommerce/changelog/update-default-payment-gateway-priority new file mode 100644 index 00000000000..1313a944650 --- /dev/null +++ b/plugins/woocommerce/changelog/update-default-payment-gateway-priority @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Add default priority for countries that are not in the payment recommendation map diff --git a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php index fbe2de7ab01..9f96721ec96 100644 --- a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php +++ b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/DefaultPaymentGateways.php @@ -11,6 +11,38 @@ defined( 'ABSPATH' ) || exit; * Default Payment Gateways */ 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. * @@ -1126,9 +1158,9 @@ class DefaultPaymentGateways { '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 ] ) ) { - return null; + return self::get_default_recommendation_priority( $gateway_id ); } $index = array_search( $gateway_id, $recommendation_priority_map[ $country_code ], true ); @@ -1140,4 +1172,18 @@ class DefaultPaymentGateways { 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 ]; + } }