Skip to content

Commit 1023684

Browse files
Set document visibility code example (#85)
* Document visibility example
1 parent 4506003 commit 1023684

File tree

5 files changed

+456
-157
lines changed

5 files changed

+456
-157
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
class ESign::Eg040SetDocumentVisibilityController < EgController
4+
before_action :check_auth
5+
6+
def create
7+
begin
8+
envelope_args = {
9+
signer1_email: param_gsub(params['signer1Email']),
10+
signer1_name: param_gsub(params['signer1Name']),
11+
signer2_email: param_gsub(params['signer2Email']),
12+
signer2_name: param_gsub(params['signer2Name']),
13+
cc_email: param_gsub(params['ccEmail']),
14+
cc_name: param_gsub(params['ccName']),
15+
status: 'sent',
16+
doc_docx: File.join('data', Rails.application.config.doc_docx),
17+
doc_pdf: File.join('data', Rails.application.config.doc_pdf)
18+
}
19+
args = {
20+
account_id: session['ds_account_id'],
21+
base_path: session['ds_base_path'],
22+
access_token: session['ds_access_token'],
23+
envelope_args: envelope_args
24+
}
25+
results = ESign::Eg040SetDocumentVisibilityService.new(args).worker
26+
@title = 'Envelope sent'
27+
@h1 = 'Envelope sent'
28+
@message = "The envelope has been created and sent!<br/>Envelope ID #{results['envelope_id']}."
29+
render 'ds_common/example_done'
30+
rescue DocuSign_eSign::ApiError => e
31+
error = JSON.parse e.response_body
32+
33+
if error['errorCode'] == "ACCOUNT_LACKS_PERMISSIONS"
34+
@error_information = '<p>See <a href="https://developers.docusign.com/docs/esign-rest-api/how-to/set-document-visibility/">How to set document visibility for envelope recipients</a> in
35+
the DocuSign Developer Center for instructions on how to
36+
enable document visibility in your developer account.</p>'
37+
38+
@error_code = error['errorCode']
39+
@error_message = error['error_description'] || error['message']
40+
41+
return render 'ds_common/error'
42+
end
43+
44+
handle_error(e)
45+
end
46+
end
47+
end
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# frozen_string_literal: true
2+
3+
class ESign::Eg040SetDocumentVisibilityService
4+
attr_reader :args
5+
include ApiCreator
6+
7+
def initialize(args)
8+
@args = args
9+
end
10+
11+
def worker
12+
# 1. Create the envelope request object
13+
envelope_definition = make_envelope args[:envelope_args]
14+
# 2. Call Envelopes::create API method
15+
# Exceptions will be caught by the calling function
16+
envelope_api = create_envelope_api(args)
17+
18+
results = envelope_api.create_envelope args[:account_id], envelope_definition
19+
envelope_id = results.envelope_id
20+
{ 'envelope_id' => envelope_id }
21+
end
22+
23+
private
24+
25+
def make_envelope(envelope_args)
26+
# document 1 (HTML) has tag **signature_1**
27+
# document 2 (DOCX) has tag /sn1/
28+
# document 3 (PDF) has tag /sn1/
29+
#
30+
# The envelope has two recipients:
31+
# recipient 1 - signer 1
32+
# recipient 2 - signer 2
33+
# recipient 3 - cc
34+
# The envelope will be sent first to the signer.
35+
# After it is signed, a copy is sent to the cc person
36+
37+
# Create the envelope definition
38+
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new
39+
40+
envelope_definition.email_subject = 'Please sign this document set'
41+
42+
# Add the documents
43+
doc1_b64 = Base64.encode64(create_document1(envelope_args))
44+
# Read files 2 and 3 from a local directory
45+
# The reads could raise an exception if the file is not available!
46+
doc2_b64 = Base64.encode64(File.binread(envelope_args[:doc_docx]))
47+
doc3_b64 = Base64.encode64(File.binread(envelope_args[:doc_pdf]))
48+
49+
# Create the document models
50+
document1 = DocuSign_eSign::Document.new(
51+
# Create the DocuSign document object
52+
documentBase64: doc1_b64,
53+
name: 'Order acknowledgement', # Can be different from actual file name
54+
fileExtension: 'html', # Many different document types are accepted
55+
documentId: '1' # A label used to reference the doc
56+
)
57+
document2 = DocuSign_eSign::Document.new(
58+
# Create the DocuSign document object
59+
documentBase64: doc2_b64,
60+
name: 'Battle Plan', # Can be different from actual file name
61+
fileExtension: 'docx', # Many different document types are accepted
62+
documentId: '2' # A label used to reference the do
63+
)
64+
document3 = DocuSign_eSign::Document.new(
65+
# Create the DocuSign document object
66+
documentBase64: doc3_b64,
67+
name: 'Lorem Ipsum', # Can be different from actual file name
68+
fileExtension: 'pdf', # Many different document types are accepted
69+
documentId: '3' # A label used to reference the doc
70+
)
71+
72+
# The order in the docs array determines the order in the envelope
73+
envelope_definition.documents = [document1, document2, document3]
74+
75+
# Create the signer recipient model
76+
signer1 = DocuSign_eSign::Signer.new
77+
signer1.email = envelope_args[:signer1_email]
78+
signer1.name = envelope_args[:signer1_name]
79+
signer1.recipient_id = '1'
80+
signer1.routing_order = '1'
81+
signer1.excluded_documents = [2, 3]
82+
83+
signer2 = DocuSign_eSign::Signer.new
84+
signer2.email = envelope_args[:signer2_email]
85+
signer2.name = envelope_args[:signer2_name]
86+
signer2.recipient_id = '2'
87+
signer2.routing_order = '2'
88+
signer2.excluded_documents = [1]
89+
## routingOrder (lower means earlier) determines the order of deliveries
90+
# to the recipients. Parallel routing order is supported by using the
91+
# same integer as the order for two or more recipients
92+
93+
# Create a cc recipient to receive a copy of the documents
94+
cc = DocuSign_eSign::CarbonCopy.new(
95+
email: envelope_args[:cc_email],
96+
name: envelope_args[:cc_name],
97+
routingOrder: '3',
98+
recipientId: '3'
99+
)
100+
# Create signHere fields (also known as tabs) on the documents
101+
# We're using anchor (autoPlace) positioning
102+
#
103+
# The DocuSign platform searches throughout your envelope's documents for matching
104+
# anchor strings. So the sign_here_2 tab will be used in both document 2 and 3
105+
# since they use the same anchor string for their "signer 1" tabs.
106+
sign_here1 = DocuSign_eSign::SignHere.new(
107+
anchorString: '**signature_1**',
108+
anchorYOffset: '10',
109+
anchorUnits: 'pixels',
110+
anchorXOffset: '20'
111+
)
112+
113+
sign_here2 = DocuSign_eSign::SignHere.new(
114+
anchorString: '/sn1/',
115+
anchorYOffset: '10',
116+
anchorUnits: 'pixels',
117+
anchorXOffset: '20'
118+
)
119+
# Add the tabs model (including the sign_here tabs) to the signer
120+
# The Tabs object takes arrays of the different field/tab types
121+
signer1_tabs = DocuSign_eSign::Tabs.new({
122+
signHereTabs: [sign_here1]
123+
})
124+
125+
signer1.tabs = signer1_tabs
126+
127+
signer2_tabs = DocuSign_eSign::Tabs.new({
128+
signHereTabs: [sign_here2]
129+
})
130+
131+
signer2.tabs = signer2_tabs
132+
133+
# Add the recipients to the envelope object
134+
recipients = DocuSign_eSign::Recipients.new(
135+
signers: [signer1, signer2],
136+
carbonCopies: [cc]
137+
)
138+
# Request that the envelope be sent by setting status to "sent".
139+
# To request that the envelope be created as a draft, set status to "created"
140+
envelope_definition.recipients = recipients
141+
envelope_definition.status = envelope_args[:status]
142+
envelope_definition
143+
end
144+
145+
def create_document1(args)
146+
"
147+
<!DOCTYPE html>
148+
<html>
149+
<head>
150+
<meta charset=\"UTF-8\">
151+
</head>
152+
<body style=\"font-family:sans-serif;margin-left:2em;\">
153+
<h1 style=\"font-family: 'Trebuchet MS', Helvetica, sans-serif;
154+
color: darkblue;margin-bottom: 0;\">World Wide Corp</h1>
155+
<h2 style=\"font-family: 'Trebuchet MS', Helvetica, sans-serif;
156+
margin-top: 0px;margin-bottom: 3.5em;font-size: 1em;
157+
color: darkblue;\">Order Processing Division</h2>
158+
<h4>Ordered by #{args[:signer1_name]}</h4>
159+
<p style=\"margin-top:0em; margin-bottom:0em;\">Email: #{args[:signer1_email]}</p>
160+
<p style=\"margin-top:0em; margin-bottom:0em;\">Copy to: #{args[:cc_name]}, #{args[:cc_email]}</p>
161+
<p style=\"margin-top:3em;\">
162+
Candy bonbon pastry jujubes lollipop wafer biscuit biscuit. Topping brownie sesame snaps sweet roll pie. Croissant danish biscuit soufflé caramels jujubes jelly. Dragée danish caramels lemon drops dragée. Gummi bears cupcake biscuit tiramisu sugar plum pastry. Dragée gummies applicake pudding liquorice. Donut jujubes oat cake jelly-o. Dessert bear claw chocolate cake gummies lollipop sugar plum ice cream gummies cheesecake.
163+
</p>
164+
<!-- Note the anchor tag for the signature field is in white. -->
165+
<h3 style=\"margin-top:3em;\">Agreed: <span style=\"color:white;\">**signature_1**/</span></h3>
166+
</body>
167+
</html>"
168+
end
169+
end

0 commit comments

Comments
 (0)