Skip to content

Commit 0e68e78

Browse files
Merge branch 'master' of github.com:docusign/code-examples-ruby-private
2 parents f1ae513 + 074802a commit 0e68e78

File tree

7 files changed

+298
-81
lines changed

7 files changed

+298
-81
lines changed

app/controllers/e_sign/eg030_brands_apply_to_template_controller.rb

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,48 @@ def get
1313
accounts_api = create_account_api(args)
1414
brand_lists = accounts_api.list_brands(args[:account_id], options = DocuSign_eSign::ListBrandsOptions.default)
1515
@brand_names = brand_lists.brands
16-
# get the template lists
17-
template_api = create_template_api(args)
18-
template_lists = template_api.list_templates(args[:account_id], options = DocuSign_eSign::ListTemplatesOptions.default)
19-
@templates = template_lists.envelope_templates
2016
super
2117
end
2218

2319
def create
24-
begin
25-
envelope_args = {
26-
signer_email: param_gsub(params[:signerEmail]),
27-
signer_name: param_gsub(params[:signerName]),
28-
cc_email: param_gsub(params[:ccEmail]),
29-
cc_name: param_gsub(params[:ccName]),
30-
brand_id: params[:brands],
31-
template_id: params[:templates],
32-
status: 'sent'
33-
34-
}
35-
args = {
36-
account_id: session['ds_account_id'],
37-
base_path: session['ds_base_path'],
38-
access_token: session['ds_access_token'],
39-
envelope_args: envelope_args
40-
}
20+
template_id = session[:template_id]
4121

42-
results = ESign::Eg030BrandsApplyToTemplateService.new(args).worker
43-
session[:envelope_id] = results.envelope_id
22+
if template_id
23+
begin
24+
envelope_args = {
25+
signer_email: param_gsub(params[:signerEmail]),
26+
signer_name: param_gsub(params[:signerName]),
27+
cc_email: param_gsub(params[:ccEmail]),
28+
cc_name: param_gsub(params[:ccName]),
29+
brand_id: params[:brands],
30+
template_id: template_id,
31+
status: 'sent'
4432

45-
# Step 4. a) Call the eSignature API
46-
# b) Display the JSON response
47-
# brand_id = results.brands[0].brand_id
48-
@title = 'Applying a brand to an envelope using a template'
49-
@h1 = 'Applying a brand to an envelope using a template'
50-
@message = "The envelope has been created and sent!<br/>Envelope ID #{results.envelope_id}."
51-
@json = results.to_json.to_json
52-
render 'ds_common/example_done'
33+
}
34+
args = {
35+
account_id: session['ds_account_id'],
36+
base_path: session['ds_base_path'],
37+
access_token: session['ds_access_token'],
38+
envelope_args: envelope_args
39+
}
5340

