|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Example 004: Send third party notary |
| 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 |
| 12 | + */ |
| 13 | + |
| 14 | +const sendEnvelope = async (args) => { |
| 15 | + // Data for this method |
| 16 | + // args.basePath |
| 17 | + // args.accessToken |
| 18 | + // args.accountId |
| 19 | + |
| 20 | + //ds-snippet-start:Notary4Step2 |
| 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 | + //ds-snippet-end:Notary4Step2 |
| 26 | + let results = null; |
| 27 | + |
| 28 | + // Make the envelope request body |
| 29 | + //ds-snippet-start:Notary4Step3 |
| 30 | + let envelope = makeEnvelope(args.envelopeArgs); |
| 31 | + //ds-snippet-end:Notary4Step3 |
| 32 | + |
| 33 | + // Call the Envelopes::create API method |
| 34 | + // Exceptions will be caught by the calling function |
| 35 | + //ds-snippet-start:Notary4Step4 |
| 36 | + results = await envelopesApi.createEnvelope(args.accountId, { |
| 37 | + envelopeDefinition: envelope, |
| 38 | + }); |
| 39 | + let envelopeId = results.envelopeId; |
| 40 | + //ds-snippet-end:Notary4Step4 |
| 41 | + |
| 42 | + console.log(`Envelope was created. EnvelopeId ${envelopeId}`); |
| 43 | + return { envelopeId: envelopeId }; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Creates envelope |
| 48 | + * @function |
| 49 | + * @param {Object} args parameters for the envelope |
| 50 | + * @returns {Envelope} An envelope definition |
| 51 | + * @private |
| 52 | + */ |
| 53 | + |
| 54 | +//ds-snippet-start:Notary4Step3 |
| 55 | +function makeEnvelope(args) { |
| 56 | + // Data for this method |
| 57 | + // args.signerEmail |
| 58 | + // args.signerName |
| 59 | + // args.status |
| 60 | + |
| 61 | + // document 1 (html) has tag **signature_1** |
| 62 | + // |
| 63 | + // The envelope has two recipients. |
| 64 | + // recipient 1 - signer |
| 65 | + // The envelope will be sent first to the signer. |
| 66 | + // After it is signed, a copy is sent to the cc person. |
| 67 | + |
| 68 | + // create the envelope definition |
| 69 | + let env = new docusign.EnvelopeDefinition(); |
| 70 | + env.emailSubject = 'Please sign this document set'; |
| 71 | + |
| 72 | + // add the documents |
| 73 | + let doc1 = new docusign.Document(); |
| 74 | + let doc1b64 = Buffer.from(document1(args)).toString('base64'); |
| 75 | + doc1.documentBase64 = doc1b64; |
| 76 | + doc1.name = 'Order acknowledgement'; // can be different from actual file name |
| 77 | + doc1.fileExtension = 'html'; // Source data format. Signed docs are always pdf. |
| 78 | + doc1.documentId = '1'; // a label used to reference the doc |
| 79 | + |
| 80 | + // The order in the docs array determines the order in the envelope |
| 81 | + env.documents = [doc1]; |
| 82 | + |
| 83 | + // create a signer recipient to sign the document, identified by name and email |
| 84 | + // We're setting the parameters via the object constructor |
| 85 | + let signer1 = docusign.Signer.constructFromObject({ |
| 86 | + email: args.signerEmail, |
| 87 | + name: args.signerName, |
| 88 | + recipientId: '2', |
| 89 | + routingOrder: '1', |
| 90 | + notaryId: '1', |
| 91 | + clientUserId: '1000', |
| 92 | + }); |
| 93 | + // routingOrder (lower means earlier) determines the order of deliveries |
| 94 | + // to the recipients. Parallel routing order is supported by using the |
| 95 | + // same integer as the order for two or more recipients. |
| 96 | + |
| 97 | + // Create signHere fields (also known as tabs) on the documents, |
| 98 | + // We're using anchor (autoPlace) positioning |
| 99 | + // |
| 100 | + // The DocuSign platform searches throughout your envelope's |
| 101 | + // documents for matching anchor strings. So the |
| 102 | + // use the same anchor string for their "signer 1" tabs. |
| 103 | + let signHere1 = docusign.SignHere.constructFromObject({ |
| 104 | + xPosition: '200', |
| 105 | + yPosition: '235', |
| 106 | + pageNumber: '1', |
| 107 | + documentId: '1' |
| 108 | + }); |
| 109 | + let signHere2 = docusign.SignHere.constructFromObject({ |
| 110 | + stampType: 'stamp', |
| 111 | + documentId: '1', |
| 112 | + xPosition: '200', |
| 113 | + yPosition: '150', |
| 114 | + pageNumber: '1', |
| 115 | + |
| 116 | + }); |
| 117 | + // Tabs are set per recipient / signer |
| 118 | + let signer1Tabs = docusign.Tabs.constructFromObject({ |
| 119 | + signHereTabs: [signHere1, signHere2], |
| 120 | + }); |
| 121 | + signer1.tabs = signer1Tabs; |
| 122 | + |
| 123 | + const notarySealTab = { |
| 124 | + xPosition: '300', |
| 125 | + yPosition: '235', |
| 126 | + documentId: '1', |
| 127 | + pageNumber: '1', |
| 128 | +}; |
| 129 | + |
| 130 | +const notarySignHereTab = { |
| 131 | + xPosition: '300', |
| 132 | + yPosition: '150', |
| 133 | + documentId: '1', |
| 134 | + pageNumber: '1', |
| 135 | +}; |
| 136 | + |
| 137 | +const notaryTabs = { |
| 138 | + signHereTabs: [notarySignHereTab], |
| 139 | + notarySealTabs: [notarySealTab], |
| 140 | +}; |
| 141 | + |
| 142 | +const notaryRecipient = [ |
| 143 | + { |
| 144 | + name: 'Notary', |
| 145 | + recipientId: '1', |
| 146 | + routingOrder: '1', |
| 147 | + tabs: notaryTabs, |
| 148 | + notaryType: 'remote', |
| 149 | + notarySourceType: 'thirdparty', |
| 150 | + notaryThirdPartyPartner: 'onenotary', |
| 151 | + recipientSignatureProviders: [ |
| 152 | + { |
| 153 | + sealDocumentsWithTabsOnly: 'false', |
| 154 | + signatureProviderName: 'ds_authority_idv', |
| 155 | + signatureProviderOptions: {} |
| 156 | + } |
| 157 | + ], |
| 158 | + }, |
| 159 | +]; |
| 160 | + |
| 161 | + // Add the recipients to the envelope object |
| 162 | + let recipients = docusign.Recipients.constructFromObject({ |
| 163 | + signers: [signer1], |
| 164 | + notaries: [...notaryRecipient], |
| 165 | + }); |
| 166 | + env.recipients = recipients; |
| 167 | + |
| 168 | + // Request that the envelope be sent by setting |status| to "sent". |
| 169 | + // To request that the envelope be created as a draft, set to "created" |
| 170 | + env.status = args.status; |
| 171 | + |
| 172 | + return env; |
| 173 | +} |
| 174 | +//ds-snippet-end:Notary4Step3 |
| 175 | + |
| 176 | +/** |
| 177 | + * Creates document 1 |
| 178 | + * @function |
| 179 | + * @private |
| 180 | + * @param {Object} args parameters for the envelope |
| 181 | + * @returns {string} A document in HTML format |
| 182 | + */ |
| 183 | + |
| 184 | +function document1(args) { |
| 185 | + // Data for this method |
| 186 | + // args.signerEmail |
| 187 | + // args.signerName |
| 188 | + // args.ccEmail |
| 189 | + // args.ccName |
| 190 | + |
| 191 | + return ` |
| 192 | + <!DOCTYPE html> |
| 193 | + <html> |
| 194 | + <head> |
| 195 | + <meta charset="UTF-8"> |
| 196 | + </head> |
| 197 | + <body style="font-family:sans-serif;margin-left:2em;"> |
| 198 | + <h1 style="font-family: 'Trebuchet MS', Helvetica, sans-serif; |
| 199 | + color: darkblue;margin-bottom: 0;">World Wide Corp</h1> |
| 200 | + <h2 style="font-family: 'Trebuchet MS', Helvetica, sans-serif; |
| 201 | + margin-top: 0px;margin-bottom: 3.5em;font-size: 1em; |
| 202 | + color: darkblue;">Order Processing Division</h2> |
| 203 | + <h4>Ordered by ${args.signerName}</h4> |
| 204 | + <p style="margin-top:0em; margin-bottom:0em;">Email: ${args.signerEmail}</p> |
| 205 | + <p style="margin-top:0em; margin-bottom:0em;">Copy to: ${args.ccName}, ${args.ccEmail}</p> |
| 206 | + <p style="margin-top:3em;"> |
| 207 | + Candy bonbon pastry jujubes lollipop wafer biscuit biscuit. Topping brownie sesame snaps sweet roll pie. Croissant danish biscuit soufflé caramels jujubes jelly. Dragée danish caramels lemon drops dragée. Gummi bears cupcake biscuit tiramisu sugar plum pastry. Dragée gummies applicake pudding liquorice. Donut jujubes oat cake jelly-o. Dessert bear claw chocolate cake gummies lollipop sugar plum ice cream gummies cheesecake. |
| 208 | + </p> |
| 209 | + <!-- Note the anchor tag for the signature field is in white. --> |
| 210 | + <h3 style="margin-top:3em;">Agreed: <span style="color:white;">**signature_1**/</span></h3> |
| 211 | + </body> |
| 212 | + </html> |
| 213 | + `; |
| 214 | +} |
| 215 | + |
| 216 | +module.exports = { sendEnvelope, makeEnvelope, document1 }; |
0 commit comments