From 10581b092e8323202c6f0fb1a1b133f5ad046f5c Mon Sep 17 00:00:00 2001 From: andy Date: Wed, 27 Apr 2022 07:55:45 +0000 Subject: [PATCH] Check WHMCS version compatible with our plugin --- modules/gateways/xendit/hooks.php | 3 +- modules/gateways/xendit/lib/ActionBase.php | 30 +++++++++++++++++++ .../gateways/xendit/tests/WHMCSModuleTest.php | 23 ++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/modules/gateways/xendit/hooks.php b/modules/gateways/xendit/hooks.php index 35349af..d488c25 100644 --- a/modules/gateways/xendit/hooks.php +++ b/modules/gateways/xendit/hooks.php @@ -30,8 +30,9 @@ */ add_hook("ClientAreaPageCart", 1, function ($vars) { if ($vars['templatefile'] == 'viewcart') { + $actionBase = new ActionBase(); $activeCurrency = $vars['activeCurrency']->code; - if (!in_array($activeCurrency, ActionBase::ALLOW_CURRENCIES)) { + if (!$actionBase->validateCompatibilityVersion() || !in_array($activeCurrency, ActionBase::ALLOW_CURRENCIES)) { unset($vars['gateways']["xendit"]); } } diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index f864240..4b0c90d 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -13,6 +13,7 @@ class ActionBase { const ALLOW_CURRENCIES = ['IDR', 'PHP', 'USD']; + const WHMCS_MIN_VERSION_SUPPORT = 7.9; protected $moduleDomain = 'xendit'; protected $xenditRequest; @@ -44,6 +45,24 @@ public function getXenditConfig() */ public function createConfig() { + if (!$this->validateCompatibilityVersion()) { + return array( + 'FriendlyName' => array( + 'Type' => 'System', + 'Value' => 'Xendit Payment Gateway', + ), + 'description' => array( + 'FriendlyName' => '', + 'Type' => 'hidden', + 'Size' => '72', + 'Default' => '', + 'Description' => '
+Your WHMCS version not compatibility with Xendit Payment Gateway. See more +
', + ), + ); + } + return array( 'FriendlyName' => array( 'Type' => 'System', @@ -298,4 +317,15 @@ public function roundUpTotal(float $total): float { return ceil($total); } + + /*** + * @param string|null $currentVersion + * @return bool + */ + public function validateCompatibilityVersion(string $currentVersion = null): bool + { + global $CONFIG; + $version = !empty($currentVersion) ? $currentVersion : $CONFIG['Version']; + return version_compare($version, self::WHMCS_MIN_VERSION_SUPPORT, ">="); + } } diff --git a/modules/gateways/xendit/tests/WHMCSModuleTest.php b/modules/gateways/xendit/tests/WHMCSModuleTest.php index 62923ae..d8796c2 100644 --- a/modules/gateways/xendit/tests/WHMCSModuleTest.php +++ b/modules/gateways/xendit/tests/WHMCSModuleTest.php @@ -111,4 +111,27 @@ public function testCallbackPaidTotal() ); } } + + public function testVersionCompatibility() + { + $actionBase = new \Xendit\Lib\ActionBase(); + $versions = [ + "7.5" => false, + "7.6" => false, + "7.7.2" => false, + "7.8.1" => false, + "7.9" => true, + "8.0.1" => true, + "8.1.1" => true, + "8.2" => true, + "8.4" => true + ]; + foreach ($versions as $version => $expect) { + if ($expect) { + $this->assertTrue($actionBase->validateCompatibilityVersion($version)); + } else { + $this->assertFalse($actionBase->validateCompatibilityVersion($version)); + } + } + } }