54-
rescue DocuSign_eSign::ApiError => e
55-
handle_error(e)
41+
results = ESign::Eg030BrandsApplyToTemplateService.new(args).worker
42+
session[:envelope_id] = results.envelope_id
43+
44+
# Step 4. a) Call the eSignature API
45+
# b) Display the JSON response
46+
# brand_id = results.brands[0].brand_id
47+
@title = 'Applying a brand to an envelope using a template'
48+
@h1 = 'Applying a brand to an envelope using a template'
49+
@message = "The envelope has been created and sent!<br/>Envelope ID #{results.envelope_id}."
50+
@json = results.to_json.to_json
51+
render 'ds_common/example_done'
52+
rescue DocuSign_eSign::ApiError => e
53+
handle_error(e)
54+
end
55+
elsif !template_id
56+
@title = 'Use embedded signing from template and extra doc'
57+
@template_ok = false
5658
end
5759
end
5860
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
class ESign::Eg038ResponsiveSigningController < EgController
4+
before_action :check_auth
5+
6+
def create
7+
begin
8+
args = {
9+
account_id: session[:ds_account_id],
10+
base_path: session[:ds_base_path],
11+
access_token: session[:ds_access_token],
12+
signer_email: param_gsub(params[:signerEmail]),
13+
signer_name: param_gsub(params[:signerName]),
14+
cc_email: param_gsub(params[:ccEmail]),
15+
cc_name: param_gsub(params[:ccName]),
16+
ds_ping_url: Rails.application.config.app_url,
17+
signer_client_id: 1000,
18+
doc_file: 'data/order_form.html'
19+
}
20+
21+
redirect_url = ESign::Eg038ResponsiveSigningService.new(args).worker
22+
redirect_to redirect_url
23+
rescue DocuSign_eSign::ApiError => e
24+
handle_error(e)
25+
end
26+
end
27+
end
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# frozen_string_literal: true
2+
3+
class ESign::Eg038ResponsiveSigningService
4+
attr_reader :args
5+
include ApiCreator
6+
7+
def initialize(args)
8+
@args = args
9+
end
10+
11+
# ***DS.snippet.0.start
12+
def worker
13+
ds_return_url = "#{args[:ds_ping_url]}/ds_common-return"
14+
15+
# Step 1. Create the envelope definition
16+
envelope = make_envelope(args)
17+
18+
# Step 2. Call DocuSign to create the envelope
19+
envelope_api = create_envelope_api(args)
20+
21+
results = envelope_api.create_envelope args[:account_id], envelope
22+
envelope_id = results.envelope_id
23+
# Save for future use within the example launcher
24+
# session[:envelope_id] = envelope_id
25+
26+
# Step 3. Create the recipient view for the embedded signing
27+
view_request = make_recipient_view_request(args, ds_return_url)
28+
29+
# Call the CreateRecipientView API
30+
results = envelope_api.create_recipient_view args[:account_id], envelope_id, view_request
31+
32+
# Step 4. Redirect the user to the embedded signing
33+
# Don't use an iframe!
34+
# State can be stored/recovered using the framework's session or a
35+
# query parameter on the returnUrl (see the makeRecipientViewRequest method)
36+
# Redirect to results.url
37+
results.url
38+
end
39+
40+
private
41+
42+
def make_recipient_view_request(args, ds_return_url)
43+
view_request = DocuSign_eSign::RecipientViewRequest.new
44+
# Set the URL where you want the recipient to go once they are done signing
45+
# should typically be a callback route somewhere in your app.
46+
# The query parameter is included as an example of how
47+
# to save/recover state information during the redirect to
48+
# the DocuSign signing. It's usually better to use
49+
# the session mechanism of your web framework. Query parameters
50+
# can be changed/spoofed very easily.
51+
view_request.return_url = ds_return_url + '?state=123'
52+
53+
# How has your app authenticated the user? In addition to your app's
54+
# authentication, you can include authenticate steps from DocuSign;
55+
# e.g., SMS authentication
56+
view_request.authentication_method = 'none'
57+
58+
# Recipient information must match the embedded recipient info
59+
# that was used to create the envelope
60+
view_request.email = args[:signer_email]
61+
view_request.user_name = args[:signer_name]
62+
view_request.client_user_id = args[:signer_client_id]
63+
64+
# DocuSign recommends that you redirect to DocuSign for the embedded signing. There are
65+
# multiple ways to save state. To maintain your application's session, use the pingUrl
66+
# parameter. It causes the DocuSign signing web page (not the DocuSign server)
67+
# to send pings via AJAX to your app
68+
view_request.ping_frequency = '600' # seconds
69+
# NOTE: The pings will only be sent if the pingUrl is an HTTPS address
70+
view_request.ping_url = args[:ds_ping_url] # Optional setting
71+
72+
view_request
73+
end
74+
75+
def make_envelope(args)
76+
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new
77+
envelope_definition.email_subject = 'Example Signing Document'
78+
79+
html_definition = DocuSign_eSign::DocumentHtmlDefinition.new
80+
html_definition.source = get_html_content(args)
81+
82+
doc = DocuSign_eSign::Document.new
83+
doc.name = 'doc1.html'
84+
doc.document_id = '1'
85+
doc.html_definition = html_definition
86+
87+
# The order in the docs array determines the order in the envelope
88+
envelope_definition.documents = [doc]
89+
# Create a signer recipient to sign the document, identified by name and email
90+
# We're setting the parameters via the object creation
91+
signer = DocuSign_eSign::Signer.new ({
92+
email: args[:signer_email],
93+
name: args[:signer_name],
94+
clientUserId: args[:signer_client_id],
95+
recipientId: 1,
96+
role_name: "Signer"
97+
})
98+
99+
cc = DocuSign_eSign::CarbonCopy.new ({
100+
email: args[:cc_email], name: args[:cc_name], recipientId: 2
101+
})
102+
103+
# Add the recipients to the envelope object
104+
recipients = DocuSign_eSign::Recipients.new
105+
recipients.signers = [signer]
106+
recipients.carbon_copies = [cc]
107+
108+
envelope_definition.recipients = recipients
109+
# Request that the envelope be sent by setting status to "sent".
110+
# To request that the envelope be created as a draft, set status to "created"
111+
envelope_definition.status = 'sent'
112+
envelope_definition
113+
end
114+
115+
def get_html_content(args)
116+
doc_html = File.open(args[:doc_file]).read
117+
# Substitute values into the HTML
118+
# Substitute for: {signerName}, {signerEmail}, {ccName}, {ccEmail}
119+
return doc_html.gsub('{signerName}', args[:signer_name]) \
120+
.gsub('{signerEmail}', args[:signer_email]) \
121+
.gsub('{ccName}', args[:cc_name]) \
122+
.gsub('{ccEmail}', args[:cc_email]) \
123+
.gsub("/sn1/", "<ds-signature data-ds-role=\"Signer\"/>") \
124+
.gsub("/l1q/", "<input data-ds-type=\"number\"/>") \
125+
.gsub("/l2q/", "<input data-ds-type=\"number\"/>")
126+
127+
end
128+
# ***DS.snippet.0.end
129+
end

