Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 5 tasks
t-heuser opened this issue Aug 20, 2021 · 15 comments
Open
1 of 5 tasks
Assignees
Labels
Area: Payments Component: Payment Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: dev in progress Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@t-heuser
Copy link

t-heuser commented Aug 20, 2021

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.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@m2-assistant
Copy link

m2-assistant bot commented Aug 20, 2021

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

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

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


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 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.

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

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

@t-heuser
Copy link
Author

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

@engcom-Echo engcom-Echo self-assigned this Aug 23, 2021
@m2-assistant
Copy link

m2-assistant bot commented Aug 23, 2021

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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@JeffersonTeixeira
Copy link

I reported this issue before It was closed with no solution:
#30211

@t-heuser
Copy link
Author

@JeffersonTeixeira The classical stale-bot treatment

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

@t-heuser
Copy link
Author

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
Copy link

m2-assistant bot commented Sep 2, 2021

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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Lima
Copy link
Contributor

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 ?

@engcom-Lima engcom-Lima added the Issue: needs update Additional information is require, waiting for response label Sep 20, 2021
@m2-community-project m2-community-project bot added Issue: ready for confirmation and removed Issue: needs update Additional information is require, waiting for response labels Sep 20, 2021
@engcom-Lima engcom-Lima added the Issue: needs update Additional information is require, waiting for response label Sep 20, 2021
@t-heuser
Copy link
Author

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
Copy link

vivekchoudhari commented Nov 3, 2021

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
Copy link
Author

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-November engcom-November added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Apr 7, 2022
@sdzhepa sdzhepa added Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. and removed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. labels Jul 21, 2022
@engcom-Hotel engcom-Hotel removed Component: Payment Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Area: Payments labels Aug 2, 2022
@engcom-Hotel engcom-Hotel added Component: Payment Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Progress: ready for dev Area: Payments labels Aug 2, 2022
@github-jira-sync-bot
Copy link

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

@m2-assistant
Copy link

m2-assistant bot commented Aug 2, 2022

✅ 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.

@github-jira-sync-bot github-jira-sync-bot added Priority: P2 A defect with this priority could have functionality issues which are not to expectations. and removed Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. labels Aug 31, 2022
@edwinljacobs
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Payments Component: Payment Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: dev in progress Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Status: Dev In Progress
Development

No branches or pull requests

10 participants