[英]confused and disoriented with paypal ipn
我正在使用此gem在Paypal中付款https://github.com/tc/paypal_adaptive
我对这个宝石感到非常困惑和迷失。 它的文档记录不充分,让我很难理解如何在ipn响应中从Paypal获取数据。
我希望这个问题能帮助更多的人遇到同样的问题。
我的步骤是:
1º我使用orders_controller.rb
方法从我的orders_controller.rb
向Paypal发送请求。
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
在我的日志控制台中的请求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
到目前为止,一切正常。
现在,我需要获取从贝宝(Paypal)接收的数据,并在成功处理付款后将数据库保存为新订单。
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
在我的routes.rb中 ,添加:
match "/ipn_notification" => PaypalIpn
我的两个问题是:
一)我不认为这使得支付该文件被解雇后,我不能在我的控制台数据看,我来自PayPal获得。
b)我想发送给我的请求给paypal,对象@gift
的ID,以便以后可以在paypal_ipn.rb
恢复并保存我的数据库。
我在做错什么以及如何解决这些问题?
谢谢
我没有使用过该宝石,但是我以前使用过PayPal IPN。 您应该检查以下几件事:
您是否设置了PayPal帐户以使用IPN? 您必须在帐户上启用此设置才能起作用。
您是否已验证在付款过程中通过ipn_notification_url时是否与您的“ / ipn_notification”路由匹配?
为此,PayPal必须能够直接与运行此应用程序的服务器通信。 这意味着通常,除非您在具有动态DNS或其他内容的本地计算机上进行了自定义设置,否则您将需要实际将此代码部署到服务器上,以便PayPal能够与您的应用进行通信。 换句话说,如果它在http://localhost:3000
,则将无法使用。
要回答您的第二个问题,如何恢复@gift以便在数据库中记录它已支付的事实,我不完全确定如何使用此gem,但我将告诉您如何使用ActiveMerchant进行操作-可能非常相似。
在向PayPal的付款请求中,您可以输入发票编号。 我相信该字段仅称为“发票”。 在这里,您将传递礼物的ID。
当贝宝(PayPal)通过IPN通知您的应用程序订单已付款时,它将把发票编号传回给您。 使用此发票编号检索@gift,然后您就可以使用它进行所需的操作。
这是使用ActiveMerchant gem进行工作的PayPal代码的相关部分: https : //gist.github.com/3198178
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.