Last active
August 29, 2015 14:23
-
-
Save mediocretes/511f49ce2febaf245df1 to your computer and use it in GitHub Desktop.
Filtering webhook events in chargify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before_filter :validate_chargify_webhook | |
def validate_chargify_webhook | |
signature = request.headers["X-Chargify-Webhook-Signature-Hmac-Sha-256"].to_s | |
body = request.body.read | |
computed_signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), | |
YOUR_CHARGIFY_SITE_KEY, | |
body) | |
head :unauthorized unless signature == computed_signature | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EVENTS_ABOUT_WHICH_WE_CARE = ["payment_success", "billing_date_change"] | |
def handle_event | |
event = params["event"] | |
return head(:ok) unless EVENTS_ABOUT_WHICH_WE_CARE.include?(event) | |
# your code here | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EVENTS_ABOUT_WHICH_WE_CARE = ["payment_success", "billing_date_change"] | |
def handle_event | |
event = params["event"] | |
payload = params["payload"] | |
return head(:unprocessable_entity) if event.blank? || payload.blank? | |
return head(:ok) unless EVENTS_ABOUT_WHICH_WE_CARE.include?(event) | |
self.send(event, payload) | |
end | |
private | |
def payment_success(payload) | |
# do things | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SOME_SUBSCRIPTION_EVENTS = ["payment_success", "billing_date_change"] | |
def handle_event | |
event = params["event"] | |
payload = params["payload"] | |
return head(:unprocessable_entity) unless if event.blank? || payload.blank? | |
return head(:ok) unless SOME_SUBSCRIPTION_EVENTS.include?(event) | |
subscription_id = payload["subscription"]["id"] | |
user = User.find_by_subscription_id(subscription_id) | |
# probably put this in the user model, but it is included here for simplicity | |
# this assumes you've already set things up using activeresource | |
subscription = Chargify::Subscription.find(self.subscription_id) | |
# "active" is just an example, your model will vary | |
user.active = subscription.state == "active" | |
# ... and so on | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment