Question

I have a weird issue with the PayPal express checkout. When I pass in an options[:item][:quantity], I get an error code from PayPal that the transaction in invalid.

#controllers/orders_controller.rb
def express
  options = {
    :ip => request.remote_ip,
    :return_url => new_order_url,
    :cancel_return_url => products_url,
    :items => current_cart.line_items_hash
  }
  response = EXPRESS_GATEWAY.setup_purchase(current_cart.build_order.price_in_cents, 
    options)
  redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
end

# models/cart.rb
def line_items_hash
  self.line_items.map do |line_item|
  {
    :name => Product.find(line_item.product_id).name
    # :quantity => line_item.quantity,
    :description => line_item.product.description,
    :amount => line_item.gross_price_in_cents}
  end
end

So this works fine. The problem is that the correct quantity is not shown on the PayPal order confirmation page. However, if I uncomment the quantity variable in the line_items_hash function the whole thing breaks and I get "invalid transaction from paypal". Any ideas?

Was it helpful?

Solution

Silly old me. Paypal keeps invalidating my transaction because I'm passing bad information. I was setting the amount to line_item total which is already the total of quantity * product.price. So the information that Paypal was receiving is quantity * (quantity * product.price).

Silly little mistake but I managed to catch it in the end.

# models/cart.rb
def line_items_hash
  self.line_items.map do |line_item|
  {
    :name => Product.find(line_item.product_id).name
    :quantity => line_item.quantity,
    :description => line_item.product.description,
    :amount => line_item.price.in_cents}
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top