Repository navigation
Expand file tree
/
Copy pathquickstart.php
More file actions
72 lines (62 loc) · 2.72 KB
/
Copy pathquickstart.php
File metadata and controls
72 lines (62 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
/*
* Quickstart: create a verification session, read its decision and verify a webhook.
*
* DIDIT_API_KEY=... DIDIT_WORKFLOW_ID=... php examples/quickstart.php
*/
require __DIR__ . '/../vendor/autoload.php';
use Didit\DiditClient;
use Didit\Exception\DiditException;
use Didit\Webhooks\Events\SessionEvent;
$workflowId = getenv('DIDIT_WORKFLOW_ID');
if (false === $workflowId || '' === $workflowId) {
fwrite(\STDERR, "Set DIDIT_WORKFLOW_ID to the id of a workflow from the Didit Console.\n");
exit(1);
}
$baseUrl = getenv('DIDIT_BASE_URL');
// The API key comes from DIDIT_API_KEY. DIDIT_BASE_URL is only needed for a local mock server.
$didit = new DiditClient(baseUrl: false === $baseUrl || '' === $baseUrl ? null : $baseUrl);
try {
// 1. Create a session and send your user to its URL.
$session = $didit->sessions->create([
'workflow_id' => $workflowId,
'vendor_data' => 'user-123',
]);
$sessionId = $session->sessionId ?? throw new UnexpectedValueException('The response has no session_id.');
echo "Created session {$sessionId} ({$session->status})\n";
echo "Verification URL: {$session->url}\n";
// 2. Read the decision once the user has finished (your callback URL or a webhook tells you when).
$decision = $didit->sessions->retrieveDecision($sessionId);
echo "Decision: {$decision->status}\n";
// 3. Verify a webhook. This signs a sample delivery the way Didit does; in your
// endpoint, pass the raw request body and the request headers instead.
$secret = getenv('DIDIT_WEBHOOK_SECRET');
$secret = false === $secret || '' === $secret ? 'whsec_test_quickstart' : $secret;
$now = time();
$payload = [
'webhook_type' => 'status.updated',
'event_id' => '6f1c2d3e-0000-4000-8000-000000000001',
'session_id' => $sessionId,
'status' => 'Approved',
'workflow_id' => $workflowId,
'vendor_data' => 'user-123',
'environment' => 'sandbox',
'created_at' => $now,
'timestamp' => $now,
];
ksort($payload, \SORT_STRING); // Didit sends sorted keys and no whitespace
$body = json_encode($payload, \JSON_UNESCAPED_SLASHES | \JSON_THROW_ON_ERROR);
$headers = [
'X-Signature' => hash_hmac('sha256', $body, $secret),
'X-Timestamp' => (string) $now,
];
$event = $didit->webhooks->verify($body, $headers, $secret);
echo 'Webhook verified: ' . $event::class . "\n";
if ($event instanceof SessionEvent) {
echo " {$event->webhookType}: session {$event->sessionId} is {$event->status} (vendor_data {$event->vendorData})\n";
}
} catch (DiditException $e) {
fwrite(\STDERR, 'Didit error: ' . $e->getMessage() . "\n");
exit(1);
}