在发送信封之前需要帮助使用动态数据预填充 DocuSign 模板文档字段

问题描述 投票:0回答:1

我目前正在开发一个项目,需要在发送信封之前用动态数据预填充 DocuSign 模板文档字段。我尝试了多种方法,但到目前为止还没有成功。
目前的做法:
我正在使用 DocuSign eSign Ruby SDK 与 DocuSign API 进行交互。这是我的代码的简化版本:

 def send_data_using_template(access_token)
    api_client = DocuSign_eSign::ApiClient.new
    api_client.default_headers['Authorization'] = 'Bearer ' + access_token

    templates_api = DocuSign_eSign::TemplatesApi.new(api_client)
    envelopes_api = DocuSign_eSign::EnvelopesApi.new(api_client)
    options = DocuSign_eSign::ListTemplatesOptions.new

    options.search_text = "Example document generation template"

    results = templates_api.list_templates(ENV['DOCUSIGN_ACCOUNT_ID'], options)
    template = results.envelope_templates.find { |template| template.name == "Example document generation template" }
    byebug
    template_id = template.template_id if template

    byebug
    current_user_name = "Alia"
    current_user_email = "[email protected]"

    envelope_definition = DocuSign_eSign::EnvelopeDefinition.new({
                        status: 'sent',
                        templateId: template_id,
                        emailSubject:" EMAIL_SUBJECT"
                      })

    text_tab = DocuSign_eSign::Text.new
    text_tab.tab_label = 'Text 9104f26a-e0fc-4b18-9843-07944aaedcda'
    text_tab.value = 'Jabberwocky!'

    tabs = DocuSign_eSign::Tabs.new
    tabs.text_tabs = [text_tab]

    signer = DocuSign_eSign::TemplateRole.new({
      email: current_user_email,
      name: current_user_name,
      roleName: 'signer',
      tabs: tabs     
    })
        
    envelope_definition.template_roles = [signer]

    envelopes_api = DocuSign_eSign::EnvelopesApi.new api_client
    response = envelopes_api.create_envelope ENV['DOCUSIGN_ACCOUNT_ID'], envelope_definition
  

    if response.envelope_id.present?
      puts "Envelope created successfully"
      loop do
        envelope_status = envelopes_api.get_envelope ENV['DOCUSIGN_ACCOUNT_ID'], response.envelope_id
        puts "Envelope status: #{envelope_status.status}.................................."
        break if ['completed', 'signed'].include?(envelope_status.status)
        sleep 5
      end

      response = HTTParty.get(
              "https://demo.docusign.net/restapi/v2.1/accounts/#{ENV['DOCUSIGN_ACCOUNT_ID']}/envelopes/#{response.envelope_id}/documents/combined",
              headers: { "Authorization" => "Bearer #{access_token}" }
            )

      if response.success?
        timestamp = Time.now.strftime("%Y%m%d%H%M%S")
        signed_document_path = "config/signed_document_#{timestamp}.pdf"
        File.open(signed_document_path, "wb") { |file| file.write(response.body) }
        puts "Signed document saved successfully at #{signed_document_path}"
      else
        puts "Error retrieving signed document: #{response}"
      end
    else
      puts "Error creating envelope: #{response}"
    end
  end

我探索了以下资源和方法:

  1. 有关预填充选项卡的 DocuSign 文档:链接

  2. 堆栈溢出线程:线程 1线程 2

但是,我无法使用这些方法成功地使用动态数据预填充模板字段。

ruby-on-rails templates docusignapi custom-fields docusign-sdk
1个回答
0
投票

首先,我建议您尝试 https://developers.docusign.com/docs/esign-rest-api/quickstart/ 并选择 Ruby 和完整包,您将获得可以正常工作的代码。

