-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCreateEtchESignPacket.java
More file actions
212 lines (174 loc) · 8.5 KB
/
Copy pathCreateEtchESignPacket.java
File metadata and controls
212 lines (174 loc) · 8.5 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
package com.useanvil.examples.runnable;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.useanvil.examples.client.GraphqlClient;
import com.useanvil.examples.entity.*;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
// Example: Create an e-sign packet via the Anvil API
//
// * E-sign API docs: https://www.useanvil.com/docs/api/e-signatures
//
// This script is runnable as is, all you need to do is supply your own API key
// in the ANVIL_API_KEY environment variable.
//
// ANVIL_API_KEY=<yourAPIKey> java -jar target/Examples-1.0-SNAPSHOT.jar create-etch-packet <your.real.email@ex.com>
//
// This script will create an e-sign packet with one signer and two documents.
// Then it will send a signature request to the email you specified.
// Use your real email address!
//
// This script will:
//
// * Use a template sample PDF document
// * Upload a new PDF document
// * Insert data (names, emails, addresses, etc) on both PDFs
// * Have the signer sign both documents
//
// After you run the script, you can find the new packet from the e-sign "Sent"
// area in your dashboard. The dashboard URL to the new packet will be output as
// well.
public class CreateEtchESignPacket implements IRunnable {
ObjectMapper _objectMapper = new ObjectMapper();
private byte[] getBase64File(String filePath) {
byte[] encodedFile;
try {
encodedFile = Base64.getUrlEncoder().encode(Files.readAllBytes(Paths.get(filePath)));
} catch (IOException e) {
throw new RuntimeException(e);
}
return encodedFile;
}
private List<EtchSigner> getSigners(String signerName, String signerEmail) {
ArrayList<EtchSigner> signers = new ArrayList<>();
// Signers will sign in the order they are specified in this array.
// e.g. `employer` will sign after `employee` has finished signing
EtchSigner signer = new EtchSigner();
// `employee` is the first signer
signer.id = "signer1";
signer.name = signerName;
signer.email = signerEmail;
signer.signerType = "email";
// These fields will be presented when this signer signs.
// The signer will need to click through the signatures in
// the order of this array.
ArrayList<SignerField> signerFields = new ArrayList<>();
// File IDs are specified in the `files` property above
signerFields.add(new SignerField("sampleTemplate", "signature"));
signerFields.add(new SignerField("sampleTemplate", "signatureInitial"));
signerFields.add(new SignerField("sampleTemplate", "signatureDate"));
signerFields.add(new SignerField("sampleTemplate", "signerName"));
signerFields.add(new SignerField("sampleTemplate", "signerEmail"));
// Two more for the other one
signerFields.add(new SignerField("fileUploadNDA", "recipientSignature"));
signerFields.add(new SignerField("fileUploadNDA", "recipientSignatureDate"));
signer.fields = signerFields.toArray(new SignerField[0]);
signers.add(signer);
return signers;
}
private PayloadData getPayloadData(String signerName, String signerEmail) {
var data = new PayloadData();
var sampleTemplateData = new HashMap<String, Serializable>();
sampleTemplateData.put("name", signerName);
sampleTemplateData.put("email", signerEmail);
// 'sampleTemplate' is the sample template ID specified above
var sampleTemplate = new HashMap<String, Serializable>();
sampleTemplate.put("data", sampleTemplateData);
data.payloads.put("sampleTemplate", sampleTemplate);
return data;
}
private CreateEtchPacket getPacketVariables(String pdfTemplateEid, String signerName, String signerEmail) {
CreateEtchPacket packetPayload = new CreateEtchPacket();
// Subject & body of the emails to signers
packetPayload.name = "Test Docs - " + signerName;
packetPayload.signatureEmailSubject = "Custom email subject";
packetPayload.signatureEmailBody = "Custom please sign these documents....";
// Merge all PDFs into one PDF before signing.
// Signing users will get one PDF instead of all PDFs as separate files.
// packetPayload.mergePDFs = false;
// Gather all file data. We'll use this in the final payload below.
List<IAttachable> etchFiles = this.getFiles(pdfTemplateEid);
List<EtchSigner> signers = this.getSigners(signerName, signerEmail);
// This data will fill the PDF before it's sent to any signers.
// IDs here were set up on each field while templatizing the PDF.
PayloadData data = this.getPayloadData(signerName, signerEmail);
packetPayload.files = etchFiles.toArray(new IAttachable[0]);
packetPayload.signers = signers.toArray(new EtchSigner[0]);
packetPayload.data = data;
return packetPayload;
}
private List<IAttachable> getFiles(String pdfTemplateEid) {
ArrayList<IAttachable> ret = new ArrayList<>();
EtchCastRef ref = new EtchCastRef("sampleTemplate", pdfTemplateEid);
ArrayList<CastField> fields = new ArrayList<>();
// The second file is an NDA we'll upload and specify the field locations
String ndaFilePath = "../static/test-pdf-nda.pdf";
Path filePath = Paths.get(ndaFilePath);
// This is a file we will upload and specify the fields ourselves
fields.add(new CastField("recipientName", "fullName", new Rect(223, 120, 12, 140), 0));
fields.add(new CastField("recipientEmail", "email", new Rect(367, 120, 12, 166), 1));
fields.add(new CastField("recipientSignatureName", "fullName", new Rect(107, 374, 22, 157), 1));
fields.add(new CastField("recipientSignature", "signature", new Rect(270, 374, 22, 142), 1));
fields.add(new CastField("recipientSignatureDate", "signatureDate", new Rect(419, 374, 22, 80), 1));
FileUpload file = new FileUpload("fileUploadNDA", "Demo NDA", filePath, fields.toArray(new CastField[0]));
ret.add(ref);
ret.add(file);
return ret;
}
@Override
public void run(String apiKey, String otherArg) {
// The PDF template ID to fill. This PDF template ID is a sample template
// available to anyone.
//
// See https://www.useanvil.com/help/tutorials/set-up-a-pdf-template for details
// on setting up your own template
String pdfTemplateId = "B5Loz3C7GVortDmn4p2P";
// Signer information
String signerName = "Testy Signer";
// Signer email comes from a CLI argument
String signerEmail = otherArg;
CreateEtchPacket payload = this.getPacketVariables(pdfTemplateId, signerName, signerEmail);
Map<Integer, Path> filesToUpload = new LinkedHashMap<>();
for (int idx = 0; idx < payload.files.length; idx++) {
// Go through the files and find ones that have a path.
// Note that _order matters_ even though it gets mapped to a key.
var file = payload.files[idx];
if (file instanceof FileUpload) {
FileUpload _file = (FileUpload) file;
Path _filePath = _file.getFilePath();
if (_filePath != null) {
// We need to know which file (via index) this path belongs
// to, and we will map this later during the multipart request
// creation stage.
filesToUpload.put(idx, _filePath);
}
}
}
GraphqlClient client;
try {
client = new GraphqlClient(apiKey);
System.out.println("Creating Etch e-sign packet...");
HttpResponse<String> response = client.doRequest(
Paths.get("src/main/resources/mutations/create-etch-packet.graphql"),
payload,
filesToUpload
);
String headers = String.valueOf(response.request().headers());
String res = response.body();
int statusCode = response.statusCode();
System.out.println(statusCode);
System.out.println(headers);
System.out.println(res);
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void run(String apiKey) throws Exception {
}
}