spree-contrib / better_spree_paypal_express

A better Spree PayPal Express Extension.
http://guides.spreecommerce.org
BSD 3-Clause "New" or "Revised" License
110 stars 269 forks source link

PayPal failed. The totals of the cart item amounts do not match order amounts. #129

Open peterfealey opened 9 years ago

peterfealey commented 9 years ago

screenshot 2014-10-30 16 19 43 Having a problem integrating this. Gemfile attached, if that helps.

At checkout the above message prevents even getting to the PayPal form.


source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', group: :development
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# Use jquery as the JavaScript library
gem 'jquery-rails', '~> 3.1.0'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

gem 'spree', '2.3.2'
gem 'spree_gateway', :git => 'https://github.com/spree/spree_gateway.git', :branch => '2-3-stable'
gem 'spree_auth_devise', :git => 'https://github.com/spree/spree_auth_devise.git', :branch => '2-3-stable'
gem 'spree_bootstrap_frontend', github: '200Creative/spree_bootstrap_frontend', branch: '2-3-stable'
gem 'spree_digital', :git => 'git://github.com/halo/spree_digital.git', :branch => '2-3-stable'
gem 'spree_product_assembly', github: 'spree/spree-product-assembly', branch: '2-3-stable'
gem 'spree_static_content', github: 'spree/spree_static_content', branch: '2-3-stable'

gem 'spree_paypal_express', github: 'spree-contrib/better_spree_paypal_express', branch: '2-3-stable'
gem 'spree_blogging_spree', github: 'stefansenk/spree-blogging-spree', branch: '2-3-stable'

gem 'spreadsheet', '0.9.7'
gem 'datashift', :git => 'git://github.com/autotelik/datashift'
gem 'datashift_spree', :git => 'git://github.com/autotelik/datashift_spree'

gem 'rails_12factor', group: :production

gem 'mysql2', group: :production

gem 'better_errors', group: :development
gem 'binding_of_caller', group: :development

gem 'sprockets-rails', '2.1.3'

gem 'paperclip'
gem 'aws-sdk'
peterfealey commented 9 years ago

Ok, on further inspection, choosing not to 'include Tax In Prices' means I can get through the process, but I'd really not like to be displaying ex VAT pricing and adding it on at checkout.

paulgroves commented 9 years ago

I am experiencing this exact issue, where unchecking 'INCLUDED IN PRICE' in /admin/tax_rates/1/edit resolved the problem, but is undesired.

Very grateful for any help on this :)

ouechtasvu commented 9 years ago

same here ...

I'm using the 2-3 stable branch and for a quick fix I have juste commented this section in paypal_controller :

  # TMP fix for tax included in product price

  # order.all_adjustments.eligible.each do |adjustment|
  #   next if (tax_adjustments + shipping_adjustments).include?(adjustment)
  #   items << {
  #     :Name => adjustment.label,
  #     :Quantity => 1,
  #     :Amount => {
  #       :currencyID => order.currency,
  #       :value => adjustment.amount
  #     }
  #   }
  # end

It's working because I don't have other taxes and they are included in product price but it's not a good solution.

Hates commented 9 years ago

These are the changes I made to have inclusive VAT work on 2-2-stable. Ignore my reference to paypal_price as that's an internal thing we use for calculating gift wrapping.

https://github.com/Hates/better_spree_paypal_express/commit/b00e970bc2147b8f7fb60a9f638ea92adb127edd

peterfealey commented 9 years ago

@Hates - Ah wow. Thank you.

This is my first Spree project, so please excuse the stupidity that's about to come from my keyboard.

Could I theoretically take a copy of paypal_controller.rb and drop it into /app/spree/controllers/ and it will override the gem's copy? I expect it's not best practice but deadlines are looming and better launched than not!

peterfealey commented 9 years ago

...But I should really fork the Gem based on the branch I'm using and bundle from my fork...

Hates commented 9 years ago

@peterfealey Putting something like this in app/controllers/spree/paypal_controller_decorator.rb should work: https://gist.github.com/Hates/fb67e791c5d239efdbff

peterfealey commented 9 years ago

Thank you so much @Hates

alepore commented 9 years ago

anyone figured out what actually happened here and the proper way to fix this? i have a 2.2 store with tax included in price that works as expected, and a brand new 2.3 one with this error.

alepore commented 9 years ago

maybe this commit? https://github.com/spree-contrib/better_spree_paypal_express/commit/057973b9a8520736290c75b4466ff0ee25d9f743 /c @gmacdougall

Hates commented 9 years ago

AFAIK this gem has never worked when tax is included in the price.

alepore commented 9 years ago

