Я пытаюсь внедрить Paypal Express Checkout в приложение Rails.Я хотел бы сохранить свой заказ со всеми данными клиента в моей базе данных заказов и отправить клиента в PayPal, где он должен принять платеж и быть перенаправлен на мой веб-сайт.
Торговец PayPal будет локализованв ЕС.Я уточняю это, так как я прочитал, что Active Merchant имеет или имел некоторые ограничения в этом отношении.
Я могу быть перенаправлен на PayPal, войдите в систему, вернитесь на мой веб-сайт, где написаны express_token
и express_payer_id
в базу данных orders
.
К сожалению, единственным параметром, который, по-видимому, передается PayPal, является :description => 'Books'
, на странице песочницы PayPal больше ничего не отображается, и платеж не выполняется.
относительная часть моего checkout_controller:
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
Закрытое действие checkout_paypal в том же контроллере:
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
Закрытое действие populate_order в том же контроллере:
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
Когда я добавляю :items => @order.order_items
в опции checkout_paypal
, я получаю https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=
с No token passed
и статус: failed
, а такжеerror_message ok
записывается в базу данных заказов.
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)
Сообщение об ошибке "ok" очевидно изумительно!
Когда я не добавляю его, я продолжаю:
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
записывается в @ order.status
, а также EC-sandboxtoken
to @ order.express_token
и sandboxpayerid
to @ order.express_payer_id,
, но в окне Paypal Express ничего не отображается:
Это результат перенаправления:
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)
Как я могу заставить это работать и делает Active Merchant +PayPal Express Checkout + ЕС работает в любом случае?