app/views/ds_common/index.html.erb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,18 @@
318318
<a target='_blank'
319319
href="https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopes/create/">Envelopes::create</a>.
320320
</p>
321-
</div>
322321

323-
<!-- anchor-js is only for the index page -->
322+
<h4 id="example038">38. <a href="eg038">Create a signable HTML document</a></h4>
323+
<p>
324+
Demonstrates how to create an HTML document for responsive signing.
325+
<p>
326+
API methods used:
327+
<a target="_blank" href="https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopes/create/">Envelopes::create</a>,
328+
<a target='_blank'
329+
href="https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopeviews/createrecipient/">EnvelopeViews::createRecipient</a>.
330+
</p>
331+
</div>
332+
333+
<!-- anchor-js is only for the index page -->
324334
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.1.1/anchor.min.js"></script>
325335
<script>anchors.options.placement = 'left'; anchors.add('h4')</script>
Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<h4>30. Applying a brand to an envelope using a template</a></h4>
1+
<h4>30. Applying a brand to an envelope using a template</h4>
22

3-
<p>
4-
This code example demonstrates how to apply a brand to a template
5-
</p>
3+
<% if @template_ok %>
4+
<p>
5+
This code example demonstrates how to apply a brand to a template
6+
</p>
7+
<% end %>
68

79
<p>API method used:
810
<a target='_blank' href="https://developers.docusign.com/docs/esign-rest-api/reference/envelopes/envelopes/create/">Envelopes::create</a>.
@@ -12,45 +14,47 @@
1214
View source file <a target="_blank" href="<%= @source_url %>"><%= @source_file %></a> on GitHub.
1315
</p>
1416