uhm, looks like this was fixed month ago on https://github.com/spree-contrib/better_spree_paypal_express/commit/d446645c0ba09fa61b902982a5f57d2713fd9868 but this never reached 2-3-stable and master.

Hates commented 9 years ago

I'm running the 2-2-stable branch with that change and was still running into problems with inclusive tax rates. It's possible that the problem I'm having is un-related. My issue occurs when shipping to a tax zone where the inclusive rate should not be applied.

brianmcmahon commented 9 years ago

So I'm not sure if this has been fixed and explained somewhere else but the following worked for me.

For those of you needing to fix it yourself you can modify the paypal controller and make sure that the tax adjustments include the line item taxes. The issue seemed to be that the line item tax adjustments were being added as order items (this is for 2.3-stable). So for line 6 I changed it to:

tax_adjustments = order.adjustments.tax.additional + order.line_item_adjustments.tax.additional

The reason there is this issue is because there are two places where the cart item totals are added up: line 137 and line 148. Line 137 uses current_order.total - shipment_sum - current_order.additional_tax_total to calculate the total, and line 148 sums up the items in the item array, and the tax for the line items is included as an item.

I hope this helps!

geermc4 commented 9 years ago

I just ran into this, curious, why was it closed by tests?

mleglise commented 9 years ago

+1 Also experiencing this bug. @alepore can this be re-opened?

I'm getting this message when a line item has a promotion adjustment, shipping to a location without taxes.

alepore commented 9 years ago

@mleglise on which version? are you up2date?

geermc4 commented 9 years ago

@alepore I was on 69fd18c48b98 although I later figured out my issue was caused by tax adjustments from https://github.com/railsdog/spree_avatax_certified

mleglise commented 9 years ago

@alepore I'm running latest 3-0-stable for this gem and Spree itself. Working on a test case to show the breakage.

alepore commented 9 years ago

Thanks, the problem here is always the same : PayPal checks if the sum of all items and adjustments matches the total amount.

So we must be very careful to include (or exclude) all kinds of spree adjustments and taxes

albertliu-nwb commented 9 years ago

hi mleglise, I am on 3-0-stable too. in paypal_controller.rb line 8, I changed additional_adjustments = order.all_adjustments.additional to additional_adjustments = order.all_adjustments

After line 22, I also added (you may don't need it) order.update!

Now it works. good luck. Albert

mleglise commented 9 years ago

@albertliu-nwb found the right place. I dug deeper and discovered that this is because of a change in Spree::Core 3.0. PR submitted: https://github.com/spree/spree/pull/6695

Theoretically, that PR should solve this issue, with no changes needed in the PayPal gem.

GerardGarcia commented 9 years ago

I have applied the PR to spree branch 3-0-stable and I still experience the issue. Changing line 8 of paypal_controller.rb as @albertliu-nwb suggested solved it.

In my case the error was caused beacuse an order adjustment was not included and not beacuse of a tax adjustment.

alepore commented 8 years ago

uhm, this spree included: nil is pretty bad. changing that line on paypal controller will probably break again in other situations...

mmintel commented 8 years ago

Hi there,

i was getting the same error and fixed it with this gist mentioned above (https://gist.github.com/Hates/fb67e791c5d239efdbff).

Now this works for most of my orders but we have a special discounting system. if someone places an order in our store he must enter a unique ID, some of them are linked to a discount. so for every order including a discount I am still getting this error. I guess that happens because the discount is applied to the whole order and not to the single items. as I am not the developer of that discounting system and my rails knowledge is very limited: do you know a quick way to disable this "security mechanism" in this paypal plugin here? Which line of code is responsible for that?

Hope you understand my problem!

Hates commented 8 years ago

@mmintel It should be added as an adjustment, which AFAIK can be a negative value. You can see where it builds them up on line 11 of the gist you linked to. Apart from that I don't think it can be disabled as all the values need to add up otherwise Paypal reject the order.

cjgarciaj commented 6 years ago

@Hates as an adjustments works but the promotions won't be collected in the aditional therefore the issue will persist if the order has any discount. My fix for future readers:

    additional_adjustments = order.all_adjustments.additional
    tax_adjustments = additional_adjustments.tax
    shipping_adjustments = additional_adjustments.shipping
    promotion_adjustments = order.all_adjustments.promotion

    (additional_adjustments.eligible + promotion_adjustments.eligible).each do |adjustment|
      next if (shipping_adjustments + tax_adjustments).include?(adjustment)
      items << {
        :Name => adjustment.label,
        :Quantity => 1,
        :Amount => {
          :currencyID => order.currency,
          :value => adjustment.amount
        }
      }
    end

Hope this helps

Note: I had to add the tax adjustments since paypal was capturing the taxonomies apart from the items So far: Tested at 11/07/2018 20:57 UTC