Webhook Code Examples

Example Payload

1
{
2
"id": "237d6330-f8e0-410b-b927-dd7b07bf99ec",
3
"ip": null,
4
"event": "verification.pending",
5
"trust_id": 1,
6
"created_at": "2020-09-11T01:33:51.000000Z",
7
"ip_country": null,
8
"user_status": "Active",
9
"reference_id": null,
10
"last_activity": "2020-11-10 12:27:16",
11
"verifications": [
12
{
13
"id": 1,
14
"name": "Email",
15
"status": {
16
"value": 0,
17
"friendly": "Pending"
18
}
19
},
20
{
21
"id": 2,
22
"name": "Phone / SMS",
23
"status": {
24
"value": 0,
25
"friendly": "Pending"
26
}
27
},
28
{
29
"id": 3,
30
"name": "Document / ID",
31
"status": {
32
"value": 0,
33
"friendly": "Pending"
34
}
35
}
36
]
37
}
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['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['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!
Last modified 4mo ago