Skip to content

Commit

Permalink
Fix problems in checkout without data in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
moneimagento committed Dec 19, 2024
1 parent 8012adb commit 900fbd1
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 19 deletions.
25 changes: 25 additions & 0 deletions Api/Config/AllMoneiPaymentModuleConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

declare(strict_types=1);

namespace Monei\MoneiPayment\Api\Config;

/**
* Get payment method configuration interface.
*/
interface AllMoneiPaymentModuleConfigInterface
{

/**
* Check if any payment methods is enabled
*
* @param null $storeId
* @return bool
*/
public function isAnyPaymentEnabled($storeId = null): bool;
}
5 changes: 5 additions & 0 deletions Model/CheckoutConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use Monei\MoneiPayment\Api\Config\AllMoneiPaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiBizumPaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiCardPaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiGoogleApplePaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiPaymentModuleConfigInterface;
use Monei\MoneiPayment\Block\Monei\Customer\CardRenderer;
use Monei\MoneiPayment\Model\Config\AllMoneiPaymentModuleConfig;
use Monei\MoneiPayment\Model\Config\Source\Mode;
use Monei\MoneiPayment\Model\Payment\Monei;
use Monei\MoneiPayment\Service\Shared\IsEnabledApplePayInMoneiAccount;
Expand All @@ -40,6 +42,7 @@ class CheckoutConfigProvider implements ConfigProviderInterface

