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

feat: Handle configuration module to save and display the form #608

Open
wants to merge 11 commits into
base: feature/ecom-2150-ps-refactor-controller-getcontent
Choose a base branch
from
Open
621 changes: 40 additions & 581 deletions alma/controllers/hook/GetContentHookController.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
* @license https://opensource.org/licenses/MIT The MIT License
*/

namespace Alma\PrestaShop\Tests\Unit\Services;
namespace Alma\PrestaShop\Exceptions;

use PHPUnit\Framework\TestCase;
if (!defined('_PS_VERSION_')) {
exit;
}

class PaymentServiceTest extends TestCase
class AlmaFormConfigurationException extends AlmaException
{
public function testCreatePaymentOptions()
{
}
}
7 changes: 7 additions & 0 deletions alma/exceptions/PnxFormException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Alma\PrestaShop\Exceptions;

class PnxFormException extends AlmaException
{
}
4 changes: 3 additions & 1 deletion alma/lib/Builders/Helpers/FeePlanHelperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function getInstance()
{
return new FeePlanHelper(
$this->getSettingsHelper(),
$this->getEligibilityFactory()
$this->getEligibilityFactory(),
$this->getPriceHelper(),
$this->getToolsProxy()
);
}
}
11 changes: 5 additions & 6 deletions alma/lib/Factories/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Alma\API\Client;
use Alma\API\DependenciesError;
use Alma\API\ParamsError;
use Alma\PrestaShop\Helpers\SettingsHelper;
use Alma\PrestaShop\Logger;

