forked from docusign/code-examples-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eg031_service.rb
185 lines (160 loc) · 6.24 KB
/
eg031_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
# frozen_string_literal: true
class ESign::Eg031Service
include ApiCreator
attr_reader :args, :signers
def initialize(request, session)
@signers = {
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 \-\@\.\,])+/, ''),
status: 'created',
signer_email1: request.params['signerEmail1'].gsub(/([^\w \-\@\.\,])+/, ''),
signer_name1: request.params['signerName1'].gsub(/([^\w \-\@\.\,])+/, ''),
cc_email1: request.params['ccEmail1'].gsub(/([^\w \-\@\.\,])+/, ''),
cc_name1: request.params['ccName1'].gsub(/([^\w \-\@\.\,])+/, '')
}
@args = {
account_id: session['ds_account_id'],
base_path: session['ds_base_path'],
access_token: session['ds_access_token']
}
end
def call
# Step 1. Obtain your OAuth token
configuration = DocuSign_eSign::Configuration.new
configuration.host = args[:base_path]
api_client = DocuSign_eSign::ApiClient.new configuration
# Step 2. Construct your API headers
construct_api_headers(api_client)
# Step 3. Create and submit the bulk sending list
bulk_envelopes_api = DocuSign_eSign::BulkEnvelopesApi.new api_client
bulk_sending_list = create_bulk_sending_list
bulk_list = bulk_envelopes_api.create_bulk_send_list(args[:account_id], bulk_sending_list)
bulk_list_id = bulk_list.list_id
# Step 4. Create the draft envelope
envelope_api = create_envelope_api(args)
envelope_definition = make_envelope
envelope = envelope_api.create_envelope(args[:account_id], envelope_definition, options = DocuSign_eSign::CreateEnvelopeOptions.default)
envelope_id = envelope.envelope_id
# Step 5. Attach your bulk list ID to the envelope
envelope_api.create_custom_fields(args[:account_id], envelope_id, custom_fields(bulk_list_id))
# Step 6. Add placeholder recipients
recipients = DocuSign_eSign::Recipients.new(signers: recipients_data)
envelope_api.create_recipient(args[:account_id], envelope_id, recipients, options = DocuSign_eSign::CreateRecipientOptions.default)
# Step 7. Initiate bulk send
bulk_send_request = DocuSign_eSign::BulkSendRequest.new(envelopeOrTemplateId: envelope_id)
batch = bulk_envelopes_api.create_bulk_send_request(args[:account_id], bulk_list_id, bulk_send_request)
batch_id = batch.batch_id
end
private
def construct_api_headers(api_client)
api_client.default_headers['Authorization'] = "Bearer #{args[:access_token]}"
api_client.default_headers['Content-Type'] = "application/json;charset=UTF-8"
api_client.default_headers['Accept'] = "application/json, text/plain, */*"
api_client.default_headers['Accept-Encoding'] = "gzip, deflate, br"
api_client.default_headers['Accept-Language'] = "en-US,en;q=0.9"
end
def create_bulk_sending_list
bulk_copies = []
recipient1 = DocuSign_eSign::BulkSendingCopyRecipient.new(
roleName: 'signer',
tabs: [],
name: signers[:signer_name],
email: signers[:signer_email],
)
# Create a cc recipient to receive a copy of the documents
recipient2 = DocuSign_eSign::BulkSendingCopyRecipient.new(
roleName: 'cc',
tabs: [],
name: signers[:cc_name],
email: signers[:cc_email],
)
recipient3 = DocuSign_eSign::BulkSendingCopyRecipient.new(
roleName: 'signer',
tabs: [],
name: signers[:signer_name1],
email: signers[:signer_email1],
)
recipient4 = DocuSign_eSign::BulkSendingCopyRecipient.new(
roleName: 'cc',
tabs: [],
name: signers[:cc_name1],
email: signers[:cc_email1],
)
# Add the recipients to the envelope object
bulk_copy1 = DocuSign_eSign::BulkSendingCopy.new(
recipients: [recipient1, recipient2],
custom_fields: []
)
bulk_copy2 = DocuSign_eSign::BulkSendingCopy.new(
recipients: [recipient3, recipient4],
custom_fields: []
)
bulk_copies.append(bulk_copy1, bulk_copy2)
bulk_sending_list = DocuSign_eSign::BulkSendingList.new(
name: "sample.csv",
bulkCopies: bulk_copies
)
bulk_sending_list
end
def custom_fields(bulk_list_id)
text_custom_fields = DocuSign_eSign::TextCustomField.new(
name:'mailingListId',
required: 'false',
show: 'false',
value: bulk_list_id
)
custom_fields = DocuSign_eSign::CustomFields.new(
listCustomFields: [],
textCustomFields: [text_custom_fields]
)
end
def recipients_data
signer = DocuSign_eSign::Signer.new(
name: "Multi Bulk Recipient::signer",
email: "[email protected]",
roleName: "signer",
note: "",
routingOrder: 1,
status: "created",
templateAccessCodeRequired: "null",
deliveryMethod: "email",
recipientId: "10",
recipientType: "signer"
)
cc = DocuSign_eSign::Signer.new(
name: "Multi Bulk Recipient::cc",
email: "[email protected]",
roleName: "cc",
note: "",
routingOrder: 1,
status: "created",
templateAccessCodeRequired: "null",
deliveryMethod: "email",
recipientId: "11",
recipientType: "signer"
)
[signer, cc]
end
def make_envelope
# Create the envelope definition
envelope_definition = DocuSign_eSign::EnvelopeDefinition.new
envelope_definition.email_subject = 'Please sign this document set'
# Add the documents
doc_b64 = "DQoNCg0KCQkJCXRleHQgZG9jDQoNCg0KDQoNCg0KUk0gIwlSTSAjCVJNICMNCg0KDQoNClxzMVwNCg0KLy9hbmNoMSANCgkvL2FuY2gyDQoJCS8vYW5jaDM="
# Create the document models
doc = DocuSign_eSign::Document.new(
# Create the DocuSign Document object
documentBase64: doc_b64,
name: 'NDA', # Can be different from the actual file name
fileExtension: 'txt', # Many different document types are accepted
documentId: '1' # A label used to reference the doc
)
# The order in the docs array determines the order in the envelope
envelope_definition.documents = [doc]
envelope_definition.envelope_id_stamping = "true"
envelope_definition.status = "created"
envelope_definition
end
end