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.47k stars 9.28k forks source link

Method Magento\Payment\Gateway\Data\Quote\QuoteAdapter::getGrandTotalAmount() always returns null #33872

Open t-heuser opened 3 years ago

t-heuser commented 3 years ago

Preconditions (*)

  1. Magento 2.4.3

Steps to reproduce (*)

  1. Call Magento\Payment\Gateway\Data\Quote\QuoteAdapter::getGrandTotalAmount()

Expected result (*)

  1. The grand total amount of the quote gets returned.

Actual result (*)

  1. null is always returned.

Cause of the bug

The method always returns null as seen here: QuoteAdapter::getGrandTotalAmount()

/**
     * Returns order grand total amount
     *
     * @return null
     */
    public function getGrandTotalAmount()
    {
        return null;
    }

The solution

Change the function as follows:

    /**
     * Returns order grand total amount
     *
     * @return float
     */
    public function getGrandTotalAmount()
    {
        return $this->quote->getBaseGrandTotal();
    }

Example

My use case is to use the Gateway Implementation for Payment methods which magento provides and requires me to use. Specifically I need it for the availability Validator as configured as decribed here.

Here is a code example:

<?php

declare(strict_types=1);

namespace Oneserv\B2BInvoice\Gateway\Validator;

use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Payment\Gateway\Validator\AbstractValidator;

/**
 * Class AvailabilityValidator
 */
class AvailabilityValidator extends AbstractValidator
{
    /**
     * @param array{payment: PaymentDataObjectInterface} $validationSubject
     * @return ResultInterface
     */
    public function validate(array $validationSubject): ResultInterface
    {
        $order = $validationSubject['payment']->getOrder();

        $grandTotal = (float)$order->getGrandTotalAmount();

        if ($grandTotal === (float)0) {
            return $this->createResult(false, [__('The order total amount must be greater than 0.')]);
        }

        return $this->createResult(true);
    }
}

