我正在贝宝中使用此宝石进行支付https://github.com/tc/paypal_adaptive
我对这块宝石感到困惑和迷茫。它的文档很差,我很难理解如何从paypal获得ipn响应的数据。
我希望这个问题能帮助更多有同样问题的人。
我的步骤是:
1º我从我的orders_controller.rb
向贝宝发送请求,方法为preproval_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
我在orders_controller.rb
中有response_paypal
动作。这是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º为此,我在lib/paypal_ipn.rb
中创建了一个文件,并将来自https://github.com/tc/paypal_adaptive/blob/master/templates/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
在我的路线.rb中,我添加了:
match "/ipn_notification" => PaypalIpn
我的两个问题是:
a)我没有看到付款后这个文件会被解雇,我也没有在控制台中看到我从贝宝获得的数据。
b)我想在请求中向贝宝发送对象@gift
的id,以便稍后在paypal_ipn.rb
中恢复并保存我的数据库。
我做错了什么?如何解决这些问题
感谢
我没有用过那个宝石,但我以前用过PayPal IPN。这里有一些你应该检查的东西:
-
您是否已将PayPal帐户设置为使用IPN?您必须在帐户上启用此设置才能正常工作。
-
您是否验证过,当您在付款过程中通过ipn_notification_url时,它与您的"/ipn_notification"路线相匹配?
-
为了实现这一点,PayPal必须能够直接与运行该应用程序的服务器进行通信。这意味着,通常情况下,除非您在本地机器上使用动态DNS或其他功能进行自定义设置,否则您需要将此代码实际部署到服务器上,以便PayPal能够与您的应用程序通信。换句话说,如果这是在
http://localhost:3000
上运行的,这将不起作用。
为了回答你的第二个问题,如何恢复@gift,以便在你的数据库中记录它被支付的事实,我不完全确定如何使用这个宝石,但我会告诉你我是如何使用ActiveMerchant完成的——它可能很相似。
-
在PayPal的付款请求中,您可以输入发票号码。我相信这个字段就是"发票"。在这里你可以传递礼物的ID。
-
当PayPal通过IPN通知您的应用程序订单已付款时,它会将发票号码传回您。使用这个发票号码检索@gift,然后你可以用它做你需要的事情。
以下是我使用ActiveMerchant gem的PayPal工作代码的相关部分:https://gist.github.com/3198178
祝你好运!