This issue is different than https://drupal.org/node/1787172 and might be similar to https://drupal.org/node/2013883.

now the issue
site's default currency is set to INR (indian rupees)
paypal's Primary currency is set to USD , as INR is not supported yet.
https://drupal.org/project/commerce_multicurrency is used to convert INR to USD.

I am testing a flow with live paypal account, when trying to navigating to paypal using express checkout button error is displaying

The totals of the cart item amounts do not match order amounts
PayPal API request to https://api-3t.paypal.com/nvp:

Array
(
    [METHOD] => SetExpressCheckout
    [SOLUTIONTYPE] => Sole
    [LANDINGPAGE] => Billing
    [ALLOWNOTE] => 0
    [PAYMENTREQUEST_0_PAYMENTACTION] => Sale
    [PAYMENTREQUEST_0_AMT] => 900.00
    [PAYMENTREQUEST_0_CURRENCYCODE] => USD
    [PAYMENTREQUEST_0_INVNUM] => 25-1383049878
    [RETURNURL] => http://xxxtest.org/checkout/25/payment/return/X2a6r1fwFlVQe-2E7TKHUVzuNuHrxHYsA8M9b9v51eA
    [CANCELURL] => http://xxxtest.org/checkout/25/payment/back/X2a6r1fwFlVQe-2E7TKHUVzuNuHrxHYsA8M9b9v51eA
    [L_PAYMENTREQUEST_0_NAME0] => Basic testing for INR
    [L_PAYMENTREQUEST_0_AMT0] => 14.63
    [L_PAYMENTREQUEST_0_QTY0] => 1
    [L_PAYMENTREQUEST_0_NUMBER0] => basic_test_inr
    [PAYMENTREQUEST_0_ITEMAMT] => 14.63
    [PAYMENTREQUEST_0_TAXAMT] => 0.00
    [NOSHIPPING] => 1
    [USER] => xxxxxxxxxx_api1.gmail.com
    [PWD] => XXXXXXXXXXXXXXXX
    [SIGNATURE] => XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    [VERSION] => 76.0
)

PayPal server response:

Array
(
    [TIMESTAMP] => 2013-10-29T12:21:40Z
    [CORRELATIONID] => 5a8aaa42eb654
    [ACK] => Failure
    [VERSION] => 76.0
    [BUILD] => 8275157
    [L_ERRORCODE0] => 10413
    [L_SHORTMESSAGE0] => Transaction refused because of an invalid argument. See additional error messages for details.
    [L_LONGMESSAGE0] => The totals of the cart item amounts do not match order amounts.
    [L_SEVERITYCODE0] => Error
)

I don't get whether this is rounding problem or something else is causing.Attached is the Commerce Multicurrency settings screenshot.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

linitrex’s picture

Status: Active » Fixed

In functions:

function commerce_paypal_ec_set_express_checkout($payment_method, $order, $flow)
function commerce_paypal_ec_do_payment($payment_method, $order, $charge)

Replace:

'PAYMENTREQUEST_0_AMT' => commerce_paypal_price_amount($amount, $currency_code)

with

'PAYMENTREQUEST_0_AMT' => commerce_paypal_price_amount(commerce_currency_convert($order_total['amount'], $order_total['currency_code'], $currency_code), $currency_code)

to convert the Order total amount from local to the payment currency used by PayPal

andyg5000’s picture

Status: Fixed » Active

It's not fixed unless it's committed to the project. Marking back as active. Thanks for your suggestion. If you could put your update in a patch that would help speed things along.

torgosPizza’s picture

Version: 7.x-2.2 » 7.x-2.x-dev
Status: Active » Needs review
FileSize
853 bytes

It does appear that the currency is converted already in commerce_paypal_ec_do_payment() as there is a call to alter the $amount before it is added to the $nvp array like so:

  // Convert the charge amount to the specified currency.
  $amount = commerce_currency_convert($charge['amount'], $charge['currency_code'], $currency_code);

So really this only needs to change in commerce_paypal_ec_set_express_checkout(). Patch attached.

Marko B’s picture

There is also another problem when using currencies that are not supported and need to convert to dollars (or default currency).

I will demonstrate it with picture. So when buying more products, like 2x of one and 2x of another. There is a high chance the conversion will be wrong by one decimal at least. For example, you can see my picture, and see that PAYMENTREQUEST_0_AMT equals PAYMENTREQUEST_0_ITEMAMT but if you count the total of line items in
L_PAYMENTREQUEST_0_NUMBER_$i it will be 387,7. Which will break the payment as PP checks the sum of totals of line items and you will get the

"REDIRECT TO PAYPAL EXPRESS CHECKOUT FAILED. PLEASE TRY AGAIN OR CONTACT AN ADMINISTRATOR TO RESOLVE THE ISSUE."

With

PayPal server response:
Array
(
    [TIMESTAMP] => 2016-05-13T09:26:26Z
    [CORRELATIONID] => b9d2d051626a3
    [ACK] => Failure
    [VERSION] => 76.0
    [BUILD] => 000000
    [L_ERRORCODE0] => 10413
    [L_SHORTMESSAGE0] => Transaction refused because of an invalid argument. See additional error messages for details.
    [L_LONGMESSAGE0] => The totals of the cart item amounts do not match order amounts.
    [L_SEVERITYCODE0] => Error
)
Marko B’s picture

I made a patch that will fix above issue, which normalizes total amount according to line items total if totals miss-match.

tko’s picture

Marko, patch seems to be working however if there is a shipping line item its not being converted. It just gets a changed currency symbol.

Marko B’s picture

Ok I made new patch, this one solves Shipping also, but also there were few problems with old one.

Please needs review. this is also a critical thing for, well most of the world :)

adrien.felipe’s picture

Patch #7 have solved the issue for me. Thanks.

tonysbag’s picture

#7 works! thank u Marko!

bnadem’s picture

Hi everybody,
patch#7 works! thank you Marko!

torgosPizza’s picture

Status: Needs review » Reviewed & tested by the community

Sounds like it's RTBC?

mglaman’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/modules/ec/commerce_paypal_ec.module
    @@ -1257,15 +1257,15 @@ function commerce_paypal_ec_do_payment($payment_method, $order, $charge) {
    -function commerce_paypal_ec_itemize_order($order, $currency_code) {
    -  $nvp = array();
    +function commerce_paypal_ec_itemize_order($order, $currency_code, $nvp) {
    

    This is a breaking change. NVP needs to default to an empty array

  2. +++ b/modules/ec/commerce_paypal_ec.module
    @@ -1257,15 +1257,15 @@ function commerce_paypal_ec_do_payment($payment_method, $order, $charge) {
    +  $amt = 0;
    

    $amt is no longer used

mglaman’s picture

Status: Needs work » Needs review
FileSize
3.97 KB

Here is a new version of the patch, which has only has the logic in commerce_paypal_ec_itemize_order updated for better math and currency conversion handled. Along with the ensured currency conversion in PAYMENTREQUEST_0_AMT.

mglaman’s picture

Issue tags: +Needs manual testing

This needs manual testing in order to commit. INR is now supported by PayPal, so a different currency will have to be tested.

rafiqasad’s picture

Hello,

This patch is not working with commerce_paypal 7.x-2.6 and 7..x-2.7. This patch is also not inclued/fixed in the updated version.

Best Regards,

tko’s picture

FileSize
3.72 KB

Updated mglaman's patch from #13 to work on latest dev