Skip to content

Commit 073ed07

Browse files
add second example
1 parent 502cd58 commit 073ed07

File tree

10 files changed

+244
-6
lines changed

10 files changed

+244
-6
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ gem 'docusign_click', '~> 1.4.0'
7373
gem 'docusign_esign', '~> 5.0.0'
7474
gem 'docusign_monitor', '~> 1.2.0'
7575
gem 'docusign_rooms', '~> 1.3.0'
76-
gem 'docusign_webforms', '~> 2.0.0'
76+
gem 'docusign_webforms', '~> 2.1.0'
7777
gem 'omniauth-oauth2', '~> 1.8.0'
7878
gem 'omniauth-rails_csrf_protection'
7979

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ GEM
134134
json (~> 2.1, >= 2.1.0)
135135
jwt (~> 2.2, >= 2.2.1)
136136
typhoeus (~> 1.0, >= 1.0.1)
137-
docusign_webforms (2.0.0)
137+
docusign_webforms (2.1.0)
138138
addressable (~> 2.7, >= 2.7.0)
139139
json (~> 2.1, >= 2.1.0)
140140
jwt (~> 2.2, >= 2.2.1)
@@ -397,7 +397,7 @@ DEPENDENCIES
397397
docusign_esign (~> 5.0.0)
398398
docusign_monitor (~> 1.2.0)
399399
docusign_rooms (~> 1.3.0)
400-
docusign_webforms (~> 2.0.0)
400+
docusign_webforms (~> 2.1.0)
401401
jbuilder (~> 2.13.0)
402402
listen (~> 3.9.0)
403403
matrix (~> 0.4.2)

app/controllers/eg_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ def ensure_manifest
105105
end
106106

