是否有 Ruby on Rails 的 PayPal IPN 代码示例? [英] Is there a PayPal IPN code sample for Ruby on Rails?

查看:57
本文介绍了是否有 Ruby on Rails 的 PayPal IPN 代码示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多种语言的官方代码示例,但找不到用于 Rails 的代码示例.

There are official code samples for several languages but couldn't find one for Rails.

推荐答案

我在这里发布了我的 Rails 控制器工作代码示例.它进行验证.我希望它会有用.

I post here my working code sample for a Rails controller. It does verification. I hope it will be useful.

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)
    live = 'https://ipnpb.paypal.com/cgi-bin'
    sandbox = 'https://ipnpb.sandbox.paypal.com/cgi-bin'
    uri = URI.parse(sandbox + '/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

Code is inspired by Railscast 142 and this post by Tanel Suurhans

这篇关于是否有 Ruby on Rails 的 PayPal IPN 代码示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