4
4
* @author DocuSign
5
5
*/
6
6
7
- const path = require ( "path" ) ;
8
- const { sendEnvelopeForEmbeddedSigning } = require ( "../../../embeddedSigning" ) ;
9
- const validator = require ( "validator" ) ;
10
- const { getExampleByNumber } = require ( "../../manifestService" ) ;
11
- const dsConfig = require ( "../../../config/index.js" ) . config ;
12
-
13
- const eg001EmbeddedSigning = exports ;
14
- const exampleNumber = 1 ;
15
- const eg = `eg00${ exampleNumber } ` ; // This example reference.
16
- const mustAuthenticate = "/ds/mustAuthenticate" ;
17
- const minimumBufferMin = 3 ;
18
- const signerClientId = 1000 ; // The id of the signer within this application.
19
- const demoDocsPath = path . resolve ( __dirname , "../../../demo_documents" ) ;
20
- const pdf1File = "World_Wide_Corp_lorem.pdf" ;
21
- const dsReturnUrl = dsConfig . appUrl + "/ds-return" ;
22
- const dsPingUrl = dsConfig . appUrl + "/" ; // Url that will be pinged by the DocuSign signing via Ajax
23
-
24
- /**
25
- * Create the envelope, the embedded signing, and then redirect to the DocuSign signing
26
- * @param {object } req Request obj
27
- * @param {object } res Response obj
28
- */
29
- eg001EmbeddedSigning . createController = async ( req , res ) => {
30
- // Step 1. Check the token
31
- // At this point we should have a good token. But we
32
- // double-check here to enable a better UX to the user.
33
- const isTokenOK = req . dsAuth . checkToken ( minimumBufferMin ) ;
34
- if ( ! isTokenOK ) {
35
- req . flash ( "info" , "Sorry, you need to re-authenticate." ) ;
36
- // Save the current operation so it will be resumed after authentication
37
- req . dsAuth . setEg ( req , eg ) ;
38
- return res . redirect ( mustAuthenticate ) ;
39
- }
40
-
41
- // Step 2. Call the worker method
42
- const { body } = req ;
43
- const envelopeArgs = {
44
- signerEmail : validator . escape ( body . signerEmail ) ,
45
- signerName : validator . escape ( body . signerName ) ,
46
- signerClientId : signerClientId ,
47
- dsReturnUrl : dsReturnUrl ,
48
- dsPingUrl : dsPingUrl ,
49
- docFile : path . resolve ( demoDocsPath , pdf1File ) ,
50
- } ;
51
- const args = {
52
- accessToken : req . user . accessToken ,
53
- basePath : req . session . basePath ,
54
- accountId : req . session . accountId ,
55
- envelopeArgs : envelopeArgs ,
56
- } ;
57
- let results = null ;
58
-
59
- try {
60
- results = await sendEnvelopeForEmbeddedSigning ( args ) ;
61
- } catch ( error ) {
62
- const errorBody = error && error . response && error . response . body ;
63
- // we can pull the DocuSign error code and message from the response body
64
- const errorCode = errorBody && errorBody . errorCode ;
65
- const errorMessage = errorBody && errorBody . message ;
66
- // In production, may want to provide customized error messages and
67
- // remediation advice to the user.
68
- res . render ( "pages/error" , { err : error , errorCode, errorMessage } ) ;
69
- }
70
- if ( results ) {
71
- // Redirect the user to the embedded signing
72
- // Don't use an iFrame!
73
- // State can be stored/recovered using the framework's session or a
74
- // query parameter on the returnUrl (see the makeRecipientViewRequest method)
75
- res . redirect ( results . redirectUrl ) ;
76
- }
77
- } ;
78
-
79
- /**
80
- * Form page for this application
81
- */
82
- eg001EmbeddedSigning . getController = ( req , res ) => {
83
- console . log ( req . dsAuth ) ;
84
- // Check that the authentication token is ok with a long buffer time.
85
- // If needed, now is the best time to ask the user to authenticate
86
- // since they have not yet entered any information into the form.
87
- const isTokenOK = req . dsAuth . checkToken ( ) ;
88
- if ( ! isTokenOK ) {
89
- // Save the current operation so it will be resumed after authentication
90
- req . dsAuth . setEg ( req , eg ) ;
91
- return res . redirect ( mustAuthenticate ) ;
92
- }
93
-
94
- const example = getExampleByNumber ( res . locals . manifest , exampleNumber ) ;
95
- if ( res . locals . quickACG ) {
96
- res . render ( "pages/examples/quickEmbeddedSigning" , {
97
- eg : eg ,
98
- csrfToken : req . csrfToken ( ) ,
99
- title : "Use embedded signing" ,
100
- sourceFile : path . basename ( __filename ) ,
101
- sourceUrl :
102
- "https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js" ,
103
- documentation : dsConfig . documentation + eg ,
104
- showDoc : dsConfig . documentation ,
105
- } ) ;
106
- } else {
107
- res . render ( "pages/examples/eg001EmbeddedSigning" , {
108
- eg : eg ,
109
- csrfToken : req . csrfToken ( ) ,
110
- example : example ,
111
- sourceFile : path . basename ( __filename ) ,
112
- sourceUrl :
113
- "https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js" ,
114
- documentation : dsConfig . documentation + eg ,
115
- showDoc : dsConfig . documentation ,
116
- } ) ;
117
- }
118
- } ;
7
+ const path = require ( "path" ) ;
8
+ const { sendEnvelopeForEmbeddedSigning } = require ( "../../../embeddedSigning" ) ;
9
+ const validator = require ( "validator" ) ;
10
+ const { getExampleByNumber } = require ( "../../manifestService" ) ;
11
+ const dsConfig = require ( "../../../config/index.js" ) . config ;
12
+
13
+ const eg001EmbeddedSigning = exports ;
14
+ const exampleNumber = 1 ;
15
+ const eg = `eg00${ exampleNumber } ` ; // This example reference.
16
+ const mustAuthenticate = "/ds/mustAuthenticate" ;
17
+ const minimumBufferMin = 3 ;
18
+ const signerClientId = 1000 ; // The id of the signer within this application.
19
+ const demoDocsPath = path . resolve ( __dirname , "../../../demo_documents" ) ;
20
+ const pdf1File = "World_Wide_Corp_lorem.pdf" ;
21
+ const dsReturnUrl = dsConfig . appUrl + "/ds-return" ;
22
+ const dsPingUrl = dsConfig . appUrl + "/" ; // Url that will be pinged by the DocuSign signing via Ajax
23
+
24
+ /**
25
+ * Create the envelope, the embedded signing, and then redirect to the DocuSign signing
26
+ * @param {object } req Request obj
27
+ * @param {object } res Response obj
28
+ */
29
+ eg001EmbeddedSigning . createController = async ( req , res ) => {
30
+ // Step 1. Check the token
31
+ // At this point we should have a good token. But we
32
+ // double-check here to enable a better UX to the user.
33
+ const isTokenOK = req . dsAuth . checkToken ( minimumBufferMin ) ;
34
+ if ( ! isTokenOK ) {
35
+ req . flash ( "info" , "Sorry, you need to re-authenticate." ) ;
36
+ // Save the current operation so it will be resumed after authentication
37
+ req . dsAuth . setEg ( req , eg ) ;
38
+ return res . redirect ( mustAuthenticate ) ;
39
+ }
40
+
41
+ // Step 2. Call the worker method
42
+ const { body } = req ;
43
+ const envelopeArgs = {
44
+ signerEmail : validator . escape ( body . signerEmail ) ,
45
+ signerName : validator . escape ( body . signerName ) ,
46
+ signerClientId : signerClientId ,
47
+ dsReturnUrl : dsReturnUrl ,
48
+ dsPingUrl : dsPingUrl ,
49
+ docFile : path . resolve ( demoDocsPath , pdf1File ) ,
50
+ } ;
51
+ const args = {
52
+ accessToken : req . user . accessToken ,
53
+ basePath : req . session . basePath ,
54
+ accountId : req . session . accountId ,
55
+ envelopeArgs : envelopeArgs ,
56
+ } ;
57
+ let results = null ;
58
+
59
+ try {
60
+ results = await sendEnvelopeForEmbeddedSigning ( args ) ;
61
+ } catch ( error ) {
62
+ const errorBody = error && error . response && error . response . body ;
63
+ // we can pull the DocuSign error code and message from the response body
64
+ const errorCode = errorBody && errorBody . errorCode ;
65
+ const errorMessage = errorBody && errorBody . message ;
66
+ // In production, may want to provide customized error messages and
67
+ // remediation advice to the user.
68
+ res . render ( "pages/error" , { err : error , errorCode, errorMessage } ) ;
69
+ }
70
+ if ( results ) {
71
+ // Redirect the user to the embedded signing
72
+ // Don't use an iFrame!
73
+ // State can be stored/recovered using the framework's session or a
74
+ // query parameter on the returnUrl (see the makeRecipientViewRequest method)
75
+ res . redirect ( results . redirectUrl ) ;
76
+ }
77
+ } ;
78
+
79
+ /**
80
+ * Form page for this application
81
+ */
82
+ eg001EmbeddedSigning . getController = ( req , res ) => {
83
+ console . log ( req . dsAuth ) ;
84
+ // Check that the authentication token is ok with a long buffer time.
85
+ // If needed, now is the best time to ask the user to authenticate
86
+ // since they have not yet entered any information into the form.
87
+ const isTokenOK = req . dsAuth . checkToken ( ) ;
88
+ if ( ! isTokenOK ) {
89
+ // Save the current operation so it will be resumed after authentication
90
+ req . dsAuth . setEg ( req , eg ) ;
91
+ return res . redirect ( mustAuthenticate ) ;
92
+ }
93
+
94
+ const example = getExampleByNumber ( res . locals . manifest , exampleNumber ) ;
95
+ if ( res . locals . quickACG ) {
96
+ res . render ( "pages/examples/quickEmbeddedSigning" , {
97
+ eg : eg ,
98
+ csrfToken : req . csrfToken ( ) ,
99
+ title : "Use embedded signing" ,
100
+ example : example ,
101
+ sourceFile : path . basename ( __filename ) ,
102
+ sourceUrl :
103
+ "https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js" ,
104
+ documentation : dsConfig . documentation + eg ,
105
+ showDoc : dsConfig . documentation ,
106
+ } ) ;
107
+ } else {
108
+ res . render ( "pages/examples/eg001EmbeddedSigning" , {
109
+ eg : eg ,
110
+ csrfToken : req . csrfToken ( ) ,
111
+ example : example ,
112
+ sourceFile : path . basename ( __filename ) ,
113
+ sourceUrl :
114
+ "https://github.com/docusign/code-examples-node/blob/master/embeddedSigning.js" ,
115
+ documentation : dsConfig . documentation + eg ,
116
+ showDoc : dsConfig . documentation ,
117
+ } ) ;
118
+ }
119
+ } ;
0 commit comments