forked from docusign/code-examples-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eg014_service.rb
221 lines (203 loc) · 8.61 KB
/
eg014_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# frozen_string_literal: true
class ESign::Eg014Service
include ApiCreator
attr_reader :args, :envelope_args
def initialize(request, session)
@envelope_args = {
signer_email: request.params['signerEmail'].gsub(/([^\w \-\@\.\,])+/, ''),
signer_name: request.params['signerName'].gsub(/([^\w \-\@\.\,])+/, ''),
cc_email: request.params['ccEmail'].gsub(/([^\w \-\@\.\,])+/, ''),
cc_name: request.params['ccName'].gsub(/([^\w \-\@\.\,])+/, ''),
gateway_account_id: Rails.application.config.gateway_account_id,
gateway_name: Rails.application.config.gateway_name,
gateway_display_name: Rails.application.config.gateway_display_name
}
@args = {
account_id: session['ds_account_id'],
base_path: session['ds_base_path'],
access_token: session['ds_access_token'],
envelope_args: @envelope_args
}
end
def call
results = worker args
end
private
# ***DS.snippet.0.start
def worker(args)
envelope_definition = make_envelope(envelope_args)
# 2. Create and send the envelope
# Exceptions will be caught by the calling function
envelope_api = create_envelope_api(args)
results = envelope_api.create_envelope args[:account_id], envelope_definition
envelope_id = results.envelope_id
{ envelope_id: envelope_id }
end
def make_envelope(args)
# This function creates the envelope definition for the order form
# document 1 (html) has multiple tags:
# /l1q/ and /l2q/ -- quantities: drop down
# /l1e/ and /l2e/ -- extended: payment lines
# /l3t/ -- total -- formula
#
# The envelope has two recipients:
# recipient 1 - signer
# recipient 2 - cc
# The envelope will be sent first to the signer
# After it is signed, a copy is sent to the cc person
#
# #################################################################
# #
# # NOTA BENE: This method programmatically constructs the
# # order form. For many use cases, it would be
# # better to create the order form as a template
# # using the DocuSign web tool as WYSIWYG
# # form designer.
# #
# #################################################################
#
# Order form constants
l1_name = 'Harmonica'
l1_price = 5
l1_description = "#{l1_price} each"
l2_name = 'Xylophone'
l2_price = 150
l2_description = "#{l2_price} each"
currency_multiplier = 100
# Read the HTML file from a local directory
# The read could raise an exception if the file is not available!
doc1_file = 'order_form.html'
doc1_html_v1 = File.binread(File.join('data', doc1_file))
# Substitute values into the HTML
# Substitute for: {signerName}, {signerEmail}, {ccName}, {ccEmail}
doc1_html_v2 = doc1_html_v1.gsub('{signerName}', args[:signer_name]) \
.gsub('{signerEmail}', args[:signer_email]) \
.gsub('{ccName}', args[:cc_name]) \
.gsub('{ccEmail}', args[:cc_email])
# Create the envelope definition
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new(
emailSubject: 'Please complete your order',
status: 'sent'
)
# Add the document
doc1_b64 = Base64.encode64(doc1_html_v2)
doc1 = DocuSign_eSign::Document.new(
documentBase64: doc1_b64,
name: 'Order form', # Can be different from actual file name
fileExtension: 'html', # Source data format
documentId: '1' # A label used to reference the doc
)
envelope_definition.documents = [doc1]
# Create a signer recipient to sign the document
signer1 = DocuSign_eSign::Signer.new(
email: args[:signer_email], name: args[:signer_name],
recipientId: '1', routingOrder: '1'
)
# Create a cc recipient to receive a copy of the documents
cc1 = DocuSign_eSign::CarbonCopy.new(
email: args[:cc_email], name: args[:cc_name],
routingOrder: '2', recipientId: '2'
)
# Create signHere fields (also known as tabs) on the documents,
# We're using anchor (autoPlace) positioning
sign_here1 = DocuSign_eSign::SignHere.new(
anchorString: '/sn1/', anchorUnits: 'pixels',
anchorYOffset: '10', anchorXOffset: '20'
)
list_item0 = DocuSign_eSign::ListItem.new(text: 'none', value: '0')
list_item1 = DocuSign_eSign::ListItem.new(text: '1', value: '1')
list_item2 = DocuSign_eSign::ListItem.new(text: '2', value: '2')
list_item3 = DocuSign_eSign::ListItem.new(text: '3', value: '3')
list_item4 = DocuSign_eSign::ListItem.new(text: '4', value: '4')
list_item5 = DocuSign_eSign::ListItem.new(text: '5', value: '5')
list_item6 = DocuSign_eSign::ListItem.new(text: '6', value: '6')
list_item7 = DocuSign_eSign::ListItem.new(text: '7', value: '7')
list_item8 = DocuSign_eSign::ListItem.new(text: '8', value: '8')
list_item9 = DocuSign_eSign::ListItem.new(text: '9', value: '9')
list_item10 = DocuSign_eSign::ListItem.new(text: '10', value: '10')
listl1q = DocuSign_eSign::List.new(
font: 'helvetica', fontSize: 'size11',
anchorString: '/l1q/', anchorUnits: 'pixels',
anchorYOffset: '-10', anchorXOffset: '0',
listItems: [list_item0, list_item1, list_item2,
list_item3, list_item4, list_item5, list_item6,
list_item7, list_item8, list_item9, list_item10],
required: 'true', tabLabel: 'l1q'
)
listl2q = DocuSign_eSign::List.new(
font: 'helvetica', fontSize: 'size11',
anchorString: '/l2q/', anchorUnits: 'pixels',
anchorYOffset: '-10', anchorXOffset: '0',
listItems: [list_item0, list_item1, list_item2,
list_item3, list_item4, list_item5, list_item6,
list_item7, list_item8, list_item9, list_item10],
required: 'true', tabLabel: 'l2q'
)
# Create two formula tabs for the extended price on the line items
formulal1e = DocuSign_eSign::FormulaTab.new(
font: 'helvetica', fontSize: 'size11',
anchorString: '/l1e/', anchorUnits: 'pixels',
anchorYOffset: '-8', anchorXOffset: '105',
tabLabel: 'l1e', formula: "[l1q] * #{l1_price}",
roundDecimalPlaces: '0', required: 'true',
locked: 'true', disableAutoSize: 'false'
)
formulal2e = DocuSign_eSign::FormulaTab.new(
font: 'helvetica', fontSize: 'size11',
anchorString: '/l2e/', anchorUnits: 'pixels',
anchorYOffset: '-8', anchorXOffset: '105',
tabLabel: 'l2e', formula: "[l2q] * #{l2_price}",
roundDecimalPlaces: '0', required: 'true',
locked: 'true', disableAutoSize: 'false'
)
# Formula for the total
formulal3t = DocuSign_eSign::FormulaTab.new(
font: 'helvetica', fontSize: 'size11',
anchorString: '/l3t/', anchorUnits: 'pixels',
anchorYOffset: '-8', anchorXOffset: '50',
tabLabel: 'l3t', formula: '[l1e] + [l2e]',
roundDecimalPlaces: '0', required: 'true',
locked: 'true', disableAutoSize: 'false'
)
# Payment line items
payment_line_iteml1 = DocuSign_eSign::PaymentLineItem.new(
name: l1_name, description: l1_description,
amountReference: 'l1e'
)
payment_line_iteml2 = DocuSign_eSign::PaymentLineItem.new(
name: l2_name, description: l2_description,
amountReference: 'l2e'
)
payment_details = DocuSign_eSign::PaymentDetails.new(
gatewayAccountId: args[:gateway_account_id],
currencyCode: 'USD',
gatewayName: args[:gateway_name],
lineItems: [payment_line_iteml1, payment_line_iteml2]
)
# Hidden formula for the payment itself
formula_payment = DocuSign_eSign::FormulaTab.new(
tabLabel: 'payment',
formula: "([l1e] + [l2e]) * #{currency_multiplier}",
roundDecimalPlaces: '0',
paymentDetails: payment_details,
hidden: 'true', required: 'true', locked: 'true',
documentId: '1', pageNumber: '1',
xPosition: '0', yPosition: '0'
)
# Tabs are set per recipient / signer
signer1_tabs = DocuSign_eSign::Tabs.new(
signHereTabs: [sign_here1],
listTabs: [listl1q, listl2q],
formulaTabs: [formulal1e, formulal2e,
formulal3t, formula_payment]
)
signer1.tabs = signer1_tabs
# Add the recipients to the envelope object
recipients = DocuSign_eSign::Recipients.new(
signers: [signer1], carbonCopies: [cc1]
)
envelope_definition.recipients = recipients
envelope_definition
end
# ***DS.snippet.0.end
end