Last active
August 29, 2015 14:17
-
-
Save ssaunier/95572b4d49effdb8935f to your computer and use it in GitHub Desktop.
Respond to HelloSign callback (or webhook) from within a Rails application - https://www.hellosign.com/api/eventsAndCallbacksWalkthrough
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
# app/controllers/hello_sign_webhooks_controller.rb | |
require 'openssl' unless defined?(OpenSSL) | |
class HelloSignWebhooksController < ApplicationController | |
class InvalidSignature < StandardError; end | |
before_action :parse_params | |
before_action :check_event_hash! | |
def create | |
# TODO: implement your logic with what you received in `params[:event]` | |
render text: 'Hello API Event Received' | |
end | |
private | |
def check_event_hash! | |
message = "#{params[:event][:event_time]}#{params[:event][:event_type]}" | |
expected_hash = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, ENV['HELLOSIGN_API_KEY'], message) | |
unless ActiveSupport::SecurityUtils.secure_compare(params[:event][:event_hash], expected_hash) | |
fail InvalidSignature | |
end | |
end | |
def parse_params | |
params.merge! JSON.parse(params[:json]) | |
params.delete :json | |
end | |
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
# config/routes.rb | |
Rails.application.routes.draw do | |
# [...] | |
resource :hello_sign_webhooks, only: [ :create ] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment