magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.45k stars 9.29k forks source link

Custom payment methods are always available when using new Payment Provider Gateway #33869

Open t-heuser opened 3 years ago

t-heuser commented 3 years ago

Preconditions (*)

  1. Magento 2.3.4
  2. A custom payment method created according to the instructions
  3. The payment method must have an availability validator which always returns false

Steps to reproduce (*)

  1. Get the available payment methods for a quote via MethodList::getAvailableMethods(). You can also use graphql as the resolver for the "available_payment_methods" field from a cart calls this method.

Expected result (*)

  1. The method should not be available as the default validator always returns false.

Actual result (*)

  1. The method is always available.

Cause of the problem

The cause of the problem is that Adapter::isAvailable() gets called from MethodList::getAvailableMethods(). The Adapter::isAvailable() method then tries to get the infoInstance as seen on line 290. If it's null it will not execute the availability validator from the validatorPool which would return false. The infoInstance is ALWAYS null in this case as the method instance gets freshly created from it's factory. The MethodList::getAvailableMethods() sets the infoInstance AFTER it calls the Adapter::isAvailable() method. So there is no way the validator gets called when using the new Payment Provider Gateway.

I guess this issue was never found as all of magentos payment methods are using the deprecated AbstractMethod class. I think this should be fixed asap as the method MethodList::getAvailableMethods() MUST work for all payment methods, especially if they're implemented the way they should be and not with deprecated classes.

Possible workarounds

  1. Create a patch for MethodList::getAvailableMethods() and move line 76 below line 74. This is definitly nothing I want to do as I have no idea about the side effects on other payment methods using the deprecated model.
  2. Overwrite the Adapter::isAvailable() method and manually call the validator.
  3. Add an observer for "payment_method_is_active" event. I will do this until this issue is fixed.

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @oneserv-heuser. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

t-heuser commented 3 years ago

I can confirm that this issue exists, but not on an vanilla instance because of the nature of the problem.

m2-assistant[bot] commented 3 years ago

Hi @engcom-Echo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

JeffersonTeixeira commented 3 years ago

I reported this issue before It was closed with no solution: https://github.com/magento/magento2/issues/30211

t-heuser commented 3 years ago

@JeffersonTeixeira The classical stale-bot treatment

How did you fix it? Did you use the fix @ksenia-zlotin provided?

t-heuser commented 3 years ago

For anyone stumbling across this, here is a temporary solution:

<?php

declare(strict_types=1);

namespace Oneserv\B2BInvoice\Model\Method;

use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Command\CommandManagerInterface;
use Magento\Payment\Gateway\Command\CommandPoolInterface;
use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
use Magento\Payment\Model\Method\Adapter;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote;
use Psr\Log\LoggerInterface;

/**
 * Class B2BInvoice
 */
class B2BInvoice extends Adapter
{
    private State $state;

    /**
     * B2BInvoice constructor.
     *
     * @param ManagerInterface $eventManager
     * @param ValueHandlerPoolInterface $valueHandlerPool
     * @param PaymentDataObjectFactory $paymentDataObjectFactory
     * @param string $code
     * @param string $formBlockType
     * @param string $infoBlockType
     * @param State $state
     * @param CommandPoolInterface|null $commandPool
     * @param ValidatorPoolInterface|null $validatorPool
     * @param CommandManagerInterface|null $commandExecutor
     * @param LoggerInterface|null $logger
     */
    public function __construct(
        ManagerInterface $eventManager,
        ValueHandlerPoolInterface $valueHandlerPool,
        PaymentDataObjectFactory $paymentDataObjectFactory,
        string $code,
        string $formBlockType,
        string $infoBlockType,
        State $state,
        CommandPoolInterface $commandPool = null,
        ValidatorPoolInterface $validatorPool = null,
        CommandManagerInterface $commandExecutor = null,
        LoggerInterface $logger = null
    ) {
        parent::__construct(
            $eventManager,
            $valueHandlerPool,
            $paymentDataObjectFactory,
            $code,
            $formBlockType,
            $infoBlockType,
            $commandPool,
            $validatorPool,
            $commandExecutor,
            $logger
        );
        $this->state = $state;
    }

    /**
     * @inheritdoc
     * This is a temporary workaround for https://github.com/magento/magento2/issues/33869.
     * It sets the info instance before the method gets executed. Otherwise, the validator doesn't get called
     * correctly.
     */
    public function isAvailable(CartInterface $quote = null)
    {
        if ($quote === null) {
            return parent::isAvailable($quote);
        }
        /** @var Quote $quote */
        /*
         * This is needed to avoid issues when creating a new order via adminhtml. There is an error as the quote has
         * empty data and somewhere deep in magento it causes an issue.
         */
        try {
            if (
                $this->state->getAreaCode() === Area::AREA_ADMINHTML &&
                $quote->getDataByKey(Quote::KEY_STORE_ID) === null
            ) {
                return parent::isAvailable($quote);
            }
        } catch (LocalizedException $exception) {
            return parent::isAvailable($quote);
        }

        $this->setInfoInstance($quote->getPayment());
        return parent::isAvailable($quote);
    }
}
m2-assistant[bot] commented 3 years ago

Hi @engcom-Lima. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Lima commented 2 years ago

Hi @oneserv-heuser,

Thank you for reporting the issue. Can you please provide more detailed 'Steps to reproduce' so that I can try to reproduce it ? If you can provide graphql steps to get the issue result, that would be helpful as well.

Also please update below information:

  1. Is this issue reproducible on 2.4-develop instance also or is it specific to 2.3.4 ?
  2. With which Payment Gateway you are currently facing this issue ?
t-heuser commented 2 years ago

Hi @engcom-Lima, I've created an example module for this problem. You can find it here: https://github.com/oneserv-heuser/magento2-33869. I've also added detailed steps to reproduce this issue there and also implemented a workaround for this bug.

vivekchoudhari commented 2 years ago

Probably this is not an issue, but the way checks for Payment Method need to be added is bit different?

The checks of Payment Method is also handled using the interface Magento\Payment\Model\Checks\SpecificationInterface and Magento\Payment\Model\Checks\SpecificationFactory

So we need to add a custom mapping and pass this as an argument in di.xml

So something like below

<type name="Magento\Payment\Model\Checks\SpecificationFactory">
        <arguments>
            <argument name="mapping" xsi:type="array">
                <item name="customcheck" xsi:type="object">Vendor\Module\Model\Checks\CustomCheck</item>
            </argument>
        </arguments>
    </type>

Our custom check class should implement Magento\Payment\Model\Checks\SpecificationInterface

t-heuser commented 2 years ago

Hi @engcom-Lima, I've created an example module for this problem. You can find it here: https://github.com/oneserv-heuser/magento2-33869. I've also added detailed steps to reproduce this issue there and also implemented a workaround for this bug.

Hey @engcom-Lima Can you please have a look at my example and try to reproduce the issue?

engcom-Lima commented 2 years ago

Hi @oneserv-heuser ,

Thank you for providing the custom module and detailed explanation to the problem.

I have checked with my own created custom payment method and also with @oneserv’s custom module and it seems the issue is there as Payment method is showing after making its validity false in Validator pool. Probable cause being getInfoInstance() being null so Validator pool not getting called although it is defined in di.xml as per DevDocs.

Based on it, I am confirming this issue so that the core team can do further analysis and work on a permanent solution to this problem.

Thanks

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-6081 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

edwinljacobs commented 1 month ago

Having the same issue.... after roughly two years. Thanks for the workaround though.