public function __construct(
UrlInterface $urlBuilder,
AllMoneiPaymentModuleConfigInterface $allMoneiPaymentModuleConfig,
MoneiPaymentModuleConfigInterface $moneiPaymentConfig,
MoneiCardPaymentModuleConfigInterface $moneiCardPaymentConfig,
MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentConfig,
Expand All @@ -49,6 +52,7 @@ public function __construct(
StoreManagerInterface $storeManager
)
{
$this->allMoneiPaymentModuleConfig = $allMoneiPaymentModuleConfig;
$this->moneiGoogleApplePaymentConfig = $moneiGoogleApplePaymentConfig;
$this->moneiBizumPaymentModuleConfig = $moneiBizumPaymentModuleConfig;
$this->moneiCardPaymentConfig = $moneiCardPaymentConfig;
Expand All @@ -65,6 +69,7 @@ public function getConfig(): array
return [
'moneiAccountId' => $this->moneiPaymentConfig->getAccountId($storeId),
'moneiApiKey' => $this->moneiPaymentConfig->getApiKey($storeId),
'moneiPaymentIsEnabled' => $this->allMoneiPaymentModuleConfig->isAnyPaymentEnabled($storeId),
'isMoneiTestMode' => $this->moneiPaymentConfig->getMode($storeId) === Mode::MODE_TEST,
'moneiLanguage' => $this->moneiPaymentConfig->getLanguage($storeId),
'payment' => [
Expand Down
49 changes: 49 additions & 0 deletions Model/Config/AllMoneiPaymentModuleConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

declare(strict_types=1);

namespace Monei\MoneiPayment\Model\Config;

use Monei\MoneiPayment\Api\Config\AllMoneiPaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiBizumPaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiCardPaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiGoogleApplePaymentModuleConfigInterface;
use Monei\MoneiPayment\Api\Config\MoneiPaymentModuleConfigInterface;

class AllMoneiPaymentModuleConfig implements AllMoneiPaymentModuleConfigInterface
{

private MoneiPaymentModuleConfigInterface $moneiPaymentModuleConfig;
private MoneiCardPaymentModuleConfigInterface $moneiCardPaymentModuleConfig;
private MoneiBizumPaymentModuleConfigInterface $moneiBizumPaymentModuleConfig;
private MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentModuleConfig;

public function __construct(
MoneiPaymentModuleConfigInterface $moneiPaymentModuleConfig,
MoneiCardPaymentModuleConfigInterface $moneiCardPaymentModuleConfig,
MoneiBizumPaymentModuleConfigInterface $moneiBizumPaymentModuleConfig,
MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentModuleConfig,
) {

$this->moneiPaymentModuleConfig = $moneiPaymentModuleConfig;
$this->moneiCardPaymentModuleConfig = $moneiCardPaymentModuleConfig;
$this->moneiBizumPaymentModuleConfig = $moneiBizumPaymentModuleConfig;
$this->moneiGoogleApplePaymentModuleConfig = $moneiGoogleApplePaymentModuleConfig;
}

/**
* @inheritDoc
*/
public function isAnyPaymentEnabled($storeId = null): bool
{
return (bool) $this->moneiPaymentModuleConfig->isEnabled($storeId)
|| (bool) $this->moneiCardPaymentModuleConfig->isEnabled($storeId)
|| (bool) $this->moneiBizumPaymentModuleConfig->isEnabled($storeId)
|| (bool) $this->moneiGoogleApplePaymentModuleConfig->isEnabled($storeId);
}
}
36 changes: 20 additions & 16 deletions Service/GetPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,29 @@ public function execute(): array
$storeId = (int) $this->storeManager->getStore()->getId();
$accountId = $this->registryAccountId->get() ?? $this->moduleConfig->getAccountId($storeId);

$client = $this->createClient();
if ($accountId) {
$client = $this->createClient();

$this->logger->debug('------------------ START GET PAYMENT METHODS REQUEST -----------------');
$this->logger->debug('Account id = ' . $accountId);
$this->logger->debug('------------------- END GET PAYMENT METHODS REQUEST ------------------');
$this->logger->debug('');
$this->logger->debug('------------------ START GET PAYMENT METHODS REQUEST -----------------');
$this->logger->debug('Account id = ' . $accountId);
$this->logger->debug('------------------- END GET PAYMENT METHODS REQUEST ------------------');
$this->logger->debug('');

$response = $client->get(
self::METHOD . '?accountId=' . $accountId . '&' . time(),
[
'headers' => $this->getHeaders(),
]
);
$response = $client->get(
self::METHOD . '?accountId=' . $accountId . '&' . time(),
[
'headers' => $this->getHeaders(),
]
);

$this->logger->debug('----------------- START GET PAYMENT METHODS RESPONSE -----------------');
$this->logger->debug((string) $response->getBody());
$this->logger->debug('------------------ END GET PAYMENT METHODS RESPONSE ------------------');
$this->logger->debug('');

$this->logger->debug('----------------- START GET PAYMENT METHODS RESPONSE -----------------');
$this->logger->debug((string) $response->getBody());
$this->logger->debug('------------------ END GET PAYMENT METHODS RESPONSE ------------------');
$this->logger->debug('');
return $this->serializer->unserialize($response->getBody());
}

return $this->serializer->unserialize($response->getBody());
return [];
}
}
10 changes: 9 additions & 1 deletion Service/Shared/IsEnabledApplePayInMoneiAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@

namespace Monei\MoneiPayment\Service\Shared;

use Monei\MoneiPayment\Api\Config\MoneiGoogleApplePaymentModuleConfigInterface;
use Monei\MoneiPayment\Model\Payment\Monei;

class IsEnabledApplePayInMoneiAccount
{
private MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentModuleConfig;
private GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods;

public function __construct(GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods)
public function __construct(
MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentModuleConfig,
GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods)
{
$this->moneiGoogleApplePaymentModuleConfig = $moneiGoogleApplePaymentModuleConfig;
$this->getAvailableMoneiPaymentMethods = $getAvailableMoneiPaymentMethods;
}


public function execute(): bool
{
if (!$this->moneiGoogleApplePaymentModuleConfig->isEnabled()) {
return false;
}
$availableMoneiPaymentMethods = $this->getAvailableMoneiPaymentMethods->execute();

return in_array(Monei::MONEI_APPLE_CODE, $availableMoneiPaymentMethods, true);
Expand Down
10 changes: 9 additions & 1 deletion Service/Shared/IsEnabledGooglePayInMoneiAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@

namespace Monei\MoneiPayment\Service\Shared;

use Monei\MoneiPayment\Api\Config\MoneiGoogleApplePaymentModuleConfigInterface;
use Monei\MoneiPayment\Model\Payment\Monei;

class IsEnabledGooglePayInMoneiAccount
{
private MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentModuleConfig;
private GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods;

public function __construct(GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods)
public function __construct(
MoneiGoogleApplePaymentModuleConfigInterface $moneiGoogleApplePaymentModuleConfig,
GetAvailableMoneiPaymentMethods $getAvailableMoneiPaymentMethods)
{
$this->getAvailableMoneiPaymentMethods = $getAvailableMoneiPaymentMethods;
$this->moneiGoogleApplePaymentModuleConfig = $moneiGoogleApplePaymentModuleConfig;
}


public function execute(): bool
{
if (!$this->moneiGoogleApplePaymentModuleConfig->isEnabled()) {
return false;
}
$availableMoneiPaymentMethods = $this->getAvailableMoneiPaymentMethods->execute();

return in_array(Monei::MONEI_GOOGLE_CODE, $availableMoneiPaymentMethods, true);
Expand Down
2 changes: 2 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Monei\MoneiPayment\Api\Config\AllMoneiPaymentModuleConfigInterface"
type="Monei\MoneiPayment\Model\Config\AllMoneiPaymentModuleConfig"/>
<preference for="Monei\MoneiPayment\Api\Config\MoneiPaymentModuleConfigInterface"
type="Monei\MoneiPayment\Model\Config\MoneiPaymentModuleConfig"/>
<preference for="Monei\MoneiPayment\Api\Config\MoneiCardPaymentModuleConfigInterface"
Expand Down
4 changes: 3 additions & 1 deletion view/frontend/web/js/view/payment-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define([

accountId: '',
apiKey: '',
isEnabled: false,

initialize: function () {
this._super();
Expand All @@ -25,7 +26,8 @@ define([
showErrorMessages: function () {
this.accountId = window.checkoutConfig.moneiAccountId;
this.apiKey = window.checkoutConfig.moneiApiKey;
if(!this.accountId || !this.apiKey) {
this.isEnabled = window.checkoutConfig.moneiPaymentIsEnabled;
if(this.isEnabled && (!this.accountId || !this.apiKey)) {
globalMessageList.addErrorMessage({
message: $.mage.__('Monei payment methods are not available. Please, check your Monei configuration.')
});
Expand Down

0 comments on commit 900fbd1

Please sign in to comment.