Webhook Code Examples

Example Payload

1
{
2
"id":"7d2dde92-885b-4159-8772-0367e4e39b6f",
3
"ip":"127.0.0.1",
4
"event":"verification.pending",
5
"email":"[email protected]",
6
"trust_id":28,
7
"created_at":"2022-01-28T21:06:44.000000Z",
8
"ip_country":"US",
9
"user_status":"Active",
10
"reference_id":null,
11
"last_activity":"2022-01-28 22:46:44",
12
"verifications":[
13
{
14
"id":3,
15
"name":"Document \/ ID",
16
"status":{
17
"value":1,
18
"friendly":"Processing"
19
},
20
"attributes":{
21
"workflow":"ID + Selfie"
22
}
23
},
24
{
25
"id":1,
26
"name":"Email",
27
"status":{
28
"value":0,
29
"friendly":"Assigned"
30
}
31
}
32
]
33
}
Copied!

Example Handling

1
$payload = @file_get_contents('php://input');
2
$event = null;
3
$json = json_decode($payload);
4
5
//first lets validate the request!
6
$computed_signature = hash_hmac('sha256', file_get_contents("php://input"), $configuredSigningSecret);
7
$received_signature = $_SERVER['HTTP_SIGNATURE'];
8
9
if($computed_signature !== $received_signature) {
10
http_response_code(500);
11
die('Tampered Request');
12
}
13
14
foreach($json->verifications as $verification) {
15
switch($verification->name) {
16
case "Email":
17
18
break;
19
case "Phone / SMS":
20
21
break;
22
default:
23
echo 'We received a different verification method! - '.$verification->verification;
24
}
25
}
26
27
http_response_code(200);
Copied!

Example Signature Verification

1
public function verifyWebhook(Request $request)
2
{
3
4
$computed_signature = hash_hmac(
5
'sha256',
6
$request->getContent(),
7
config('services.trustswiftly.signature_secret')
8
);
9
10
$received_signature = $_SERVER['HTTP_SIGNATURE'];
11
12
if($computed_signature !== $received_signature) {
13
\Log::info('received_signature wrong');
14
return response('Tampered Request', 500);
15
}
16
17
$user = User::find($request->reference_id);
18
19
if (! $user) {
20
\Log::info('not found user');
21
return response('Tampered Request', 500);
22
}
23
24
$requiredVerifications = [];
25
foreach($request->verifications as $verification) {
26
$requiredVerifications[$verification['id']] = $verification['status']['value'];
27
}
28
29
$user->required_verifications = $requiredVerifications;
30
$user->save();
31
32
return response(200);
33
}
Copied!