0

I am getting below error

Missing required argument $additionalChecks of Vendor\\QuoteGraphQl\\Model\\MethodList.

<?php
namespace Vendor\QuoteGraphQl\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\State;

/**
 * Methods List service class.
 *
 * @api
 * @since 100.0.2
 */
class MethodList extends \Magento\Payment\Model\MethodList
{
     /**
     * Configuration path for Payment from Applicable Countries (Afterpay)
     */
    const XML_PATH_PAYMENT_FROM_APPLICABLE_COUNTRIES = 'payment/afterpaypayovertime/allowspecific';

    /**
     * Configuration path for Payment from Specific Countries (Afterpay)
     */
    const XML_PATH_PAYMENT_FROM_SPECIFIC_COUNTRIES = 'payment/afterpaypayovertime/specificcountry';

    /**
     * Graphql area code
     */
    const AREA_CODE_GRAPHQL = 'graphql';

    /**
     * @var \Magento\Payment\Helper\Data
     * @deprecated 100.1.0 Do not use this property in case of inheritance.
     */
    protected $paymentHelper;

    /**
     * @var \Magento\Payment\Model\Checks\SpecificationFactory
     * @deprecated 100.2.0 Do not use this property in case of inheritance.
     */
    protected $methodSpecificationFactory;

    /**
     * @var \Magento\Payment\Api\PaymentMethodListInterface
     */
    private $paymentMethodList;

    /**
     * @var \Magento\Payment\Model\Method\InstanceFactory
     */
    private $paymentMethodInstanceFactory;

    /**
     * @var array
     */
    private $additionalChecks;

    /**
     * @var ScopeConfigInterface
     */
    private $config;

    /**
     * @var AppState
     */
    private $appState;

    /**
     * @param \Magento\Payment\Helper\Data $paymentHelper
     * @param Checks\SpecificationFactory  $specificationFactory
     * @param array                        $additionalChecks
     * @param ScopeConfigInterface         $config
     * @param AppState $appState
     */
    public function __construct(
        \Magento\Payment\Helper\Data $paymentHelper,
        \Magento\Payment\Model\Checks\SpecificationFactory $specificationFactory,
        array $additionalChecks = [],
        ScopeConfigInterface $config,
        State $appState
    ) {
        $this->paymentHelper = $paymentHelper;
        $this->methodSpecificationFactory = $specificationFactory;
        $this->additionalChecks = $additionalChecks;
        $this->config           = $config;
        $this->appState         = $appState;
    }

    /**
     * Returns all available payment methods for the given quote.
     *
     * @param \Magento\Quote\Api\Data\CartInterface $quote
     * @return \Magento\Payment\Model\MethodInterface[]
     */
    public function getAvailableMethods(\Magento\Quote\Api\Data\CartInterface $quote = null)
    {
        $store = $quote ? $quote->getStoreId() : null;
        $availableMethods = [];
        $checkAfterpayAvailability = $quote ? $this->checkAfterpayAvailability($quote) : false;
        foreach ($this->getPaymentMethodList()->getActiveList($store) as $method) {
            if($checkAfterpayAvailability && $method->getCode() == "afterpaypayovertime"){
                continue;
            }
            $methodInstance = $this->getPaymentMethodInstanceFactory()->create($method);
            if ($methodInstance->isAvailable($quote) && $this->_canUseMethod($methodInstance, $quote)) {
                $methodInstance->setInfoInstance($quote->getPayment());
                $availableMethods[] = $methodInstance;
            }
        }
        return $availableMethods;
    }

    /**
     * Check afterpay payment method allowed for country
     *
     * @return array
     */
    public function checkAfterpayAvailability($quote) 
    {
        $allowedSpecific =  $this->config->getValue(self::XML_PATH_PAYMENT_FROM_APPLICABLE_COUNTRIES, ScopeInterface::SCOPE_STORE);
        $allowedCountries =  explode(",", $this->config->getValue(self::XML_PATH_PAYMENT_FROM_SPECIFIC_COUNTRIES));
        $countryId = $quote->getShippingAddress()->getCountryId();
        if($allowedSpecific && $allowedCountries && $countryId && $this->appState->getAreaCode() == self::AREA_CODE_GRAPHQL) {
            $country_exist = array_intersect(array($countryId), $allowedCountries);
            if (!$country_exist){
                return true;
            }
        }
        return false;
    }

    /**
     * Check payment method model
     *
     * @param \Magento\Payment\Model\MethodInterface $method
     * @param \Magento\Quote\Api\Data\CartInterface $quote
     * @return bool
     */
    protected function _canUseMethod($method, \Magento\Quote\Api\Data\CartInterface $quote)
    {
        return $this->methodSpecificationFactory->create(
            array_merge(
                [
                    AbstractMethod::CHECK_USE_CHECKOUT,
                    AbstractMethod::CHECK_USE_FOR_COUNTRY,
                    AbstractMethod::CHECK_USE_FOR_CURRENCY,
                    AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
                ],
                $this->additionalChecks
            )
        )->isApplicable(
            $method,
            $quote
        );
    }

