Skip to content

Commit

Permalink
Make credentials validation strict and prevent showing payfast in lis…
Browse files Browse the repository at this point in the history
…t if invalid credentials are entered.
  • Loading branch information
iamdharmesh committed Oct 21, 2024
1 parent e2631d7 commit 6b940b2
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions includes/class-wc-gateway-payfast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 '';
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
?>
<div class="notice notice-error is-dismissible">
<p><?php esc_html_e( 'Invalid Payfast credentials. Please verify and enter the correct details.', 'woocommerce-gateway-payfast' ); ?></p>
</div>
<?php
}
);
update_option( 'woocommerce_payfast_invalid_credentials', 'yes' );
}
}
}

0 comments on commit 6b940b2

Please sign in to comment.