-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpostback-handler.php
87 lines (70 loc) · 2.11 KB
/
postback-handler.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require_once __DIR__ . '/config.php';
/**
* Read reqeust data, extract params.
*/
$body = file_get_contents('php://input');
$params = json_decode($body, true);
/**
* Write log helper
* @param mixed $data
*/
function writeLog($data) {
$path = __DIR__ . '/postback.log';
if (is_writable($path)) {
file_put_contents($path, $data . PHP_EOL, FILE_APPEND);
}
}
/**
* Download signed file helper
* @param string $url
*/
function downloadFile($url, $prefix = 'signed') {
writeLog("Downloading signed file from " . $url);
// Using curl to download file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
$error = curl_error($ch);
// Log errors
if ($error) {
writeLog("Error: " . print_r($error, true));
exit;
}
// Save downloaded file
$name = $prefix . mt_rand() . '.pdf';
$path = __DIR__ . '/' . $name;
writeLog("Saving file to " . $path);
file_put_contents($path, $data);
curl_close($ch);
}
/**
* Write log
*/
$log = '[' . date('Y-m-d H:i:s') . '] ' . PHP_EOL;
$log .= $body . PHP_EOL;
$log .= print_r($params, true);
writeLog($log);
if ($params['action'] == 'signer_signed') {
// One of the signers has signed document
// ... you can download signed file
// or just use as notification...
} elseif ($params['action'] == 'signing_completed') {
// All signers assigned to signing has signed document
// $accessToken - API access token
$url = $params['file'] . '?access_token=' . $accessToken;
// Download signed file
downloadFile($url);
} elseif ($params['action'] == 'signing_archived') {
// Signing has been archived
// $accessToken - API access token
$url = $params['file'] . '?access_token=' . $accessToken;
// Download signed file
downloadFile($url, 'archived');
} elseif ($params['action'] == 'signing_archive_failed') {
// Signing archiving failed
}
writeLog('End.');