11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Threading . Tasks ;
53using DocuSign . eSign . Api ;
64using DocuSign . eSign . Model ;
75using eg_03_csharp_auth_code_grant_core . Models ;
86using Microsoft . AspNetCore . Mvc ;
9- using System . IO ;
107using System . Text ;
8+ using DocuSign . eSign . Client ;
119
1210namespace eg_03_csharp_auth_code_grant_core . Controllers
1311{
@@ -22,21 +20,38 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi
2220
2321 public override string EgName => "eg002" ;
2422
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+
2834 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 ) ;
3139 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 ;
3641 }
3742
3843 private EnvelopeDefinition MakeEnvelope ( string signerEmail , string signerName , string ccEmail , string ccName )
3944 {
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+
4055 // document 1 (html) has tag **signature_1**
4156 // document 2 (docx) has tag /sn1/
4257 // document 3 (pdf) has tag /sn1/
@@ -53,6 +68,8 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
5368 // create the envelope definition
5469 EnvelopeDefinition env = new EnvelopeDefinition ( ) ;
5570 env . EmailSubject = "Please sign this document set" ;
71+
72+ // Create document objects, one per document
5673 Document doc1 = new Document ( ) ;
5774 string b64 = Convert . ToBase64String ( document1 ( signerEmail , signerName , ccEmail , ccName ) ) ;
5875 doc1 . DocumentBase64 = b64 ;
@@ -65,16 +82,13 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
6582 FileExtension = "docx" ,
6683 DocumentId = "2"
6784 } ;
68-
6985 Document doc3 = new Document
7086 {
7187 DocumentBase64 = doc3PdfBytes ,
7288 Name = "Lorem Ipsum" , // can be different from actual file name
7389 FileExtension = "pdf" ,
7490 DocumentId = "3"
7591 } ;
76-
77-
7892 // The order in the docs array determines the order in the envelope
7993 env . Documents = new List < Document > { doc1 , doc2 , doc3 } ;
8094
@@ -124,12 +138,10 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
124138 AnchorXOffset = "20"
125139 } ;
126140
127-
128141 // Tabs are set per recipient / signer
129142 Tabs signer1Tabs = new Tabs {
130143 SignHereTabs = new List < SignHere > { signHere1 , signHere2 }
131144 } ;
132-
133145 signer1 . Tabs = signer1Tabs ;
134146
135147 // Add the recipients to the envelope object
@@ -138,9 +150,7 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
138150 Signers = new List < Signer > { signer1 } ,
139151 CarbonCopies = new List < CarbonCopy > { cc1 }
140152 } ;
141-
142153 env . Recipients = recipients ;
143-
144154 // Request that the envelope be sent by setting |status| to "sent".
145155 // To request that the envelope be created as a draft, set to "created"
146156 env . Status = RequestItemsService . Status ;
@@ -150,6 +160,12 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
150160
151161 private byte [ ] document1 ( string signerEmail , string signerName , string ccEmail , string ccName )
152162 {
163+ // Data for this method
164+ // signerEmail
165+ // signerName
166+ // ccEmail
167+ // ccName
168+
153169 return Encoding . UTF8 . GetBytes (
154170 " <!DOCTYPE html>\n " +
155171 " <html>\n " +
@@ -174,5 +190,26 @@ private byte[] document1(string signerEmail, string signerName, string ccEmail,
174190 " </html>"
175191 ) ;
176192 }
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+ }
177214 }
178215}
0 commit comments