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
0 commit comments