尝试从docusign Rails示例向信封定义添加自定义字段

时间:2019-07-15 15:44:25

标签: ruby-on-rails docusignapi

我从Rails示例中获得了有关如何在您的Rails应用程序中实现docusign嵌入式签名的嵌入式签名方法。

我添加了一个custom_fields对象,并添加到了根据示例创建的信封对象

def embedded_signing
    # base_url is the url of this application. Eg http://localhost:3000
    base_url = request.base_url
    user = HiringManager.find params[:hiring_manager_id]
    # Fill in these constants
    # Obtain an OAuth token from https://developers.hqtest.tst/oauth-token-generator

    access_token = Token.access_token

    # Obtain your accountId from demo.docusign.com -- the account id is shown in the drop down on the
    # upper right corner of the screen by your picture or the default picture.
    account_id = ENV["docusign_client_id"]

    # Recipient Information:
    signer_name = user.full_name 
    signer_email = user.email

    base_path = 'http://demo.docusign.net/restapi'
    client_user_id = user.id # Used to indicate that the signer will use an embedded
    # Signing Ceremony. Represents the signer's userId within
    # your application.
    authentication_method = 'None' # How is this application authenticating
    # the signer? See the `authenticationMethod' definition
    file_name = 'agreement.pdf' # The document to be signed.

    # Step 1. Create the envelope request definition
    envelope_definition = DocuSign_eSign::EnvelopeDefinition.new
    envelope_definition.email_subject = "Please sign this Newcraft Placement Agreement"

    doc = DocuSign_eSign::Document.new({
      :documentBase64 => Base64.encode64(File.binread(File.join('data', file_name))),
      :name => "Agreement signed", :fileExtension => "pdf", :documentId => "1"})

    # The order in the docs array determines the order in the envelope
    envelope_definition.documents = [doc]
    # create a signer recipient to sign the document, identified by name and email
    # We're setting the parameters via the object creation
    signer = DocuSign_eSign::Signer.new ({
        :email => signer_email, :name => signer_name,
        :clientUserId => client_user_id,  :recipientId => 1
    })
    sign_here = DocuSign_eSign::SignHere.new ({
        :documentId => '1', :pageNumber => '4',
        :recipientId => '1', :tabLabel => 'SignHereTab',
        :xPosition => '75', :yPosition => '70'
    })

    # Tabs are set per recipient / signer
    tabs = DocuSign_eSign::Tabs.new({:signHereTabs => [sign_here]})

    signer.tabs = tabs
    # Add the recipients to the envelope object
    recipients = DocuSign_eSign::Recipients.new({:signers => [signer]})

    envelope_definition.recipients = recipients
    # Add custom fields to the envelope object
    custom_fields = DocuSign_eSign::CustomFieldV2.new({
        :configuration_type => 'text', :required => 'true',
        :name => 'date', :fieldId => '', :value => 'Todays date'
    })

    envelope_definition.custom_fields = custom_fields
    # Request that the envelope be sent by setting |status| to "sent".
    # To request that the envelope be created as a draft, set to "created"
    envelope_definition.status = "sent"

    # Step 2. Call DocuSign with the envelope definition to have the
    #         envelope created and sent
    configuration = DocuSign_eSign::Configuration.new
    configuration.host = base_path
    api_client = DocuSign_eSign::ApiClient.new configuration
    api_client.default_headers["Authorization"] = "Bearer " + access_token
    envelopes_api = DocuSign_eSign::EnvelopesApi.new api_client

    results = envelopes_api.create_envelope account_id, envelope_definition
    envelope_id = results.envelope_id

    # Step 3. create the recipient view request for the Signing Ceremony
    view_request =  DocuSign_eSign::RecipientViewRequest.new
    # Set the url where you want the recipient to go once they are done signing
    # should typically be a callback route somewhere in your app.
    view_request.return_url = "https://juice.newcraft.io/edit-manager"
    # How has your app authenticated the user? In addition to your app's
    # authentication, you can include authenticate steps from DocuSign.
    # Eg, SMS authentication
    view_request.authentication_method = authentication_method
    # Recipient information must match embedded recipient info
    # we used to create the envelope.
    view_request.email = signer_email
    view_request.user_name = signer_name
    view_request.client_user_id = client_user_id

    # Step 4. call the CreateRecipientView API
    results = envelopes_api.create_recipient_view account_id, envelope_id, view_request

    user.signed_agreement = true 
    user.save

    # Step 5. Redirect the user to the Signing Ceremony
    # Don't use an iFrame!
    # State can be stored/recovered using the framework's session or a
    # query parameter on the returnUrl (see the makeRecipientViewRequest method)
    render json: results

  rescue DocuSign_eSign::ApiError => e
    @error_msg = e.response_body
    render json: @error_msg
  end

我发现很难理解如何插入自定义字段,以便用户可以手动填写显示给用户签名的pdf协议文档。我也知道我需要添加自定义字段标签将位于的位置,文档并没有真正解释如何添加到通过方法创建的信封对象。

1 个答案:

答案 0 :(得分:1)

首先,让我们尝试看看我们是否了解您的要求。您希望用户在信封上填写一些数据,然后在信封完成后在您的应用程序中收集这些数据,对吗? 为此,您不需要自定义字段。您可以使用常规标签轻松地做到这一点。文本选项卡可能是最简单的方法。您将文本选项卡添加到信封中,类似于添加SignHere选项卡的方式,并且用户必须填写文本/值。然后,您可以使用其他API调用获取此信息。 这是获取选项卡值的API调用: https://developers.docusign.com/esign-rest-api/reference/Envelopes/EnvelopeRecipientTabs/ 如果您使用的是v2或V2.1 API(只需将2.1替换为2),基本上就可以执行GET /v2.1/accounts/{accountId}/envelopes/{envelopeId}/recipients/{recipientId}/tabs