简介
DocuSign, Inc. 是一家总部位于加利福尼亚州旧金山的美国公司,它允许组织管理电子协议。作为 DocuSign 协议云的一部分,DocuSign 提供电子签名,这是一种在不同设备上进行电子签名的方式
准备工作
-
注册DocuSign开发者平台账号
-
登录开发者平台
- 设置app和集成密钥
嵌入式集成
- 依赖引入
<dependency>
<groupId>com.docusign</groupId>
<artifactId>docusign-esign-java</artifactId>
<version>3.7.0-BETA</version>
</dependency>
- 获取token
@BeforeEach
public void init() throws Exception {
apiClient = new ApiClient(baseUrl).setOAuthBasePath(oAuthBasePath);
// apiClient.setAccessToken(accessToken,3600L);
OAuth.OAuthToken oAuthToken = apiClient
.requestJWTUserToken(clientId, userId, Arrays.asList(OAuth.Scope_SIGNATURE), privateKey.getBytes(),
3 * 24 * 60 * 60);
apiClient.addDefaultHeader("Authorization", "Bearer " + oAuthToken.getAccessToken());
templatesApi = new TemplatesApi(apiClient);
envelopesApi = new EnvelopesApi(apiClient);
}
- 创建 envelope definition
String s1 = UUID.randomUUID().toString();
String s2 = UUID.randomUUID().toString();
SignerEntity signerEntity1 = new SignerEntity(s1,s1, "x", "[email protected]", "first party");
SignerEntity signerEntity2 = new SignerEntity(s2,s2, "xx", "[email protected]", "second party");
String docName = "测试合同";
String docType = "docx";
// Step 1. Create the envelope definition
EnvelopeDefinition envelope =
makeEnvelope(docName, docType, new ArrayList<>(Arrays.asList(signerEntity1, signerEntity2)));
private EnvelopeDefinition makeEnvelope(String docName, String docType, List<SignerEntity> signerEntityList)
throws Exception {
// Data for this method
// signerEmail (argument)
// signerName (argument)
// signerClientId (class constant) the id of the signer in this app
// location of the source document
File file = new File("xxxxx");
byte[] buffer = IOUtils.toByteArray(file.toURI());
EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition();
envelopeDefinition.setEmailSubject("Please sign this document");
Document doc = new Document();
String doc1b64 = new String(Base64.getEncoder().encode(buffer));
doc.setDocumentBase64(doc1b64);
doc.setName(docName); // can be different from actual file name
doc.setFileExtension(docType);
doc.setDocumentId("1");
// The order in the docs array determines the order in the envelope
envelopeDefinition.setDocuments(Arrays.asList(doc));
List<Signer> signerList = new ArrayList<>();
// Create a signer recipient to sign the document, identified by name and email
// We set the clientUserId to enable embedded signing for the recipient
// We're setting the parameters via the object creation
for (SignerEntity signerEntity : signerEntityList) {
Signer signer = new Signer();
signer.setEmail(signerEntity.getEmail());
signer.setName(signerEntity.getName());
signer.recipientId(signerEntity.getUuid());
signer.setClientUserId(signerEntity.getUserId());
signerList.add(signer);
}
// Create signHere fields (also known as tabs) on the documents,
// We're using anchor (autoPlace) positioning
//
// The DocuSign platform seaches throughout your envelope's
// documents for matching anchor strings.
// Tabs are set per recipient / signer
// Add the recipient to the envelope object
Recipients recipients = new Recipients();
recipients.setSigners(signerList);
envelopeDefinition.setRecipients(recipients);
// Request that the envelope be sent by setting |status| to "sent".
// To request that the envelope be created as a draft, set to "created"
envelopeDefinition.setStatus("sent");
return envelopeDefinition;
}
- 调用DocuSign api 创建envelope
// Step 2. Call DocuSign to create the envelope
EnvelopeSummary results = envelopesApi.createEnvelope(accountId, envelope);
String envelopeId = results.getEnvelopeId();
- 调用DocuSign api 创建签名url
// Step 3. create the recipient view, the Signing Ceremony
RecipientViewRequest viewRequest1 =
makeRecipientViewRequest(envelopeId, signerEntity1);
ViewUrl results1 = envelopesApi.createRecipientView(accountId, envelopeId, viewRequest1);
String redirectUrl1 = results1.getUrl();
System.out.println(redirectUrl1);
RecipientViewRequest viewRequest2 =
makeRecipientViewRequest(envelopeId, signerEntity2);
ViewUrl results2 = envelopesApi.createRecipientView(accountId, envelopeId, viewRequest2);
String redirectUrl2 = results2.getUrl();
System.out.println(redirectUrl2);
- 进入DocuSign 签名url进行签名
- 后续可通过DocuSign api获取各方签名是否完成,也可以下载签名后的合同文件