我正在使用这个 gem 在 paypal https://github.com/tc/paypal_adaptive中付款
我对这颗宝石感到非常困惑和迷失方向。它的文档记录很差,我很难理解如何从贝宝获取关于 ipn 响应的数据。
我希望这个问题能帮助更多有同样问题的人。
我的步骤是:
1º我orders_controller.rb
使用方法 preaproval_payment 从我的贝宝发送请求。
def preapproval_payment
preapproval_request = PaypalAdaptive::Request.new
data = {
"returnUrl" => response_paypal_user_orders_url(current_user),
"cancelUrl"=> cancel_payment_gift_url(@gift),
"requestEnvelope" => {"errorLanguage" => "en_US"},
"senderEmail" => "gift_1342711309_per@gmail.com",
"startingDate" => Time.now,
"endingDate" => Time.now + (60*60*24) * 30,
"currencyCode"=>"USD",
"maxAmountPerPayment" => "@gift.price",
"ipnNotificationUrl" => ipn_notification_url,
"ip" => request.remote_ip
}
preapproval_response = preapproval_request.preapproval(data)
puts data
if preapproval_response.success?
redirect_to preapproval_response.preapproval_paypal_payment_url
else
redirect_to gift_url(@gift), alert: t(".something_was_wrong")
end
end
2º这些是我从命令的日志控制台中请求的数据puts data
:
{"returnUrl"=>"http://localhost:3000/en/u/maserranocaceres/orders/response_paypal", "cancelUrl"=>"http://localhost:3000/en/gifts/gift-1/cancel_payment", "requestEnvelope"=>{"errorLanguage"=>"en_US"}, "senderEmail"=>"gift_1342711309_per@gmail.com", "startingDate"=>2012-07-29 13:05:49 +0200, "endingDate"=>2012-08-28 13:05:49 +0200, "currencyCode"=>"USD", "maxAmountPerPayment"=>9, "ipnNotificationUrl"=>"http://localhost:3000/ipn_notification?locale=en", "ip"=>"127.0.0.1"}
3º我重定向到贝宝页面,我在贝宝上成功付款:D。
4º付款成功完成后,我将被引导至:
http://localhost:3000/en/u/maserranocaceres/orders/response_paypal
我有response_paypal
行动orders_controller.rb
。这是 GET 操作,我的操作代码是:
def response_paypal
respond_to do |format|
format.html { redirect_to user_orders_url(current_user), :alert => "works fine return url"}
end
end
到目前为止,一切正常。
现在我需要的是获取我从贝宝收到的数据,并在付款成功处理后为我的数据库保存一个新订单。
5º为此,我制作了一个文件,并将来自https://github.com/tc/paypal_adaptive/blob/master/templates/paypal_ipn.rblib/paypal_ipn.rb
的内容添加到该文件中
# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
class PaypalIpn
def self.call(env)
if env["PATH_INFO"] =~ /^\/paypal_ipn/
request = Rack::Request.new(env)
params = request.params
ipn = PaypalAdaptive::IpnNotification.new
ipn.send_back(env['rack.request.form_vars'])
if ipn.verified?
#mark transaction as completed in your DB
output = "Verified."
else
output = "Not Verified."
end
[200, {"Content-Type" => "text/html"}, [output]]
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
end
end
在我的routes.rb我添加:
match "/ipn_notification" => PaypalIpn
我的两个问题是:
a)我没有看到付款后这个文件被解雇,我在控制台数据中看不到我从贝宝获得的数据。
b)我想在我的请求中发送到贝宝,对象的 ID@gift
以便以后能够恢复paypal_ipn.rb
并保存我的数据库。
我做错了什么以及如何解决这些问题?
谢谢