Skip to content

Commit

Permalink
Show available methods by account monei. Validate agreements for Bizu…
Browse files Browse the repository at this point in the history
…m and Google,Apple Pay
  • Loading branch information
moneimagento committed Apr 26, 2024
1 parent 2b664a6 commit 65477d2
Show file tree
Hide file tree
Showing 21 changed files with 597 additions and 26 deletions.
23 changes: 23 additions & 0 deletions Api/Service/GetPaymentMethodsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @author Monei Team
* @copyright Copyright © Monei (https://monei.com)
*/

declare(strict_types=1);

namespace Monei\MoneiPayment\Api\Service;

/**
* Monei get payment REST integration service interface.
*/
interface GetPaymentMethodsInterface
{
/**
* Service execute method
*
* @return array
*/
public function execute(): array;
}
10 changes: 10 additions & 0 deletions Model/CheckoutConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Monei\MoneiPayment\Api\Config\MoneiPaymentModuleConfigInterface;
use Monei\MoneiPayment\Block\Monei\Customer\CardRenderer;
use Monei\MoneiPayment\Model\Payment\Monei;
use Monei\MoneiPayment\Service\Shared\IsEnabledApplePayInMoneiAccount;
use Monei\MoneiPayment\Service\Shared\IsEnabledGooglePayInMoneiAccount;

/**
* Provides config data for payment.
Expand All @@ -30,17 +32,23 @@ class CheckoutConfigProvider implements ConfigProviderInterface
private MoneiCardPaymentModuleConfigInterface $moneiCardPaymentConfig;
private MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentConfig;
private StoreManagerInterface $storeManager;
private IsEnabledGooglePayInMoneiAccount $isEnabledGooglePayInMoneiAccount;
private IsEnabledApplePayInMoneiAccount $isEnabledApplePayInMoneiAccount;

public function __construct(
UrlInterface $urlBuilder,
MoneiPaymentModuleConfigInterface $moneiPaymentConfig,
MoneiCardPaymentModuleConfigInterface $moneiCardPaymentConfig,
MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentConfig,
IsEnabledGooglePayInMoneiAccount $isEnabledGooglePayInMoneiAccount,
IsEnabledApplePayInMoneiAccount $isEnabledApplePayInMoneiAccount,
StoreManagerInterface $storeManager
)
{
$this->moneiGoogleApplePaymentConfig = $moneiGoogleApplePaymentConfig;
$this->moneiCardPaymentConfig = $moneiCardPaymentConfig;
$this->isEnabledGooglePayInMoneiAccount = $isEnabledGooglePayInMoneiAccount;
$this->isEnabledApplePayInMoneiAccount = $isEnabledApplePayInMoneiAccount;
$this->moneiPaymentConfig = $moneiPaymentConfig;
$this->urlBuilder = $urlBuilder;
$this->storeManager = $storeManager;
Expand Down Expand Up @@ -87,6 +95,8 @@ public function getConfig(): array
'accountId' => $this->moneiPaymentConfig->getAccountId($this->getStoreId())
],
Monei::GOOGLE_APPLE_CODE => [
'isEnabledGooglePay' => $this->isEnabledGooglePayInMoneiAccount->execute(),
'isEnabledApplePay' => $this->isEnabledApplePayInMoneiAccount->execute(),
'googleTitle' => $this->moneiGoogleApplePaymentConfig->getGoogleTitle($this->getStoreId()),
'appleTitle' => $this->moneiGoogleApplePaymentConfig->getAppleTitle($this->getStoreId()),
'redirectUrl' => $this->urlBuilder->getUrl('monei/payment/redirect'),
Expand Down
75 changes: 75 additions & 0 deletions Model/Config/Backend/Enable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* @author Monei Team
* @copyright Copyright © Monei (https://monei.com)
*/

declare(strict_types=1);

namespace Monei\MoneiPayment\Model\Config\Backend;

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Monei\MoneiPayment\Api\Service\GetPaymentMethodsInterface;
use Monei\MoneiPayment\Model\Payment\Monei;
use Monei\MoneiPayment\Service\Shared\GetAvailableMoneiPaymentMethods;
use Monei\MoneiPayment\Service\Shared\GetMoneiPaymentCodesByMagentoPaymentCode;

/**
* Get Monei payment method configuration class.
*/
class Enable extends Value
{
private GetMoneiPaymentCodesByMagentoPaymentCode $getMoneiPaymentCodesByMagentoPaymentCode;
protected ManagerInterface $messageManager;
private GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods;

/**
* Enable constructor.
*
* @param GetPaymentMethodsInterface $getPaymentMethodsService
* @param GetMoneiPaymentCodesByMagentoPaymentCode $getMoneiPaymentCodesByMagentoPaymentCode
* @param Context $context
* @param Registry $registry
* @param ScopeConfigInterface $config
* @param TypeListInterface $cacheTypeList
* @param ManagerInterface $messageManager
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods,
GetMoneiPaymentCodesByMagentoPaymentCode $getMoneiPaymentCodesByMagentoPaymentCode,
Context $context,
Registry $registry,
ScopeConfigInterface $config,
TypeListInterface $cacheTypeList,
ManagerInterface $messageManager,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = [])
{
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
$this->getAvailableMoneiPaymentMethods = $getAvailableMoneiPaymentMethods;
$this->getMoneiPaymentCodesByMagentoPaymentCode = $getMoneiPaymentCodesByMagentoPaymentCode;
$this->messageManager = $messageManager;
}

protected function getAvailablePaymentMethods(): array
{
return $this->getAvailableMoneiPaymentMethods->execute();
}

protected function getMoneiPaymentCodesByMagentoPaymentCode(string $magentoPaymentCode): array
{
return $this->getMoneiPaymentCodesByMagentoPaymentCode->execute($magentoPaymentCode);
}
}
50 changes: 50 additions & 0 deletions Model/Config/Backend/EnableBizum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @author Monei Team
* @copyright Copyright © Monei (https://monei.com)
*/

declare(strict_types=1);

namespace Monei\MoneiPayment\Model\Config\Backend;

use Monei\MoneiPayment\Model\Payment\Monei;

class EnableBizum extends Enable
{
protected const PAYMENT_METHOD_CODE = Monei::BIZUM_CODE;

/**
* @return EnableBizum
*/
public function beforeSave(): EnableBizum
{
if (!$this->isPaymentAvailable() && $this->getValue()) {
$this->setValue("0");
// phpcs:disable
$this->messageManager->addNoticeMessage(
__("Bizum payment method is not available in your Monei account. Please enable it in your Monei account to use it."));
// phpcs:enable
}

return parent::beforeSave();
}

/**
* @return bool
*/
private function isPaymentAvailable(): bool
{
$availablePaymentMethods = $this->getAvailablePaymentMethods();
$moneiPaymentCodes = $this->getMoneiPaymentCodesByMagentoPaymentCode(self::PAYMENT_METHOD_CODE);

foreach ($moneiPaymentCodes as $moneiPaymentCode) {
if (in_array($moneiPaymentCode, $availablePaymentMethods, true)) {
return true;
}
}

return false;
}
}
50 changes: 50 additions & 0 deletions Model/Config/Backend/EnableCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @author Monei Team
* @copyright Copyright © Monei (https://monei.com)
*/

declare(strict_types=1);

namespace Monei\MoneiPayment\Model\Config\Backend;

use Monei\MoneiPayment\Model\Payment\Monei;

class EnableCard extends Enable
{
protected const PAYMENT_METHOD_CODE = Monei::CARD_CODE;

/**
* @return EnableBizum
*/
public function beforeSave(): EnableCard
{
if (!$this->isPaymentAvailable() && $this->getValue()) {
$this->setValue("0");
// phpcs:disable
$this->messageManager->addNoticeMessage(
__("Card payment method is not available in your Monei account. Please enable it in your Monei account to use it."));
// phpcs:enable
}

return parent::beforeSave();
}

/**
* @return bool
*/
private function isPaymentAvailable(): bool
{
$availablePaymentMethods = $this->getAvailablePaymentMethods();
$moneiPaymentCodes = $this->getMoneiPaymentCodesByMagentoPaymentCode(self::PAYMENT_METHOD_CODE);

foreach ($moneiPaymentCodes as $moneiPaymentCode) {
if (in_array($moneiPaymentCode, $availablePaymentMethods, true)) {
return true;
}
}

return false;
}
}
50 changes: 50 additions & 0 deletions Model/Config/Backend/EnableGoogleApplePay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @author Monei Team
* @copyright Copyright © Monei (https://monei.com)
*/

declare(strict_types=1);

namespace Monei\MoneiPayment\Model\Config\Backend;

use Monei\MoneiPayment\Model\Payment\Monei;

class EnableGoogleApplePay extends Enable
{
protected const PAYMENT_METHOD_CODE = Monei::GOOGLE_APPLE_CODE;

/**
* @return EnableBizum
*/
public function beforeSave(): EnableGoogleApplePay
{
if (!$this->isPaymentAvailable() && $this->getValue()) {
$this->setValue("0");
// phpcs:disable
$this->messageManager->addNoticeMessage(
__("Google Pay and Apple Pay payment methods are not available in your Monei account. Enable at least one of them in your Monei account to use it."));
// phpcs:enable
}

return parent::beforeSave();
}

/**
* @return bool
*/
private function isPaymentAvailable(): bool
{
$availablePaymentMethods = $this->getAvailablePaymentMethods();
$moneiPaymentCodes = $this->getMoneiPaymentCodesByMagentoPaymentCode(self::PAYMENT_METHOD_CODE);

foreach ($moneiPaymentCodes as $moneiPaymentCode) {
if (in_array($moneiPaymentCode, $availablePaymentMethods, true)) {
return true;
}
}

return false;
}
}
17 changes: 17 additions & 0 deletions Model/Payment/Monei.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,21 @@ class Monei
public const BIZUM_CODE = 'monei_bizum';

public const GOOGLE_APPLE_CODE = 'monei_google_apple';

public const PAYMENT_METHODS_MONEI = [
Monei::CODE,
Monei::CARD_CODE,
Monei::CC_VAULT_CODE,
Monei::BIZUM_CODE,
Monei::GOOGLE_APPLE_CODE
];

public const MONEI_GOOGLE_CODE = 'googlePay';
public const MONEI_APPLE_CODE = 'applePay';

public const MAPPER_MAGENTO_MONEI_PAYMENT_CODE = [
Monei::BIZUM_CODE => ['bizum'],
Monei::GOOGLE_APPLE_CODE => [self::MONEI_GOOGLE_CODE, self::MONEI_APPLE_CODE],
Monei::CARD_CODE => ['card']
];
}
Loading

0 comments on commit 65477d2

Please sign in to comment.