PayPal express checkout issue on custom discount

I have an issue during the paypal express checkout.

I have a custom module for applying custom discount on quote. During paypal payment, it fails returning this error:

report.CRITICAL: PayPal NVP gateway errors: The totals of the cart item amounts do not match order amounts (#10413: Transaction refused because of an invalid argument. See additional error messages for details). Correlation ID: 9bf727de5b54e. Version: 72.0. []

I did eventually find a solution to this problem, which was adding another custom module that triggers on payment_cart_collect_items_and_amounts event and add a custom item with the negative amount of the discount to the quote:

public function execute(Observer $observer){
    $this->logger->info("Add Product PayPal payment ", array());
    $cart = $observer->getEvent()->getCart();
    $quote = $this->checkout_session->getQuote();
    $this->promoter->setQuote($quote);
    $customAmount = $this->promoter->getQuoteTotalDiscount();
    $this->logger->info("quote discount: ", array($customAmount));
    $cart->addCustomItem(__("Promozioni"), 1, -1.00 * $customAmount, "Promozioni");
}

The solution was working perfectly, but then i had to change a configuration in the PayPal module.

I had to set in Stores -> Configuration -> Sales -> Payment methods.

Under PayPal configure button -> Basic Settings - PayPal Express Checkout -> Payment Action i changed from "Authorization" to "Sale".

After saving the configuration the PayPal express checkout returned the same error as before (inside var/log/system.log)

[2020-08-11 10:09:34] report.INFO: Add Product PayPal payment []
[2020-08-11 10:09:35] report.INFO: quote discount:: [139.8]
[2020-08-11 10:09:36] report.CRITICAL: PayPal NVP gateway errors: The totals of the cart item amounts do not match order amounts (#10413: Transaction refused because of an invalid argument. See additional error messages for details). Correlation ID: 9bf727de5b54e. Version: 72.0. []

As you can see the module is running because it is printing the correct test logs.

Checking the quote and sales_order table the totals values are correct.

If i bring back that configuration to "Authorization" the PayPal payment is working perfectly.

I also tried to change PayPal configuration under Basic Settings - PayPal Express Checkout -> Advanced Settings -> Transfer Cart Line Items from "Yes" to "No" with no results.

My magento version is Magento Enterprise 2.3.3

Thanks for the help.



from Active questions tagged magento-enterprise - Magento Stack Exchange

Click link to view all answer https://magento.stackexchange.com/questions/319556/paypal-express-checkout-issue-on-custom-discount