Skip to content

Commit a9c96a9

Browse files
Merge branch 'master' of github.com:docusign/code-examples-ruby-private
2 parents 2aaa584 + e65f572 commit a9c96a9

File tree

43 files changed

+321
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+321
-83
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
class ESign::Eg039SigningInPersonController < EgController
4+
def create
5+
minimum_buffer_min = 10
6+
token_ok = check_token(minimum_buffer_min)
7+
unless token_ok
8+
flash[:messages] = 'Sorry, you need to re-authenticate.'
9+
# We could store the parameters of the requested operation
10+
# so it could be restarted automatically.
11+
# But since it should be rare to have a token issue here,
12+
# we'll make the user re-enter the form data after
13+
# authentication.
14+
return redirect_to '/ds/mustAuthenticate'
15+
end
16+
17+
access_token = session[:ds_access_token]
18+
base_path = session[:ds_base_path]
19+
email = ESign::GetDataService.new(access_token, base_path).get_current_user_email
20+
name = ESign::GetDataService.new(access_token, base_path).get_current_user_name
21+
22+
args = {
23+
account_id: session[:ds_account_id],
24+
base_path: session[:ds_base_path],
25+
access_token: session[:ds_access_token],
26+
host_email: param_gsub(email),
27+
host_name: param_gsub(name),
28+
signer_name: param_gsub(params[:signer_name]),
29+
ds_ping_url: Rails.application.config.app_url,
30+
pdf_filename: 'data/World_Wide_Corp_lorem.pdf'
31+
}
32+
33+
redirect_url = ESign::Eg039SigningInPersonService.new(args).worker
34+
redirect_to redirect_url
35+
end
36+
end
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# frozen_string_literal: true
2+
3+
class ESign::Eg039SigningInPersonService
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_ping_url = args[:ds_ping_url]
14+
ds_return_url = "#{ds_ping_url}/ds_common-return"
15+
pdf_filename = args[:pdf_filename]
16+
host_email = args[:host_email]
17+
host_name = args[:host_name]
18+
signer_name = args[:signer_name]
19+
20+
# Step 1. Create the envelope definition
21+
envelope = make_envelope(pdf_filename, host_email, host_name, signer_name)
22+
23+
# Step 2. Call DocuSign to create the envelope
24+
envelope_api = create_envelope_api(args)
25+
26+
results = envelope_api.create_envelope args[:account_id], envelope
27+
envelope_id = results.envelope_id
28+
29+
# Step 3. Create the recipient view for the embedded signing
30+
view_request = make_recipient_view_request(ds_return_url, ds_ping_url, host_email, host_name
31+
)
32+
33+
# Call the CreateRecipientView API
34+
results = envelope_api.create_recipient_view args[:account_id], envelope_id, view_request
35+
36+
# Step 4. Redirect the user to the embedded signing
37+
# Don't use an iframe!
38+
# State can be stored/recovered using the framework's session or a
39+
# query parameter on the returnUrl (see the makeRecipientViewRequest method)
40+
# Redirect to results.url
41+
results.url
42+
end
43+
44+
private
45+
46+
def make_recipient_view_request(ds_return_url, ds_ping_url, host_email, host_name)
47+
view_request = DocuSign_eSign::RecipientViewRequest.new
48+
# Set the URL where you want the recipient to go once they are done signing
49+
# should typically be a callback route somewhere in your app.
50+
# The query parameter is included as an example of how
51+
# to save/recover state information during the redirect to
52+
# the DocuSign signing. It's usually better to use
53+
# the session mechanism of your web framework. Query parameters
54+
# can be changed/spoofed very easily.
55+
view_request.return_url = ds_return_url + '?state=123'
56+
57+
# How has your app authenticated the user? In addition to your app's
58+
# authentication, you can include authenticate steps from DocuSign;
59+
# e.g., SMS authentication
60+
view_request.authentication_method = 'none'
61+
62+
# Recipient information must match the embedded recipient info
63+
# that was used to create the envelope
64+
view_request.email = host_email
65+
view_request.user_name = host_name
66+
67+
# DocuSign recommends that you redirect to DocuSign for the embedded signing. There are
68+
# multiple ways to save state. To maintain your application's session, use the pingUrl
69+
# parameter. It causes the DocuSign signing web page (not the DocuSign server)
70+
# to send pings via AJAX to your app
71+
view_request.ping_frequency = '600' # seconds
72+
# NOTE: The pings will only be sent if the pingUrl is an HTTPS address
73+
view_request.ping_url = ds_ping_url # Optional setting
74+
75+
view_request
76+
end
77+
78+
def make_envelope(pdf_filename, host_email, host_name, signer_name)
79+
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new
80+
envelope_definition.email_subject = 'Please sign this document sent from Ruby SDK'
81+
82+
doc1 = DocuSign_eSign::Document.new
83+
doc1.document_base64 = Base64.encode64(File.binread(pdf_filename))
84+
doc1.name = 'Lorem Ipsum'
85+
doc1.file_extension = 'pdf'
86+
doc1.document_id = '1'
87+
88+
# The order in the docs array determines the order in the envelope
89+
envelope_definition.documents = [doc1]
90+
# Create an in person signer recipient to sign the document
91+
# We're setting the parameters via the object creation
92+
93+
in_person_signer = DocuSign_eSign::InPersonSigner.new
94+
in_person_signer.host_email = host_email
95+
in_person_signer.host_name = host_name
96+
in_person_signer.signer_name = signer_name
97+
in_person_signer.recipient_id = '1'
98+
in_person_signer.routing_order = '1'
99+
100+
# The DocuSign platform searches throughout your envelope's documents for matching
101+
# anchor strings.
102+
sign_here = DocuSign_eSign::SignHere.new
103+
sign_here.anchor_string = '/sn1/'
104+
sign_here.anchor_units = 'pixels'
105+
sign_here.anchor_x_offset = '20'
106+
sign_here.anchor_y_offset = '10'
107+
# Tabs are set per recipient/signer
108+
tabs = DocuSign_eSign::Tabs.new
109+
tabs.sign_here_tabs = [sign_here]
110+
in_person_signer.tabs = tabs
111+
# Add the recipients to the envelope object
112+
recipients = DocuSign_eSign::Recipients.new
113+
recipients.in_person_signers = [in_person_signer]
114+
115+
envelope_definition.recipients = recipients
116+
# Request that the envelope be sent by setting status to "sent".
117+
# To request that the envelope be created as a draft, set status to "created"
118+
envelope_definition.status = 'sent'
119+
envelope_definition
120+
end
121+
# ***DS.snippet.0.end
122+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class ESign::GetDataService
2+
attr_reader :args
3+
4+
def initialize(access_token, base_path)
5+
@args = {
6+
access_token: access_token,
7+
base_path: base_path
8+
}
9+
end
10+
11+
def get_current_user_email
12+
worker
13+
14+
user_info = @api_client.get_user_info(args[:access_token])
15+
unless user_info
16+
raise "The user does not have access to account"
17+
end
18+
19+
user_info.email
20+
end
21+
22+
def get_current_user_name
23+
worker
24+
25+
user_info = @api_client.get_user_info(args[:access_token])
26+
unless user_info
27+
raise "The user does not have access to account"
28+
end
29+
30+
user_info.name
31+
end
32+
33+
private
34+
35+
def worker
36+
configuration = DocuSign_eSign::Configuration.new
37+
configuration.host = args[:base_path]
38+
39+
@api_client = DocuSign_eSign::ApiClient.new(configuration)
40+
@api_client.set_base_path(args[:base_path])
41+
@api_client.set_default_header("Authorization", "Bearer #{args[:access_token]}")
42+
43+
end
44+
end

0 commit comments

Comments
 (0)