Saving JSON to a Database in Ruby on Rails from a Venmo Webhook

407 views Asked by At

I'm new to JSON, callbacks, and webhooks so I appreciate any help.

I'm looking to use Venmo's webhook here: (https://developer.venmo.com/docs/webhooks) to collect JSON and store it in the database of my Rails app on heroku.

Any help on how to set up the receive action would be a huge help!

My Controller:

class WebhooksController < ApplicationController 
  skip_before_filter :verify_authenticity_token

  def receive
  end

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

end

My routes:

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

My heroku logs

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

There are 2 answers

1
vansan On

The venmo call isn't to the right url. It's making a GET to: "/articles?venmo_challenge=b376d17e-977b-4a91-829c-b2eb9a074686"

But you want it to make a GET to: "/webhooks/receive?venmo_challenge=b376d17e-977b-4a91-829c-b2eb9a074686"

I don't know where you set this in venmo, but it looks like it's set wrong. As a result, the action is hitting your ArticlesController instead of WebhooksController.

0
Will Dennis On

Here is what the final controller ended up looking like:

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