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