107107
def format_string(string, *args)
108-
string.gsub(/\{(\d+)\}/) { |s| args[s.to_i] }
108+
string.gsub(/\{(\d+)\}/) { args[$1.to_i] }
109109
end
110110
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
class Webforms::Weg002CreateRemoteInstanceController < EgController
4+
before_action -> { check_auth('WebForms') }
5+
before_action -> { @example = Utils::ManifestUtils.new.get_example(@manifest, 2, 'WebForms') }
6+
7+
def create_web_form_template
8+
args = {
9+
template_name: 'Web Form Example Template',
10+
account_id: session[:ds_account_id],
11+
base_path: session[:ds_base_path],
12+
access_token: session[:ds_access_token]
13+
}
14+
15+
begin
16+
web_form_template_id = Webforms::Eg002CreateRemoteInstanceService.new(args).create_web_form_template
17+
Utils::FileUtils.new.replace_template_id(File.join('data', Rails.application.config.web_form_config_file), web_form_template_id)
18+
session[:web_form_template_id] = web_form_template_id
19+
20+
redirect_to '/weg002webForm'
21+
rescue DocuSign_eSign::ApiError => e
22+
handle_error(e)
23+
end
24+
end
25+
26+
def create_web_form_instance
27+
args = {
28+
form_name: 'Web Form Example Template',
29+
account_id: session[:ds_account_id],
30+
base_path: Rails.application.config.webforms_host,
31+
signer_name: Rails.application.config.signer_name,
32+
signer_email: Rails.application.config.signer_email,
33+
access_token: session[:ds_access_token]
34+
}
35+
create_remote_instance_service = Webforms::Eg002CreateRemoteInstanceService.new(args)
36+
web_forms = create_remote_instance_service.list_web_forms
37+
38+
if web_forms.items.nil? || web_forms.items.empty?
39+
@error_code = '404'
40+
@error_message = @example['CustomErrorTexts'][0]['ErrorMessage']
41+
return render 'ds_common/error'
42+
end
43+
results = create_remote_instance_service.create_web_form_instance web_forms.items.first.id
44+
45+
@title = @example['ExampleName']
46+
@message = format_string(@example['ResultsPageText'], results.envelopes[0].id, results.id)
47+
render 'ds_common/example_done'
48+
end
49+
50+
def get_web_form_create_view
51+
redirect_to '/weg002' if session[:web_form_template_id].nil?
52+
53+
additional_page = @example['AdditionalPage'].find { |p| p['Name'] == 'create_web_form' }
54+
@title = @example['ExampleName']
55+
@description = format_string(additional_page['ResultsPageText'], 'data')
56+
57+
render 'webforms/weg002_create_remote_instance/web_form_create'
58+
end
59+
end
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# frozen_string_literal: true
2+
3+
class Webforms::Eg002CreateRemoteInstanceService
4+
attr_reader :args
5+
6+
include ApiCreator
7+
8+
def initialize(args)
9+
@args = args
10+
end
11+
12+
def create_web_form_template
13+
templates_api = create_template_api args
14+
15+
options = DocuSign_eSign::ListTemplatesOptions.new
16+
options.search_text = args[:template_name]
17+
web_forms_templates = templates_api.list_templates(args[:account_id], options)
18+
19+
if web_forms_templates.result_set_size.to_i.positive?
20+
template_id = web_forms_templates.envelope_templates[0].template_id
21+
else
22+
template_req_object = make_web_forms_template
23+
template = templates_api.create_template(args[:account_id], template_req_object)
24+
template_id = template.template_id
25+
end
26+
27+
template_id
28+
end
29+
30+
def list_web_forms
31+
#ds-snippet-start:WebForms1Step2
32+
configuration = DocuSign_WebForms::Configuration.new
33+
api_client = DocuSign_WebForms::ApiClient.new(configuration)
34+
api_client.set_default_header('Authorization', "Bearer #{args[:access_token]}")
35+
#ds-snippet-end:WebForms1Step2
36+
37+
#ds-snippet-start:WebForms1Step3
38+
webforms_api = DocuSign_WebForms::FormManagementApi.new(api_client)
39+
40+
options = DocuSign_WebForms::ListFormsOptions.new
41+
options.search = args[:form_name]
42+
43+
webforms_api.list_forms(args[:account_id], options)
44+
#ds-snippet-end:WebForms1Step3
45+
end
46+
47+
def create_web_form_instance(form_id)
48+
configuration = DocuSign_WebForms::Configuration.new
49+
50+
api_client = DocuSign_WebForms::ApiClient.new(configuration)
51+
api_client.set_default_header('Authorization', "Bearer #{args[:access_token]}")
52+
53+
#ds-snippet-start:WebForms1Step4
54+
web_form_values = {
55+
'PhoneNumber' => '555-555-5555',
56+
'Yes' => ['Yes'],
57+
'Company' => 'Tally',
58+
'JobTitle' => 'Programmer Writer'
59+
}
60+
recipient = DocuSign_WebForms::CreateInstanceRequestBodyRecipients.new({
61+
'roleName' => 'signer',
62+
'name' => args[:signer_name],
63+
'email' => args[:signer_email]
64+
})
65+
web_form_req_object = DocuSign_WebForms::CreateInstanceRequestBody.new({
66+
'formValues' => web_form_values,
67+
'recipients' => [recipient],
68+
'sendOption' => 'now'
69+
})
70+
#ds-snippet-end:WebForms1Step4
71+
72+
#ds-snippet-start:WebForms1Step5
73+
webforms_api = DocuSign_WebForms::FormInstanceManagementApi.new(api_client)
74+
webforms_api.create_instance(args[:account_id], form_id, web_form_req_object)
75+
#ds-snippet-end:WebForms1Step5
76+
end
77+
78+
private
79+
80+
def make_web_forms_template
81+
template_name = args[:template_name]
82+
doc_file = 'World_Wide_Corp_Web_Form.pdf'
83+
base64_file_content = Base64.encode64(File.binread(File.join('data', doc_file)))
84+
85+
# Create the document model
86+
document = DocuSign_eSign::Document.new({
87+
# Create the Docusign document object
88+
'documentBase64' => base64_file_content,
89+
'name' => 'World_Wide_Web_Form', # Can be different from actual file name
90+
'fileExtension' => 'pdf', # Many different document types are accepted
91+
'documentId' => '1' # A label used to reference the doc
92+
})
93+
94+
# Create the signer recipient model
95+
# Since these are role definitions, no name/email:
96+
signer = DocuSign_eSign::Signer.new({
97+
'roleName' => 'signer', 'recipientId' => '1', 'routingOrder' => '1'
98+
})
99+
# Create fields using absolute positioning
100+
# Create a sign_here tab (field on the document)
101+
sign_here = DocuSign_eSign::SignHere.new(
102+
'documentId' => '1', 'tabLabel' => 'Signature',
103+
'anchorString' => '/SignHere/', 'anchorUnits' => 'pixels',
104+
'anchorXOffset' => '0', 'anchorYOffset' => '0'
105+
)
106+
check = DocuSign_eSign::Checkbox.new(
107+
'documentId' => '1', 'tabLabel' => 'Yes',
108+
'anchorString' => '/SMS/', 'anchorUnits' => 'pixels',
109+
'anchorXOffset' => '0', 'anchorYOffset' => '0'
110+
)
111+
text1 = DocuSign_eSign::Text.new(
112+
'documentId' => '1', 'tabLabel' => 'FullName',
113+
'anchorString' => '/FullName/', 'anchorUnits' => 'pixels',
114+
'anchorXOffset' => '0', 'anchorYOffset' => '0'
115+
)
116+
text2 = DocuSign_eSign::Text.new(
117+
'documentId' => '1', 'tabLabel' => 'PhoneNumber',
118+
'anchorString' => '/PhoneNumber/', 'anchorUnits' => 'pixels',
119+
'anchorXOffset' => '0', 'anchorYOffset' => '0'
120+
)
121+
text3 = DocuSign_eSign::Text.new(
122+
'documentId' => '1', 'tabLabel' => 'Company',
123+
'anchorString' => '/Company/', 'anchorUnits' => 'pixels',
124+
'anchorXOffset' => '0', 'anchorYOffset' => '0'
125+
)
126+
text4 = DocuSign_eSign::Text.new(
127+
'documentId' => '1', 'tabLabel' => 'JobTitle',
128+
'anchorString' => '/JobTitle/', 'anchorUnits' => 'pixels',
129+
'anchorXOffset' => '0', 'anchorYOffset' => '0'
130+
)
131+
date_signed = DocuSign_eSign::DateSigned.new(
132+
'documentId' => '1', 'tabLabel' => 'DateSigned',
133+
'anchorString' => '/Date/', 'anchorUnits' => 'pixels',
134+
'anchorXOffset' => '0', 'anchorYOffset' => '0'
135+
)
136+
137+
# Add the tabs model to the signer
138+
# The Tabs object takes arrays of the different field/tab types
139+
signer.tabs = DocuSign_eSign::Tabs.new(
140+
'signHereTabs' => [sign_here],
141+
'checkboxTabs' => [check],
142+
'textTabs' => [text1, text2, text3, text4],
143+
'dateSignedTabs' => [date_signed]
144+
)
145+
# Create top two objects
146+
envelope_template_definition = DocuSign_eSign::EnvelopeTemplate.new(
147+
'description' => 'Example template created via the eSignature API',
148+
'shared' => 'false'
149+
)
150+
151+
# Top object:
152+
DocuSign_eSign::EnvelopeTemplate.new(
153+
'documents' => [document],
154+
'name' => template_name,
155+
'emailSubject' => 'Please sign this document',
156+
'envelopeTemplateDefinition' => envelope_template_definition,
157+
'recipients' => DocuSign_eSign::Recipients.new(
158+
'signers' => [signer]
159+
),
160+
'status' => 'created'
161+
)
162+
end
163+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<%= render('partials/example_info') %>
2+
3+
<form class="eg" id="data_form" action="" method="post" data-busy="form">
4+
<%= render('partials/submit_button') %>
5+
</form>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h4><%= @title %></h4>
2+
<p><%= sanitize @description %></p>
3+
4+
<form class="eg" id="data_form" action="" method="post" data-busy="form">
5+
<%= render('partials/submit_button') %>
6+
</form>

