是否有针对Ruby on Rails的PayPal IPN代码示例?
有几种语言的官方代码示例,但无法找到Rails的一个。
我在这里发布了Rails控制器的工作代码示例。 它做validation。 我希望它会有用。
class PaymentNotificationsController < ApplicationController protect_from_forgery :except => [:create] #Otherwise the request from PayPal wouldn't make it to the controller def create response = validate_IPN_notification(request.raw_post) case response when "VERIFIED" # check that paymentStatus=Completed # check that txnId has not been previously processed # check that receiverEmail is your Primary PayPal email # check that paymentAmount/paymentCurrency are correct # process payment when "INVALID" # log for investigation else # error end render :nothing => true end protected def validate_IPN_notification(raw) uri = URI.parse('https://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate') http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = 60 http.read_timeout = 60 http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.use_ssl = true response = http.post(uri.request_uri, raw, 'Content-Length' => "#{raw.size}", 'User-Agent' => "My custom user agent" ).body end end
代码的灵感来自Railscast 142和Tanel Suurhans的这篇文章
PayPal的Ruby Merchant SDK提供了ipn_valid?
布尔方法让你超级容易。
def notify @api = PayPal::SDK::Merchant.new if @api.ipn_valid?(request.raw_post) # return true or false # params contains the data end end
https://github.com/paypal/merchant-sdk-ruby/blob/master/samples/IPN-README.md
IPNgem
DWilke的Paypal IPNgem可以在这里找到:
https://github.com/dwilkie/paypal
查看IPN模块。 这是很好的代码:
https://github.com/dwilkie/paypal/blob/master/lib/paypal/ipn/ipn.rb
针对模拟器进行测试
您可以在此处针对IPN模拟器对其进行测试:
https://developer.paypal.com/webapps/developer/applications/ipn_simulator
我使用ngrok在公共URL上公开localhost:3000,然后将模拟器指向它。
我在我的一个项目中实现了IPN,你的代码看起来很好。 那么你面临的问题是什么?
看看ActiveMerchant gem,其中包括多个网关实现,其中包括Paypal的IPN 。
HTH
你可以这样做来获取ipn细节。 结果将显示您是否已validation。 你可以从身体获得所有细节
发布’/ english / ipn’做
url =“ https://sandbox.paypal.com/cgi-bin/webscr?cmd=_notify-validate&# {@query}”
body = request.body.string
result = RestClient.post url,body
结束