forked from nicholasareed/DocuSign-Twilio-Void-Sent-Envelope
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsent_to_void.php
More file actions
241 lines (196 loc) · 5.72 KB
/
sent_to_void.php
File metadata and controls
241 lines (196 loc) · 5.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
include_once 'include/account_creds.php';
include_once 'api/APIService.php';
if(!isset($_GET['step'])){
$step = '1';
} else {
$step = $_GET['step'];
}
// Switch Statement for choosing Step
switch($step){
/*-------------------- Step 1 (Get Recent Envelopes) --------------------*/
case '1':
// Get Envelopes Sent
$allSent = getFolderItems();
$sent = array();
foreach($allSent->GetFolderItemsResult->FolderItems->FolderItem as $item){
// Get only the actually Sent ones
if($item->Status == 'Sent'){
$item->TimeAgo = timeAgo($item->Sent,1);
array_push($sent,$item);
}
}
// Sort by Sent time
usort($sent, "compareSentTime");
// Output TwiML
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Gather action="sent_to_void.php?step=2" numDigits="1" method="POST">
<?
$i = 1;
foreach($sent as $item){
?>
<Say>Press <?= $i; ?> for <?= $item->Subject; ?>. Sent <?= $item->TimeAgo;?> ago.</Say>
<?
$i++;
if($i > 9){
break;
}
}
?>
</Gather>
</Response>
<?
break;
/*-------------------- Step 2 (Confirm Envelope) --------------------*/
case '2':
// Get the Digit
if(strlen($_POST['Digits']) != 1){
header("Location: sent_to_void.php");
die;
}
if($_POST['Digits'] < 1 || $_POST['Digits'] > 9){
header("Location: sent_to_void.php");
die;
}
$digit = $_POST['Digits'];
// Get Envelopes Sent
$allSent = getFolderItems();
$sent = array();
$i = 1;
foreach($allSent->GetFolderItemsResult->FolderItems->FolderItem as $item){
// Get only the actually Sent ones
if($item->Status == 'Sent'){
$item->TimeAgo = timeAgo($item->Sent,1);
array_push($sent,$item);
$i++;
}
}
// Sort by Sent time
usort($sent, "compareSentTime");
// Get the right Envelope
$envelope = array();
$i = 1;
foreach($sent as $item){
if($i == $digit){
$envelope = $item;
}
$i++;
}
if(empty($envelope)){
header("Location: sent_to_void.php");
die;
}
// Read the Envelope Name, if it is correct, then remove it
// - Do you want to Void the Envelope "dkflj" sent "2 days ago"
// - Press 1 to confirm
// - Press 0 to go back to the previous menu
// Output TwiML
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Gather action="sent_to_void.php?step=3&id=<?= $envelope->EnvelopeId; ?>" numDigits="1">
<Say>Press 1 to confirm voiding the Envelope. <?= $envelope->Subject; ?>. Sent <?= $item->TimeAgo; ?> ago.</Say>
</Gather>
</Response>
<?
break;
/*-------------------- Step 3 (Void Envelope) --------------------*/
case '3':
if($_POST['Digits'] != "1"){
header("Location: sent_to_void.php");
die;
}
// Get the envelopeID
$envelopeID = $_GET['id'];
// Void the Envelope
$response = voidEnvelope($envelopeID);
// Output TwiML
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Say>Envelope has been voided. Thank you. Goodbye. </Say>
</Response>
<?
break;
} // End Switch
/*-------------------- Utility Functions --------------------*/
function getAPI() {
global $api_endpoint;
global $IntegratorsKey;
global $UserID;
global $Password;
$api_wsdl = "api/APIService.wsdl";
$api_options = array('location'=>$api_endpoint,'trace'=>true,'features' => SOAP_SINGLE_ELEMENT_ARRAYS);
$api = new APIService($api_wsdl, $api_options);
$api->setCredentials("[" . $IntegratorsKey . "]" . $UserID, $Password);
return $api;
}
function getFolderItems() {
global $AccountID;
$api = getAPI();
// Create the folder filter to specify the scope of your search
// Here, we are limiting the item search to the inbox
// You can also limit by owner, date, status and position
$filter = new FolderFilter();
$filter->AccountId = $AccountID;
$filter->StartPosition = 0;
$filterTypeInfo = new FolderTypeInfo();
$filterTypeInfo->FolderType = FolderType::SentItems;
$filter->FolderTypeInfo = $filterTypeInfo;
// Send
$getFolderItemsparams = new GetFolderItems();
$getFolderItemsparams->FolderFilter = $filter;
$response = $api->GetFolderItems($getFolderItemsparams);
return $response;
}
function compareSentTime($a, $b){
$a = strtotime($a->Sent);
$b = strtotime($b->Sent);
if ($a == $b) {
return 0;
}
return ($a < $b) ? 1 : -1;
}
function timeAgo($date,$granularity=1) {
$date = strtotime($date);
$difference = time() - $date;
$periods = array('decade' => 315360000,
'year' => 31536000,
'month' => 2628000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1);
$retval = null;
if($difference == 0){
return 'a moment';
}
foreach ($periods as $key => $value) {
if ($difference >= $value) {
$time = round($difference/$value);
$difference %= $value;
$retval .= ($retval ? ' ' : '').$time.' ';
$retval .= (($time > 1) ? $key.'s' : $key);
$granularity--;
}
if ($granularity == '0') {
break;
}
}
return $retval;
}
function voidEnvelope($envelopeID = '') {
$api = getAPI();
$voidEnvelopeparams = new VoidEnvelope();
$voidEnvelopeparams->EnvelopeID = $envelopeID;
$voidEnvelopeparams->Reason = "Voided from phone call"; // Turn into transcription
$response = $api->VoidEnvelope($voidEnvelopeparams);
return $response;
}
?>