Docusign:在没有templateID的情况下获取嵌入式收件人视图

时间:2014-03-14 10:01:00

标签: javascript digital-signature docusignapi electronic-signature

我有这样的场景,我需要用户访问网站上传文档,而其他用户必须签署此文档。

到目前为止我做了什么:

第1步:通过电子邮件,密码和Integratorkey登录

function(next) {
        var url = "https://demo.docusign.net/restapi/v2/login_information";
        var body = "";  // no request body for login api call

        // set request url, method, body, and headers
        var options = initializeRequest(url, "GET", body, email, password);

        // send the request...
        request(options, function(err, res, body) {
            if(!parseResponseBody(err, res, body)) {
                return;
            }
            baseUrl = JSON.parse(body).loginAccounts[0].baseUrl;
            next(null); // call next function
        });
    },

我收到有效的回复,包括有效的帐号ID。

第2步:现在我通过此API上传文档

function(next) {    
        var url = baseUrl + "/envelopes";
        // following request body will place 1 signature tab 100 pixels to the right and
        // 100 pixels down from the top left of the document that you send in the request
        var body = {
            "recipients": {
                "signers": [{
                    "email": recipientEmail,
                    "name": recipientName,
                    "recipientId": 1,
                    "tabs": {
                        "signHereTabs": [{
                            "xPosition": "100",
                            "yPosition": "100",
                            "documentId": "1",
                            "pageNumber": "1"                                                                                   
                        }]
                    }
                }]
            },
            "emailSubject": 'checkkkkkkkk API !!!!!',
            "documents": [{
                "name": "abc.pdf",
                "documentId": 1,
            }],
            "status": "sent",
        };

        // set request url, method, body, and headers
        var options = initializeRequest(url, "POST", body, email, password);

        // change default Content-Type header from "application/json" to "multipart/form-data"
        options.headers["Content-Type"] = "multipart/form-data";

        // configure a multipart http request with JSON body and document bytes
        options.multipart = [{
                    "Content-Type": "application/json",
                    "Content-Disposition": "form-data",
                    "body": JSON.stringify(body),
                }, {
                    "Content-Type": "application/pdf",
                    'Content-Disposition': 'file; filename="' + documentName + '"; documentId=1',
                    "body": fs.readFileSync(documentName),
                }
        ];

        // send the request...
        request(options, function(err, res, body) {
            parseResponseBody(err, res, body);
            envelopeId = JSON.parse(body).envelopeId;
            console.log(envelopeId);
            next(null);
        });

    }, 

这里作为回应我得到一个有效的EnvelopeID(肯定!!)

第3步:现在我希望其他用户(如上面提供的recipientEmail / name)在我的网站上的嵌入视图中签署此文档 为此,我正在使用此API http://iodocs.docusign.com/APIWalkthrough/embeddedSigning#js 但是这需要一个templateId和角色,而上面使用的API没有返回给我们。这需要手动上传模板和获取模板ID,这在我的场景中是不可能的,因为我希望一切都是自动的。

任何人都可以指导我如何进行嵌入式签名。

DocuSignAPI

2 个答案:

答案 0 :(得分:1)

如果您希望签名者通过您的网站访问信封,则需要将签名者指定为" embedded / captive"创建信封时的签名者。这是通过在Create Envelope API Request中的Recipient对象上设置clientUserId属性来完成的。 (此属性可以设置为您选择的任何值 - 最大长度为100个字符,但您的应用程序需要跟踪它,因为当他们来到时,您需要它来启动收件人的签名会话你的网站。)