config/appsettings.example.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ default: &default
2323
rooms_host: "https://demo.rooms.docusign.com/restapi"
2424
monitor_host: "https://lens-d.docusign.net"
2525
admin_host: "https://api-d.docusign.net/management"
26-
webforms_host: "https://apps-d.docusign.com/api/webforms/v1.1"
26+
webforms_host: "https://apps-d.docusign.com/api/webforms"
2727
allow_silent_authentication: true # A user can be silently authenticated if they have an
2828
# Active login session on another tab of the same browser
2929
# Set if you want a specific Docusign AccountId, If false, the users default account will be used.

config/routes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@
243243
get 'weg001webForm' => 'weg001_create_instance#get_web_form_create_view'
244244
post 'weg001' => 'weg001_create_instance#create_web_form_template'
245245
post 'weg001webForm' => 'weg001_create_instance#create_web_form_instance'
246+
247+
get 'weg002' => 'weg002_create_remote_instance#get'
248+
get 'weg002webForm' => 'weg002_create_remote_instance#get_web_form_create_view'
249+
post 'weg002' => 'weg002_create_remote_instance#create_web_form_template'
250+
post 'weg002webForm' => 'weg002_create_remote_instance#create_web_form_instance'
246251
end
247252

248253
scope module: 'notary' do

data/web-form-config.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)