Last active
August 13, 2024 15:52
-
-
Save simonhamp/f5bf9f890c274c14715d298c32ff1df6 to your computer and use it in GitHub Desktop.
Handling Typeform Webhooks in Laravel
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
<?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