Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 是否有Ruby on Rails的PayPal IPN代码示例?_Ruby On Rails_Paypal Ipn - Fatal编程技术网

Ruby on rails 是否有Ruby on Rails的PayPal IPN代码示例?

Ruby on rails 是否有Ruby on Rails的PayPal IPN代码示例?,ruby-on-rails,paypal-ipn,Ruby On Rails,Paypal Ipn,有几种语言的官方代码示例,但找不到Rails的示例。我在这里发布了Rails控制器的工作代码示例。它进行验证。我希望它会有用 class PaymentNotificationsController < ApplicationController protect_from_forgery :except => [:create] #Otherwise the request from PayPal wouldn't make it to the controller def

有几种语言的官方代码示例,但找不到Rails的示例。

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

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
class PaymentNotificationsController[:创建]#否则来自PayPal的请求不会发送到控制器
def创建
响应=验证IPN通知(request.raw\u post)
个案回应
“验证”时
#检查paymentStatus=已完成
#检查txnId之前是否未被处理
#检查receiverEmail是否是您的主要PayPal电子邮件
#检查paymentAmount/paymentCurrency是否正确
#处理付款
当“无效”时
#调查日志
其他的
#错误
结束
render:nothing=>true
结束
受保护的
def验证IPN通知(原始)
现场直播https://ipnpb.paypal.com/cgi-bin'
沙箱https://ipnpb.sandbox.paypal.com/cgi-bin'
uri=uri.parse(sandbox+'/webscr?cmd=\u notify-validate')
http=Net::http.new(uri.host,uri.port)
http.open\u超时=60
http.read_timeout=60
http.verify\u mode=OpenSSL::SSL::verify\u PEER
http.use_ssl=true
response=http.post(uri.request\u uri,raw,
'内容长度'=>“#{raw.size}”,
“用户代理”=>“我的自定义用户代理”
).身体
结束
结束

代码的灵感来源于,本文的灵感来源于

我在我的一个项目中实现了IPN,您的代码看起来不错。那么您面临的问题是什么呢?

看看gem,它包括多个网关实现,其中包括


HTH

贝宝的Ruby Merchant SDK提供了一个
ipn\u有效?
boolean方法,让您可以轻松完成此任务

def notify
  @api = PayPal::SDK::Merchant.new
  if @api.ipn_valid?(request.raw_post)  # return true or false
    # params contains the data
  end
end

您只需执行此操作即可获取ipn详细信息。结果将显示您是否已验证。您可以从body获得所有详细信息

post'/english/ipn'do

url=“{@query}”

body=request.body.string

结果=RestClient.post url,正文

结束

IPN gem DWilke的Paypal IPN gem可在以下位置找到:

查看IPN模块。这是一个很好的代码:

针对模拟器进行测试 您可以在此处对IPN模拟器进行测试:


我使用ngrok在公共URL上公开localhost:3000,然后将模拟器指向它。

有一些PayPal Gem,其中至少有一个(PayPal sdk rest)包括
PayPal::sdk::Core::API::IPN.valid?
方法

以下是如何使用它:

class YourController < ApplicationController

  skip_before_action :verify_authenticity_token, only: :your_action

  def your_action
    verified = PayPal::SDK::Core::API::IPN.valid?(request.raw_post)

    if verified
      # Verification passed, do something useful here.
      render nothing: true, status: :ok
    else
      # Verification failed!
      render nothing: true, status: :unprocessable_entity
    end
  end

end
class YourController
Hi Rovin。是的,我的代码工作正常,谢谢确认。我之所以把它贴在这里,是因为我花了一些时间才想出了有效的解决方案,而且因为没有官方的例子,我认为其他人可以从中受益。你知道贝宝支付标准是否在所有国家都得到支持吗?根据活跃商户的文档,这些是受支持的PayPal网关:PayPal Express Checkout-US、CA、SG、AU PayPal Payflow Pro-US、CA、SG、AU PayPal网站支付专业版(英国)-UK PayPal网站支付专业版(CA)-CA PayPal Express Checkout-US PayPal网站支付专业版(美国)-US@joscas,抱歉,没有。我在英国和美国都使用了AM的PayPal IPN,没有问题。对不起,我帮不了你了。真的很有帮助,谢谢!尽管需要注意的是,如果他们真的想确保安全,就必须使用OpenSSL::SSL::VERIFY_PEER。这为我节省了下午的大量时间。我现在可以早点回家了。非常感谢!这节省了我很多时间,谢谢!另外,记住在不处于生产模式时使用沙盒paypal URL进行验证!()哦,关于“检查txnId之前没有被处理过”,这里有一个重要的问题。如果在“完成”付款状态之前收到“待定”付款状态,则这两个状态都将具有相同的交易ID。换句话说,为了澄清,您必须验证仅收到一个带有txn_ID的“完成”通知。url现在已更改,在我意识到问题之前的半天我一直在处理此问题。以下是新的URL:
live=https://ipnpb.paypal.com/cgi-bin‘沙箱’https://ipnpb.sandbox.paypal.com/cgi-bin'uri=uri.parse(sandbox+'/webscr?cmd=\u notify-validate')
确保添加
保护\u免受伪造,除了:[:notify]
发送到您的控制器,以便帖子不会因为无法验证CSRF令牌的真实性而被拒绝。