简体   繁体   中英

Docusign iFrame

I'm trying to get the Docusign work with a PDF document that is generated by based on few parameters. The workflow is., user will generate the PDF and then when clicking a button it will bring a docusign iFrame with this PDF. User signs and docusign will send the document to us. There are lot of excellent API's in docusign. hoping someone will have an experience which is meeting this narrowed scope.

-Thanks

From the DocuSign Developer Center go to Quick Start -> API Tools and from there you will see some great tools like the API Explorer and the API Walkthroughs. The API Walkthroughs have 9 of the most common DocuSign API use cases covered with sample code in 6 different languages for all 9 scenarios, including Javascript (Node.js).

The "narrow" use case you require is not narrow at all, it's one of these 9 common use cases that we have sample code for. See the one called Request Signature on Document . Follow the instructions for copying that code, filling in the variables at the top, and supplying a document in the same folder and it will work. Then from there you can make whatever modifications you need.

Here is the actual javascript for that walkthrough:

// Request Signature on a Document (Node.js)

// 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
//     npm install fs
//  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
        documentName = "***",           // copy document with this name into same directory!
        baseUrl = "";               // we will retrieve this 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: Request Signature on a PDF Document
    /////////////////////////////////////////////////////////////////////////////////////
    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": email,
                    "name": recipientName,
                    "recipientId": 1,
                    "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) {
            parseResponseBody(err, res, body);
        });
    } // end function    
]);

//***********************************************************************************************
// --- 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM