diff --git a/src/includes/AlmaSettings.php b/src/includes/AlmaSettings.php index 2e4173fb..c59ddafb 100644 --- a/src/includes/AlmaSettings.php +++ b/src/includes/AlmaSettings.php @@ -71,6 +71,7 @@ */ class AlmaSettings { + const OPTIONS_KEY = 'wc_alma_settings'; // Generated by WooCommerce in WC_Settings_API::get_option_key(). /** @@ -192,7 +193,7 @@ class AlmaSettings { * Constructor. */ public function __construct() { - $this->logger = new AlmaLogger(); + $this->logger = new AlmaLogger(); $this->encryptor_helper = new EncryptorHelper(); $this->fee_plan_helper = new FeePlanHelper(); $this->internationalization_helper = new InternationalizationHelper(); @@ -229,7 +230,7 @@ public function get_settings() { $settings = (array) get_option( self::OPTIONS_KEY, array() ); if ( ! empty( $settings['allowed_fee_plans'] ) && ! is_array( $settings['allowed_fee_plans'] ) ) { - $settings['allowed_fee_plans'] = unserialize( $settings['allowed_fee_plans'] ); // phpcs:ignore + $settings['allowed_fee_plans'] = unserialize($settings['allowed_fee_plans']); // phpcs:ignore } return array_merge( $this->settings_helper->default_settings(), $settings ); @@ -418,7 +419,11 @@ public function is_pnx_plus_4( $fee_plan ) { */ public function has_pay_now() { foreach ( $this->get_enabled_plans_definitions() as $plan_definition ) { - if ( 1 === $plan_definition['installments_count'] ) { + if ( + 1 === $plan_definition['installments_count'] && + 0 === $plan_definition['deferred_days'] && + 0 === $plan_definition['deferred_months'] + ) { return true; } } @@ -495,7 +500,7 @@ public function get_description( $payment_method, $is_blocks = false ) { * @return bool */ public function is_test() { - return $this->get_environment() === 'test'; + return $this->get_environment() === 'test'; } /** @@ -504,7 +509,7 @@ public function is_test() { * @return string */ public function get_environment() { - return 'live' === $this->environment ? 'live' : 'test'; + return 'live' === $this->environment ? 'live' : 'test'; } /** @@ -535,7 +540,7 @@ public function get_active_api_key() { * @return bool */ public function is_live() { - return $this->get_environment() === 'live'; + return $this->get_environment() === 'live'; } /** @@ -818,7 +823,6 @@ public function get_active_merchant_id() { * @throws ParamsError Params exceptions. */ public function get_alma_merchant_id() { - $this->get_alma_client(); if ( ! empty( $this->alma_client ) ) { @@ -890,7 +894,7 @@ function ( $fee_plan ) { } ); - $this->settings['allowed_fee_plans'] = serialize( $this->allowed_fee_plans ); // phpcs:ignore + $this->settings['allowed_fee_plans'] = serialize($this->allowed_fee_plans); // phpcs:ignore foreach ( $this->allowed_fee_plans as $fee_plan ) { $plan_key = $fee_plan->getPlanKey(); @@ -1050,12 +1054,12 @@ public function should_display_plan( $plan_key, $gateway_id ) { case ConstantsHelper::GATEWAY_ID_PAY_NOW: case ConstantsHelper::GATEWAY_ID_IN_PAGE_PAY_NOW: $display_plan = $this->get_installments_count( $plan_key ) === 1 - && ( $this->get_deferred_days( $plan_key ) === 0 && $this->get_deferred_months( $plan_key ) === 0 ); + && ( $this->get_deferred_days( $plan_key ) === 0 && $this->get_deferred_months( $plan_key ) === 0 ); break; case ConstantsHelper::GATEWAY_ID_PAY_LATER: case ConstantsHelper::GATEWAY_ID_IN_PAGE_PAY_LATER: $display_plan = $this->get_installments_count( $plan_key ) === 1 - && ( $this->get_deferred_days( $plan_key ) !== 0 || $this->get_deferred_months( $plan_key ) !== 0 ); + && ( $this->get_deferred_days( $plan_key ) !== 0 || $this->get_deferred_months( $plan_key ) !== 0 ); break; case ConstantsHelper::GATEWAY_ID_IN_PAGE_MORE_THAN_FOUR: case ConstantsHelper::GATEWAY_ID_MORE_THAN_FOUR: diff --git a/src/tests/AlmaSettingsTest.php b/src/tests/AlmaSettingsTest.php new file mode 100644 index 00000000..dc74854d --- /dev/null +++ b/src/tests/AlmaSettingsTest.php @@ -0,0 +1,79 @@ +alma_settings = \Mockery::mock(AlmaSettings::class)->makePartial(); + } + + public function test_has_pay_now() + { + $this->alma_settings + ->shouldReceive('get_enabled_plans_definitions') + ->andReturn([ + "general_1_0_0" => [ + 'installments_count' => 1, + 'min_amount' => 100, + 'max_amount' => 1000, + 'deferred_days' => 0, + 'deferred_months' => 0 + ], + "general_3_0_0" => [ + 'installments_count' => 3, + 'min_amount' => 100, + 'max_amount' => 1000, + 'deferred_days' => 0, + 'deferred_months' => 0 + ], + ]); + $this->assertTrue($this->alma_settings->has_pay_now()); + } + + public function test_has_pay_now_with_invalid() + { + $this->alma_settings + ->shouldReceive('get_enabled_plans_definitions') + ->andReturn([ + "general_1_15_0" => [ + 'installments_count' => 1, + 'min_amount' => 100, + 'max_amount' => 1000, + 'deferred_days' => 15, + 'deferred_months' => 0 + ], + "general_1_0_1" => [ + 'installments_count' => 1, + 'min_amount' => 100, + 'max_amount' => 1000, + 'deferred_days' => 0, + 'deferred_months' => 1 + ], + "general_10_0_0" => [ + 'installments_count' => 10, + 'min_amount' => 100, + 'max_amount' => 1000, + 'deferred_days' => 0, + 'deferred_months' => 0 + ] + ]); + $this->assertFalse($this->alma_settings->has_pay_now()); + } + + public function tear_down() + { + \Mockery::close(); + } + +}