1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Threading . Tasks ;
5
3
using DocuSign . eSign . Api ;
6
4
using DocuSign . eSign . Model ;
7
5
using eg_03_csharp_auth_code_grant_core . Models ;
8
6
using Microsoft . AspNetCore . Mvc ;
9
- using System . IO ;
10
7
using System . Text ;
8
+ using DocuSign . eSign . Client ;
11
9
12
10
namespace eg_03_csharp_auth_code_grant_core . Controllers
13
11
{
@@ -22,21 +20,38 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi
22
20
23
21
public override string EgName => "eg002" ;
24
22
25
- [ HttpPost ]
26
- public IActionResult Create ( string signerEmail , string signerName , string ccEmail , string ccName )
27
- {
23
+ public EnvelopeSummary DoWork ( string signerEmail , string signerName , string ccEmail , string ccName )
24
+ {
25
+ // Data for this method
26
+ // signerEmail
27
+ // signerName
28
+ // ccEmail
29
+ // ccName
30
+ var accessToken = RequestItemsService . User . AccessToken ;
31
+ var basePath = RequestItemsService . Session . BasePath + "/restapi" ;
32
+ var accountId = RequestItemsService . Session . AccountId ;
33
+
28
34
EnvelopeDefinition env = MakeEnvelope ( signerEmail , signerName , ccEmail , ccName ) ;
29
- EnvelopesApi envelopesApi = new EnvelopesApi ( RequestItemsService . DefaultConfiguration ) ;
30
- EnvelopeSummary results = envelopesApi . CreateEnvelope ( RequestItemsService . Session . AccountId , env ) ;
35
+ var config = new Configuration ( new ApiClient ( basePath ) ) ;
36
+ config . AddDefaultHeader ( "Authorization" , "Bearer " + accessToken ) ;
37
+ EnvelopesApi envelopesApi = new EnvelopesApi ( config ) ;
38
+ EnvelopeSummary results = envelopesApi . CreateEnvelope ( accountId , env ) ;
31
39
RequestItemsService . EnvelopeId = results . EnvelopeId ;
32
- ViewBag . h1 = "Envelope sent" ;
33
- ViewBag . message = "The envelope has been created and sent!<br />Envelope ID " + results . EnvelopeId + "." ;
34
- //return results;
35
- return View ( "example_done" ) ;
40
+ return results ;
36
41
}
37
42
38
43
private EnvelopeDefinition MakeEnvelope ( string signerEmail , string signerName , string ccEmail , string ccName )
39
44
{
45
+ // Data for this method
46
+ // signerEmail
47
+ // signerName
48
+ // ccEmail
49
+ // ccName
50
+ // Config.docDocx
51
+ // Config.docPdf
52
+ // RequestItemsService.Status -- the envelope status ('created' or 'sent')
53
+
54
+
40
55
// document 1 (html) has tag **signature_1**
41
56
// document 2 (docx) has tag /sn1/
42
57
// document 3 (pdf) has tag /sn1/
@@ -53,6 +68,8 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
53
68
// create the envelope definition
54
69
EnvelopeDefinition env = new EnvelopeDefinition ( ) ;
55
70
env . EmailSubject = "Please sign this document set" ;
71
+
72
+ // Create document objects, one per document
56
73
Document doc1 = new Document ( ) ;
57
74
string b64 = Convert . ToBase64String ( document1 ( signerEmail , signerName , ccEmail , ccName ) ) ;
58
75
doc1 . DocumentBase64 = b64 ;
@@ -65,16 +82,13 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
65
82
FileExtension = "docx" ,
66
83
DocumentId = "2"
67
84
} ;
68
-
69
85
Document doc3 = new Document
70
86
{
71
87
DocumentBase64 = doc3PdfBytes ,
72
88
Name = "Lorem Ipsum" , // can be different from actual file name
73
89
FileExtension = "pdf" ,
74
90
DocumentId = "3"
75
91
} ;
76
-
77
-
78
92
// The order in the docs array determines the order in the envelope
79
93
env . Documents = new List < Document > { doc1 , doc2 , doc3 } ;
80
94
@@ -124,12 +138,10 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
124
138
AnchorXOffset = "20"
125
139
} ;
126
140
127
-
128
141
// Tabs are set per recipient / signer
129
142
Tabs signer1Tabs = new Tabs {
130
143
SignHereTabs = new List < SignHere > { signHere1 , signHere2 }
131
144
} ;
132
-
133
145
signer1 . Tabs = signer1Tabs ;
134
146
135
147
// Add the recipients to the envelope object
@@ -138,9 +150,7 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
138
150
Signers = new List < Signer > { signer1 } ,
139
151
CarbonCopies = new List < CarbonCopy > { cc1 }
140
152
} ;
141
-
142
153
env . Recipients = recipients ;
143
-
144
154
// Request that the envelope be sent by setting |status| to "sent".
145
155
// To request that the envelope be created as a draft, set to "created"
146
156
env . Status = RequestItemsService . Status ;
@@ -150,6 +160,12 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
150
160
151
161
private byte [ ] document1 ( string signerEmail , string signerName , string ccEmail , string ccName )
152
162
{
163
+ // Data for this method
164
+ // signerEmail
165
+ // signerName
166
+ // ccEmail
167
+ // ccName
168
+
153
169
return Encoding . UTF8 . GetBytes (
154
170
" <!DOCTYPE html>\n " +
155
171
" <html>\n " +
@@ -174,5 +190,26 @@ private byte[] document1(string signerEmail, string signerName, string ccEmail,
174
190
" </html>"
175
191
) ;
176
192
}
193
+
194
+ [ HttpPost ]
195
+ public IActionResult Create ( string signerEmail , string signerName , string ccEmail , string ccName )
196
+ {
197
+ // Check the token with minimal buffer time.
198
+ bool tokenOk = CheckToken ( 3 ) ;
199
+ if ( ! tokenOk )
200
+ {
201
+ // We could store the parameters of the requested operation
202
+ // so it could be restarted automatically.
203
+ // But since it should be rare to have a token issue here,
204
+ // we'll make the user re-enter the form data after
205
+ // authentication.
206
+ RequestItemsService . EgName = EgName ;
207
+ return Redirect ( "/ds/mustAuthenticate" ) ;
208
+ }
209
+ EnvelopeSummary results = DoWork ( signerEmail , signerName , ccEmail , ccName ) ;
210
+ ViewBag . h1 = "Envelope sent" ;
211
+ ViewBag . message = "The envelope has been created and sent!<br />Envelope ID " + results . EnvelopeId + "." ;
212
+ return View ( "example_done" ) ;
213
+ }
177
214
}
178
215
}
0 commit comments