In my code example you can see the use case. If the function getGrandTotalAmount() always returns null (which it does, as it's hardcoded to do that) my payment method will never be available.


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 this issue exists on a vanilla magento instance.

t-heuser commented 3 years ago

For everyone interested here is a patch to fix this issue temporary:

From bbfb31408bdb4a5551f54e72975233e392b94eaf Mon Sep 17 00:00:00 2001
From: Timon Heuser <heuser@bueromoebel-experte.de>
Date: Fri, 20 Aug 2021 14:13:53 +0200
Subject: [PATCH] fix

---
 .../module-payment/Gateway/Data/Quote/QuoteAdapter.php        | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/vendor/magento/module-payment/Gateway/Data/Quote/QuoteAdapter.php b/vendor/magento/module-payment/Gateway/Data/Quote/QuoteAdapter.php
index 645c347..f130572 100644
--- a/vendor/magento/module-payment/Gateway/Data/Quote/QuoteAdapter.php
+++ b/vendor/magento/module-payment/Gateway/Data/Quote/QuoteAdapter.php
@@ -122,11 +122,11 @@ class QuoteAdapter implements OrderAdapterInterface
     /**
      * Returns order grand total amount
      *
-     * @return null
+     * @return float
      */
     public function getGrandTotalAmount()
     {
-        return null;
+        return $this->quote->getBaseGrandTotal();
     }

     /**
-- 
2.27.0

Just copy it to a .patch file and follow instructions here: https://github.com/cweagans/composer-patches

m2-assistant[bot] commented 3 years ago

Hi @engcom-Delta. 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-Delta commented 3 years ago

Hi @oneserv-heuser,

Thanks for logging an issue. Kindly see if you can provide list of functional scenarios. Hence, added the label 'Needs Update'

Thanks

t-heuser commented 3 years ago

@engcom-Delta Why should I provide functional scenarios? There is a obvious bug in the code base which also violates coding standards as in the interface OrderAdapterInterface (which the QuoteAdapter implements) the function getGrandTotalAmount() has the return type float. But in the QuoteAdapter it's implemented (hardcoded!) to return null which makes no sense at all as the required data can be obtained from the quote.

Anyway, my use case is to use the Gateway Implementation for Payment methods which magento provides and requires me to use. Specifically I need it for the availability Validator as configured as decribed here.

Here is a code example:

<?php

declare(strict_types=1);

namespace Oneserv\B2BInvoice\Gateway\Validator;

use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Payment\Gateway\Validator\AbstractValidator;

/**
 * Class AvailabilityValidator
 */
class AvailabilityValidator extends AbstractValidator
{
    /**
     * @param array{payment: PaymentDataObjectInterface} $validationSubject
     * @return ResultInterface
     */
    public function validate(array $validationSubject): ResultInterface
    {
        $order = $validationSubject['payment']->getOrder();

        $grandTotal = (float)$order->getGrandTotalAmount();

        if ($grandTotal === (float)0) {
            return $this->createResult(false, [__('The order total amount must be greater than 0.')]);
        }

        return $this->createResult(true);
    }
}

In my code example you can see the use case. If the function getGrandTotalAmount() always returns null (which it does, as it's hardcoded to do that) my payment method will never be available.

t-heuser commented 3 years ago

@engcom-Delta I've provided the requested information, can you please change the labels/ confirm that the issue exists?

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,

I have gone through your comments and agrees that hardcoded null should not be there in getGrandTotalAmount() of class QuoteAdapter. Also found that in the interface OrderAdapterInterface which is implemented by QuoteAdapter class, there is a function getGrandTotalAmount() which has the return type float and is inaccurate in that respect also. So confirming this issue.

Thanks

github-jira-sync-bot commented 2 years ago

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

m2-assistant[bot] commented 2 years ago

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

engcom-Lima commented 2 years ago

Hi @oneserv-heuser,

Before we move ahead with this issue confirmation, there is 1 more thing which we need to clarify:

As per your use case, you are calling $order->getGrandTotalAmount() but you are not calling getGrandTotalAmount method from QuoteAdapter class. We need to understand the impact of it that is why we need some more information. So please clarify how QuoteAdapter class is creating issue as per your use case ?

Thanks

t-heuser commented 2 years ago

@engcom-Lima Lets dive deeper into the code:

The object passed to the validate function of a Payment Gateway Validator Instance is an array: https://github.com/magento/magento2/blob/192b28038dac2e993c65b49fb8b5af10cb520945/app/code/Magento/Payment/Gateway/Validator/ValidatorInterface.php#L22

This array contains an an object of the type PaymentDataObjectInterface: https://github.com/magento/magento2/blob/192b28038dac2e993c65b49fb8b5af10cb520945/app/code/Magento/Payment/Gateway/Data/PaymentDataObjectInterface.php#L1-L31

My use case here is now to get the order total. The way I can achieve this is by calling the getOrder() functiom: https://github.com/magento/magento2/blob/192b28038dac2e993c65b49fb8b5af10cb520945/app/code/Magento/Payment/Gateway/Data/PaymentDataObjectInterface.php#L18-L23

This function now returns an object of the type OrderAdapterInterface. This interface is implemented by the classes OrderAdapter and QuoteAdapter. In my case I get an instance of the QuoteAdapter as I'm handling a Quote. And then I run into the problem that the function returns hardcoded null: https://github.com/magento/magento2/blob/192b28038dac2e993c65b49fb8b5af10cb520945/app/code/Magento/Payment/Gateway/Data/Quote/QuoteAdapter.php#L122-L130

I hope that this explanation now is enough for this obvious bug to be fixed.

engcom-Lima commented 2 years ago

Hi @oneserv-heuser,

Thank you for the detailed explanation. Based on it we can proceed with this issue accordingly and therefore confirming this issue again.

github-jira-sync-bot commented 2 years ago

:x: Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.magento.com/browse/AC-1290

KrasnoshchokBohdan commented 2 years ago

@magento I am working on this