Skip to content

Instantly share code, notes, and snippets.

@mediocretes
Last active August 29, 2015 14:23
Show Gist options
  • Save mediocretes/d74cce5d014aa9aee6ec to your computer and use it in GitHub Desktop.
Save mediocretes/d74cce5d014aa9aee6ec to your computer and use it in GitHub Desktop.
How to use a webhook to trigger subscription data refresh
EVENTS_THAT_INCLUDE_SUBSCRIPTION_INFORMATION = ["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 EVENTS_ABOUT_WHICH_WE_CARE.include?(event)
subscription_id = payload["subscription"]["id"]
user = User.find_by_subscription_id(subscription_id)
# normally I'd suggest putting this in the user model, but it is included here for simplicity
subscription = Chargify::Subscription.find(self.subscription_id) # assuming you've already set things up using activeresource
user.active = subscription.state == "active" # for example, your user model may vary
# ... and so on
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment