|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 038: Signable HTML document |
| 4 | + * @author DocuSign |
| 5 | + */ |
| 6 | + |
| 7 | +const fs = require("fs-extra"); |
| 8 | +const docusign = require("docusign-esign"); |
| 9 | + |
| 10 | +/** |
| 11 | + * This function does the work of creating the envelope in |
| 12 | + * draft mode and returning a URL for the sender's view |
| 13 | + * @param {object} args object |
| 14 | + */ |
| 15 | + const sendEnvelope = async (args) => { |
| 16 | + // Data for this method |
| 17 | + // args.basePath |
| 18 | + // args.accessToken |
| 19 | + // args.accountId |
| 20 | + |
| 21 | + let dsApiClient = new docusign.ApiClient(); |
| 22 | + dsApiClient.setBasePath(args.basePath); |
| 23 | + dsApiClient.addDefaultHeader("Authorization", "Bearer " + args.accessToken); |
| 24 | + let envelopesApi = new docusign.EnvelopesApi(dsApiClient); |
| 25 | + |
| 26 | + // Step 1. Make the envelope body |
| 27 | + let envelope = makeEnvelope(args.envelopeArgs); |
| 28 | + |
| 29 | + // Step 2. Send the envelope |
| 30 | + let results = await envelopesApi.createEnvelope(args.accountId, { |
| 31 | + envelopeDefinition: envelope, |
| 32 | + }); |
| 33 | + let envelopeId = results.envelopeId; |
| 34 | + |
| 35 | + // Step 3. Create the recipient view |
| 36 | + let viewRequest = makeRecipientViewRequest(args.envelopeArgs); |
| 37 | + |
| 38 | + // Call the CreateRecipientView API |
| 39 | + // Exceptions will be caught by the calling function |
| 40 | + results = await envelopesApi.createRecipientView(args.accountId, envelopeId, { |
| 41 | + recipientViewRequest: viewRequest, |
| 42 | + }); |
| 43 | + |
| 44 | + return { envelopeId: envelopeId, redirectUrl: results.url }; |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Creates envelope |
| 49 | + * @function |
| 50 | + * @param {Object} args parameters for the envelope |
| 51 | + * @returns {Envelope} An envelope definition |
| 52 | + * @private |
| 53 | + */ |
| 54 | +function makeEnvelope(args) { |
| 55 | + // Data for this method |
| 56 | + // args.signerEmail |
| 57 | + // args.signerName |
| 58 | + // args.ccEmail |
| 59 | + // args.ccName |
| 60 | + // args.status |
| 61 | + |
| 62 | + // document (html) has tag /sn1/ |
| 63 | + // |
| 64 | + // The envelope has two recipients. |
| 65 | + // recipient 1 - signer |
| 66 | + // recipient 2 - cc |
| 67 | + // The envelope will be sent first to the signer. |
| 68 | + // After it is signed, a copy is sent to the cc person. |
| 69 | + |
| 70 | + // create a signer recipient to sign the document, identified by name and email |
| 71 | + // We're setting the parameters via the object constructor |
| 72 | + let signer = docusign.Signer.constructFromObject({ |
| 73 | + email: args.signerEmail, |
| 74 | + name: args.signerName, |
| 75 | + clientUserId: args.signerClientId, |
| 76 | + recipientId: "1", |
| 77 | + routingOrder: "1", |
| 78 | + roleName: "Signer", |
| 79 | + }); |
| 80 | + // routingOrder (lower means earlier) determines the order of deliveries |
| 81 | + // to the recipients. Parallel routing order is supported by using the |
| 82 | + // same integer as the order for two or more recipients. |
| 83 | + |
| 84 | + // create a cc recipient to receive a copy of the documents, identified by name and email |
| 85 | + // We're setting the parameters via setters |
| 86 | + let cc = new docusign.CarbonCopy.constructFromObject({ |
| 87 | + email: args.ccEmail, |
| 88 | + name: args.ccName, |
| 89 | + routingOrder: "2", |
| 90 | + recipientId: "2", |
| 91 | + }); |
| 92 | + |
| 93 | + // Add the recipients to the envelope object |
| 94 | + let recipients = docusign.Recipients.constructFromObject({ |
| 95 | + signers: [signer], |
| 96 | + carbonCopies: [cc], |
| 97 | + }); |
| 98 | + |
| 99 | + // add the document |
| 100 | + |
| 101 | + let htmlDefinition = new docusign.DocumentHtmlDefinition(); |
| 102 | + htmlDefinition.source = getHTMLDocument(args); |
| 103 | + |
| 104 | + let document = new docusign.Document(); |
| 105 | + document.name = "doc1.html"; // can be different from actual file name |
| 106 | + document.documentId = "1"; // a label used to reference the doc |
| 107 | + document.htmlDefinition = htmlDefinition; |
| 108 | + |
| 109 | + // create the envelope definition |
| 110 | + let env = new docusign.EnvelopeDefinition(); |
| 111 | + env.emailSubject = "Example Signing Document"; |
| 112 | + env.documents = [document]; |
| 113 | + env.recipients = recipients; |
| 114 | + env.status = args.status; |
| 115 | + |
| 116 | + return env; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Gets the HTML document |
| 121 | + * @function |
| 122 | + * @private |
| 123 | + * @param {Object} args parameters for the envelope |
| 124 | + * @returns {string} A document in HTML format |
| 125 | + */ |
| 126 | + |
| 127 | +function getHTMLDocument(args) { |
| 128 | + // Data for this method |
| 129 | + // args.signerEmail |
| 130 | + // args.signerName |
| 131 | + // args.ccEmail |
| 132 | + // args.ccName |
| 133 | + |
| 134 | + let docHTMLContent = fs.readFileSync(args.docFile, { encoding: "utf8" }); |
| 135 | + |
| 136 | + // Substitute values into the HTML |
| 137 | + // Substitute for: {signerName}, {signerEmail}, {ccName}, {ccEmail} |
| 138 | + return docHTMLContent |
| 139 | + .replace("{signerName}", args.signerName) |
| 140 | + .replace("{signerEmail}", args.signerEmail) |
| 141 | + .replace("{ccName}", args.ccName) |
| 142 | + .replace("{ccEmail}", args.ccEmail) |
| 143 | + .replace("/sn1/", "<ds-signature data-ds-role=\"Signer\"/>") |
| 144 | + .replace("/l1q/", "<input data-ds-type=\"number\"/>") |
| 145 | + .replace("/l2q/", "<input data-ds-type=\"number\"/>"); |
| 146 | +} |
| 147 | + |
| 148 | +function makeRecipientViewRequest(args) { |
| 149 | + // Data for this method |
| 150 | + // args.dsReturnUrl |
| 151 | + // args.signerEmail |
| 152 | + // args.signerName |
| 153 | + // args.signerClientId |
| 154 | + // args.dsPingUrl |
| 155 | + |
| 156 | + let viewRequest = new docusign.RecipientViewRequest(); |
| 157 | + |
| 158 | + // Set the url where you want the recipient to go once they are done signing |
| 159 | + // should typically be a callback route somewhere in your app. |
| 160 | + // The query parameter is included as an example of how |
| 161 | + // to save/recover state information during the redirect to |
| 162 | + // the DocuSign signing. It's usually better to use |
| 163 | + // the session mechanism of your web framework. Query parameters |
| 164 | + // can be changed/spoofed very easily. |
| 165 | + viewRequest.returnUrl = args.dsReturnUrl + "?state=123"; |
| 166 | + |
| 167 | + // How has your app authenticated the user? In addition to your app's |
| 168 | + // authentication, you can include authenticate steps from DocuSign. |
| 169 | + // Eg, SMS authentication |
| 170 | + viewRequest.authenticationMethod = "none"; |
| 171 | + |
| 172 | + // Recipient information must match embedded recipient info |
| 173 | + // we used to create the envelope. |
| 174 | + viewRequest.email = args.signerEmail; |
| 175 | + viewRequest.userName = args.signerName; |
| 176 | + viewRequest.clientUserId = args.signerClientId; |
| 177 | + |
| 178 | + // DocuSign recommends that you redirect to DocuSign for the |
| 179 | + // embedded signing. There are multiple ways to save state. |
| 180 | + // To maintain your application's session, use the pingUrl |
| 181 | + // parameter. It causes the DocuSign signing web page |
| 182 | + // (not the DocuSign server) to send pings via AJAX to your |
| 183 | + // app, |
| 184 | + viewRequest.pingFrequency = 600; // seconds |
| 185 | + // NOTE: The pings will only be sent if the pingUrl is an https address |
| 186 | + viewRequest.pingUrl = args.dsPingUrl; // optional setting |
| 187 | + |
| 188 | + return viewRequest; |
| 189 | +} |
| 190 | + |
| 191 | +module.exports = { sendEnvelope }; |
0 commit comments