From db56aba8017fd0b3a10a306cd7f9c26e57285520 Mon Sep 17 00:00:00 2001 From: apetrovici Date: Wed, 11 Aug 2021 13:49:48 +0300 Subject: [PATCH] TR-27: [WooCommerce]: credentials toggle --- assets/admin/js/globalpayments-admin.js | 2 +- ...ments-gateway-provider-for-woocommerce.php | 22 ++++++++----- src/Gateways/AbstractGateway.php | 3 +- src/Gateways/HeartlandGateway.php | 7 +++++ src/Gateways/TransitGateway.php | 31 ++++++------------- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/assets/admin/js/globalpayments-admin.js b/assets/admin/js/globalpayments-admin.js index 67095a9..8db6fee 100644 --- a/assets/admin/js/globalpayments-admin.js +++ b/assets/admin/js/globalpayments-admin.js @@ -25,7 +25,7 @@ * @returns {*|jQuery} */ isLiveMode: function() { - return $( '#woocommerce_' + this.id + '_is_production' ).is( ':checked' ); + return $( this.getLiveModeSelector() ).is( ':checked' ); }, /** diff --git a/globalpayments-gateway-provider-for-woocommerce.php b/globalpayments-gateway-provider-for-woocommerce.php index adaf5ae..4a1a30f 100644 --- a/globalpayments-gateway-provider-for-woocommerce.php +++ b/globalpayments-gateway-provider-for-woocommerce.php @@ -24,17 +24,13 @@ add_action( 'plugins_loaded', array( \GlobalPayments\WooCommercePaymentGatewayProvider\Plugin::class, 'init' ) ); } -function globalpayments_v110_v111( WP_Upgrader $wp_upgrader, $hook_extra ) { - if ( 'plugin' !== $hook_extra[ 'type' ] || plugin_basename( __FILE__ ) == $hook_extra[ 'plugins' ] ) { +function globalpayments_update_v110_v111( WP_Upgrader $wp_upgrader, $hook_extra ) { + if ( 'plugin' !== $hook_extra[ 'type' ] || ! in_array( plugin_basename( __FILE__ ), $hook_extra[ 'plugins' ] ) ) { return; } if ( 'update' === $hook_extra[ 'action' ] || 'install' === $hook_extra[ 'action' ] ) { $current_plugin_version = get_option( 'woocommerce_globalpayments_version' ); if ( ! empty( $current_plugin_version ) ) { - if ( version_compare( \GlobalPayments\WooCommercePaymentGatewayProvider\Plugin::VERSION, $current_plugin_version, '>') ) { - delete_option( 'woocommerce_globalpayments_version' ); - update_option( 'woocommerce_globalpayments_version', \GlobalPayments\WooCommercePaymentGatewayProvider\Plugin::VERSION ); - } return; } $globalpayments_keys = [ @@ -68,7 +64,7 @@ function globalpayments_v110_v111( WP_Upgrader $wp_upgrader, $hook_extra ) { || 'globalpayments_heartland' !== $gateway_id && ! isset( $settings['is_production'] ) || isset( $settings['is_production'] ) && ! wc_string_to_bool( $settings['is_production'] ) ) { foreach ( $gateway_keys as $gateway_key ) { - if ( isset( $settings[$gateway_key] ) ) { + if ( ! empty( $settings[$gateway_key] ) ) { $settings['sandbox_' . $gateway_key] = $settings[$gateway_key]; $settings[$gateway_key] = ''; } @@ -76,7 +72,17 @@ function globalpayments_v110_v111( WP_Upgrader $wp_upgrader, $hook_extra ) { update_option( 'woocommerce_' . $gateway_id . '_settings', $settings ); } } + } +} +add_action( 'upgrader_process_complete', 'globalpayments_update_v110_v111', 9, 2 ); + +function globalpayments_update_plugin_version( WP_Upgrader $wp_upgrader, $hook_extra ) { + if ( 'plugin' !== $hook_extra[ 'type' ] || ! in_array( plugin_basename( __FILE__ ), $hook_extra[ 'plugins' ] ) ) { + return; + } + if ( 'update' === $hook_extra[ 'action' ] || 'install' === $hook_extra[ 'action' ] ) { + delete_option( 'woocommerce_globalpayments_version' ); update_option( 'woocommerce_globalpayments_version', \GlobalPayments\WooCommercePaymentGatewayProvider\Plugin::VERSION ); } } -add_action( 'upgrader_process_complete', 'globalpayments_v110_v111', 10, 2 ); \ No newline at end of file +add_action( 'upgrader_process_complete', 'globalpayments_update_plugin_version', 10, 2 ); \ No newline at end of file diff --git a/src/Gateways/AbstractGateway.php b/src/Gateways/AbstractGateway.php index caedd23..fc62a5f 100644 --- a/src/Gateways/AbstractGateway.php +++ b/src/Gateways/AbstractGateway.php @@ -208,8 +208,7 @@ public function payment_fields() { * Adds environment indicator in sandbox/test mode. */ protected function environment_indicator() { - if ( isset( $this->is_production ) && ! $this->is_production - || isset( $this->public_key ) && false === strpos( $this->public_key, 'pkapi_prod_' ) ) { + if ( ! wc_string_to_bool( $this->is_production ) ) { echo sprintf( '
%s
', __( 'This page is currently in sandbox/test mode. Do not use real/active card numbers.', 'globalpayments-gateway-provider-for-woocommerce' ) ); diff --git a/src/Gateways/HeartlandGateway.php b/src/Gateways/HeartlandGateway.php index 2a5699d..d5f2efa 100644 --- a/src/Gateways/HeartlandGateway.php +++ b/src/Gateways/HeartlandGateway.php @@ -50,6 +50,13 @@ class HeartlandGateway extends AbstractGateway { */ public $sandbox_secret_key; + /** + * Should live payments be accepted + * + * @var bool + */ + public $is_production; + /** * Allows payment via Heartland Marketing Solutions (gift cards) * diff --git a/src/Gateways/TransitGateway.php b/src/Gateways/TransitGateway.php index 68948cf..5703ea7 100644 --- a/src/Gateways/TransitGateway.php +++ b/src/Gateways/TransitGateway.php @@ -218,29 +218,18 @@ public function process_admin_options() { $option_name = sprintf( 'woocommerce_%s_settings', $this->id ); $settings = get_option( $option_name ); - if ( $this->is_production ) { - $this->user_id = $settings['user_id']; - $this->password = $settings['password']; - - if ( empty( $this->transaction_key ) && ! empty( $this->user_id ) && ! empty( $this->password ) ) { - $transaction_key = $this->create_transaction_key(); - $settings['transaction_key'] = $transaction_key; + $prefix = ( wc_string_to_bool( $settings['is_production'] ) ) ? '' : 'sandbox_'; + if ( empty( $settings[$prefix . 'transaction_key'] ) && ! empty( $settings[$prefix . 'user_id'] ) && ! empty( $settings[$prefix . 'password'] ) ) { + try { + $settings[$prefix . 'transaction_key'] = $this->create_transaction_key(); + } catch ( \Exception $e ) { + add_action( 'admin_notices', function() { + echo '

' . __( 'Invalid MultiPass User ID or Password. Please try again.' ) . '

'; + }); } - - $settings['user_id'] = null; - $settings['password'] = null; - } else { - $this->sandbox_user_id = $settings['sandbox_user_id']; - $this->sandbox_password = $settings['sandbox_password']; - - if ( empty( $this->sandbox_transaction_key ) && ! empty( $this->sandbox_user_id ) && ! empty( $this->sandbox_password ) ) { - $sandbox_transaction_key = $this->create_transaction_key(); - $settings['sandbox_transaction_key'] = $sandbox_transaction_key; - } - - $settings['sandbox_user_id'] = null; - $settings['sandbox_password'] = null; } + $settings[$prefix . 'user_id'] = null; + $settings[$prefix . 'password'] = null; update_option( $option_name, $settings ); }