    /**
     * Get payment method list.
     *
     * @return \Magento\Payment\Api\PaymentMethodListInterface
     */
    private function getPaymentMethodList()
    {
        if ($this->paymentMethodList === null) {
            $this->paymentMethodList = ObjectManager::getInstance()->get(
                \Magento\Payment\Api\PaymentMethodListInterface::class
            );
        }
        return $this->paymentMethodList;
    }

    /**
     * Get payment method instance factory.
     *
     * @return \Magento\Payment\Model\Method\InstanceFactory
     */
    private function getPaymentMethodInstanceFactory()
    {
        if ($this->paymentMethodInstanceFactory === null) {
            $this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
                \Magento\Payment\Model\Method\InstanceFactory::class
            );
        }
        return $this->paymentMethodInstanceFactory;
    }
}
1
0

You can use after, before plugin methods.we can change the after changes parameter, return value of the original getAvailableMethods()

if you want to full changes return of this function then use around methods aroundGetAvailableMethods().

<?php


namespace StackExchanges\OnlyMagento\Plugin\Magento\Payment\Model;


class MethodListPlugin {
    /**
     * @var \Magento\Payment\Helper\Data
     * @deprecated 100.1.3 Do not use this property in case of inheritance.
     */
    protected $paymentHelper;

    /**
     * @var \Magento\Payment\Model\Checks\SpecificationFactory
     * @deprecated 100.2.0 Do not use this property in case of inheritance.
     */
    protected $methodSpecificationFactory;

    /**
     * @var \Magento\Payment\Api\PaymentMethodListInterface
     */
    private $paymentMethodList;

    /**
     * @var \Magento\Payment\Model\Method\InstanceFactory
     */
    private $paymentMethodInstanceFactory;

    /**
     * @param \Magento\Payment\Helper\Data $paymentHelper
     * @param Checks\SpecificationFactory $specificationFactory
     */
    public function __construct(
        \Magento\Payment\Helper\Data $paymentHelper,
        \Magento\Payment\Model\Checks\SpecificationFactory $specificationFactory
    ) {
        $this->paymentHelper = $paymentHelper;
        $this->methodSpecificationFactory = $specificationFactory;
    }
    public function aroundGetAvailableMethods(
        \Magento\Payment\Model\MethodList $subject,
        \Closure $proceed,    
        \Magento\Quote\Api\Data\CartInterface $quote = null
    )
    {
        /*
         * if want to origina function then use 
         *  return $proceed($quote);
         */

        $store = $quote ? $quote->getStoreId() : null;
        $availableMethods = [];

        foreach ($this->getPaymentMethodList()->getActiveList($store) as $method) {
            $methodInstance = $this->getPaymentMethodInstanceFactory()->create($method);
            if ($methodInstance->isAvailable($quote) && $this->_canUseMethod($methodInstance, $quote)) {
                $methodInstance->setInfoInstance($quote->getPayment());
                $availableMethods[] = $methodInstance;
            }
        }
        /**
         * return of $availableMethod mean you have override 
         *  original function fully
         * 
         */
        return $availableMethods;        
    }
   /**
     * Check payment method model
     *
     * @param \Magento\Payment\Model\MethodInterface $method
     * @param \Magento\Quote\Api\Data\CartInterface $quote
     * @return bool
     */
    protected function _canUseMethod($method, \Magento\Quote\Api\Data\CartInterface $quote)
    {
        return $this->methodSpecificationFactory->create(
            [
                AbstractMethod::CHECK_USE_CHECKOUT,
                AbstractMethod::CHECK_USE_FOR_COUNTRY,
                AbstractMethod::CHECK_USE_FOR_CURRENCY,
                AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
            ]
        )->isApplicable(
            $method,
            $quote
        );
    }

    /**
     * Get payment method list.
     *
     * @return \Magento\Payment\Api\PaymentMethodListInterface
     */
    private function getPaymentMethodList()
    {
        if ($this->paymentMethodList === null) {
            $this->paymentMethodList = ObjectManager::getInstance()->get(
                \Magento\Payment\Api\PaymentMethodListInterface::class
            );
        }
        return $this->paymentMethodList;
    }

    /**
     * Get payment method instance factory.
     *
     * @return \Magento\Payment\Model\Method\InstanceFactory
     */
    private function getPaymentMethodInstanceFactory()
    {
        if ($this->paymentMethodInstanceFactory === null) {
            $this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
                \Magento\Payment\Model\Method\InstanceFactory::class
            );
        }
        return $this->paymentMethodInstanceFactory;
    }    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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