-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDocuSign.js
More file actions
220 lines (210 loc) · 7.01 KB
/
DocuSign.js
File metadata and controls
220 lines (210 loc) · 7.01 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
/**
* DocuSign and related operations.
*/
import { toast } from 'react-toastify';
import anchorfields_pdf from './assets/anchorfields.pdf';
const docName = 'anchorfields.pdf';
const sdkString = 'codeEg_react';
const urlFrag = '/restapi/v2.1'; // DocuSign specific
/**
* Asset files
* Add assets (eg PDF files) to the top level public directory.
* (NOT under /src.) They will be included with the packaged app.
*
*/
class DocuSign {
//
// constructor for the class
//
constructor(app) {
this.app = app;
this.sendEnvelope = this.sendEnvelope.bind(this);
}
//
// Instance methods
//
/**
* Send an envelope, return results or error
*/
async sendEnvelope() {
// get the document
let response = await fetch(anchorfields_pdf)
if (!response || response.status !== 200) {
const msg = `Could not fetch file ${anchorfields_pdf}`;
console.log(msg);
toast.error(msg, { autoClose: 10000 });
return;
}
// See https://stackoverflow.com/a/39951543/64904
const fileBlob = await response.blob();
const reader = new FileReader();
await new Promise(resolve => {
reader.onloadend = resolve;
reader.readAsDataURL(fileBlob);
});
const base64File = reader.result.split(',')[1];
const envelopeRequest = {
emailSubject: 'Please sign the attached document',
status: 'sent',
recipients: {
signers: [
{
email: this.app.state.formEmail,
name: this.app.state.formName,
recipientId: '1',
tabs: {
signHereTabs: [
{
anchorString: '/sn1/',
anchorXOffset: '20',
anchorUnits: 'pixels',
},
],
},
},
],
},
documents: [
{
name: docName,
fileExtension: 'pdf',
documentId: '1',
documentBase64: base64File,
},
],
};
try {
const url =
`${this.app.state.baseUri}${urlFrag}` +
`/accounts/${this.app.state.accountId}` +
`/envelopes`;
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(envelopeRequest),
headers: new Headers({
Authorization: `Bearer ${this.app.state.accessToken}`,
Accept: `application/json`,
'Content-Type': 'application/json',
'X-DocuSign-SDK': sdkString,
}),
});
const data = response && response.ok && (await response.json());
let result;
const headers = response.headers;
const availableApiReqHeader = headers.get('X-RateLimit-Remaining');
const availableApiRequests = availableApiReqHeader
? parseInt(availableApiReqHeader, 10)
: undefined;
const apiResetHeader = headers.get('X-RateLimit-Reset');
const apiRequestsReset = apiResetHeader
? new Date(parseInt(apiResetHeader, 10) * 1000)
: undefined;
const traceId = headers.get('X-DocuSign-TraceToken') || undefined;
if (response.ok) {
result = {
success: true,
errorMsg: undefined,
envelopeId: data.envelopeId,
availableApiRequests,
apiRequestsReset,
traceId,
};
} else {
result = {
success: false,
errorMsg: response && (await response.text()),
envelopeId: undefined,
availableApiRequests,
apiRequestsReset,
traceId,
};
}
return result;
} catch (e) {
// Unfortunately we don't have access to the real
// networking problem!
// See https://medium.com/to-err-is-aaron/detect-network-failures-when-using-fetch-40a53d56e36
const errorMsg =
e.message === 'Failed to fetch'
? 'Networking error—check your Internet and DNS connections'
: e.message;
return {
success: false,
errorMsg,
envelopeId: undefined,
availableApiRequests: undefined,
apiRequestsReset: undefined,
traceId: undefined,
};
}
}
/**
* Get envelope's status, return results or error
*/
async getEnvelope() {
try {
const url =
`${this.app.state.baseUri}${urlFrag}` +
`/accounts/${this.app.state.accountId}` +
`/envelopes/${this.app.state.responseEnvelopeId}`;
const response = await fetch(url, {
method: 'GET',
headers: new Headers({
Authorization: `Bearer ${this.app.state.accessToken}`,
Accept: `application/json`,
'Content-Type': 'application/json',
'X-DocuSign-SDK': sdkString,
}),
});
const data = response && response.ok && (await response.json());
let result;
const headers = response.headers;
const availableApiReqHeader = headers.get('X-RateLimit-Remaining');
const availableApiRequests = availableApiReqHeader
? parseInt(availableApiReqHeader, 10)
: undefined;
const apiResetHeader = headers.get('X-RateLimit-Reset');
const apiRequestsReset = apiResetHeader
? new Date(parseInt(apiResetHeader, 10) * 1000)
: undefined;
const traceId = headers.get('X-DocuSign-TraceToken') || undefined;
if (response.ok) {
result = {
success: true,
errorMsg: undefined,
resultsEnvelopeJson: data,
availableApiRequests,
apiRequestsReset,
traceId,
};
} else {
result = {
success: false,
errorMsg: response && (await response.text()),
resultsEnvelopeJson: undefined,
availableApiRequests,
apiRequestsReset,
traceId,
};
}
return result;
} catch (e) {
// Unfortunately we don't have access to the real
// networking problem!
// See https://medium.com/to-err-is-aaron/detect-network-failures-when-using-fetch-40a53d56e36
const errorMsg =
e.message === 'Failed to fetch'
? 'Networking error—check your Internet and DNS connections'
: e.message;
return {
success: false,
errorMsg,
resultsEnvelopeJson: undefined,
availableApiRequests: undefined,
apiRequestsReset: undefined,
traceId: undefined,
};
}
}
}
export default DocuSign;