Skip to content

Instantly share code, notes, and snippets.

@simonhamp
Last active November 17, 2023 17:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonhamp/f5bf9f890c274c14715d298c32ff1df6 to your computer and use it in GitHub Desktop.
Save simonhamp/f5bf9f890c274c14715d298c32ff1df6 to your computer and use it in GitHub Desktop.
Handling Typeform Webhooks in Laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class WebhookController
{
public function create(Request $request)
{
abort_unless($this->typeformSignatureMatches($request), 403);
$answers = collect($request->input('form_response.answers'))
->keyBy('field.ref')
->transform(function ($answer) {
return $answer[$answer['type']];
});
// Then you can use form values directly from the collection, e.g.:
$answers->get('email');
}
protected function typeformSignatureMatches(Request $request)
{
$body = $request->getContent();
$provided_signature = $request->header('Typeform-Signature');
$actual_signature = 'sha256=' . base64_encode(hash_hmac('sha256', $body, env('TYPEFORM_SECRET'), true));
return hash_equals($actual_signature, $provided_signature);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment