maksuturva / magento2_payment_module

Svea Payment module for Magento 2
Other
4 stars 13 forks source link

Cancelling payment causes non-nullable SelectedPaymentMethod.title error in Magento_QuoteGraphQl cart #60

Closed robertspinja closed 11 months ago

robertspinja commented 11 months ago

If payment is cancelled after checkout, if a project uses Magento_QuoteGraphQl, it will cause errors for the module.

[2023-09-05 12:24:10] main.ERROR: Cannot return null for non-nullable field SelectedPaymentMethod.title. {"exception":"[object] (GraphQL\\Error\\Error(code: 0): Cannot return null for non-nullable field SelectedPaymentMethod.title. at /home/magento/public_html/vendor/webonyx/graphql-php/src/Error/Error.php:174, GraphQL\\Error\\InvariantViolation(code: 0): Cannot return null for non-nullable field SelectedPaymentMethod.title. at /home/magento/public_html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php:808)"} []

https://github.com/maksuturva/magento2_payment_module/blob/master/app/code/Svea/Maksuturva/Model/HandlingFeeApplier.php#L28

    public function updateHandlingFee($quote, $paymentMethod = ' ', $subMethod = null, $collatedMethod = null)
    {
        if (\is_string($paymentMethod)) {
            $quote->getPayment()->setMethod($paymentMethod);
        } elseif (isset($paymentMethod['method'])) {
            $quote->getPayment()->setMethod($paymentMethod['method']);
        }

The issue is that $paymentMethod = ' ' is a space, so the cancel controller will set the method as a space character. Which then causes getMethodInstance() to not throw an error, but return "Substitution" method instance, which returns title as null. Which then causes the error. https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php#L38 Space character method cancelled payments in quote_payment table: image

Changing $paymentMethod = ' ' to $paymentMethod = '' fixes these errors, and sets the payment method in database as null. There doesn't seem to be a clear reason in the commit why a space character was chosen to unset the payment method: https://github.com/maksuturva/magento2_payment_module/commit/429df6371e1be32a72ab0fad29dad687ed7f1e48#diff-5b4b9c42871a5fd7bb5aa14e162e4ed0b1607ab339d4aee857ec9ac84ab14675

Svea-Maintainer commented 11 months ago

Thank you for taking the time to submit this issue and thorough description. Your contributions to our project are greatly appreciated.

We've reviewed your issue, and after testing your proposed fix, released a new version of the Svea payment module.

Thanks again for your contribution.