@@ -261,27 +261,60 @@ Callback.valid_signature? received_signature, callback_url, received_params, rec
261261require ' sinatra/base'
262262require ' phaxio'
263263
264- class PhaxioCallbackExample < Sinatra ::Base
265- Phaxio .config do |config |
266- config.api_key = ' 0123456789'
267- config.api_secret = ' 0123456789'
268- config.callback_token = ' 0123456789'
269- end
270-
271- post ' /phaxio_callback' do
264+ class PhaxioWebhookExample < Sinatra ::Base
265+ Phaxio .callback_token = ' YOUR WEBHOOK TOKEN HERE'
266+
267+ post ' /webhook' do
272268 signature = request.env[' HTTP_X_PHAXIO_SIGNATURE' ]
273269 url = request.url
274- file_params = params[:filename ]
275- if Phaxio ::Callback .valid_signature? signature, url, callback_params , file_params
270+ file_params = params[:file ]
271+ if Phaxio ::Callback .valid_signature? signature, url, webhook_params , file_params
276272 ' Success'
277273 else
278- ' Invalid callback signature'
274+ ' Invalid webhook signature'
279275 end
280276 end
281277
282- def callback_params
278+ def webhook_params
283279 params.select do |key , _value |
284- %w(success is_test direction fax metadata message) .include?(key)
280+ %w(success is_test direction fax metadata message event_type) .include?(key)
281+ end
282+ end
283+ end
284+ ```
285+
286+ ## Webhook Validation Example with Rails Controller
287+
288+ ``` ruby
289+ class WebhookController < ApplicationController
290+ skip_before_action :verify_authenticity_token
291+
292+ def index
293+ signature = request.headers[' X-Phaxio-Signature' ]
294+ Phaxio .callback_token = ' YOUR WEBHOOK TOKEN HERE'
295+ url = request.original_url
296+
297+ Rails .logger.debug " URL: " + url
298+ Rails .logger.debug " Signature: " + signature
299+ Rails .logger.debug " params: " + params.inspect
300+ Rails .logger.debug " webhook_params: " + webhook_params.to_h.inspect
301+
302+ if Phaxio ::Callback .valid_signature? signature, url, webhook_params.to_h, file_params
303+ Rails .logger.debug " Success"
304+ render plain: ' Success'
305+ else
306+ Rails .logger.debug " Invalid callback signature"
307+ render plain: ' Invalid callback signature'
308+ end
309+ end
310+
311+ def webhook_params
312+ params.permit(:success , :is_test , :direction , :fax , :metadata , :event_type , :message )
313+ end
314+
315+ def file_params
316+ if params[:file ]
317+ [{ :name => ' file' , :tempfile => params[:file ].tempfile }]
285318 end
286319 end
287320end
0 commit comments