以下是使用 Ruby 代码执行此操作的方法(来自 - https://github.com/docusign/code-examples-ruby/blob/master/app/services/e_sign/eg017_set_template_tab_values_service.rb):

(请注意,您不需要预填充选项卡来预填充选项卡,这是一个不幸的令人困惑的名称,常规选项卡可以预填充没有问题)

    signer_client_id = 1000
    envelope_args = args[:envelope_args]
    ds_ping_url = envelope_args[:ds_ping_url]
    ds_return_url = "#{ds_ping_url}/ds_common-return"

    # Step 4. Construct the request body
    #ds-snippet-start:eSign17Step4
    envelope_definition = make_envelope(envelope_args)
    #ds-snippet-end:eSign17Step4

    # Step 5. Call the eSignature REST API
    #ds-snippet-start:eSign17Step5
    envelope_api = create_envelope_api(args)
    results = envelope_api.create_envelope args[:account_id], envelope_definition
    envelope_id = results.envelope_id
    #ds-snippet-end:eSign17Step5

    # Step 6. Create the View Request
    #ds-snippet-start:eSign17Step6
    view_request = make_recipient_view_request(envelope_args[:signer_email], envelope_args[:signer_name], signer_client_id, ds_return_url, ds_ping_url)

    # Call the CreateRecipientView API
    results = envelope_api.create_recipient_view args[:account_id], envelope_id, view_request

    # Redirect the user to the embedded signing
    # Don't use an iframe!
    # State can be stored/recovered using the framework's session or a
    # query parameter on the return URL (see the makeRecipientViewRequest method)
    # Redirect to results.url
    results.url
    #ds-snippet-end:eSign17Step6
  end

  private

  def make_envelope(args)
    # Create the envelope definition with the template_id
    envelope_definition = DocuSign_eSign::EnvelopeDefinition.new({
                                                                   status: 'sent',
                                                                   templateId: args[:template_id]
                                                                 })
    # Create the template role elements to connect the signer and cc recipients
    # to the template
    signer = DocuSign_eSign::TemplateRole.new({
                                                clientUserId: 1000,
                                                email: args[:signer_email],
                                                name: args[:signer_name],
                                                roleName: 'signer'
                                              })
    # Create a cc template role.
    cc = DocuSign_eSign::TemplateRole.new({
                                            email: args[:cc_email],
                                            name: args[:cc_name],
                                            roleName: 'cc'
                                          })

    # Step 3. Create Tabs and CustomFields

    # List item
    #ds-snippet-start:eSign17Step3
    list1 = DocuSign_eSign::List.new
    list1.value = 'Green'
    list1.document_id = '1'
    list1.page_number = '1'
    list1.tab_label = 'list'

    # Checkboxes
    check1 = DocuSign_eSign::Checkbox.new
    check1.tab_label = 'ckAuthorization'
    check1.selected = 'true'

    check3 = DocuSign_eSign::Checkbox.new
    check3.tab_label = 'ckAgreement'
    check3.selected = 'true'

    radioGroup = DocuSign_eSign::RadioGroup.new
    radioGroup.group_name = 'radio1'
    radio = DocuSign_eSign::Radio.new
    radio.value = 'white'
    radio.selected = 'true'
    radioGroup.radios = [radio]

    text = DocuSign_eSign::Text.new
    text.tab_label = 'text'
    text.value = 'Jabberwocky!'

    # We can also add a new tab (field) to the ones already in the template
    textExtra = DocuSign_eSign::Text.new
    textExtra.document_id = '1'
    textExtra.page_number = '1'
    textExtra.x_position = '280'
    textExtra.y_position = '172'
    textExtra.font = 'helvetica'
    textExtra.font_size = 'size14'
    textExtra.tab_label = 'added text field'
    textExtra.height = '23'
    textExtra.width = '84'
    textExtra.required = 'false'
    textExtra.bold = 'true'
    textExtra.value = args[:signer_name]
    textExtra.locked = 'false'
    textExtra.tab_id = 'name'

    # Pull together the existing and new tabs in a Tabs object
    tabs = DocuSign_eSign::Tabs.new
    tabs.list_tabs = [list1]
    tabs.checkbox_tabs = [check1, check3]
    tabs.radio_group_tabs = [radioGroup]
    tabs.text_tabs = [text, textExtra]
    signer.tabs = tabs

    # Add the TemplateRole objects to the envelope object
    envelope_definition.template_roles = [signer, cc]
    envelope_definition
    #ds-snippet-end:eSign17Step3
  end
© www.soinside.com 2019 - 2024. All rights reserved.