jeudi 13 décembre 2018

Active Merchant-Paypal Express implementation does not pass order_items and price

I am trying to implement Paypal Express Checkout to a Rails App. I would like to save my order with all the client's details in my orders database and send the customer to PayPal, where he should accept the payment and be redirected to my website.

The PayPal merchant is going to be localized in the EU. I specify this, since I read that Active Merchant has or had some restrictions in this regard.

I am able to get directed to PayPal, login, return to my website where express_token and express_payer_id are written to the orders database.

Unfortunately the only parameter which appears to be handed over to PayPal is :description => 'Books', nothing else appears on PayPal's Sandbox page and no payment is executed.

The relative part of my checkout_controller is:

class CheckoutController < ApplicationController
  before_filter :initialize_cart

  def place_order
    @order = Order.new(params[:order])
    @order.customer_ip = request.remote_ip 
    populate_order
    if @order.save
      checkout_paypal
      @order.status == 'processed'
    else
      render :action => 'index'
    end
  end

The private checkout_paypal action in the same controller is:

  def checkout_paypal
    price_in_cents = (@order.total * 100).round

    options = {:order_id => @order.id, 
              # :items => @order.order_items,
              :handling => 0,
              :tax => 0,
              :currency => "EUR",
              :email => @order.email, 
              :address => { :address1 => @order.ship_to_address, 
                            :city => @order.ship_to_city, 
                            :country => @order.ship_to_country, 
                            :zip => @order.ship_to_postal_code 
                          } , 
              :description => 'Books',
              :ip => @order.customer_ip,
              :return_url => checkout_success_url(@order), # return here if payment success
              :cancel_return_url => checkout_error_url(@order) # return here if payment failed
              }

    paypal_response = GATEWAY.setup_purchase(price_in_cents, options)
    if paypal_response.success? 
      @order.status = 'processed' 
    else 
      @order.error_message = response.message 
      @order.status = 'failed' 
    end 
    @order.express_token = paypal_response.token # save paypal token to db
    @order.save
    redirect_to GATEWAY.redirect_url_for(paypal_response.token) and return  # redirect to paypal for payment
  end

The private populate_order action in the same controller is:

  def populate_order
    @cart.cart_items.each do |cart_item|
      order_item = OrderItem.new(:product_id => cart_item.product_id, :price => cart_item.price, :amount => cart_item.amount)
      @order.order_items << order_item
    end
  end
end

When I add :items => @order.order_items to the checkout_paypal options

I get https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token= with No token passed

and status: failed

as well as error_message ok written to the orders database.

Started POST "/checkout/place_order" for 127.0.0.1 at Thu Dec 13 10:13:22 +0100 2018
Processing by CheckoutController#place_order as HTML
  Parameters: {"utf8"=>"✓", "commit"=>"Paypal Express", "authenticity_token"=>"somestring=", "order"=>{"ship_to_last_name"=>"Mustermann", "ship_to_address"=>"ESpachstr. 1", "ship_to_city"=>"Freiburg", "email"=>"mustermann@example.de", "ship_to_postal_code"=>"79111", "ship_to_first_name"=>"Hans", "ship_to_country"=>"Germany", "phone_number"=>"213412341234"}}
  Cart Load (0.3ms)  SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1  [["id", 3]]
  CartItem Load (0.3ms)  SELECT `cart_items`.* FROM `cart_items` WHERE `cart_items`.`cart_id` = 3
  SQL (0.1ms)  BEGIN
  SQL (1.0ms)  INSERT INTO `orders` (`created_at`, `customer_ip`, `email`, `error_message`, `express_payer_id`, `express_token`, `phone_number`, `ship_to_address`, `ship_to_city`, `ship_to_country`, `ship_to_first_name`, `ship_to_last_name`, `ship_to_postal_code`, `status`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Thu, 13 Dec 2018 09:13:22 UTC +00:00], ["customer_ip", "127.0.0.1"], ["email", "mustermann@example.de"], ["error_message", nil], ["express_payer_id", nil], ["express_token", nil], ["phone_number", "213412341234"], ["ship_to_address", "ESpachstr. 1"], ["ship_to_city", "Freiburg"], ["ship_to_country", "Germany"], ["ship_to_first_name", "Hans"], ["ship_to_last_name", "Mustermann"], ["ship_to_postal_code", "79111"], ["status", "open"], ["updated_at", Thu, 13 Dec 2018 09:13:22 UTC +00:00]]
  SQL (1.0ms)  INSERT INTO `order_items` (`amount`, `created_at`, `order_id`, `price`, `product_id`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?)  [["amount", 2], ["created_at", Thu, 13 Dec 2018 09:13:22 UTC +00:00], ["order_id", 14], ["price", 5], ["product_id", 3], ["updated_at", Thu, 13 Dec 2018 09:13:22 UTC +00:00]]
   (0.4ms)  COMMIT
  SQL (0.1ms)  BEGIN
   (0.5ms)  UPDATE `orders` SET `updated_at` = '2018-12-13 09:13:23', `status` = 'failed', `error_message` = 'OK' WHERE `orders`.`id` = 14
   (0.4ms)  COMMIT
