从Venmo Webhook将RSON保存到Ruby on Rails中的数据库

时间:2014-11-17 01:20:57

标签: ruby-on-rails json parsing webhooks venmo

我是JSON,回调和webhook的新手,所以我感谢您的帮助。

我希望在这里使用Venmo的webhook:(https://developer.venmo.com/docs/webhooks)来收集JSON并将其存储在heroku上我的Rails应用程序的数据库中。

有关如何设置receive操作的任何帮助都将是一个巨大的帮助!

我的控制器:

class WebhooksController < ApplicationController 
  skip_before_filter :verify_authenticity_token

  def receive
  end

  def verify
      #venmo verification  
      render text: params[:venmo_challenge]      
  end

end

我的路线:

post '/webhooks/receive' => 'webhooks#receive'
get  '/webhooks/receive' => 'webhooks#verify'

我的heroku记录

2014-11-17T23:42:54.770290+00:00 heroku[web.1]: State changed from starting to up
2014-11-17T23:43:41.105707+00:00 heroku[router]: at=info method=POST path="/webhooks/receive" host=infinite-badlands-3074.herokuapp.com request_id=0ad13de0-f44c-4de3-b068-1e82800c770a fwd="184.73.153.84" dyno=web.1 connect=1ms service=79ms status=200 bytes=1145
2014-11-17T23:43:41.032647+00:00 app[web.1]: Started POST "/webhooks/receive" for 184.73.153.84 at 2014-11-17 23:43:41 +0000
2014-11-17T23:43:41.100217+00:00 app[web.1]:   Rendered webhooks/receive.html.erb within layouts/application (1.2ms)
2014-11-17T23:43:41.090310+00:00 app[web.1]: Processing by WebhooksController#receive as */*
2014-11-17T23:43:41.090341+00:00 app[web.1]:   Parameters: {"date_created"=>"2014-11-17T17:22:51.414052", "type"=>"payment.created", "data"=>{"status"=>"settled", "id"=>"1"}, "webhook"=>{}}
2014-11-17T23:43:41.090879+00:00 app[web.1]: WARNING: Can't verify CSRF token authenticity
2014-11-17T23:43:41.102859+00:00 app[web.1]: Completed 200 OK in 12ms (Views: 11.6ms | ActiveRecord: 0.0ms)

2 个答案:

答案 0 :(得分:1)

venmo呼叫不是正确的网址。它正在进行GET:"/articles?venmo_challenge=b376d17e-977b-4a91-829c-b2eb9a074686"

但是你希望它能够获得:"/webhooks/receive?venmo_challenge=b376d17e-977b-4a91-829c-b2eb9a074686"

我不知道你在venmo中将它设置在哪里,但看起来它设置错了。因此,该操作将命中您的ArticlesController而不是WebhooksController。

答案 1 :(得分:1)

以下是最终控制器的结果:

class WebhooksController < ApplicationController 


  def index
    @webhooks = Webhook.all
  end

  def receive
    @webhook = Webhook.new    :date_created => params[:date_created],
                              :payment_type => params[:type],
                              :action =>  params[:data][:action],
                              :about =>  params[:data][:actor][:about]
                              :date_joined =>  params[:data][:actor][:date_joined],
                              :display_name =>  params[:data][:actor][:display_name],
                              :first_name =>  params[:data][:actor][:first_name],
                              :actor_id =>  params[:data][:actor][:id],
                              :last_name =>  params[:data][:actor][:last_name],
                              :profile_picture_url =>  params[:data][:actor][:profile_picture_url],
                              :username =>  params[:data][:actor][:username],
                              :amount =>  params[:data][:amount],
                              :audience =>  params[:data][:audience],
                              :date_completed =>  params[:data][:date_completed],
                              :payment_date_created =>  params[:data][:date_created],
                              :payment_id =>  params[:data][:id],
                              :note =>  params[:data][:note],
                              :status =>  params[:data][:status],
                              :target_email =>  params[:data][:target][:email],
                              :target_phone =>  params[:data][:target][:phone],
                              :target_type =>  params[:data][:target][:type],
                              :target_user_about =>  params[:data][:target][:user][:about],
                              :target_user_date_joined =>  params[:data][:target][:user][:date_joined],
                              :target_user_display_name =>  params[:data][:target][:user][:display_name],
                              :target_user_first_name =>  params[:data][:target][:user][:first_name],
                              :target_user_id =>  params[:data][:target][:user][:id],
                              :target_user_last_name =>  params[:data][:target][:user][:last_name],
                              :target_user_profile_picture_url =>  params[:data][:target][:user][:profile_picture_url],
                              :target_user_username =>  params[:data][:target][:user][:username]
    @webhook.save
  end

  def verify
      #venmo verification  
      render text: params[:venmo_challenge]
  end

end