What are webhooks

A Webhook is a method of altering the behavior of a web page, or web application, with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the originating website or application.
WebHooks are an HTTP POST callback request sent to URL of a user’s choice in response to some event occurring. They offer simple and effective server to server communication without long running connections

How do I implement WebHooks?

Simply provide your users with the ability to submit their own URL, and POST to that URL when something happens. It's that simple. There are no specs you have to follow.

Uses of Webhooks:

1) Push: receiving data in real time

Push is the simplest of reasons to use WebHooks. No more polling every couple of minutes to find out if there is new information. Just register a WebHook and receive the data at your doorstep as soon as it exists. It's less work, less hassle, and you'll probably even receive it sooner than if you were asking for it every couple of minutes.

2) Pipes: receiving data and passing it on

A Pipe happens when your WebHook not only receives real-time data, but goes on to do something new and meaningful with it, triggering actions unrelated to the original event. For example, you create a script, register its URL at a photo site, and have it email you when your mother posts a new photo.

3) Plugins: processing data and giving something in return

This is where the entire web becomes a programming platform. You can use this form of WebHooks to allow others to extend your application. Facebook's Application Platform uses WebHooks in this way. The general idea is that a web application sending out data via WebHooks will also use the response to modify its own data.

Basics of webhooks

fig. Basic functionality of webhooks.

In our project we used stripe webhooks. Stripe is a payment gateway. Our requirement was very similar to the point '2' stated aboove. We wanted to send an email to the customer once the customer is successfully charged( As stripe does not send emails in the test mode ).

We used the 'stripe-event' gem to deal with the events that stripe provides us with.

Installation:

Add the following to your gem file:

gem 'stripe_event'

Then run,

bundle install

Add to the routes:
For eg:

mount StripeEvent::Engine => '/payment-webhook'

Usage:

A) Stripe API:

  1. Under 'Your Account' << 'Account Settings' << 'Webhooks'

Add the URL of your app:

Adding URLs for Stripe Webhook

  1. Stripe provides us with numerous events. Select the desired event from the event dropdown and click on 'Send test webhook':

Adding events

In our project, we made use of the 'charge.succeeded' event other events consist of 'charge.failed', 'customer.created' etc.

B) In your code:

config/initializers/stripe.rb

Stripe.api_key = #This is the secret key that the stripe provides.
You can view this under 'Your Account' << 'Account Settings' << 'API keys'

StripeEvent.configure do |events|
  events.all Hooks.new(Rails.logger)
  events.subscribe 'charge.succeeded', ChargeSucceeded.new
end

StripeEvent.configure do |events|
  events.subscribe 'charge.succeeded' do |event|
    event.class
    event.type
    event.data.object
  end
end

Subscriber objects that respond to #call

lib/classes/hooks.rb

class Hooks
  def initialize(logger)
    @logger = logger
  end

  def call(event)
    @logger.info "BILLING:#{event.type}:#{event.id}"
  end
end

Perform tasks after the charge has succeeded:

lib/classes/charge_succeeded.rb

class ChargeSucceeded
  def call(event)
    customer_id = event.data.object.customer
    // Retrieving customer_id
    email = Stripe::Customer.retrieve(customer_id).email
    // Retrieve customer's email id to send an email
    // Code to mail the customers on successful payment
  end
end

To view the stripe_event gem doc click [here(stripe_event doc)][4]

To learn more about stripe visit [stripe.com(stripe.com)][5]
[4]: https://github.com/integrallis/stripe_event
[5]: https://stripe.com/