15-
<form class="eg" action="" method="post" data-busy="form">
16-
<div class="form-group">
17-
<label for="signerEmail">Signer Email</label>
18-
<input type="email" class="form-control" id="signerEmail" name="signerEmail"
19-
aria-describedby="emailHelp" placeholder="pat@example.com" required
20-
value="<%= @config.signer_email %>">
21-
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
22-
</div>
23-
<div class="form-group">
24-
<label for="signerName">Signer Name</label>
25-
<input type="text" class="form-control" id="signerName" placeholder="Pat Johnson" name="signerName"
26-
value="<%= @config.signer_name %>" required>
27-
</div>
28-
<div class="form-group">
29-
<label for="ccEmail">CC Email</label>
30-
<input type="email" class="form-control" id="ccEmail" name="ccEmail"
31-
aria-describedby="emailHelp" placeholder="pat@example.com" required>
32-
<small id="emailHelp" class="form-text text-muted">The email for the cc recipient must be different from the signer's email.</small>
33-
</div>
34-
<div class="form-group">
35-
<label for="ccName">CC Name</label>
36-
<input type="text" class="form-control" id="ccName" placeholder="Pat Johnson" name="ccName"
37-
required>
38-
</div>
39-
<div class="form-group">
40-
<label for="templates">Envelope template</label>
41-
<select id="templates" name="templates" class="form-control">
42-
<% @templates.each do |t| %>
43-
<option value="<%= t.template_id %>" selected><%= t.name %></option>
44-
<% end %>
45-
</select>
46-
</div>
47-
<div class="form-group">
48-
<label for="brands">Brand</label>
49-
<select id="brands" name="brands" class="form-control">
50-
<% @brand_names.each do |b| %>
51-
<option value="<%= b.brand_id %>" selected><%= b.brand_name %></option>
52-
<% end %>
53-
</select>
54-
</div>
55-
<button type="submit" class="btn btn-docu">Submit</button>
56-
</form>
17+
<% if @template_ok %>
18+
<form class="eg" action="" method="post" data-busy="form">
19+
<div class="form-group">
20+
<label for="signerEmail">Signer Email</label>
21+
<input type="email" class="form-control" id="signerEmail" name="signerEmail"
22+
aria-describedby="emailHelp" placeholder="pat@example.com" required
23+
value="<%= @config.signer_email %>">
24+
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
25+
</div>
26+
<div class="form-group">
27+
<label for="signerName">Signer Name</label>
28+
<input type="text" class="form-control" id="signerName" placeholder="Pat Johnson" name="signerName"
29+
value="<%= @config.signer_name %>" required>
30+
</div>
31+
<div class="form-group">
32+
<label for="ccEmail">CC Email</label>
33+
<input type="email" class="form-control" id="ccEmail" name="ccEmail"
34+
aria-describedby="emailHelp" placeholder="pat@example.com" required>
35+
<small id="emailHelp" class="form-text text-muted">The email for the cc recipient must be different from the signer's email.</small>
36+
</div>
37+
<div class="form-group">
38+
<label for="ccName">CC Name</label>
39+
<input type="text" class="form-control" id="ccName" placeholder="Pat Johnson" name="ccName"
40+
required>
41+
</div>
42+
<div class="form-group">
43+
<label for="brands">Brand</label>
44+
<select id="brands" name="brands" class="form-control">
45+
<% @brand_names.each do |b| %>
46+
<option value="<%= b.brand_id %>" selected><%= b.brand_name %></option>
47+
<% end %>
48+
</select>
49+
</div>
50+
<button type="submit" class="btn btn-docu">Submit</button>
51+
</form>
52+
53+
<% else %>
54+
<p>Problem: please first create the template using <a href="eg008">example 8.</a> <br/>
55+
Thank you.</p>
56+
57+
<form class="eg" action="eg008" method="get">
58+
<button type="submit" class="btn btn-docu">Continue</button>
59+
</form>
60+
<% end %>

0 commit comments

Comments
 (0)