Whenever some tried to check out of PiranhaToys.com, this is the message they get after being tossed off to Paypal: "The link you have used to enter the PayPal system contains an incorrectly formatted item amount."

I'm running the latest development releases.

Comments

bc24102’s picture

I have the same issue as well. I have noticed when I go the payment page (the page that redirects you to pay with Paypal) my shopping cart goes back to 0 on items and price. Have you noticed this?

bc24102’s picture

Priority: Normal » Major
// Define a single item in the cart representing the whole order
    'amount_1' => commerce_currency_amount_to_decimal($amount, $currency_code),
    'item_name_1' => t('Order @order_number at @store', array('@order_number' => $order->order_number, '@store' => variable_get('site_name', url('<front>', array('absolute' => TRUE))))),
    'on0_1' => t('Product count'),
    'os0_1' => commerce_line_items_quantity($wrapper->commerce_line_items, 'product'),
  );

Code above is commerce_paypal_wps.module line 290

I have been doing some testing of this issue and from what I can tell it seems to be stemming from amount_1 value. It converts it into a decimal like paypal wants but maybe its in the wrong format. I tested with using just $amount in the line and I was able to complete payment. I sent $25.06 and it came out to $2,506.40. So I can only assume the decimal format is the culprit.

Here are the qualifications from the Paypal website incase anybody working on this needed quick reference.
Value must be a positive number. No currency symbol. Must have two decimal places, decimal separator must be a period (.), and the optional thousands separator must be a comma (,)

I am going to do some more investigating on commerce_currency_amount_to_decimal. Any help would be greatly appreciated. Hopefully either myself or someone else can resolve this soon.

bc24102’s picture

I think I may have resolved the problem. PayPal needs a number with only two decimal places. I checked the source of the page with the "Proceed to PayPal" button and the value that was actually being sent had more than two decimal places. It was 25.064. I rounded the number to two decimal places and now I am able to complete payment with the correct pricing.

Here is the change I made.
Code below is on commerce.module line 493

function commerce_currency_amount_to_decimal($amount, $currency_code) {
  static $divisors;

  // If the divisor for this currency hasn't been calculated yet...
  if (empty($divisors[$currency_code])) {
    // Load the currency and calculate its divisor as a power of 10.
    $currency = commerce_currency_load($currency_code);
    $divisors[$currency_code] = pow(10, $currency['decimals']);
  }

  return round($amount / $divisors[$currency_code], 2);
}

Please test this out and let me know if this is working properly and doesn't cause any other problems

gagoo’s picture

Subscribe

gagoo’s picture

Is there a way to solve the issue without patching the commerce.module file ?
Is there a planed next release for Commerce Paypal module ?

Thanks

gagoo’s picture

OK,

As patching the Drupal Commerce commerce_currency_amount_to_decimal function in the commerce.module file can affect some calculations because of the amount rounding, I think it's better to only patch the Commerce Paypal module.
So I only replaced the line that define the amount sent to Paypal in commerce_paypal_wps.module.

The only needed change is to replace line 292 :

'amount_1' => commerce_currency_amount_to_decimal($amount, $currency_code),

by

'amount_1' => round(commerce_currency_amount_to_decimal($amount, $currency_code),2),

Can testers give a feedback please ?

What the maintainer think about it ?

Thanks :)

bc24102’s picture

Just tested this out. It worked for me. I agree that this is better to fix it in commerce_paypal_wps.module.

rszrama’s picture

Status: Active » Fixed

Yeah, I just committed a fix to round it in our module instead of editing Commerce.

gagoo’s picture

Nice :)

Thx

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

nocean’s picture

Apologies for re-opening this issue -- but did this change get dropped in later versions? Looking in commerce_paypal_wps.module in the latest dev release, on ln 427, the round() function is missing. I re-added the round() function and it works as expected.

rszrama’s picture

Actually, this does appear to have come back to haunt us; currently it's getting visibility in #1800896-8: Product's price get bloated with float decimals, and we'll need to ensure all payment gateway modules get the memo.

nellngun’s picture

Issue summary: View changes

#2 working perfectly! For Drupal 7 is line 462:

// Define a single item in the cart representing the whole order
'amount_1' => round(commerce_paypal_price_amount(commerce_currency_convert($amount, $order_currency_code, $currency_code), $currency_code),0),
'item_name_1' => t('Order @order_number at @store', array('@order_number' => $order->order_number, '@store' => variable_get('site_name', url('', array('absolute' => TRUE))))),
'on0_1' => t('Product count'),
'os0_1' => commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()),
);

instead of

// Define a single item in the cart representing the whole order
'amount_1' => commerce_paypal_price_amount(commerce_currency_convert($amount, $order_currency_code, $currency_code), $currency_code),
'item_name_1' => t('Order @order_number at @store', array('@order_number' => $order->order_number, '@store' => variable_get('site_name', url('', array('absolute' => TRUE))))),
'on0_1' => t('Product count'),
'os0_1' => commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()),
);

* 2 for 2 decimals (for me 0 for no decimals)