Skip to content

Commit

Permalink
Multibanco redirect payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
moneimagento committed May 20, 2024
1 parent 42e268f commit cc92076
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 10 deletions.
28 changes: 26 additions & 2 deletions Controller/Payment/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\Result\Redirect as MagentoRedirect;
use Magento\Sales\Api\Data\OrderInterface;
use Monei\MoneiPayment\Service\Shared\GetMoneiPaymentCodesByMagentoPaymentCodeRedirect;

/**
* Monei payment redirect controller
Expand All @@ -35,6 +36,11 @@ class Redirect implements ActionInterface
*/
private $resultRedirectFactory;

/**
* @var GetMoneiPaymentCodesByMagentoPaymentCodeRedirect
*/
private $getMoneiPaymentCodesByMagentoPaymentCodeRedirect;

/**
* @param Session $checkoutSession
* @param CreatePaymentInterface $createPayment
Expand All @@ -43,11 +49,13 @@ class Redirect implements ActionInterface
public function __construct(
Session $checkoutSession,
CreatePaymentInterface $createPayment,
MagentoRedirect $resultRedirectFactory
MagentoRedirect $resultRedirectFactory,
GetMoneiPaymentCodesByMagentoPaymentCodeRedirect $getMoneiPaymentCodesByMagentoPaymentCodeRedirect
) {
$this->checkoutSession = $checkoutSession;
$this->createPayment = $createPayment;
$this->resultRedirectFactory = $resultRedirectFactory;
$this->getMoneiPaymentCodesByMagentoPaymentCodeRedirect = $getMoneiPaymentCodesByMagentoPaymentCodeRedirect;
}

/**
Expand All @@ -59,15 +67,21 @@ public function execute()
* @var $order OrderInterface
*/
$order = $this->checkoutSession->getLastRealOrder();

$data = [
"amount" => $order->getBaseGrandTotal() * 100,
"orderId" => (string) $order->getIncrementId(),
"currency" => $order->getBaseCurrencyCode(),
"customer" => $this->getCustomerDetails($order),
"billingDetails" => $this->getAddressDetails($order->getBillingAddress()),
"shippingDetails" => $this->getAddressDetails($order->getShippingAddress()),
"shippingDetails" => $this->getAddressDetails($order->getShippingAddress())
];

$allowedPaymentMethods = $this->getAllowedPaymentMethods($order);
if($allowedPaymentMethods){
$data['allowedPaymentMethods'] = $allowedPaymentMethods;
}

$result = $this->createPayment->execute($data);
if (!isset($result['error']) && isset($result['nextAction']['redirectUrl'])) {
$this->resultRedirectFactory->setUrl($result['nextAction']['redirectUrl']);
Expand Down Expand Up @@ -149,4 +163,14 @@ private function getAddressDetails($address)

return $moneiAddress;
}

