Skip to content

Commit a4529a3

Browse files
authored
Adding focused view example (#123)
* added FocusedView code example * refactored the changes * renamed a variable * added constants
1 parent 7850add commit a4529a3

File tree

4 files changed

+346
-0
lines changed

4 files changed

+346
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.docusign.controller.eSignature.examples;
2+
3+
import com.docusign.DSConfiguration;
4+
import com.docusign.common.WorkArguments;
5+
import com.docusign.controller.eSignature.services.FocusedViewService;
6+
import com.docusign.core.model.Session;
7+
import com.docusign.core.model.User;
8+
import com.docusign.esign.client.ApiClient;
9+
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.ui.ModelMap;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
15+
import javax.servlet.http.HttpServletResponse;
16+
17+
18+
/**
19+
* Used to generate an envelope and allow user to sign it directly from the app without having to open an email.
20+
*/
21+
@Controller
22+
@RequestMapping("/eg044")
23+
public class EG044ControllerFocusedView extends AbstractEsignatureController {
24+
public static final String EMBED = "pages/esignature/examples/embed";
25+
26+
public static final String INTEGRATION_KEY = "integrationKey";
27+
28+
public static final String URL = "url";
29+
30+
public static final String ENVELOPE_ID = "envelopeId";
31+
32+
public static final String DOCUMENTATION = "documentation";
33+
34+
@Autowired
35+
public EG044ControllerFocusedView(DSConfiguration config, Session session, User user){
36+
super(config, "eg044", session, user);
37+
}
38+
39+
@Override
40+
protected void onInitModel(WorkArguments args, ModelMap model) throws Exception {
41+
super.onInitModel(args, model);
42+
}
43+
44+
@Override
45+
protected Object doWork(WorkArguments args, ModelMap model,
46+
HttpServletResponse response) throws Exception {
47+
String signerName = args.getSignerName();
48+
String signerEmail = args.getSignerEmail();
49+
String accountId = session.getAccountId();
50+
51+
// Call DocuSign to create the envelope
52+
ApiClient apiClient = createApiClient(session.getBasePath(), user.getAccessToken());
53+
54+
// Call the method from Examples API to send envelope and generate url for focused view signing
55+
String[] envelopeIdAndRedirectUrl = new FocusedViewService().sendEnvelopeWithFocusedView(
56+
signerEmail,
57+
signerName,
58+
apiClient,
59+
accountId,
60+
config.getDsReturnUrl());
61+
62+
if(envelopeIdAndRedirectUrl.length == 2){
63+
model.addAttribute(ENVELOPE_ID, envelopeIdAndRedirectUrl[0]);
64+
model.addAttribute(URL, envelopeIdAndRedirectUrl[1]);
65+
}
66+
67+
model.addAttribute(DOCUMENTATION, config.getDocumentationPath() + exampleName);
68+
model.addAttribute(LAUNCHER_TEXTS, config.getCodeExamplesText().SupportingTexts);
69+
model.addAttribute(INTEGRATION_KEY, config.getUserId());
70+
71+
return EMBED;
72+
}
73+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.docusign.controller.eSignature.services;
2+
3+
import com.docusign.controller.eSignature.examples.EnvelopeHelpers;
4+
import com.docusign.esign.api.EnvelopesApi;
5+
import com.docusign.esign.client.ApiClient;
6+
import com.docusign.esign.client.ApiException;
7+
import com.docusign.esign.model.EnvelopeDefinition;
8+
import com.docusign.esign.model.EnvelopeSummary;
9+
import com.docusign.esign.model.RecipientViewRequest;
10+
import com.docusign.esign.model.ViewUrl;
11+
import com.docusign.esign.model.Signer;
12+
import com.docusign.esign.model.Recipients;
13+
import com.docusign.esign.model.Document;
14+
15+
import java.io.IOException;
16+
import java.util.Arrays;
17+
import java.util.Collections;
18+
19+
public class FocusedViewService {
20+
21+
private static String DOCUMENT_FILE_NAME = "World_Wide_Corp_lorem.pdf";
22+
23+
private static String DOCUMENT_NAME = "Lorem Ipsum";
24+
25+
private static String SIGNER_CLIENT_ID = "1000";
26+
27+
private static int ANCHOR_OFFSET_Y = 20;
28+
29+
private static int ANCHOR_OFFSET_X = 10;
30+
31+
public static String STATE_123 = "?state=123";
32+
33+
public static String AUTHENTICATION_METHOD = "none";
34+
35+
public String[] sendEnvelopeWithFocusedView(
36+
String signerEmail,
37+
String signerName,
38+
ApiClient apiClient,
39+
String accountId,
40+
String returnUrl
41+
) throws ApiException, IOException {
42+
//ds-snippet-start:eSign44Step3
43+
EnvelopeDefinition envelope = makeEnvelope(
44+
signerEmail,
45+
signerName,
46+
SIGNER_CLIENT_ID,
47+
ANCHOR_OFFSET_Y,
48+
ANCHOR_OFFSET_X,
49+
DOCUMENT_FILE_NAME,
50+
DOCUMENT_NAME);
51+
52+
EnvelopesApi envelopesApi = new EnvelopesApi(apiClient);
53+
54+
EnvelopeSummary envelopeSummary = envelopesApi.createEnvelope(accountId, envelope);
55+
56+
String envelopeId = envelopeSummary.getEnvelopeId();
57+
//ds-snippet-end:eSign44Step3
58+
59+
//ds-snippet-start:eSign44Step5
60+
RecipientViewRequest viewRequest = makeRecipientViewRequest(signerEmail, signerName, returnUrl, SIGNER_CLIENT_ID, returnUrl);
61+
ViewUrl viewUrl = envelopesApi.createRecipientView(accountId, envelopeId, viewRequest);
62+
String redirectUrl = viewUrl.getUrl();
63+
64+
return new String[] { envelopeId, redirectUrl };
65+
//ds-snippet-end:eSign44Step5
66+
}
67+
68+
//ds-snippet-start:eSign44Step4
69+
public RecipientViewRequest makeRecipientViewRequest(
70+
String signerEmail,String signerName, String returnUrl, String signerClientId, String pingUrl
71+
) throws ApiException {
72+
String pingFrequency = "600";
73+
String linkToLauncher = "http://localhost:8080";
74+
String linkToDocuSignServer = "https://apps-d.docusign.com";
75+
76+
RecipientViewRequest viewRequest = new RecipientViewRequest();
77+
78+
viewRequest.setReturnUrl(returnUrl + STATE_123);
79+
viewRequest.setAuthenticationMethod(AUTHENTICATION_METHOD);
80+
viewRequest.setEmail(signerEmail);
81+
viewRequest.setUserName(signerName);
82+
viewRequest.setClientUserId(signerClientId);
83+
84+
if (pingUrl != null)
85+
{
86+
viewRequest.setPingFrequency(pingFrequency);
87+
viewRequest.setPingUrl(pingUrl);
88+
}
89+
90+
viewRequest.setFrameAncestors(Arrays.asList(new String[]{linkToLauncher, linkToDocuSignServer}));
91+
viewRequest.setMessageOrigins(Arrays.asList(new String[]{linkToDocuSignServer}));
92+
93+
return viewRequest;
94+
}
95+
//ds-snippet-end:eSign44Step4
96+
97+
//ds-snippet-start:eSign44Step2
98+
public EnvelopeDefinition makeEnvelope(
99+
String signerEmail,
100+
String signerName,
101+
String signerClientId,
102+
Integer anchorOffsetY,
103+
Integer anchorOffsetX,
104+
String documentFileName,
105+
String documentName
106+
) throws IOException {
107+
String emailSubject = "Please sign this document";
108+
String recipientId = "1";
109+
String anchorString = "/sn1/";
110+
String docId = "3";
111+
112+
Signer signer = new Signer();
113+
signer.setEmail(signerEmail);
114+
signer.setName(signerName);
115+
signer.clientUserId(signerClientId);
116+
signer.recipientId(recipientId);
117+
signer.setTabs(EnvelopeHelpers.createSingleSignerTab(anchorString, anchorOffsetY, anchorOffsetX));
118+
119+
Recipients recipients = new Recipients();
120+
recipients.setSigners(Collections.singletonList(signer));
121+
122+
EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition();
123+
envelopeDefinition.setEmailSubject(emailSubject);
124+
envelopeDefinition.setRecipients(recipients);
125+
126+
Document document = EnvelopeHelpers.createDocumentFromFile(documentFileName, documentName, docId);
127+
envelopeDefinition.setDocuments(Collections.singletonList(document));
128+
envelopeDefinition.setStatus(EnvelopeHelpers.ENVELOPE_STATUS_SENT);
129+
130+
return envelopeDefinition;
131+
}
132+
//ds-snippet-end:eSign44Step2
133+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2+
<jsp:include page="../../../partials/head.jsp"/>
3+
4+
<c:set var="formNumber" value="0" scope="page" />
5+
<c:set var="signerEmailInputNumber" value="0" scope="page" />
6+
<c:set var="signerNameInputNumber" value="1" scope="page" />
7+
8+
<h4>${example.getExampleName()}</h4>
9+
<p>${example.getExampleDescription()}</p>
10+
<c:if test="${showDoc}">
11+
<p><a target='_blank' href='${documentation}'>Documentation</a> about this example.</p>
12+
</c:if>
13+
14+
<jsp:include page="../../links_to_api_methods.jsp" />
15+
16+
<p>
17+
${viewSourceFile}
18+
</p>
19+
20+
<form class="eg" action="" method="post" data-busy="form">
21+
<div class="form-group">
22+
<label for="signerEmail">
23+
${example.getForms().get(formNumber).getInputs().get(signerEmailInputNumber).getInputName()}
24+
</label>
25+
26+
<input type="email"
27+
class="form-control"
28+
id="signerEmail"
29+
name="signerEmail"
30+
aria-describedby="emailHelp"
31+
placeholder="${example.getForms().get(formNumber).getInputs().get(signerEmailInputNumber).getInputPlaceholder()}"
32+
required
33+
value="${locals.dsConfig.signerEmail}">
34+
35+
<small id="emailHelp" class="form-text text-muted">
36+
${launcherTexts.getHelpingTexts().getEmailWontBeShared()}
37+
</small>
38+
</div>
39+
<div class="form-group">
40+
<label for="signerName">
41+
${example.getForms().get(formNumber).getInputs().get(signerNameInputNumber).getInputName()}
42+
</label>
43+
44+
<input type="text"
45+
class="form-control"
46+
id="signerName"
47+
placeholder="${example.getForms().get(formNumber).getInputs().get(signerNameInputNumber).getInputPlaceholder()}"
48+
name="signerName"
49+
value="${locals.dsConfig.signerName}"
50+
required>
51+
</div>
52+
<input type="hidden" name="_csrf" value="${csrfToken}">
53+
<button type="submit" class="btn btn-docu">${launcherTexts.getSubmitButton()}</button>
54+
</form>
55+
56+
<jsp:include page="../../../partials/foot.jsp"/>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2+
<jsp:include page="../../../partials/head.jsp"/>
3+
4+
<br />
5+
<h2>The document has been embedded with focused view.</h2>
6+
<br />
7+
8+
<!--
9+
//ds-snippet-start:eSign44Step6
10+
-->
11+
12+
<!DOCTYPE html>
13+
<html>
14+
<head>
15+
<meta charset="utf-8" />
16+
<title>Signing</title>
17+
<style>
18+
html,
19+
body {
20+
margin-top: 30px;
21+
padding: 0;
22+
font: 13px Helvetica, Arial, sans-serif;
23+
}
24+
25+
.docusign-agreement {
26+
width: 75%;
27+
height: 800px;
28+
}
29+
</style>
30+
</head>
31+
<body>
32+
<div class="docusign-agreement" id="agreement"></div>
33+
</body>
34+
</html>
35+
36+
<p><a href="/">Continue</a></p>
37+
38+
<script src='http://js.docusign.com/bundle.js'></script>
39+
<script>
40+
window.DocuSign.loadDocuSign('${integrationKey}')
41+
.then((docusign) => {
42+
const signing = docusign.signing({
43+
url: '${url}',
44+
displayFormat: 'focused',
45+
style: {
46+
/** High-level variables that mirror our existing branding APIs. Reusing the branding name here for familiarity. */
47+
branding: {
48+
primaryButton: {
49+
/** Background color of primary button */
50+
backgroundColor: '#333',
51+
/** Text color of primary button */
52+
color: '#fff',
53+
}
54+
},
55+
56+
/** High-level components we allow specific overrides for */
57+
signingNavigationButton: {
58+
finishText: 'You have finished the document! Hooray!',
59+
position: 'bottom-center'
60+
}
61+
}
62+
});
63+
64+
signing.on('ready', (event) => {
65+
console.log('UI is rendered');
66+
});
67+
68+
signing.on('sessionEnd', (event) => {
69+
/** The event here denotes what caused the sessionEnd to trigger, such as signing_complete, ttl_expired etc../ **/
70+
console.log('sessionend', event);
71+
});
72+
73+
signing.mount('#agreement');
74+
})
75+
.catch((ex) => {
76+
// Any configuration or API limits will be caught here
77+
});
78+
</script>
79+
80+
<!--
81+
//ds-snippet-end:eSign44Step6
82+
-->
83+
84+
<jsp:include page="../../../partials/foot.jsp"/>

0 commit comments

Comments
 (0)