if (!defined('_PS_VERSION_')) {
Expand All @@ -44,11 +43,11 @@ class ClientFactory
/**
* @return \Alma\API\Client|null
*/
public function create()
public function create($apiKey, $mode)
{
try {
$this->alma = new Client(SettingsHelper::getActiveAPIKey(), [
'mode' => SettingsHelper::getActiveMode(),
$this->alma = new Client($apiKey, [
'mode' => $mode,
'logger' => Logger::instance(),
]);

Expand All @@ -70,10 +69,10 @@ public function create()
/**
* @return \Alma\API\Client|null
*/
public function get()
public function get($apiKey, $mode)
{
if (!$this->alma) {
$this->alma = $this->create();
$this->alma = $this->create($apiKey, $mode);
}

return $this->alma;
Expand Down
2 changes: 1 addition & 1 deletion alma/lib/Forms/PnxAdminFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function __construct(
$this->priceHelper = $priceHelperBuilder->getInstance();

if (!$clientModel) {
$clientModel = new ClientModel();
$clientModel = ClientModel::getInstance();
}
$this->clientModel = $clientModel;

Expand Down
2 changes: 1 addition & 1 deletion alma/lib/Helpers/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function getMerchant($alma = null)
*
* @throws \PrestaShopException
*/
protected function handleInsuranceFlag($merchant)
public function handleInsuranceFlag($merchant)
{
try {
$isAllowInsurance = $this->saveFeatureFlag(
Expand Down
109 changes: 106 additions & 3 deletions alma/lib/Helpers/FeePlanHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

namespace Alma\PrestaShop\Helpers;

use Alma\PrestaShop\Exceptions\PnxFormException;
use Alma\PrestaShop\Factories\EligibilityFactory;
use Alma\PrestaShop\Proxy\ToolsProxy;

if (!defined('_PS_VERSION_')) {
exit;
Expand All @@ -44,11 +46,25 @@ class FeePlanHelper
* @var EligibilityFactory
*/
protected $eligibilityFactory;

public function __construct($settingsHelper, $eligibilityFactory)
{
/**
* @var PriceHelper
*/
private $priceHelper;
/**
* @var ToolsProxy
*/
private $toolsProxy;

public function __construct(
$settingsHelper,
$eligibilityFactory,
$priceHelper,
$toolsProxy
) {
$this->settingsHelper = $settingsHelper;
$this->eligibilityFactory = $eligibilityFactory;
$this->priceHelper = $priceHelper;
$this->toolsProxy = $toolsProxy;
}

/**
Expand Down Expand Up @@ -108,4 +124,91 @@ public function getEligibleFeePlans($feePlans, $purchaseAmount)

return $activePlans;
}

/**
* @throws \Alma\PrestaShop\Exceptions\PnxFormException
*/
public function checkLimitsSaveFeePlans($feePlans)
{
foreach ($feePlans as $feePlan) {
$installment = $feePlan->installments_count;
$deferred_days = $feePlan->deferred_days;
$deferred_months = $feePlan->deferred_months;
$key = $this->settingsHelper->keyForFeePlan($feePlan);

if (1 != $installment && $this->settingsHelper->isDeferred($feePlan)) {
continue;
}

$min = $this->priceHelper->convertPriceToCents((int) $this->toolsProxy->getValue("ALMA_{$key}_MIN_AMOUNT"));
$max = $this->priceHelper->convertPriceToCents((int) $this->toolsProxy->getValue("ALMA_{$key}_MAX_AMOUNT"));
$limitMin = $this->priceHelper->convertPriceFromCents($feePlan->min_purchase_amount);
$limitMax = $this->priceHelper->convertPriceFromCents(min($max, $feePlan->max_purchase_amount));

$enablePlan = (bool) $this->toolsProxy->getValue("ALMA_{$key}_ENABLED_ON");
if ($enablePlan
&& !(
$min >= $feePlan->min_purchase_amount
&& $min <= min($max, $feePlan->max_purchase_amount)
)
) {
$message = sprintf(
'Minimum amount for %1$d-installment plan must be within %2$d and %3$d.',
$installment,
$limitMin,
$limitMax
);
if ($installment == 1 && $deferred_days > 0 && $deferred_months == 0) {
$message = sprintf(
'Minimum amount for deferred + %1$s days plan must be within %2$d and %3$d.',
$deferred_days,
$limitMin,
$limitMax
);
}
if ($installment == 1 && $deferred_days == 0 && $deferred_months > 0) {
$message = sprintf(
'Minimum amount for deferred + %1$s months plan must be within %2$d and %3$d.',
$deferred_months,
$limitMin,
$limitMax
);
}

throw new PnxFormException($message);
}

if ($enablePlan
&& !(
$max >= $min
&& $max <= $feePlan->max_purchase_amount
)
) {
$message = sprintf(
'Maximum amount for %1$d-installment plan must be within %2$d and %3$d.',
$installment,
$limitMin,
$limitMax
);
if ($installment == 1 && $deferred_days > 0 && $deferred_months == 0) {
$message = sprintf(
'Maximum amount for deferred + %1$s days plan must be within %2$d and %3$d.',
$deferred_days,
$limitMin,
$limitMax
);
}
if ($installment == 1 && $deferred_days == 0 && $deferred_months > 0) {
$message = sprintf(
'Maximum amount for deferred + %1$s months plan must be within %2$d and %3$d.',
$deferred_months,
$limitMin,
$this->priceHelper->convertPriceFromCents($feePlan->max_purchase_amount)
);
}

throw new PnxFormException($message);
}
}
}
}
3 changes: 2 additions & 1 deletion alma/lib/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ public static function isShareOfCheckoutNoAnswered()
}

/**
* Get true if the consent SoC isn't answered.
* @deprecated use isShareOfCheckoutNoAnswered in ShareOfCheckoutHelper
* Get true if the consent SoC isn't answered
*
* @return bool
*/
Expand Down
13 changes: 12 additions & 1 deletion alma/lib/Helpers/ShareOfCheckoutHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function handleCheckoutConsent($consentAttribute)
*
* @return void
*
* @throws \Exception
* @throws \Alma\PrestaShop\Exceptions\ShareOfCheckoutException
*/
public function resetShareOfCheckoutConsent()
{
Expand All @@ -402,6 +402,7 @@ public function resetShareOfCheckoutConsent()
}
} catch (ShareOfCheckoutException $e) {
$this->context->smarty->assign('validation_error', 'soc_api_error');
throw new ShareOfCheckoutException($e->getMessage());
}
}

Expand Down Expand Up @@ -512,4 +513,14 @@ public function getEnabledDate()
{
return \Configuration::get(ShareOfCheckoutAdminFormBuilder::ALMA_SHARE_OF_CHECKOUT_DATE);
}

/**
* Check if SoC State are answered with Yes or No. If state is null, return false
*
* @return bool
*/
public function isShareOfCheckoutAnswered()
{
return SettingsHelper::isShareOfCheckoutAnswered() === true;
}
}
Loading