Redirected to https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=
Completed 302 Found in 1346ms (ActiveRecord: 4.0ms)

When I do not add it I proceed:

Started POST "/checkout/place_order" for 127.0.0.1 at Thu Dec 13 10:18:12 +0100 2018
Processing by CheckoutController#place_order as HTML
  Parameters: {"authenticity_token"=>"somestring=", "utf8"=>"✓", "order"=>{"ship_to_city"=>"Freiburg", "ship_to_postal_code"=>"79111", "ship_to_country"=>"Germany", "ship_to_address"=>"ESpachstr. 1", "phone_number"=>"124513451345134", "ship_to_first_name"=>"Hans", "email"=>"mustermann@example.de", "ship_to_last_name"=>"Mustermann"}, "commit"=>"Wire transfer"}
  Cart Load (0.3ms)  SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1  [["id", 3]]
  CartItem Load (0.4ms)  SELECT `cart_items`.* FROM `cart_items` WHERE `cart_items`.`cart_id` = 3
  SQL (0.1ms)  BEGIN
  SQL (2.1ms)  INSERT INTO `orders` (`created_at`, `customer_ip`, `email`, `error_message`, `express_payer_id`, `express_token`, `phone_number`, `ship_to_address`, `ship_to_city`, `ship_to_country`, `ship_to_first_name`, `ship_to_last_name`, `ship_to_postal_code`, `status`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Thu, 13 Dec 2018 09:18:12 UTC +00:00], ["customer_ip", "127.0.0.1"], ["email", "mustermann@example.de"], ["error_message", nil], ["express_payer_id", nil], ["express_token", nil], ["phone_number", "124513451345134"], ["ship_to_address", "ESpachstr. 1"], ["ship_to_city", "Freiburg"], ["ship_to_country", "Germany"], ["ship_to_first_name", "Hans"], ["ship_to_last_name", "Mustermann"], ["ship_to_postal_code", "79111"], ["status", "open"], ["updated_at", Thu, 13 Dec 2018 09:18:12 UTC +00:00]]
  SQL (1.1ms)  INSERT INTO `order_items` (`amount`, `created_at`, `order_id`, `price`, `product_id`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?)  [["amount", 2], ["created_at", Thu, 13 Dec 2018 09:18:12 UTC +00:00], ["order_id", 15], ["price", 5], ["product_id", 3], ["updated_at", Thu, 13 Dec 2018 09:18:12 UTC +00:00]]
   (0.7ms)  COMMIT
  SQL (0.2ms)  BEGIN
   (0.6ms)  UPDATE `orders` SET `express_token` = 'EC-sandboxtoken', `status` = 'processed', `updated_at` = '2018-12-13 09:18:13' WHERE `orders`.`id` = 15
   (0.8ms)  COMMIT
Redirected to https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-sandboxtoken
Completed 302 Found in 1410ms (ActiveRecord: 7.1ms)

processed is written to @order.status as well as EC-sandboxtoken to @order.express_token and sandboxpayerid to @order.express_payer_id, but nothing shows up in the Paypal Express window:

Screenshot of the customer PayPal window

This is the output of the redirect:

Started GET "/checkout/success/15?token=EC-sandboxtoken&PayerID=sandboxpayerid" for 127.0.0.1 at Thu Dec 13 10:22:59 +0100 2018
Processing by CheckoutController#success as HTML
  Parameters: {"PayerID"=>"sandboxpayerid", "id"=>"15", "token"=>"EC-sandboxtoken"}
  Cart Load (0.3ms)  SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = ? LIMIT 1  [["id", 3]]
  Order Load (0.4ms)  SELECT `orders`.* FROM `orders` WHERE `orders`.`id` = ? LIMIT 1  [["id", "15"]]
  SQL (0.1ms)  BEGIN
  OrderItem Load (0.3ms)  SELECT `order_items`.* FROM `order_items` WHERE `order_items`.`order_id` = 15
   (0.3ms)  UPDATE `orders` SET `updated_at` = '2018-12-13 09:22:59', `express_payer_id` = 'sandboxpayerid' WHERE `orders`.`id` = 15
   (0.7ms)  COMMIT
  Product Load (0.4ms)  SELECT `products`.* FROM `products` WHERE `products`.`id` = 3 LIMIT 1
  Rendered checkout/success.html.erb within layouts/application (2.8ms)
  Rendered layouts/_header.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 17ms (Views: 9.5ms | ActiveRecord: 2.5ms)

How can I get this to work and does Active Merchant + Paypal Express Checkout + EU merchant work anyway?

Aucun commentaire:

Enregistrer un commentaire