From 6b940b226db64f3c295d6a44a2a5c460e73e599d Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Mon, 21 Oct 2024 12:50:01 +0530 Subject: [PATCH] Make credentials validation strict and prevent showing payfast in list if invalid credentials are entered. --- includes/class-wc-gateway-payfast.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/includes/class-wc-gateway-payfast.php b/includes/class-wc-gateway-payfast.php index b3c0e57..b92f496 100644 --- a/includes/class-wc-gateway-payfast.php +++ b/includes/class-wc-gateway-payfast.php @@ -196,7 +196,7 @@ public function __construct() { add_filter( 'nocache_headers', array( $this, 'no_store_cache_headers' ) ); // Validate the gateway credentials. - add_filter( 'update_option_woocommerce_payfast_settings', array( $this, 'validate_payfast_credentials' ), 10, 2 ); + add_action( 'update_option_woocommerce_payfast_settings', array( $this, 'validate_payfast_credentials' ), 10, 2 ); } /** @@ -342,6 +342,8 @@ public function check_requirements() { empty( $this->get_option( 'merchant_key' ) ) ? 'wc-gateway-payfast-error-missing-merchant-key' : null, // Check if user entered a pass phrase. empty( $this->get_option( 'pass_phrase' ) ) ? 'wc-gateway-payfast-error-missing-pass-phrase' : null, + // Check if payfast credentials are valid. + ( 'yes' === get_option( 'woocommerce_payfast_invalid_credentials' ) ) ? 'wc-gateway-payfast-error-invalid-credentials' : null, ); return array_filter( $errors ); @@ -1687,6 +1689,8 @@ public function get_error_message( $key ) { return esc_html__( 'You forgot to fill your merchant key.', 'woocommerce-gateway-payfast' ); case 'wc-gateway-payfast-error-missing-pass-phrase': return esc_html__( 'Payfast requires a passphrase to work.', 'woocommerce-gateway-payfast' ); + case 'wc-gateway-payfast-error-invalid-credentials': + return esc_html__( 'Invalid Payfast credentials. Please verify and enter the correct details.', 'woocommerce-gateway-payfast' ); default: return ''; } @@ -1844,6 +1848,9 @@ public function validate_payfast_credentials( $old_settings, $settings ) { $old_pass_phrase = $old_settings['pass_phrase'] ?? ''; $old_test_mode = $old_settings['testmode'] ?? 'no'; + // Clear the invalid credentials notice. + delete_option( 'woocommerce_payfast_invalid_credentials' ); + // Bail if no merchant ID or passphrase is set. if ( empty( $merchant_id ) || empty( $pass_phrase ) ) { return; @@ -1880,16 +1887,7 @@ public function validate_payfast_credentials( $old_settings, $settings ) { // Check Payfast server response if the response code is not 200 then show an error message. if ( 200 !== wp_remote_retrieve_response_code( $results ) ) { - add_action( - 'admin_notices', - function () { - ?> -
-

-
-