Skip to content

Instantly share code, notes, and snippets.

@JoeWoodward
Created July 4, 2013 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeWoodward/5929173 to your computer and use it in GitHub Desktop.
Save JoeWoodward/5929173 to your computer and use it in GitHub Desktop.
webhooks controller for chargify
# use md5 digest to generate keys
require 'digest/md5'
class Chargify::HooksController < ApplicationController
# don't want a user to be logged in because these are callbacks from chargify
skip_before_filter :require_login
skip_before_filter :is_user_authorised
protect_from_forgery :except => :dispatch_handler
before_filter :verify, :only => :dispatch_handler
# chargify api events that apply to our app http://docs.chargify.com/api-events
EVENTS = %w[ test signup_success signup_failure renewal_success renewal_failure payment_success payment_failure billing_date_change subscription_state_change subscription_product_change ].freeze
# this method is called when chargify sends a callback
def dispatch_handler
event = params[:event]
unless EVENTS.include? event
render :nothing => true, :status => 404 and return
end
begin
# line #156
convert_payload
# call the correct method
self.send event
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def test
# test callbacks can be sent from chargify's admin panel
Rails.logger.debug "Chargify Webhook test!"
render :nothing => true, :status => 200
end
def signup_success
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.chargigy_subscription_id = @subscription.id
@user.state = @subscription.state
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save(:validate => false)
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def signup_failure
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.state = @subscription.state
@user.chargigy_subscription_id = @subscription.id
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save(:validate => false)
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def renewal_success
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.state = @subscription.state
@user.chargigy_subscription_id = @subscription.id
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save(:validate => false)
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def renewal_failure
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.state = @subscription.state
@user.chargigy_subscription_id = @subscription.id
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save(:validate => false)
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def payment_success
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.state = @subscription.state
@user.chargigy_subscription_id = @subscription.id
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save(:validate => false)
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def payment_failure
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.state = @subscription.state
@user.chargigy_subscription_id = @subscription.id
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save(:validate => false)
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def billing_date_change
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.state = @subscription.state
@user.chargigy_subscription_id = @subscription.id
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save(:validate => false)
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def subscription_state_change
begin
@user = User.find_by_token(@subscription.customer.reference)
@user.state = @subscription.state
@user.chargigy_subscription_id = @subscription.id
@user.subscription_billing_date = @subscription.current_period_ends_at
@user.save
render :nothing => true, :status => 200
rescue Exception => e
render :nothing => true, :status => 422 and return
end
end
def subscription_product_change
render :nothing => true, :status => 200
end
protected
def verify
if params[:signature].nil?
params[:signature] = request.headers["HTTP_X_CHARGIFY_WEBHOOK_SIGNATURE"]
end
unless Digest::MD5.hexdigest(site_key + request.raw_post) == params[:signature]
render :nothing => true, :status => :forbidden
end
end
def convert_payload
# chargify can return two types of payloads, transactions and subscriptions
if params[:payload].has_key? :transaction
@transaction = Chargify::Transaction.new params[:payload][:transaction]
end
if params[:payload].has_key? :subscription
@subscription = Chargify::Subscription.new params[:payload][:subscription]
end
end
def site_key
# this is configured on the server and imported for security
ENV['CHARGIFY_SITE_KEY']
end
end
@kumario
Copy link

kumario commented Jul 6, 2013

I remember this taking forever when our previous team dropped in Stripe. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment