1

I have override "Magento\Checkout\Model\PaymentInformationManagement" file in custom module

I want to prevent some "afterpay" payment method to be displayed for some country.

So i need to override "getPaymentInformation" method. I have override it & customize method

/**
 * @inheritdoc
 */
public function getPaymentInformation($cartId)
{
    $quote = $this->_quoteFactory->create()->load($cartId);
    $countryId = $quote->getShippingAddress()->getCountryId();
    $paymentMethods = $this->paymentMethodManagement->getList($cartId); 
    /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
    foreach ($paymentMethods as $method) {
        print_r($method->getCode()); echo'</br>'; //getting method code here
    }
    exit;
    $paymentDetails = $this->paymentDetailsFactory->create();
    $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
    $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
    return $paymentDetails;
}

I am not sure how to remove "afterpay" payment object/data & pass it to $paymentDetails->setPaymentMethods();

I have checked further found that it ultimately fetches methods from Magento\Payment\Model\MethodList & getAvailableMethods

May be i can customize in that method for the same requirement ?

5
  • what you want to change in this file? Add more details please. Commented Jun 15, 2021 at 12:03
  • 1
    Hello @RohanHapani, thanks for the comment. I have added details to question, please have a look.
    – devhs
    Commented Jun 15, 2021 at 12:12
  • @RohanHapani, can you please tell me in which area i should put my di.xml override changes if i only want to exucute override file only for "Graphql" calls ?
    – devhs
    Commented Jun 15, 2021 at 12:42
  • @RohanHapani any thoughts on this ?
    – devhs
    Commented Jun 15, 2021 at 12:52
  • 2
    for graphql : app/code/Vendor/Module/etc/graphql/di.xml Commented Jun 15, 2021 at 13:19

1 Answer 1

2

You don't need to override any method. You can do with the use of observer.

Step 1:- Please create events.xml file under path PackageName\Module\Observer\PaymentMethodAvailable

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="payment_method_is_active">
        <observer name="custom_payment" instance="PackageName\Module\Observer\PaymentMethodAvailable" />
    </event>
</config>

Step 2:- Create file PaymentMethodAvailable.php file under path PackageName/Module/Observer

<?php
 
namespace PackageName\Module\Observer;
 
use Magento\Framework\Event\ObserverInterface;
 
class PaymentMethodAvailable implements ObserverInterface
{
    protected $cart;

    public function __construct(
      \Magento\Checkout\Model\Cart $cart    
    ) {
        $this->cart = $cart;
    }
    /**
     * payment_method_is_active event handler.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $shippingAddress = $this->cart->getQuote()->getShippingAddress();
        if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo" && isset($shippingAddress['country_id']) && $shippingAddress['country_id'] == 'ES'){
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
        }
    }
}

In above code i have prevent "check money order" payment method for country spain. You can do it according to your payment method and country id

Above code is tested in magento 2.4.0 and it is working perfectly fine.

6
  • Thanks for the answer, but i am working with graphql calls, So not use this would work with that. Can you confirm ?
    – devhs
    Commented Jun 16, 2021 at 13:13
  • Yes, it should work.
    – ZealousWeb
    Commented Jun 16, 2021 at 13:14
  • 1
    event observer calls everywhere with API as well as with Graphql
    – ZealousWeb
    Commented Jun 16, 2021 at 13:15
  • Okay & could you please tell me when this event will be executed ?
    – devhs
    Commented Jun 16, 2021 at 13:17
  • 1
    This will execute when the page load of checkout as well as when you set shipping information and click on next button on checkout page
    – ZealousWeb
    Commented Jun 16, 2021 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.