Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix has pay now method #306

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/includes/AlmaSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
*/
class AlmaSettings {


const OPTIONS_KEY = 'wc_alma_settings'; // Generated by WooCommerce in WC_Settings_API::get_option_key().

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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';
}

/**
Expand All @@ -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';
}

/**
Expand Down Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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:
Expand Down
79 changes: 79 additions & 0 deletions src/tests/AlmaSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Alma\Woocommerce\Tests;

use Alma\Woocommerce\AlmaSettings;
use WP_UnitTestCase;


class AlmaSettingsTest extends WP_UnitTestCase
{
/**
* @var mixed
*/
private $alma_settings;

public function set_up()
{
$this->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();
}

}
Loading