所以,它的工作原理如下:

  1. 您的应用程序通过" 创建信封"创建信封; API请求,为收件人(签名者)设置clientUserId属性,以表明他们将通过您的应用程序访问信封。为了这个例子,我们假设您将 clientUserId 设置为 1234

    "签名者":[{     "电子邮件":" janesemail@outlook.com",     "姓名":" Jane Doe",     " recipientId":1,     " clientUserId":1234 }]

  2. 您的申请通知签名人(通过电子邮件),他们需要在文件上签名;该电子邮件提供有关如何通过您的网站访问信封(即签署文件)的信息。 (DocuSign不会发送"签署邀请和#34;电子邮件给指定为嵌入式/俘虏的收件人。)

  3. 签名者按照您的应用程序发送的电子邮件中的说明进行操作,并访问您的网站以签署其文档。当签名者准备签名时,您的应用程序会提交" POST收件人视图" API请求获取将为指定收件人启动DocuSign签名会话的URL。请求如下所示:

  4. POST https://{{env}}.docusign.net/restapi/{{version}}/accounts/{{acctId}}/envelopes/{{envelopeId}}/views/recipient
    
    {
         "authenticationMethod": "Email",
         "clientUserId": "1234",
         "userName": "Jane Doe",
         "email": "janesemail@outlook.com",
         "returnUrl": "URLInYourAppThatDocuSignRedirectsToWhenDocuSignSessionIsCompleted"
    }
    

    对此请求的响应将包含您的应用程序可用于启动收件人签名会话的URL。

答案 1 :(得分:0)

以下是您要完成的Node.js中的完整工作示例。您要做的是组合在文档上发送签名请求的API Walkthrough(以便它不使用模板),并将其与Embedded Signing api演练中的第三个调用相结合。

要将嵌入式收件人添加到信封,您需要将其clientUserId属性设置为任何用户可配置的值。例如,您可以将其设置为" 1"或" 1234"或" 1a2b3c"。在下面的代码中,我将其设置为" 1001"要强调您可以将其设置为您喜欢的任何内容,您只需要跟踪它。 一个重要提示:您必须确保在请求收件人的嵌入式登录网址时,您引用了最初将其添加到信封时设置的同一个clientUserId(1001 in以下示例)。

以下是代码:

// To run this sample
//  1. Copy the file to your local machine and give .js extension (i.e. example.js)
//  2. Change "***" to appropriate values
//  3. Install async and request packages
//     npm install async
//     npm install request
//  4. execute
//     node example.js 
//

var     async = require("async"),       // async module
        request = require("request"),       // request module
        fs = require("fs");         // fs module

var     email = "***",              // your account email
        password = "***",           // your account password
        integratorKey = "***",      // your Integrator Key (found on the Preferences -> API page)
        recipientName = "***",      // recipient (signer) name
        recipientEmail = "***",     // recipient email address  
        documentName = "***",       // copy document with this name into same directory!
        envelopeId = "",            // will retrieve this from second api call
        baseUrl = "";               // retrieved through the Login call

async.waterfall(
  [
    /////////////////////////////////////////////////////////////////////////////////////
    // Step 1: Login (used to retrieve your accountId and baseUrl)
    /////////////////////////////////////////////////////////////////////////////////////
    function(next) {
        var url = "https://demo.docusign.net/restapi/v2/login_information";
        var body = "";  // no request body for login api call

        // set request url, method, body, and headers
        var options = initializeRequest(url, "GET", body, email, password);

        // send the request...
        request(options, function(err, res, body) {
            if(!parseResponseBody(err, res, body)) {
                return;
            }
            baseUrl = JSON.parse(body).loginAccounts[0].baseUrl;
            next(null); // call next function
        });
    },

    /////////////////////////////////////////////////////////////////////////////////////
    // Step 2: Create Envelope with Embedded Recipient (need to set |clientUserId| property)
    /////////////////////////////////////////////////////////////////////////////////////
    function(next) {    
        var url = baseUrl + "/envelopes";
        // following request body will place 1 signature tab 100 pixels to the right and
        // 100 pixels down from the top left of the document that you send in the request
        var body = {
            "recipients": {
                "signers": [{
                    "email": recipientEmail,
                    "name": recipientName,
                    "recipientId": 1,
                    "clientUserId": "1001",     //Required for embedded recipient
                    "tabs": {
                        "signHereTabs": [{
                            "xPosition": "100",
                            "yPosition": "100",
                            "documentId": "1",
                            "pageNumber": "1"                                                                                   
                        }]
                    }
                }]
            },
            "emailSubject": 'DocuSign API - Signature Request on Document Call',
            "documents": [{
                "name": documentName,
                "documentId": 1,
            }],
            "status": "sent"
        };

        // set request url, method, body, and headers
        var options = initializeRequest(url, "POST", body, email, password);

        // change default Content-Type header from "application/json" to "multipart/form-data"
        options.headers["Content-Type"] = "multipart/form-data";

        // configure a multipart http request with JSON body and document bytes
        options.multipart = [{
                    "Content-Type": "application/json",
                    "Content-Disposition": "form-data",
                    "body": JSON.stringify(body),
                }, {
                    "Content-Type": "application/pdf",
                    'Content-Disposition': 'file; filename="' + documentName + '"; documentId=1',
                    "body": fs.readFileSync(documentName),
                }
        ];

        // send the request...
        request(options, function(err, res, body) {
            if(!parseResponseBody(err, res, body)) {
                return;
            }
            envelopeId = JSON.parse(body).envelopeId;
            next(null); // call next function
        });
    }, // end function    

    /////////////////////////////////////////////////////////////////////////////////////
    // Step 3: Generate the Embedded Signing URL
    /////////////////////////////////////////////////////////////////////////////////////

    function(next) {
        var url = baseUrl + "/envelopes/" + envelopeId + "/views/recipient";
        var method = "POST";
        var body = JSON.stringify({
                "returnUrl": "http://www.docusign.com/devcenter",
                "authenticationMethod": "email",                    
                "email": email,                 
                "userName": recipientName,      
                "clientUserId": "1001", // must match clientUserId in step 2!
            });  

        // set request url, method, body, and headers
        var options = initializeRequest(url, "POST", body, email, password);

        // send the request...
        request(options, function(err, res, body) {
            if(!parseResponseBody(err, res, body))
                return;
            else
                console.log("\nNavigate to the above URL to start the Embedded Signing workflow...");
        });
    }
]);

//***********************************************************************************************
// --- HELPER FUNCTIONS ---
//***********************************************************************************************
function initializeRequest(url, method, body, email, password) {    
    var options = {
        "method": method,
        "uri": url,
        "body": body,
        "headers": {}
    };
    addRequestHeaders(options, email, password);
    return options;
}

///////////////////////////////////////////////////////////////////////////////////////////////
function addRequestHeaders(options, email, password) {  
    // JSON formatted authentication header (XML format allowed as well)
    dsAuthHeader = JSON.stringify({
        "Username": email,
        "Password": password, 
        "IntegratorKey": integratorKey  // global
    });
    // DocuSign authorization header
    options.headers["X-DocuSign-Authentication"] = dsAuthHeader;
}

///////////////////////////////////////////////////////////////////////////////////////////////
function parseResponseBody(err, res, body) {
    console.log("\r\nAPI Call Result: \r\n", JSON.parse(body));
    if( res.statusCode != 200 && res.statusCode != 201) { // success statuses
        console.log("Error calling webservice, status is: ", res.statusCode);
        console.log("\r\n", err);
        return false;
    }
    return true;
}