private function getAllowedPaymentMethods(OrderInterface $order): array
{
$payment = $order->getPayment();
$paymentCode = $payment ? $payment->getMethod() : null;

return $paymentCode
? $this->getMoneiPaymentCodesByMagentoPaymentCodeRedirect->execute($paymentCode)
: [];
}
}
50 changes: 50 additions & 0 deletions Model/Config/Backend/EnableMultibanco.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 EnableMultibanco extends Enable
{
protected const PAYMENT_METHOD_CODE = Monei::MULTIBANCO_REDIRECT_CODE;

/**
* @return EnableBizum
*/
public function beforeSave(): EnableMultibanco
{
if (!$this->isPaymentAvailable() && $this->getValue()) {
$this->setValue("0");
// phpcs:disable
$this->messageManager->addNoticeMessage(
__("Multibanco 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;
}
}
24 changes: 16 additions & 8 deletions Model/Payment/Monei.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,28 @@ class Monei

public const GOOGLE_APPLE_CODE = 'monei_google_apple';

public const MULTIBANCO_REDIRECT_CODE = 'monei_multibanco_redirect';

public const PAYMENT_METHODS_MONEI = [
Monei::CODE,
Monei::CARD_CODE,
Monei::CC_VAULT_CODE,
Monei::BIZUM_CODE,
Monei::GOOGLE_APPLE_CODE
self::CODE,
self::CARD_CODE,
self::CC_VAULT_CODE,
self::BIZUM_CODE,
self::GOOGLE_APPLE_CODE,
self::MULTIBANCO_REDIRECT_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']
self::BIZUM_CODE => ['bizum'],
self::GOOGLE_APPLE_CODE => [self::MONEI_GOOGLE_CODE, self::MONEI_APPLE_CODE],
self::CARD_CODE => ['card'],
self::MULTIBANCO_REDIRECT_CODE => ['multibanco']
];

public const MAPPER_MAGENTO_MONEI_PAYMENT_CODE_REDIRECT = [
self::MULTIBANCO_REDIRECT_CODE => ['multibanco']
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

declare(strict_types=1);

namespace Monei\MoneiPayment\Service\Shared;

use Monei\MoneiPayment\Model\Payment\Monei;

class GetMoneiPaymentCodesByMagentoPaymentCodeRedirect
{

public function execute(string $magentoPaymentCode): array
{
return Monei::MAPPER_MAGENTO_MONEI_PAYMENT_CODE_REDIRECT[$magentoPaymentCode] ?? [];
}
}
40 changes: 40 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,46 @@
</depends>
</field>
<!-- -->
<!-- MULTIBANCO REDIRECT -->
<field id="heading7" translate="label" sortOrder="380" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Multibanco Redirect:</label>
<frontend_model>Magento\Config\Block\System\Config\Form\Field\Heading</frontend_model>
</field>
<field id="active_multibanco_redirect" translate="label comment" sortOrder="390" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable</label>
<comment>Select 'Yes' if you want to enable this payment method. Otherwise 'No'.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/monei_multibanco_redirect/active</config_path>
<backend_model>Monei\MoneiPayment\Model\Config\Backend\EnableMultibanco</backend_model>
</field>
<field id="allowspecific_multibanco_redirect" translate="label comment" sortOrder="400" type="allowspecific" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Payment from Applicable Countries</label>
<source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model>
<config_path>payment/monei_multibanco_redirect/allowspecific</config_path>
<depends>
<field id="active_multibanco_redirect">1</field>
</depends>
</field>
<field id="specificcountry_multibanco_redirect" translate="label comment" sortOrder="410" type="multiselect" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Payment from Specific Countries</label>
<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
<config_path>payment/monei_multibanco_redirect/specificcountry</config_path>
<can_be_empty>1</can_be_empty>
<depends>
<field id="active_multibanco_redirect">1</field>
<field id="allowspecific_multibanco_redirect">1</field>
</depends>
</field>
<field id="sort_order_multibanco_redirect" translate="label comment" sortOrder="420" type="text" showInDefault="1" showInWebsite="1">
<label>Sort Order</label>
<tooltip>In which order the payment method will be displayed in checkout</tooltip>
<config_path>payment/monei_multibanco_redirect/sort_order</config_path>
<frontend_class>validate-number</frontend_class>
<depends>
<field id="active_multibanco_redirect">1</field>
</depends>
</field>
<!-- -->
</group>
</section>
</system>
Expand Down
16 changes: 16 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@
<can_refund_partial_per_invoice>1</can_refund_partial_per_invoice>
<can_refund>1</can_refund>
</monei_google_apple>
<monei_multibanco_redirect>
<active>0</active>
<model>MoneiMultibancoRedirectPaymentFacade</model>
<title>Multibanco</title>
<allowspecific>1</allowspecific>
<specificcountry>PT</specificcountry>
<can_use_internal>1</can_use_internal>
<can_use_checkout>1</can_use_checkout>
<is_gateway>1</is_gateway>
<can_initialize>1</can_initialize>
<can_authorize>1</can_authorize>
<can_capture>1</can_capture>
<can_capture_partial>1</can_capture_partial>
<can_refund_partial_per_invoice>1</can_refund_partial_per_invoice>
<can_refund>1</can_refund>
</monei_multibanco_redirect>
</payment>
</default>
</config>
29 changes: 29 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,33 @@
</arguments>
</virtualType>
<!-- End Monei Google and Apple payment configuration -->

<!-- Configuration for Monei Multibanco redirect payment -->
<virtualType name="MoneiMultibancoRedirectPaymentValueHandlerPool" type="Magento\Payment\Gateway\Config\ValueHandlerPool">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="default" xsi:type="string">MoneiMultibancoRedirectPaymentValueHandler</item>
</argument>
</arguments>
</virtualType>
<virtualType name="MoneiMultibancoRedirectPaymentValueHandler" type="Magento\Payment\Gateway\Config\ConfigValueHandler">
<arguments>
<argument name="configInterface" xsi:type="object">MoneiMultibancoRedirectPaymentConfig</argument>
</arguments>
</virtualType>
<virtualType name="MoneiMultibancoRedirectPaymentConfig" type="Magento\Payment\Gateway\Config\Config">
<arguments>
<argument name="methodCode" xsi:type="const">Monei\MoneiPayment\Model\Payment\Monei::MULTIBANCO_REDIRECT_CODE</argument>
</arguments>
</virtualType>
<virtualType name="MoneiMultibancoRedirectPaymentFacade" type="Magento\Payment\Model\Method\Adapter">
<arguments>
<argument name="code" xsi:type="const">Monei\MoneiPayment\Model\Payment\Monei::MULTIBANCO_REDIRECT_CODE</argument>
<argument name="formBlockType" xsi:type="string">Magento\Payment\Block\Form</argument>
<argument name="infoBlockType" xsi:type="string">Monei\MoneiPayment\Block\Info\Monei</argument>
<argument name="valueHandlerPool" xsi:type="object">MoneiMultibancoRedirectPaymentValueHandlerPool</argument>
<argument name="commandPool" xsi:type="object">MoneiCommandPool</argument>
</arguments>
</virtualType>
<!-- End Monei Multibanco redirect payment configuration -->
</config>
1 change: 1 addition & 0 deletions i18n/es_ES.csv
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
"Bizum payment method is not available in your Monei account. Please enable it in your Monei account to use it.","El método de pago Bizum no está disponible en su cuenta de Monei. Por favor, habilítelo en su cuenta de Monei para poder usarlo."
"Card payment method is not available in your Monei account. Please enable it in your Monei account to use it.","El método de pago tarjeta no está disponible en su cuenta de Monei. Por favor, habilítelo en su cuenta de Monei para poder usarlo."
"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.","Los métodos de pago Google Pay y Apple Pay no están disponibles en su cuenta de Monei. Habilite al menos uno de ellos en su cuenta de Monei para poder usarlo."
"Multibanco payment method is not available in your Monei account. Please enable it in your Monei account to use it.","El método de pago de Multibanco no está disponible en su cuenta de Monei. Por favor, habilítelo en su cuenta de Monei para poder usarlo."
9 changes: 9 additions & 0 deletions view/frontend/layout/checkout_index_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
</item>
</item>
</item>
<item name="monei-multibanco-redirect-payment"
xsi:type="array">
<item name="component" xsi:type="string">Monei_MoneiPayment/js/view/payment/monei-multibanco-redirect</item>
<item name="methods" xsi:type="array">
<item name="monei_multibanco_redirect" xsi:type="array">
<item name="isBillingAddressRequired" xsi:type="boolean">true</item>
</item>
</item>
</item>
</item>
</item>
</item>
Expand Down
23 changes: 23 additions & 0 deletions view/frontend/web/js/view/payment/monei-multibanco-redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @author Monei Team
* @copyright Copyright © Monei (https://monei.com)
*/
define(
[
'uiComponent',
'Magento_Checkout/js/model/payment/renderer-list'
],
function (
Component,
rendererList
) {
'use strict';
rendererList.push(
{
type: 'monei_multibanco_redirect',
component: 'Monei_MoneiPayment/js/view/payment/method-renderer/monei-redirect-method'
}
);
return Component.extend({});
}
);

0 comments on commit cc92076

Please sign in to comment.