From 97a110247b310fa08e05630a5d3d5bba27ac4aa1 Mon Sep 17 00:00:00 2001 From: shadim Date: Mon, 26 Nov 2018 03:15:36 -0800 Subject: [PATCH 1/9] add client api in example usage --- .../Common/IRequestItemsService.cs | 5 +- .../Common/RequestItemService.cs | 49 ++++++------------- .../Eg001EmbeddedSigningController.cs | 9 ++-- .../Eg002SigningViaEmailController.cs | 9 +++- .../Eg003ListEnvelopesController.cs | 7 ++- .../Eg004EnvelopeInfoController.cs | 7 ++- .../Eg005EnvelopeRecipientsController.cs | 7 ++- .../Eg006EnvelopeDocsController.cs | 7 ++- .../Eg007EnvelopeGetDocController.cs | 7 ++- .../Eg008CreateTemplateController.cs | 7 ++- .../Controllers/Eg009UseTemplateController.cs | 7 ++- .../Eg011EmbeddedSendingController.cs | 9 +++- .../Eg012EmbeddedConsoleController.cs | 7 ++- .../Eg013AddDocToTemplateController.cs | 7 ++- .../Eg014CollectPaymentController.cs | 7 ++- 15 files changed, 98 insertions(+), 53 deletions(-) diff --git a/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs b/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs index 7c6bcc67..fb3765e9 100644 --- a/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs +++ b/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs @@ -5,9 +5,8 @@ namespace eg_03_csharp_auth_code_grant_core { public interface IRequestItemsService { - ApiClient DefaultApiClient { get; } - - Configuration DefaultConfiguration { get; } + //ApiClient DefaultApiClient { get; } + string EgName { get; set; } Session Session { get; set; } diff --git a/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs b/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs index 18f8fc77..f36e330a 100644 --- a/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs +++ b/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs @@ -11,8 +11,7 @@ public class RequestItemsService : IRequestItemsService private readonly IHttpContextAccessor _httpContextAccessor; private readonly IMemoryCache _cache; private readonly string _id; - private string API_CLIENT_KEY = "{0}_ApiClient"; - private string CONFIG_KEY = "{0}_DocusignConfig"; + private string API_CLIENT_KEY = "{0}_ApiClient"; private string BASE_URI = "https://demo.docusign.net/restapi"; private string _accessToken; @@ -30,38 +29,22 @@ public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCach } } - public ApiClient DefaultApiClient - { - get - { - var key = string.Format(API_CLIENT_KEY, _id); - ApiClient apiClient = _cache.Get("apiClient"); - if (apiClient == null) - { - apiClient = new ApiClient(BASE_URI); - _cache.Set(key, apiClient); - } - - return apiClient; - } - } + //public ApiClient DefaultApiClient + //{ + // get + // { + // var key = string.Format(API_CLIENT_KEY, _id); + // ApiClient apiClient = _cache.Get("apiClient"); + // if (apiClient == null) + // { + // apiClient = new ApiClient(BASE_URI); + // _cache.Set(key, apiClient); + // } - public Configuration DefaultConfiguration - { - get - { - var key = string.Format(CONFIG_KEY, _id); - var docuSignConfig = _cache.Get(key); + // return apiClient; + // } + //} - if (docuSignConfig == null) - { - docuSignConfig = new Configuration(new ApiClient(BASE_URI)); - _cache.Set(key, docuSignConfig); - } - docuSignConfig.AddDefaultHeader("Authorization", "Bearer " + _accessToken); - return docuSignConfig; - } - } private string GetKey(string key) { return string.Format("{0}_{1}", _id, key); @@ -70,7 +53,7 @@ public string EgName { get => _cache.Get(GetKey("EgName")); set => _cache.Set(GetKey("EgName"), value); } - + public Session Session { get => _cache.Get(GetKey("Session")); set => _cache.Set(GetKey("Session"), value); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs index fd368dee..f7d6f371 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Common; using eg_03_csharp_auth_code_grant_core.Controllers; @@ -33,9 +34,11 @@ public IActionResult Create(string signerEmail, string signerName) // Step 1. Create the envelope definition EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName); - // Step 2. Call DocuSign to create the envelope - //EnvelopesApi envelopesApi = new EnvelopesApi((Configuration)HttpContext.Items["docuSignConfig"]); - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + // Step 2. Call DocuSign to create the envelope + var config = new Configuration(new ApiClient(session.BasePath+ "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer "+ user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); + EnvelopeSummary results = envelopesApi.CreateEnvelope(session.AccountId, envelope); string envelopeId = results.EnvelopeId; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs index c95c2550..869b27c2 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc; using System.IO; using System.Text; +using DocuSign.eSign.Client; namespace eg_03_csharp_auth_code_grant_core.Controllers { @@ -24,9 +25,13 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) - { + { + var session = RequestItemsService.Session; + var user = RequestItemsService.User; EnvelopeDefinition env = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); EnvelopeSummary results = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, env); RequestItemsService.EnvelopeId = results.EnvelopeId; ViewBag.h1 = "Envelope sent"; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs index 506fbc3f..fa7ea570 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -25,7 +26,11 @@ public Eg003ListEnvelopesController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); ListStatusChangesOptions options = new ListStatusChangesOptions(); options.fromDate = DateTime.Now.AddDays(-30).ToString("yyyy/MM/dd"); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs index 790a7e7a..8b5e0677 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; @@ -23,7 +24,11 @@ public Eg004EnvelopeInfoController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); ViewBag.h1 = "Get envelope status results"; ViewBag.message = "Results from the Envelopes::get method:"; DocuSign.eSign.Model.Envelope results = envelopesApi.GetEnvelope(RequestItemsService.Session.AccountId, RequestItemsService.EnvelopeId); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs index ef856e8f..96b1da3a 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs @@ -1,4 +1,5 @@ using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; @@ -19,7 +20,11 @@ public Eg005EnvelopeRecipientsController(DSConfiguration config, IRequestItemsSe [HttpPost] public IActionResult Create(string signerEmail, string signerName) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); ViewBag.h1 = "List envelope recipients result"; ViewBag.message = "Results from the EnvelopeRecipients::list method:"; var results = envelopesApi.ListRecipients(RequestItemsService.Session.AccountId, RequestItemsService.EnvelopeId); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs index d56d8191..0c5641c1 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -21,7 +22,11 @@ public Eg006EnvelopeDocsController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); EnvelopeDocumentsResult result = envelopesApi.ListDocuments(RequestItemsService.Session.AccountId, RequestItemsService.EnvelopeId); // Save the envelopeId and its list of documents in the session so diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs index 9242e935..d4488caa 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs @@ -1,5 +1,6 @@ using System.Linq; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -19,7 +20,11 @@ public Eg007EnvelopeGetDocController(DSConfiguration config, IRequestItemsServic [HttpPost] public FileResult Create(string docSelect) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); // Step 1. EnvelopeDocuments::get. // Exceptions will be caught by the calling function diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs index 5e79f6bb..93bbe764 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -22,7 +23,11 @@ public Eg008CreateTemplateController(DSConfiguration config, IRequestItemsServic public IActionResult Create() { string templateName = "Example Signer and CC template"; - TemplatesApi templatesApi = new TemplatesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + TemplatesApi templatesApi = new TemplatesApi(config); TemplatesApi.ListTemplatesOptions options = new TemplatesApi.ListTemplatesOptions(); options.searchText = "Example Signer and CC template"; EnvelopeTemplateResults results = templatesApi.ListTemplates(RequestItemsService.Session.AccountId, options); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs index 1e7955f5..7120e3e7 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -22,7 +23,11 @@ public Eg009UseTemplateController(DSConfiguration config, IRequestItemsService r [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName, RequestItemsService.TemplateId); EnvelopeSummary result = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, envelope); RequestItemsService.EnvelopeId = result.EnvelopeId; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs index 9cb68493..94eb4bf5 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -24,8 +25,12 @@ public Eg011EmbeddedSendingController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string startingView) - { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + { + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); // Step 1. Make the envelope with "created" (draft) status RequestItemsService.Status = "created"; //EnvelopeSummary results = (EnvelopeSummary)controller2.doWork(args, model); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs index d7918417..f473d666 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -22,7 +23,11 @@ public Eg012EmbeddedConsoleController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string startingView) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); string dsReturnUrl = Config.AppUrl + "/ds-return"; ConsoleViewRequest viewRequest = MakeConsoleViewRequest(dsReturnUrl, startingView); // Step 1. create the NDSE view diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs index 0ad752c1..b667c4c3 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -25,7 +26,11 @@ public Eg013AddDocToTemplateController(DSConfiguration config, IRequestItemsServ [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); string dsReturnUrl = Config.AppUrl + "/dsReturn"; string dsPingUrl = Config.AppUrl + "/"; // Step 1. Make the envelope request body diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs index 4456c26b..bc6c4451 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using DocuSign.eSign.Api; +using DocuSign.eSign.Client; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; @@ -24,7 +25,11 @@ public Eg014CollectPaymentController(DSConfiguration config, IRequestItemsServic [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { - EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration); + var session = RequestItemsService.Session; + var user = RequestItemsService.User; + var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); + config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); // Step 1. Make the envelope request body EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); // Step 2. call Envelopes::create API method From 48b040d3db12c73d4efce891d5942a93eafda40b Mon Sep 17 00:00:00 2001 From: shadim Date: Mon, 26 Nov 2018 03:19:38 -0800 Subject: [PATCH 2/9] optimization --- .../Common/IRequestItemsService.cs | 4 +--- .../Common/RequestItemService.cs | 16 ---------------- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs b/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs index fb3765e9..ccca3884 100644 --- a/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs +++ b/eg-03-csharp-auth-code-grant-core/Common/IRequestItemsService.cs @@ -4,9 +4,7 @@ namespace eg_03_csharp_auth_code_grant_core { public interface IRequestItemsService - { - //ApiClient DefaultApiClient { get; } - + { string EgName { get; set; } Session Session { get; set; } diff --git a/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs b/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs index f36e330a..03abc2a4 100644 --- a/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs +++ b/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs @@ -29,22 +29,6 @@ public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCach } } - //public ApiClient DefaultApiClient - //{ - // get - // { - // var key = string.Format(API_CLIENT_KEY, _id); - // ApiClient apiClient = _cache.Get("apiClient"); - // if (apiClient == null) - // { - // apiClient = new ApiClient(BASE_URI); - // _cache.Set(key, apiClient); - // } - - // return apiClient; - // } - //} - private string GetKey(string key) { return string.Format("{0}_{1}", _id, key); From 33f0d18bc1512a79447aec64a7fb21121d5f8f94 Mon Sep 17 00:00:00 2001 From: shadim Date: Mon, 26 Nov 2018 03:30:36 -0800 Subject: [PATCH 3/9] remove unuased variables --- .../Common/RequestItemService.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs b/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs index 03abc2a4..d0677e9b 100644 --- a/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs +++ b/eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs @@ -10,9 +10,7 @@ public class RequestItemsService : IRequestItemsService { private readonly IHttpContextAccessor _httpContextAccessor; private readonly IMemoryCache _cache; - private readonly string _id; - private string API_CLIENT_KEY = "{0}_ApiClient"; - private string BASE_URI = "https://demo.docusign.net/restapi"; + private readonly string _id; private string _accessToken; public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache) From fc9413096ef5c650c6b7899452d40c93fb58640c Mon Sep 17 00:00:00 2001 From: shadim Date: Mon, 26 Nov 2018 03:31:00 -0800 Subject: [PATCH 4/9] upgrade to version 3.1.3-rc --- .../eg-03-csharp-auth-code-grant-core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj b/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj index 0b3dc188..6ff8f628 100644 --- a/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj +++ b/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj @@ -6,7 +6,7 @@ - + From 030ffb546ff94db838c05d71a4060f8c8e4aac22 Mon Sep 17 00:00:00 2001 From: shadim Date: Mon, 26 Nov 2018 05:13:50 -0800 Subject: [PATCH 5/9] modify check token logic --- .../Controllers/Eg001EmbeddedSigningController.cs | 11 +++++++++++ .../Controllers/Eg002SigningViaEmailController.cs | 11 +++++++++++ .../Controllers/Eg003ListEnvelopesController.cs | 11 +++++++++++ .../Controllers/Eg004EnvelopeInfoController.cs | 11 +++++++++++ .../Eg005EnvelopeRecipientsController.cs | 11 +++++++++++ .../Controllers/Eg006EnvelopeDocsController.cs | 11 +++++++++++ .../Controllers/Eg007EnvelopeGetDocController.cs | 13 ++++++++++++- .../Controllers/Eg008CreateTemplateController.cs | 13 ++++++++++++- .../Controllers/Eg009UseTemplateController.cs | 11 +++++++++++ .../Controllers/Eg010SendBinaryDocsController.cs | 11 +++++++++++ .../Controllers/Eg011EmbeddedSendingController.cs | 11 +++++++++++ .../Controllers/Eg012EmbeddedConsoleController.cs | 11 +++++++++++ .../Controllers/Eg013AddDocToTemplateController.cs | 11 +++++++++++ .../Controllers/Eg014CollectPaymentController.cs | 11 +++++++++++ .../Controllers/EgController.cs | 12 +++++++----- 15 files changed, 163 insertions(+), 7 deletions(-) diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs index f7d6f371..0e6ec80d 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs @@ -29,6 +29,17 @@ public Eg001EmbeddedSigningController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; // Step 1. Create the envelope definition diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs index 869b27c2..793ca247 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs @@ -26,6 +26,17 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; EnvelopeDefinition env = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs index fa7ea570..50971f8f 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs @@ -26,6 +26,17 @@ public Eg003ListEnvelopesController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs index 8b5e0677..0f6cc734 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs @@ -24,6 +24,17 @@ public Eg004EnvelopeInfoController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs index 96b1da3a..b1e4b9cb 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs @@ -20,6 +20,17 @@ public Eg005EnvelopeRecipientsController(DSConfiguration config, IRequestItemsSe [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs index 0c5641c1..9b169fb2 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs @@ -22,6 +22,17 @@ public Eg006EnvelopeDocsController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs index d4488caa..329b940f 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs @@ -18,8 +18,19 @@ public Eg007EnvelopeGetDocController(DSConfiguration config, IRequestItemsServic public override string EgName => "eg007"; [HttpPost] - public FileResult Create(string docSelect) + public ActionResult Create(string docSelect) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs index 93bbe764..ea254a6c 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs @@ -21,7 +21,18 @@ public Eg008CreateTemplateController(DSConfiguration config, IRequestItemsServic [HttpPost] public IActionResult Create() - { + { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } string templateName = "Example Signer and CC template"; var session = RequestItemsService.Session; var user = RequestItemsService.User; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs index 7120e3e7..0e31dc94 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs @@ -23,6 +23,17 @@ public Eg009UseTemplateController(DSConfiguration config, IRequestItemsService r [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs index b7314cff..dcd38f2f 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs @@ -26,6 +26,17 @@ public Eg010SendBinaryDocsController(DSConfiguration config, IRequestItemsServic [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } // Step 1. Make the envelope JSON request body dynamic envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs index 94eb4bf5..9c55aa09 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs @@ -26,6 +26,17 @@ public Eg011EmbeddedSendingController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string startingView) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs index f473d666..338e91e7 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs @@ -23,6 +23,17 @@ public Eg012EmbeddedConsoleController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string startingView) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs index b667c4c3..691bdd7b 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs @@ -26,6 +26,17 @@ public Eg013AddDocToTemplateController(DSConfiguration config, IRequestItemsServ [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs index bc6c4451..c2bd7e51 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs @@ -25,6 +25,17 @@ public Eg014CollectPaymentController(DSConfiguration config, IRequestItemsServic [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } var session = RequestItemsService.Session; var user = RequestItemsService.User; var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs index 06b205ed..3b44c25d 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs @@ -1,5 +1,6 @@ using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; +using System; namespace eg_03_csharp_auth_code_grant_core.Controllers { @@ -23,7 +24,7 @@ public IActionResult Get() // to have the user authenticate or re-authenticate. bool tokenOk = CheckToken(); - ViewBag.showDoc = true; + if (tokenOk) { //addSpecialAttributes(model); @@ -51,10 +52,11 @@ private dynamic CreateSourcePath() return Config.githubExampleUrl + source + ".cs"; } - private bool CheckToken() - { - //TODO: add token expiration time validation check like node program - return HttpContext.User.Identity.IsAuthenticated; + protected bool CheckToken(int bufferMin = 60) + { + return HttpContext.User.Identity.IsAuthenticated + && (DateTime.Now.Subtract(TimeSpan.FromMinutes(bufferMin)) < + RequestItemsService.User.ExpireIn.Value); } } } \ No newline at end of file From be55160e7ba1ab3af0118ff0696d31ce267aacc7 Mon Sep 17 00:00:00 2001 From: LarryKlugerDS Date: Mon, 10 Dec 2018 15:09:04 +0200 Subject: [PATCH 6/9] fix eg11 --- .../Controllers/Eg002SigningViaEmailController.cs | 14 ++++++++++---- .../Controllers/Eg011EmbeddedSendingController.cs | 9 +++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs index 793ca247..18225a37 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs @@ -37,6 +37,15 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } + EnvelopeSummary results = DoWork(signerEmail, signerName, ccEmail, ccName); + ViewBag.h1 = "Envelope sent"; + ViewBag.message = "The envelope has been created and sent!
Envelope ID " + results.EnvelopeId + "."; + //return results; + return View("example_done"); + } + + public EnvelopeSummary DoWork(string signerEmail, string signerName, string ccEmail, string ccName) + { var session = RequestItemsService.Session; var user = RequestItemsService.User; EnvelopeDefinition env = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); @@ -45,10 +54,7 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai EnvelopesApi envelopesApi = new EnvelopesApi(config); EnvelopeSummary results = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, env); RequestItemsService.EnvelopeId = results.EnvelopeId; - ViewBag.h1 = "Envelope sent"; - ViewBag.message = "The envelope has been created and sent!
Envelope ID " + results.EnvelopeId + "."; - //return results; - return View("example_done"); + return results; } private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName) diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs index 9c55aa09..85b02532 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs @@ -18,7 +18,7 @@ public class Eg011EmbeddedSendingController : EgController public Eg011EmbeddedSendingController(DSConfiguration config, IRequestItemsService requestItemsService) : base(config, requestItemsService) { - this.controller2 = new Eg002SigningViaEmailController(Config, requestItemsService); + ViewBag.title = "Embedded Sending"; } public override string EgName => "eg011"; @@ -43,10 +43,11 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); // Step 1. Make the envelope with "created" (draft) status + // Using eg002 to create the envelope with "created" status RequestItemsService.Status = "created"; - //EnvelopeSummary results = (EnvelopeSummary)controller2.doWork(args, model); - controller2.Create(signerEmail, signerName, ccEmail, ccName); - String envelopeId = RequestItemsService.EnvelopeId; + controller2 = new Eg002SigningViaEmailController(Config, RequestItemsService); + EnvelopeSummary results = controller2.DoWork(signerEmail, signerName, ccEmail, ccName); + String envelopeId = results.EnvelopeId; // Step 2. create the sender view // Call the CreateSenderView API From fea60e567dbd0a552a7d84173517d78eac558a4c Mon Sep 17 00:00:00 2001 From: LarryKlugerDS Date: Mon, 17 Dec 2018 09:19:20 +0200 Subject: [PATCH 7/9] Added method data comments --- .../Eg001EmbeddedSigningController.cs | 43 +++- .../Eg002SigningViaEmailController.cs | 43 +++- .../Eg003ListEnvelopesController.cs | 19 +- .../Eg004EnvelopeInfoController.cs | 15 +- .../Eg005EnvelopeRecipientsController.cs | 14 +- .../Eg006EnvelopeDocsController.cs | 19 +- .../Eg007EnvelopeGetDocController.cs | 20 +- .../Eg008CreateTemplateController.cs | 25 +- .../Controllers/Eg009UseTemplateController.cs | 27 ++- .../Eg010SendBinaryDocsController.cs | 93 ++++--- .../Eg011EmbeddedSendingController.cs | 43 ++-- .../Eg012EmbeddedConsoleController.cs | 30 ++- .../Eg013AddDocToTemplateController.cs | 227 +++++++++++------- .../Eg014CollectPaymentController.cs | 167 ++++++------- .../Controllers/EgController.cs | 1 + 15 files changed, 471 insertions(+), 315 deletions(-) diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs index 0e6ec80d..2bbd9f69 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs @@ -3,10 +3,8 @@ using DocuSign.eSign.Api; using DocuSign.eSign.Client; using DocuSign.eSign.Model; -using eg_03_csharp_auth_code_grant_core.Common; using eg_03_csharp_auth_code_grant_core.Controllers; using eg_03_csharp_auth_code_grant_core.Models; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace eg_03_csharp_auth_code_grant_core.Views @@ -29,6 +27,18 @@ public Eg001EmbeddedSigningController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + // Data for this method + // signerEmail + // signerName + // dsPingUrl -- class global + // signerClientId -- class global + // dsReturnUrl -- class global + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + + + // Check the token with minimal buffer time. bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -40,18 +50,14 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; // Step 1. Create the envelope definition EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName); // Step 2. Call DocuSign to create the envelope - var config = new Configuration(new ApiClient(session.BasePath+ "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer "+ user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - - EnvelopeSummary results = envelopesApi.CreateEnvelope(session.AccountId, envelope); - + EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelope); string envelopeId = results.EnvelopeId; // Save for future use within the example launcher @@ -60,7 +66,7 @@ public IActionResult Create(string signerEmail, string signerName) // Step 3. create the recipient view, the Signing Ceremony RecipientViewRequest viewRequest = MakeRecipientViewRequest(signerEmail, signerName); // call the CreateRecipientView API - ViewUrl results1 = envelopesApi.CreateRecipientView(session.AccountId, envelopeId, viewRequest); + ViewUrl results1 = envelopesApi.CreateRecipientView(accountId, envelopeId, viewRequest); // Step 4. Redirect the user to the Signing Ceremony // Don't use an iFrame! @@ -71,6 +77,14 @@ public IActionResult Create(string signerEmail, string signerName) private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string signerName) { + // Data for this method + // signerEmail + // signerName + // dsPingUrl -- class global + // signerClientId -- class global + // dsReturnUrl -- class global + + RecipientViewRequest viewRequest = new RecipientViewRequest(); // Set the url where you want the recipient to go once they are done signing // should typically be a callback route somewhere in your app. @@ -107,8 +121,15 @@ private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName) { - byte[] buffer = System.IO.File.ReadAllBytes(Config.docPdf); + // Data for this method + // signerEmail + // signerName + // signerClientId -- class global + // Config.docPdf + + byte[] buffer = System.IO.File.ReadAllBytes(Config.docPdf); + EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition(); envelopeDefinition.EmailSubject = "Please sign this document"; Document doc1 = new Document(); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs index 18225a37..f13ea63e 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs @@ -26,6 +26,7 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + // Check the token with minimal buffer time. bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -46,19 +47,36 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai public EnvelopeSummary DoWork(string signerEmail, string signerName, string ccEmail, string ccName) { - var session = RequestItemsService.Session; - var user = RequestItemsService.User; + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + EnvelopeDefinition env = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - EnvelopeSummary results = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, env); + EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, env); RequestItemsService.EnvelopeId = results.EnvelopeId; return results; } private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // Config.docDocx + // Config.docPdf + // RequestItemsService.Status -- the envelope status ('created' or 'sent') + + // document 1 (html) has tag **signature_1** // document 2 (docx) has tag /sn1/ // document 3 (pdf) has tag /sn1/ @@ -75,6 +93,8 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s // create the envelope definition EnvelopeDefinition env = new EnvelopeDefinition(); env.EmailSubject = "Please sign this document set"; + + // Create document objects, one per document Document doc1 = new Document(); string b64 = Convert.ToBase64String(document1(signerEmail, signerName, ccEmail, ccName)); doc1.DocumentBase64 = b64; @@ -87,7 +107,6 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s FileExtension = "docx", DocumentId = "2" }; - Document doc3 = new Document { DocumentBase64 = doc3PdfBytes, @@ -95,8 +114,6 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s FileExtension = "pdf", DocumentId = "3" }; - - // The order in the docs array determines the order in the envelope env.Documents = new List { doc1, doc2, doc3}; @@ -146,12 +163,10 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s AnchorXOffset = "20" }; - // Tabs are set per recipient / signer Tabs signer1Tabs = new Tabs { SignHereTabs = new List { signHere1, signHere2} }; - signer1.Tabs = signer1Tabs; // Add the recipients to the envelope object @@ -160,9 +175,7 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s Signers = new List { signer1 }, CarbonCopies = new List { cc1 } }; - env.Recipients = recipients; - // Request that the envelope be sent by setting |status| to "sent". // To request that the envelope be created as a draft, set to "created" env.Status = RequestItemsService.Status; @@ -172,6 +185,12 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s private byte[] document1(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + return Encoding.UTF8.GetBytes( " \n" + " \n" + diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs index 50971f8f..bdb019a1 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs @@ -26,6 +26,12 @@ public Eg003ListEnvelopesController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + // Data for this method + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + + // Check the token with minimal buffer time. bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -37,16 +43,13 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - ListStatusChangesOptions options = new ListStatusChangesOptions(); - + ListStatusChangesOptions options = new ListStatusChangesOptions(); options.fromDate = DateTime.Now.AddDays(-30).ToString("yyyy/MM/dd"); - - EnvelopesInformation results = envelopesApi.ListStatusChanges(RequestItemsService.Session.AccountId, options); + // Call the API method: + EnvelopesInformation results = envelopesApi.ListStatusChanges(accountId, options); ViewBag.h1 = "List envelopes results"; ViewBag.message = "Results from the Envelopes::listStatusChanges method:"; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs index 0f6cc734..ca5c423d 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs @@ -24,6 +24,13 @@ public Eg004EnvelopeInfoController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + // Data for this method + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + var envelopeId = RequestItemsService.EnvelopeId; + + // Check the token with minimal buffer time. bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -35,14 +42,12 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); ViewBag.h1 = "Get envelope status results"; ViewBag.message = "Results from the Envelopes::get method:"; - DocuSign.eSign.Model.Envelope results = envelopesApi.GetEnvelope(RequestItemsService.Session.AccountId, RequestItemsService.EnvelopeId); + DocuSign.eSign.Model.Envelope results = envelopesApi.GetEnvelope(accountId, envelopeId); ViewBag.Locals.Json = JsonConvert.SerializeObject(results, Formatting.Indented); return View("example_done"); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs index b1e4b9cb..3e375055 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs @@ -20,6 +20,12 @@ public Eg005EnvelopeRecipientsController(DSConfiguration config, IRequestItemsSe [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + // Data for this method + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + var envelopeId = RequestItemsService.EnvelopeId; + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -31,14 +37,12 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); ViewBag.h1 = "List envelope recipients result"; ViewBag.message = "Results from the EnvelopeRecipients::list method:"; - var results = envelopesApi.ListRecipients(RequestItemsService.Session.AccountId, RequestItemsService.EnvelopeId); + var results = envelopesApi.ListRecipients(accountId, envelopeId); ViewBag.Locals.Json = JsonConvert.SerializeObject(results, Formatting.Indented); return View("example_done"); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs index 9b169fb2..a5400fc7 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs @@ -22,6 +22,13 @@ public Eg006EnvelopeDocsController(DSConfiguration config, IRequestItemsService [HttpPost] public IActionResult Create(string signerEmail, string signerName) { + // Data for this method + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + var envelopeId = RequestItemsService.EnvelopeId; + + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -33,16 +40,12 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - EnvelopeDocumentsResult result = envelopesApi.ListDocuments(RequestItemsService.Session.AccountId, - RequestItemsService.EnvelopeId); + EnvelopeDocumentsResult result = envelopesApi.ListDocuments(accountId, envelopeId); // Save the envelopeId and its list of documents in the session so // they can be used in example 7 (download a document) - List envelopeDocItems = new List(); envelopeDocItems.Add(new EnvelopeDocItem { Name= "Combined", Type= "content", DocumentId = "combined" }); envelopeDocItems.Add(new EnvelopeDocItem { Name = "Zip archive", Type = "zip", DocumentId = "archive" }); @@ -57,7 +60,7 @@ public IActionResult Create(string signerEmail, string signerName) } EnvelopeDocuments envelopeDocuments = new EnvelopeDocuments(); - envelopeDocuments.EnvelopeId = RequestItemsService.EnvelopeId; + envelopeDocuments.EnvelopeId = envelopeId; envelopeDocuments.Documents = envelopeDocItems; RequestItemsService.EnvelopeDocuments = envelopeDocuments; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs index 329b940f..0f18e95a 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs @@ -20,6 +20,15 @@ public Eg007EnvelopeGetDocController(DSConfiguration config, IRequestItemsServic [HttpPost] public ActionResult Create(string docSelect) { + // Data for this method + // docSelect -- argument + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + var envelopeId = RequestItemsService.EnvelopeId; + // documents data for the envelope. See example EG006 + var documents = RequestItemsService.EnvelopeDocuments.Documents; + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -31,17 +40,14 @@ public ActionResult Create(string docSelect) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); // Step 1. EnvelopeDocuments::get. // Exceptions will be caught by the calling function - System.IO.Stream results = envelopesApi.GetDocument(RequestItemsService.Session.AccountId, - RequestItemsService.EnvelopeId, docSelect); - var documents = RequestItemsService.EnvelopeDocuments.Documents; + System.IO.Stream results = envelopesApi.GetDocument(accountId, + envelopeId, docSelect); EnvelopeDocItem docItem = documents.FirstOrDefault(d => docSelect.Equals(d.DocumentId)); string docName = docItem.Name; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs index ea254a6c..da343bad 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs @@ -22,6 +22,12 @@ public Eg008CreateTemplateController(DSConfiguration config, IRequestItemsServic [HttpPost] public IActionResult Create() { + // Data for this method + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -33,15 +39,14 @@ public IActionResult Create() RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } + // Step 1. List templates to see if ours exists already string templateName = "Example Signer and CC template"; - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); TemplatesApi templatesApi = new TemplatesApi(config); TemplatesApi.ListTemplatesOptions options = new TemplatesApi.ListTemplatesOptions(); options.searchText = "Example Signer and CC template"; - EnvelopeTemplateResults results = templatesApi.ListTemplates(RequestItemsService.Session.AccountId, options); + EnvelopeTemplateResults results = templatesApi.ListTemplates(accountId, options); string templateId; string resultsTemplateName; @@ -49,6 +54,7 @@ public IActionResult Create() if (int.Parse(results.ResultSetSize) > 0) { + // Found the template! Record its id EnvelopeTemplateResult template = results.EnvelopeTemplates[0]; templateId = template.TemplateId; resultsTemplateName = template.Name; @@ -56,13 +62,14 @@ public IActionResult Create() } else { + // No template! Create one! EnvelopeTemplate templateReqObject = MakeTemplate(templateName); - TemplateSummary template = templatesApi.CreateTemplate(RequestItemsService.Session.AccountId, templateReqObject); + TemplateSummary template = templatesApi.CreateTemplate(accountId, templateReqObject); templateId = template.TemplateId; resultsTemplateName = template.Name; createdNewTemplate = true; } - + // Save the templateId RequestItemsService.TemplateId = templateId; string msg = createdNewTemplate ? "The template has been created!" : @@ -74,6 +81,10 @@ public IActionResult Create() private EnvelopeTemplate MakeTemplate(string resultsTemplateName) { + // Data for this method + // resultsTemplateName + + // document 1 (pdf) has tag /sn1/ // // The template has two recipient roles. diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs index 0e31dc94..20c95107 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs @@ -23,6 +23,16 @@ public Eg009UseTemplateController(DSConfiguration config, IRequestItemsService r [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + var templateId = RequestItemsService.TemplateId; + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -34,13 +44,11 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName, RequestItemsService.TemplateId); - EnvelopeSummary result = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, envelope); + EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName, templateId); + EnvelopeSummary result = envelopesApi.CreateEnvelope(accountId, envelope); RequestItemsService.EnvelopeId = result.EnvelopeId; ViewBag.message = "The envelope has been created and sent!
Envelope ID " + result.EnvelopeId + "."; return View("example_done"); @@ -49,6 +57,13 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName, string templateId) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // templateId + EnvelopeDefinition env = new EnvelopeDefinition(); env.TemplateId = templateId; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs index dcd38f2f..ddee8e54 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs @@ -26,6 +26,17 @@ public Eg010SendBinaryDocsController(DSConfiguration config, IRequestItemsServic [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // Config.docDocx + // Config.docPdf + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -73,14 +84,14 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai byte[] boundary = Encoding.ASCII.GetBytes("multipartboundary_multipartboundary"); byte[] hyphens = Encoding.ASCII.GetBytes("--"); - string uri = RequestItemsService.Session.BasePath - + "/restapi/v2/accounts/" + RequestItemsService.Session.AccountId + "/envelopes"; + string uri = basePath + + "/v2/accounts/" + accountId + "/envelopes"; HttpWebRequest request = WebRequest.CreateHttp(uri); request.Method = "POST"; request.Accept = "application/json"; request.ContentType = "multipart/form-data; boundary=" + Encoding.ASCII.GetString(boundary); - request.Headers.Add("Authorization", "Bearer " + RequestItemsService.User.AccessToken); + request.Headers.Add("Authorization", "Bearer " + accessToken); using (var buffer = new BinaryWriter(request.GetRequestStream(), Encoding.ASCII)) { @@ -160,6 +171,13 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai private string document1(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + + return " \n" + " \n" + " \n" + @@ -185,6 +203,13 @@ private string document1(string signerEmail, string signerName, string ccEmail, private object MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + + // document 1 (html) has tag **signature_1** // document 2 (docx) has tag /sn1/ // document 3 (pdf) has tag /sn1/ @@ -221,22 +246,26 @@ private object MakeEnvelope(string signerEmail, string signerName, string ccEmai // create a signer recipient to sign the document, identified by name and email // We're setting the parameters via the object creation - Signer signer1 = new Signer(); - signer1.Email = signerEmail; - signer1.Name = signerName; - signer1.RecipientId = "1"; - signer1.RoutingOrder = "1"; + Signer signer1 = new Signer + { + Email = signerEmail, + Name = signerName, + RecipientId = "1", + RoutingOrder = "1" + }; // routingOrder (lower means earlier) determines the order of deliveries // to the recipients. Parallel routing order is supported by using the // same integer as the order for two or more recipients. // create a cc recipient to receive a copy of the documents, identified by name and email // We're setting the parameters via setters - CarbonCopy cc1 = new CarbonCopy(); - cc1.Email = ccEmail; - cc1.Name = ccName; - cc1.RoutingOrder = "2"; - cc1.RecipientId = "2"; + CarbonCopy cc1 = new CarbonCopy + { + Email = ccEmail, + Name = ccName, + RoutingOrder = "2", + RecipientId = "2" + }; // Create signHere fields (also known as tabs) on the documents, // We're using anchor (autoPlace) positioning // @@ -244,26 +273,34 @@ private object MakeEnvelope(string signerEmail, string signerName, string ccEmai // documents for matching anchor strings. So the // signHere2 tab will be used in both document 2 and 3 since they // use the same anchor string for their "signer 1" tabs. - SignHere signHere1 = new SignHere(); - signHere1.AnchorString = "**signature_1**"; - signHere1.AnchorYOffset = "10"; - signHere1.AnchorUnits = "pixels"; - signHere1.AnchorXOffset = "20"; - SignHere signHere2 = new SignHere(); - signHere2.AnchorString = "/sn1/"; - signHere2.AnchorYOffset = "10"; - signHere2.AnchorUnits = "pixels"; - signHere2.AnchorXOffset = "20"; + SignHere signHere1 = new SignHere + { + AnchorString = "**signature_1**", + AnchorYOffset = "10", + AnchorUnits = "pixels", + AnchorXOffset = "20" + }; + SignHere signHere2 = new SignHere + { + AnchorString = "/sn1/", + AnchorYOffset = "10", + AnchorUnits = "pixels", + AnchorXOffset = "20" + }; // Tabs are set per recipient / signer - Tabs signer1Tabs = new Tabs(); - signer1Tabs.SignHereTabs = new List { signHere1, signHere2 }; + Tabs signer1Tabs = new Tabs + { + SignHereTabs = new List { signHere1, signHere2 } + }; signer1.Tabs = signer1Tabs; // Add the recipients to the envelope object - Recipients recipients = new Recipients(); - recipients.Signers = new List { signer1 }; - recipients.CarbonCopies = new List { cc1 }; + Recipients recipients = new Recipients + { + Signers = new List { signer1 }, + CarbonCopies = new List { cc1 } + }; dynamic env = new diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs index 85b02532..ece39e37 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs @@ -26,6 +26,18 @@ public Eg011EmbeddedSendingController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string startingView) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // startingView + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + string dsReturnUrl = Config.AppUrl + "/dsReturn"; + + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -37,11 +49,10 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); + // Step 1. Make the envelope with "created" (draft) status // Using eg002 to create the envelope with "created" status RequestItemsService.Status = "created"; @@ -52,10 +63,11 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai // Step 2. create the sender view // Call the CreateSenderView API // Exceptions will be caught by the calling function - string dsReturnUrl = Config.AppUrl + "/ds-return"; - ReturnUrlRequest viewRequest = MakeSenderViewRequest(dsReturnUrl); - - ViewUrl result1 = envelopesApi.CreateSenderView(RequestItemsService.Session.AccountId, envelopeId, viewRequest); + ReturnUrlRequest viewRequest = new ReturnUrlRequest + { + ReturnUrl = dsReturnUrl + }; + ViewUrl result1 = envelopesApi.CreateSenderView(accountId, envelopeId, viewRequest); // Switch to Recipient and Documents view if requested by the user String url = result1.Url; Console.WriteLine("startingView: " + startingView); @@ -68,20 +80,5 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai return Redirect(url); } - - private ReturnUrlRequest MakeSenderViewRequest(string dsReturnUrl) - { - ReturnUrlRequest viewRequest = new ReturnUrlRequest(); - // Set the url where you want the recipient to go once they are done signing - // should typically be a callback route somewhere in your app. - // The query parameter is included as an example of how - // to save/recover state information during the redirect to - // the DocuSign signing ceremony. It's usually better to use - // the session mechanism of your web framework. Query parameters - // can be changed/spoofed very easily. - viewRequest.ReturnUrl = dsReturnUrl + "?state=123"; - - return viewRequest; - } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs index 338e91e7..592b30a6 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs @@ -23,6 +23,17 @@ public Eg012EmbeddedConsoleController(DSConfiguration config, IRequestItemsServi [HttpPost] public IActionResult Create(string startingView) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // startingView + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + string dsReturnUrl = Config.AppUrl + "/dsReturn"; + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -34,17 +45,15 @@ public IActionResult Create(string startingView) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - string dsReturnUrl = Config.AppUrl + "/ds-return"; ConsoleViewRequest viewRequest = MakeConsoleViewRequest(dsReturnUrl, startingView); + // Step 1. create the NDSE view // Call the CreateSenderView API // Exceptions will be caught by the calling function - ViewUrl results = envelopesApi.CreateConsoleView(RequestItemsService.Session.AccountId, viewRequest); + ViewUrl results = envelopesApi.CreateConsoleView(accountId, viewRequest); Console.WriteLine("NDSE view URL: " + results.Url); @@ -53,6 +62,11 @@ public IActionResult Create(string startingView) private ConsoleViewRequest MakeConsoleViewRequest(string dsReturnUrl, string startingView) { + // Data for this method + // dsReturnUrl + // startingView + string envelopeId = RequestItemsService.EnvelopeId; + ConsoleViewRequest viewRequest = new ConsoleViewRequest(); // Set the url where you want the recipient to go once they are done // with the NDSE. It is usually the case that the @@ -60,9 +74,9 @@ private ConsoleViewRequest MakeConsoleViewRequest(string dsReturnUrl, string sta // Assume that control will not be passed back to your app. viewRequest.ReturnUrl = dsReturnUrl; - if ("envelope".Equals(startingView) && RequestItemsService.EnvelopeId != null) + if ("envelope".Equals(startingView) && envelopeId != null) { - viewRequest.EnvelopeId = RequestItemsService.EnvelopeId; + viewRequest.EnvelopeId = envelopeId; } return viewRequest; diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs index 691bdd7b..ca882b5d 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; using DocuSign.eSign.Api; using DocuSign.eSign.Client; using DocuSign.eSign.Model; @@ -26,6 +24,19 @@ public Eg013AddDocToTemplateController(DSConfiguration config, IRequestItemsServ [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // item + // quantity + // signerClientId -- class global + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + string dsReturnUrl = Config.AppUrl + "/dsReturn"; + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -37,69 +48,71 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - string dsReturnUrl = Config.AppUrl + "/dsReturn"; - string dsPingUrl = Config.AppUrl + "/"; + // Step 1. Make the envelope request body EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName, item, quantity); // Step 2. call Envelopes::create API method // Exceptions will be caught by the calling function - EnvelopeSummary results = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, envelope); + EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelope); String envelopeId = results.EnvelopeId; Console.WriteLine("Envelope was created. EnvelopeId " + envelopeId); // Step 3. create the recipient view, the Signing Ceremony - RecipientViewRequest viewRequest = MakeRecipientViewRequest(signerEmail, signerName, dsReturnUrl, dsPingUrl); - ViewUrl results1 = envelopesApi.CreateRecipientView(RequestItemsService.Session.AccountId, envelopeId, viewRequest); + RecipientViewRequest viewRequest = MakeRecipientViewRequest(signerEmail, signerName, dsReturnUrl); + ViewUrl results1 = envelopesApi.CreateRecipientView(accountId, envelopeId, viewRequest); return Redirect(results1.Url); } private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string signerName, - string dsReturnUrl, string dsPingUrl) + string dsReturnUrl) { - RecipientViewRequest viewRequest = new RecipientViewRequest(); - // Set the url where you want the recipient to go once they are done signing - // should typically be a callback route somewhere in your app. - // The query parameter is included as an example of how - // to save/recover state information during the redirect to - // the DocuSign signing ceremony. It's usually better to use - // the session mechanism of your web framework. Query parameters - // can be changed/spoofed very easily. - viewRequest.ReturnUrl = dsReturnUrl + "?state=123"; - - // How has your app authenticated the user? In addition to your app's - // authentication, you can include authenticate steps from DocuSign. - // Eg, SMS authentication - viewRequest.AuthenticationMethod = "none"; - - // Recipient information must match embedded recipient info - // we used to create the envelope. - viewRequest.Email = signerEmail; - viewRequest.UserName = signerName; - viewRequest.ClientUserId = signerClientId; + // Data for this method + // signerEmail + // signerName + // dsReturnUrl + // signerClientId -- class global + + RecipientViewRequest viewRequest = new RecipientViewRequest + { + // Set the url where you want the recipient to go once they are done signing + // should typically be a callback route somewhere in your app. + ReturnUrl = dsReturnUrl, + + // How has your app authenticated the user? In addition to your app's + // authentication, you can include authenticate steps from DocuSign. + // Eg, SMS authentication + AuthenticationMethod = "none", + + // Recipient information must match embedded recipient info + // we used to create the envelope. + Email = signerEmail, + UserName = signerName, + ClientUserId = signerClientId + }; // DocuSign recommends that you redirect to DocuSign for the // Signing Ceremony. There are multiple ways to save state. - // To maintain your application's session, use the pingUrl - // parameter. It causes the DocuSign Signing Ceremony web page - // (not the DocuSign server) to send pings via AJAX to your - // app, - viewRequest.PingFrequency = "600"; // seconds - // NOTE: The pings will only be sent if the pingUrl is an https address - viewRequest.PingUrl = dsPingUrl; // optional setting return viewRequest; - } private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // item + // quantity + // signerClientId -- class global + + // The envelope request object uses Composite Template to // include in the envelope: // 1. A template stored on the DocuSign service @@ -109,60 +122,80 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s // is used, not TemplateRole // // Create a signer recipient for the signer role of the server template - Signer signer1 = new Signer(); - signer1.Email = signerEmail; - signer1.Name = signerName; - signer1.RoleName = "signer"; - signer1.RecipientId = "1"; - // Adding clientUserId transforms the template recipient - // into an embedded recipient: - signer1.ClientUserId = signerClientId; + Signer signer1 = new Signer + { + Email = signerEmail, + Name = signerName, + RoleName = "signer", + RecipientId = "1", + // Adding clientUserId transforms the template recipient + // into an embedded recipient: + ClientUserId = signerClientId + }; // Create the cc recipient - CarbonCopy cc1 = new CarbonCopy(); - cc1.Email = ccEmail; - cc1.Name = ccName; - cc1.RoleName = "cc"; - cc1.RecipientId = "2"; + CarbonCopy cc1 = new CarbonCopy + { + Email = ccEmail, + Name = ccName, + RoleName = "cc", + RecipientId = "2" + }; // Recipients object: - Recipients recipientsServerTemplate = new Recipients(); - recipientsServerTemplate.CarbonCopies = new List { cc1 }; - recipientsServerTemplate.Signers = new List { signer1 }; + Recipients recipientsServerTemplate = new Recipients + { + CarbonCopies = new List { cc1 }, + Signers = new List { signer1 } + }; // create a composite template for the Server Template - CompositeTemplate compTemplate1 = new CompositeTemplate(); - compTemplate1.CompositeTemplateId = "1"; - ServerTemplate serverTemplates = new ServerTemplate(); - serverTemplates.Sequence = "1"; - serverTemplates.TemplateId = RequestItemsService.TemplateId; + CompositeTemplate compTemplate1 = new CompositeTemplate + { + CompositeTemplateId = "1" + }; + ServerTemplate serverTemplates = new ServerTemplate + { + Sequence = "1", + TemplateId = RequestItemsService.TemplateId + }; compTemplate1.ServerTemplates = new List { serverTemplates }; // Add the roles via an inlineTemplate - InlineTemplate inlineTemplate = new InlineTemplate(); - inlineTemplate.Sequence = "1"; - inlineTemplate.Recipients = recipientsServerTemplate; + InlineTemplate inlineTemplate = new InlineTemplate + { + Sequence = "1", + Recipients = recipientsServerTemplate + }; compTemplate1.InlineTemplates = new List { inlineTemplate }; // The signer recipient for the added document with // a tab definition: - SignHere signHere1 = new SignHere(); - signHere1.AnchorString = "**signature_1**"; - signHere1.AnchorYOffset = "10"; - signHere1.AnchorUnits = "pixels"; - signHere1.AnchorXOffset = "20"; - - Tabs signer1Tabs = new Tabs(); - signer1Tabs.SignHereTabs = new List { signHere1 }; + SignHere signHere1 = new SignHere + { + AnchorString = "**signature_1**", + AnchorYOffset = "10", + AnchorUnits = "pixels", + AnchorXOffset = "20" + }; + + Tabs signer1Tabs = new Tabs + { + SignHereTabs = new List { signHere1 } + }; // Signer definition for the added document - Signer signer1AddedDoc = new Signer(); - signer1AddedDoc.Email = signerEmail; - signer1AddedDoc.Name = signerName; - signer1AddedDoc.ClientUserId = signerClientId; - signer1AddedDoc.RoleName = "signer"; - signer1AddedDoc.RecipientId = "1"; - signer1AddedDoc.Tabs = signer1Tabs; + Signer signer1AddedDoc = new Signer + { + Email = signerEmail, + Name = signerName, + ClientUserId = signerClientId, + RoleName = "signer", + RecipientId = "1", + Tabs = signer1Tabs + }; // Recipients object for the added document: - Recipients recipientsAddedDoc = new Recipients(); - recipientsAddedDoc.CarbonCopies = new List { cc1 }; - recipientsAddedDoc.Signers = new List { signer1AddedDoc }; + Recipients recipientsAddedDoc = new Recipients + { + CarbonCopies = new List { cc1 }, + Signers = new List { signer1AddedDoc } + }; // create the HTML document Document doc1 = new Document(); @@ -173,18 +206,24 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s doc1.FileExtension = "html"; doc1.DocumentId = "1"; // create a composite template for the added document - CompositeTemplate compTemplate2 = new CompositeTemplate(); - compTemplate2.CompositeTemplateId = "2"; + CompositeTemplate compTemplate2 = new CompositeTemplate + { + CompositeTemplateId = "2" + }; // Add the recipients via an inlineTemplate - InlineTemplate inlineTemplate2 = new InlineTemplate(); - inlineTemplate2.Sequence = "2"; - inlineTemplate2.Recipients = recipientsAddedDoc; + InlineTemplate inlineTemplate2 = new InlineTemplate + { + Sequence = "2", + Recipients = recipientsAddedDoc + }; compTemplate2.InlineTemplates = new List { inlineTemplate2}; compTemplate2.Document = doc1; - EnvelopeDefinition env = new EnvelopeDefinition(); - env.Status = "sent"; - env.CompositeTemplates = new List { compTemplate1, compTemplate2}; + EnvelopeDefinition env = new EnvelopeDefinition + { + Status = "sent", + CompositeTemplates = new List { compTemplate1, compTemplate2 } + }; return env; } @@ -192,6 +231,14 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s private byte[] document1(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // item + // quantity + return Encoding.UTF8.GetBytes(" \n" + " \n" + " \n" + diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs index c2bd7e51..f6fcca83 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs @@ -25,6 +25,16 @@ public Eg014CollectPaymentController(DSConfiguration config, IRequestItemsServic [HttpPost] public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + + bool tokenOk = CheckToken(3); if (!tokenOk) { @@ -36,16 +46,16 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var session = RequestItemsService.Session; - var user = RequestItemsService.User; - var config = new Configuration(new ApiClient(session.BasePath + "/restapi")); - config.AddDefaultHeader("Authorization", "Bearer " + user.AccessToken); + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); + // Step 1. Make the envelope request body EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); + // Step 2. call Envelopes::create API method // Exceptions will be caught by the calling function - EnvelopeSummary results = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, envelope); + EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelope); string envelopeId = results.EnvelopeId; Console.WriteLine("Envelope was created.EnvelopeId " + envelopeId); ViewBag.h1 = "Envelope sent"; @@ -55,6 +65,13 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName) { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + + // document 1 (html) has multiple tags: // /l1q/ and /l2q/ -- quantities: drop down // /l1e/ and /l2e/ -- extended: payment lines @@ -88,9 +105,6 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s // read file from a local directory // The read could raise an exception if the file is not available! string doc1HTML1 = System.IO.File.ReadAllText("order_form.html"); - //string doc1HTML1 = fs.readFileSync(path.resolve(demoDocsPath, doc1File), - // { encoding: 'utf8'}); - // Substitute values into the HTML // Substitute for: {signerName}, {signerEmail}, {ccName}, {ccEmail} var doc1HTML2 = doc1HTML1.Replace("{signerName}", signerName) @@ -98,19 +112,21 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s .Replace("{ccName}", ccName) .Replace("{ccEmail}", ccEmail); - // create the envelope definition - EnvelopeDefinition env = new EnvelopeDefinition(); - env.EmailSubject = "Please complete your order"; + EnvelopeDefinition env = new EnvelopeDefinition + { + EmailSubject = "Please complete your order" + }; // add the documents - Document doc1 = new Document(); string doc1b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(doc1HTML2)); - - doc1.DocumentBase64 = doc1b64; - doc1.Name = "Order form"; // can be different from actual file name - doc1.FileExtension = "html"; // Source data format. Signed docs are always pdf. - doc1.DocumentId = "1"; // a label used to reference the doc + Document doc1 = new Document + { + DocumentBase64 = doc1b64, + Name = "Order form", // can be different from actual file name + FileExtension = "html", // Source data format. Signed docs are always pdf. + DocumentId = "1" // a label used to reference the doc + }; env.Documents = new List { doc1 }; // create a signer recipient to sign the document, identified by name and email @@ -128,12 +144,13 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s // create a cc recipient to receive a copy of the documents, identified by name and email // We're setting the parameters via setters - CarbonCopy cc1 = new CarbonCopy(); - cc1.Email = ccEmail; - cc1.Name = ccName; - cc1.RoutingOrder = "2"; - cc1.RecipientId = "2"; - + CarbonCopy cc1 = new CarbonCopy + { + Email = ccEmail, + Name = ccName, + RoutingOrder = "2", + RecipientId = "2" + }; // Create signHere fields (also known as tabs) on the documents, // We're using anchor (autoPlace) positioning @@ -144,62 +161,18 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s AnchorUnits = "pixels", AnchorXOffset = "20" }; - ListItem listItem0 = new ListItem - { - Text = "none", - Value = "0" - } - , listItem1 = new ListItem - { - Text = "1", - Value = "1" - } - , listItem2 = new ListItem - { - Text = "2", - Value = "2" - } - , listItem3 = new ListItem - { - Text = "3", - Value = "3" - } - , listItem4 = new ListItem - { - Text = "4", - Value = "4" - } - , listItem5 = new ListItem - { - Text = "5", - Value = "5" - } - , listItem6 = new ListItem - { - Text = "6", - Value = "6" - } - , listItem7 = new ListItem - { - Text = "7", - Value = "7" - } - , listItem8 = new ListItem - { - Text = "8", - Value = "8" - } - , listItem9 = new ListItem - { - Text = "9", - Value = "9" - } - , listItem10 = new ListItem - { - Text = "10", - Value = "10" - }; - + ListItem listItem0 = new ListItem { Text = "none", Value = "0" } + , listItem1 = new ListItem { Text = "1", Value = "1" } + , listItem2 = new ListItem { Text = "2", Value = "2" } + , listItem3 = new ListItem { Text = "3", Value = "3" } + , listItem4 = new ListItem { Text = "4", Value = "4" } + , listItem5 = new ListItem { Text = "5", Value = "5" } + , listItem6 = new ListItem { Text = "6", Value = "6" } + , listItem7 = new ListItem { Text = "7", Value = "7" } + , listItem8 = new ListItem { Text = "8", Value = "8" } + , listItem9 = new ListItem { Text = "9", Value = "9" } + , listItem10 = new ListItem { Text = "10", Value = "10" } + ; List listl1q = new List { Font = "helvetica", @@ -259,24 +232,24 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s Required = "true", Locked = "true", DisableAutoSize = "false", - } - // Formula for the total - , formulal3t = new FormulaTab - { - Font = "helvetica", - Bold = "true", - FontSize = "size12", - AnchorString = "/l3t/", - AnchorYOffset = "-8", - AnchorUnits = "pixels", - AnchorXOffset = "50", - TabLabel = "l3t", - Formula = $"[l1e] + [l2e]", - RoundDecimalPlaces = "0", - Required = "true", - Locked = "true", - DisableAutoSize = "false", - }; + }, + // Formula for the total + formulal3t = new FormulaTab + { + Font = "helvetica", + Bold = "true", + FontSize = "size12", + AnchorString = "/l3t/", + AnchorYOffset = "-8", + AnchorUnits = "pixels", + AnchorXOffset = "50", + TabLabel = "l3t", + Formula = $"[l1e] + [l2e]", + RoundDecimalPlaces = "0", + Required = "true", + Locked = "true", + DisableAutoSize = "false", + }; // Payment line items PaymentLineItem paymentLineIteml1 = new PaymentLineItem diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs index 3b44c25d..eb37e27d 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/EgController.cs @@ -17,6 +17,7 @@ public EgController(DSConfiguration config, IRequestItemsService requestItemsSer ViewBag.csrfToken = ""; } + [HttpGet] public IActionResult Get() { // Check that the token is valid and will remain valid for awhile to enable the From 4c6f4a02ce577037a99651c8345cc11abbaad506 Mon Sep 17 00:00:00 2001 From: LarryKlugerDS Date: Mon, 17 Dec 2018 21:48:08 +0200 Subject: [PATCH 8/9] Added DoWork methods --- .../Eg001EmbeddedSigningController.cs | 58 +++-- .../Eg002SigningViaEmailController.cs | 46 ++-- .../Eg003ListEnvelopesController.cs | 35 ++- .../Eg004EnvelopeInfoController.cs | 34 ++- .../Eg005EnvelopeRecipientsController.cs | 27 ++- .../Eg006EnvelopeDocsController.cs | 71 ++++-- .../Eg007EnvelopeGetDocController.cs | 67 ++++-- .../Eg008CreateTemplateController.cs | 70 ++++-- .../Controllers/Eg009UseTemplateController.cs | 63 +++-- .../Eg010SendBinaryDocsController.cs | 218 ++++++++++-------- .../Eg011EmbeddedSendingController.cs | 67 ++++-- .../Eg012EmbeddedConsoleController.cs | 77 ++++--- .../Eg013AddDocToTemplateController.cs | 71 ++++-- .../Eg014CollectPaymentController.cs | 72 +++--- .../Controllers/HomeController.cs | 1 + 15 files changed, 610 insertions(+), 367 deletions(-) diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs index 2bbd9f69..16f10b45 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg001EmbeddedSigningController.cs @@ -24,32 +24,20 @@ public Eg001EmbeddedSigningController(DSConfiguration config, IRequestItemsServi ViewBag.title = "Embedded Signing Ceremony"; } - [HttpPost] - public IActionResult Create(string signerEmail, string signerName) + private string DoWork(string signerEmail, string signerName, + string accessToken, string basePath, string accountId) { // Data for this method // signerEmail // signerName + // accessToken + // basePath + // accountId + // dsPingUrl -- class global // signerClientId -- class global // dsReturnUrl -- class global - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - - // Check the token with minimal buffer time. - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } // Step 1. Create the envelope definition EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName); @@ -72,7 +60,8 @@ public IActionResult Create(string signerEmail, string signerName) // Don't use an iFrame! // State can be stored/recovered using the framework's session or a // query parameter on the returnUrl (see the makeRecipientViewRequest method) - return Redirect(results1.Url); + string redirectUrl = results1.Url; + return redirectUrl; } private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string signerName) @@ -188,5 +177,36 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName) } public override string EgName => "eg001"; + + [HttpPost] + public IActionResult Create(string signerEmail, string signerName) + { + // Data for this method + // signerEmail + // signerName + // dsPingUrl -- class global + // signerClientId -- class global + // dsReturnUrl -- class global + string accessToken = RequestItemsService.User.AccessToken; + string basePath = RequestItemsService.Session.BasePath + "/restapi"; + string accountId = RequestItemsService.Session.AccountId; + + // Check the token with minimal buffer time. + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + string redirectUrl = DoWork(signerEmail, signerName, accessToken, basePath, accountId); + // Redirect the user to the Signing Ceremony + return Redirect(redirectUrl); + } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs index f13ea63e..710c472b 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg002SigningViaEmailController.cs @@ -1,12 +1,9 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using DocuSign.eSign.Api; using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; -using System.IO; using System.Text; using DocuSign.eSign.Client; @@ -23,28 +20,6 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi public override string EgName => "eg002"; - [HttpPost] - public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) - { - // Check the token with minimal buffer time. - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } - EnvelopeSummary results = DoWork(signerEmail, signerName, ccEmail, ccName); - ViewBag.h1 = "Envelope sent"; - ViewBag.message = "The envelope has been created and sent!
Envelope ID " + results.EnvelopeId + "."; - //return results; - return View("example_done"); - } - public EnvelopeSummary DoWork(string signerEmail, string signerName, string ccEmail, string ccName) { // Data for this method @@ -215,5 +190,26 @@ private byte[] document1(string signerEmail, string signerName, string ccEmail, " " ); } + + [HttpPost] + public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) + { + // Check the token with minimal buffer time. + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + EnvelopeSummary results = DoWork(signerEmail, signerName, ccEmail, ccName); + ViewBag.h1 = "Envelope sent"; + ViewBag.message = "The envelope has been created and sent!
Envelope ID " + results.EnvelopeId + "."; + return View("example_done"); + } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs index bdb019a1..777a474f 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg003ListEnvelopesController.cs @@ -23,13 +23,30 @@ public Eg003ListEnvelopesController(DSConfiguration config, IRequestItemsService public override string EgName => "eg003"; + private EnvelopesInformation DoWork(string accessToken, string basePath, string accountId) + { + // Data for this method + // accessToken + // basePath + // accountId + + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); + ListStatusChangesOptions options = new ListStatusChangesOptions(); + options.fromDate = DateTime.Now.AddDays(-30).ToString("yyyy/MM/dd"); + // Call the API method: + EnvelopesInformation results = envelopesApi.ListStatusChanges(accountId, options); + return results; + } + [HttpPost] public IActionResult Create(string signerEmail, string signerName) { // Data for this method - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; + string accessToken = RequestItemsService.User.AccessToken; + string basePath = RequestItemsService.Session.BasePath + "/restapi"; + string accountId = RequestItemsService.Session.AccountId; // Check the token with minimal buffer time. bool tokenOk = CheckToken(3); @@ -43,14 +60,10 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var config = new Configuration(new ApiClient(basePath)); - config.AddDefaultHeader("Authorization", "Bearer " + accessToken); - EnvelopesApi envelopesApi = new EnvelopesApi(config); - ListStatusChangesOptions options = new ListStatusChangesOptions(); - options.fromDate = DateTime.Now.AddDays(-30).ToString("yyyy/MM/dd"); - // Call the API method: - EnvelopesInformation results = envelopesApi.ListStatusChanges(accountId, options); - + + // Call the worker + EnvelopesInformation results = DoWork(accessToken, basePath, accountId); + // Process results ViewBag.h1 = "List envelopes results"; ViewBag.message = "Results from the Envelopes::listStatusChanges method:"; ViewBag.Locals.Json = JsonConvert.SerializeObject(results,Formatting.Indented); diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs index ca5c423d..9dcdba8d 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg004EnvelopeInfoController.cs @@ -1,9 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using DocuSign.eSign.Api; +using DocuSign.eSign.Api; using DocuSign.eSign.Client; +using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; @@ -21,6 +18,25 @@ public Eg004EnvelopeInfoController(DSConfiguration config, IRequestItemsService public override string EgName => "eg004"; + private Envelope DoWork(string accessToken, string basePath, string accountId, + string envelopeId) + { + // Data for this method + // accessToken + // basePath + // accountId + // envelopeId + + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); + ViewBag.h1 = "Get envelope status results"; + ViewBag.message = "Results from the Envelopes::get method:"; + Envelope results = envelopesApi.GetEnvelope(accountId, envelopeId); + return results; + } + + [HttpPost] public IActionResult Create(string signerEmail, string signerName) { @@ -42,14 +58,12 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var config = new Configuration(new ApiClient(basePath)); - config.AddDefaultHeader("Authorization", "Bearer " + accessToken); - EnvelopesApi envelopesApi = new EnvelopesApi(config); + + Envelope results = DoWork(accessToken, basePath, accountId, envelopeId); + ViewBag.h1 = "Get envelope status results"; ViewBag.message = "Results from the Envelopes::get method:"; - DocuSign.eSign.Model.Envelope results = envelopesApi.GetEnvelope(accountId, envelopeId); ViewBag.Locals.Json = JsonConvert.SerializeObject(results, Formatting.Indented); - return View("example_done"); } } diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs index 3e375055..0815105b 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg005EnvelopeRecipientsController.cs @@ -1,5 +1,6 @@ using DocuSign.eSign.Api; using DocuSign.eSign.Client; +using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; @@ -17,6 +18,24 @@ public Eg005EnvelopeRecipientsController(DSConfiguration config, IRequestItemsSe public override string EgName => "eg005"; + private Recipients DoWork(string accessToken, string basePath, string accountId, + string envelopeId) + { + // Data for this method + // accessToken + // basePath + // accountId + // envelopeId + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); + ViewBag.h1 = "List envelope recipients result"; + ViewBag.message = "Results from the EnvelopeRecipients::list method:"; + Recipients results = envelopesApi.ListRecipients(accountId, envelopeId); + return results; + } + + [HttpPost] public IActionResult Create(string signerEmail, string signerName) { @@ -37,14 +56,8 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var config = new Configuration(new ApiClient(basePath)); - config.AddDefaultHeader("Authorization", "Bearer " + accessToken); - EnvelopesApi envelopesApi = new EnvelopesApi(config); - ViewBag.h1 = "List envelope recipients result"; - ViewBag.message = "Results from the EnvelopeRecipients::list method:"; - var results = envelopesApi.ListRecipients(accountId, envelopeId); + Recipients results = DoWork(accessToken, basePath, accountId, envelopeId); ViewBag.Locals.Json = JsonConvert.SerializeObject(results, Formatting.Indented); - return View("example_done"); } } diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs index a5400fc7..98a68734 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg006EnvelopeDocsController.cs @@ -19,6 +19,49 @@ public Eg006EnvelopeDocsController(DSConfiguration config, IRequestItemsService public override string EgName => "eg006"; + private (EnvelopeDocumentsResult results, EnvelopeDocuments envelopeDocuments) DoWork( + string accessToken, string basePath, string accountId, string envelopeId) + { + // Data for this method + // accessToken + // basePath + // accountId + // envelopeId + + var config = new Configuration(new ApiClient(basePath)); + config.AddDefaultHeader("Authorization", "Bearer " + accessToken); + EnvelopesApi envelopesApi = new EnvelopesApi(config); + EnvelopeDocumentsResult results = envelopesApi.ListDocuments(accountId, envelopeId); + + // Prepare and save the envelopeId and its list of documents in the session so + // they can be used in example 7 (download a document) + List envelopeDocItems = new List + { + new EnvelopeDocItem { Name = "Combined", Type = "content", DocumentId = "combined" }, + new EnvelopeDocItem { Name = "Zip archive", Type = "zip", DocumentId = "archive" } + }; + + foreach (EnvelopeDocument doc in results.EnvelopeDocuments) + { + envelopeDocItems.Add(new EnvelopeDocItem + { + DocumentId = doc.DocumentId, + Name = doc.DocumentId == "certificate" ? "Certificate of completion" : doc.Name, + Type = doc.Type + }); + } + + EnvelopeDocuments envelopeDocuments = new EnvelopeDocuments + { + EnvelopeId = envelopeId, + Documents = envelopeDocItems + }; + + return (results, envelopeDocuments); + + } + + [HttpPost] public IActionResult Create(string signerEmail, string signerName) { @@ -40,34 +83,18 @@ public IActionResult Create(string signerEmail, string signerName) RequestItemsService.EgName = EgName; return Redirect("/ds/mustAuthenticate"); } - var config = new Configuration(new ApiClient(basePath)); - config.AddDefaultHeader("Authorization", "Bearer " + accessToken); - EnvelopesApi envelopesApi = new EnvelopesApi(config); - EnvelopeDocumentsResult result = envelopesApi.ListDocuments(accountId, envelopeId); + + (EnvelopeDocumentsResult results, EnvelopeDocuments envelopeDocuments) = + DoWork(accessToken, basePath, accountId, envelopeId); + // Save the envelopeId and its list of documents in the session so // they can be used in example 7 (download a document) - List envelopeDocItems = new List(); - envelopeDocItems.Add(new EnvelopeDocItem { Name= "Combined", Type= "content", DocumentId = "combined" }); - envelopeDocItems.Add(new EnvelopeDocItem { Name = "Zip archive", Type = "zip", DocumentId = "archive" }); - - foreach (EnvelopeDocument doc in result.EnvelopeDocuments) - { - envelopeDocItems.Add(new EnvelopeDocItem { - DocumentId = doc.DocumentId, - Name = doc.DocumentId == "certificate"? "Certificate of completion" : doc.Name, - Type = doc.Type - }); - } - - EnvelopeDocuments envelopeDocuments = new EnvelopeDocuments(); - envelopeDocuments.EnvelopeId = envelopeId; - envelopeDocuments.Documents = envelopeDocItems; RequestItemsService.EnvelopeDocuments = envelopeDocuments; - + ViewBag.envelopeDocuments = envelopeDocuments; ViewBag.h1 = "List envelope documents result"; ViewBag.message = "Results from the EnvelopeDocuments::list method:"; - ViewBag.Locals.Json = JsonConvert.SerializeObject(result, Formatting.Indented); + ViewBag.Locals.Json = JsonConvert.SerializeObject(results, Formatting.Indented); return View("example_done"); } } diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs index 0f18e95a..08e0cd4b 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg007EnvelopeGetDocController.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using DocuSign.eSign.Api; using DocuSign.eSign.Client; using eg_03_csharp_auth_code_grant_core.Models; @@ -17,29 +18,16 @@ public Eg007EnvelopeGetDocController(DSConfiguration config, IRequestItemsServic public override string EgName => "eg007"; - [HttpPost] - public ActionResult Create(string docSelect) + private FileStreamResult DoWork(string accessToken, string basePath, string accountId, + string envelopeId, List documents, string docSelect) { // Data for this method - // docSelect -- argument - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - var envelopeId = RequestItemsService.EnvelopeId; - // documents data for the envelope. See example EG006 - var documents = RequestItemsService.EnvelopeDocuments.Documents; - - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } + // accessToken + // basePath + // accountId + // envelopeId + // docSelect -- the requested documentId + // documents -- from eg 6 var config = new Configuration(new ApiClient(basePath)); config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); @@ -48,8 +36,10 @@ public ActionResult Create(string docSelect) // Exceptions will be caught by the calling function System.IO.Stream results = envelopesApi.GetDocument(accountId, envelopeId, docSelect); - EnvelopeDocItem docItem = documents.FirstOrDefault(d => docSelect.Equals(d.DocumentId)); + // Step 2. Look up the document from the list of documents + EnvelopeDocItem docItem = documents.FirstOrDefault(d => docSelect.Equals(d.DocumentId)); + // Process results. Determine the file name and mimetype string docName = docItem.Name; bool hasPDFsuffix = docName.ToUpper().EndsWith(".PDF"); bool pdfFile = hasPDFsuffix; @@ -83,5 +73,36 @@ public ActionResult Create(string docSelect) return File(results, mimetype, docName); } + + + + [HttpPost] + public ActionResult Create(string docSelect) + { + // Data for this method + // docSelect -- argument + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + var envelopeId = RequestItemsService.EnvelopeId; + // documents data for the envelope. See example EG006 + List documents = RequestItemsService.EnvelopeDocuments.Documents; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + FileStreamResult result = DoWork(accessToken, basePath, accountId, + envelopeId, documents, docSelect); + return result; + } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs index da343bad..b658ebaf 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg008CreateTemplateController.cs @@ -19,26 +19,16 @@ public Eg008CreateTemplateController(DSConfiguration config, IRequestItemsServic public override string EgName => "eg008"; - [HttpPost] - public IActionResult Create() + // Returns a tuple. See https://stackoverflow.com/a/36436255/64904 + private (bool createdNewTemplate, string templateId, string resultsTemplateName) DoWork( + string accessToken, string basePath, string accountId) { // Data for this method - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; + // accessToken + // basePath + // accountId - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } // Step 1. List templates to see if ours exists already string templateName = "Example Signer and CC template"; var config = new Configuration(new ApiClient(basePath)); @@ -51,7 +41,8 @@ public IActionResult Create() string templateId; string resultsTemplateName; bool createdNewTemplate; - + + // Step 2. Process results if (int.Parse(results.ResultSetSize) > 0) { // Found the template! Record its id @@ -69,14 +60,9 @@ public IActionResult Create() resultsTemplateName = template.Name; createdNewTemplate = true; } - // Save the templateId - RequestItemsService.TemplateId = templateId; - string msg = createdNewTemplate ? - "The template has been created!" : - "The template already exists in your account."; - ViewBag.message = msg + "
Template name: " + resultsTemplateName + ", ID " + templateId + "."; - - return View("example_done"); + + return (createdNewTemplate: createdNewTemplate, + templateId: templateId, resultsTemplateName: resultsTemplateName); } private EnvelopeTemplate MakeTemplate(string resultsTemplateName) @@ -251,5 +237,39 @@ private EnvelopeTemplate MakeTemplate(string resultsTemplateName) return template; } + + [HttpPost] + public IActionResult Create() + { + // Data for this method + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + // Call DoWork. It Returns a tuple. See https://stackoverflow.com/a/36436255/64904 + (bool createdNewTemplate, string templateId, string resultsTemplateName) = DoWork( + accessToken, basePath, accountId); + + // Save the templateId + RequestItemsService.TemplateId = templateId; + string msg = createdNewTemplate ? + "The template has been created!" : + "The template already exists in your account."; + ViewBag.message = msg + "
Template name: " + resultsTemplateName + ", ID " + templateId + "."; + + return View("example_done"); + } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs index 20c95107..b479535e 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg009UseTemplateController.cs @@ -20,38 +20,26 @@ public Eg009UseTemplateController(DSConfiguration config, IRequestItemsService r public override string EgName => "eg009"; - [HttpPost] - public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) + string DoWork (string signerEmail, string signerName, string ccEmail, + string ccName, string accessToken, string basePath, + string accountId, string templateId) { // Data for this method // signerEmail // signerName // ccEmail // ccName - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - var templateId = RequestItemsService.TemplateId; + // accessToken + // basePath + // accountId + // templateId - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } var config = new Configuration(new ApiClient(basePath)); config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName, templateId); - EnvelopeSummary result = envelopesApi.CreateEnvelope(accountId, envelope); - RequestItemsService.EnvelopeId = result.EnvelopeId; - ViewBag.message = "The envelope has been created and sent!
Envelope ID " + result.EnvelopeId + "."; - return View("example_done"); + EnvelopeSummary result = envelopesApi.CreateEnvelope(accountId, envelope); + return result.EnvelopeId; } private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, @@ -81,5 +69,38 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, env.Status = "sent"; return env; } + + [HttpPost] + public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) + { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + var templateId = RequestItemsService.TemplateId; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + string envelopeId = DoWork(signerEmail, signerName, ccEmail, + ccName, accessToken, basePath, accountId, templateId); + + RequestItemsService.EnvelopeId = envelopeId; + ViewBag.message = "The envelope has been created and sent!
Envelope ID " + envelopeId + "."; + return View("example_done"); + } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs index ddee8e54..5bb5c03e 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg010SendBinaryDocsController.cs @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using System.Net; using System.Text; -using DocuSign.eSign.Model; using eg_03_csharp_auth_code_grant_core.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; @@ -13,8 +11,6 @@ namespace eg_03_csharp_auth_code_grant_core.Controllers [Route("eg010")] public class Eg010SendBinaryDocsController : EgController { - private const string V = ""; - public Eg010SendBinaryDocsController(DSConfiguration config, IRequestItemsService requestItemsService) : base(config, requestItemsService) { @@ -23,58 +19,50 @@ public Eg010SendBinaryDocsController(DSConfiguration config, IRequestItemsServic public override string EgName => "eg010"; - [HttpPost] - public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) + // Returns a tuple. See https://stackoverflow.com/a/36436255/64904 + (bool statusOk, string envelopeId, string errorCode, string errorMessage) DoWork( + string signerEmail, string signerName, string ccEmail, + string ccName, string accessToken, string basePath, + string accountId) { // Data for this method // signerEmail // signerName // ccEmail // ccName + // accessToken + // basePath + // accountId // Config.docDocx // Config.docPdf - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } // Step 1. Make the envelope JSON request body dynamic envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName); // Step 2. Gather documents and their headeres // Read files from a local directory // The reads could raise an exception if the file is not available! - dynamic doc1 = envelope.documents[0]; - dynamic doc2 = envelope.documents[1]; - dynamic doc3 = envelope.documents[2]; + dynamic doc1 = envelope["documents"][0]; + dynamic doc2 = envelope["documents"][1]; + dynamic doc3 = envelope["documents"][2]; dynamic documents = new[] { new { mime = "text/html", - filename = (string) doc1.name, - documentId = (string) doc1.documentId, + filename = (string) doc1["name"], + documentId = (string) doc1["documentId"], bytes = Encoding.ASCII.GetBytes(document1(signerEmail, signerName, ccEmail, ccName)) }, new { mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - filename = (string) doc2.name, - documentId = (string) doc2.documentId, + filename = (string) doc2["name"], + documentId = (string) doc2["documentId"], bytes = System.IO.File.ReadAllBytes(Config.docDocx) }, new { mime = "application/pdf", - filename = (string) doc3.name, - documentId = (string) doc3.documentId, + filename = (string) doc3["name"], + documentId = (string) doc3["documentId"], bytes = System.IO.File.ReadAllBytes(Config.docPdf) } }; @@ -103,7 +91,7 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai buffer.Write(Encoding.ASCII.GetBytes("Content-Disposition: form-data")); buffer.Write(CRLF); buffer.Write(CRLF); - + var json = JsonConvert.SerializeObject(envelope, Formatting.Indented); buffer.Write(Encoding.ASCII.GetBytes(json)); // Loop to add the documents. @@ -154,19 +142,22 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai HttpStatusCode code = ((HttpWebResponse)response).StatusCode; dynamic obj = JsonConvert.DeserializeObject(res); - if (code >= HttpStatusCode.OK && code < HttpStatusCode.MultipleChoices) - { - RequestItemsService.EnvelopeId = obj.envelopeId; - ViewBag.h1 = "Envelope sent"; - ViewBag.message = "The envelope has been created and sent!
Envelope ID " + obj.envelopeId + "."; - return View("example_done"); + bool statusOk = code >= HttpStatusCode.OK && code < HttpStatusCode.MultipleChoices; + string envelopeId = null; + string errorCode = null; + string errorMessage = null; + + if (statusOk) + { + envelopeId = obj.envelopeId; } else - { - ViewBag.errorCode = obj.errorCode; - ViewBag.errorMessage = obj.message; - return View("error"); + { + errorCode = obj.errorCode; + errorMessage = obj.message; } + + return (statusOk, envelopeId, errorCode, errorMessage); } private string document1(string signerEmail, string signerName, string ccEmail, string ccName) @@ -201,7 +192,7 @@ private string document1(string signerEmail, string signerName, string ccEmail, " "; } - private object MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName) + private Dictionary MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName) { // Data for this method // signerEmail @@ -221,37 +212,34 @@ private object MakeEnvelope(string signerEmail, string signerName, string ccEmai // After it is signed, a copy is sent to the cc person. // create the envelope definition // add the documents - var doc1 = new + + Dictionary doc1 = new Dictionary() { - name = "Order acknowledgement", // can be different from actual file name - fileExtension = "html", // Source data format. Signed docs are always pdf. - documentId = "1" // a label used to reference the doc + { "name", "Order acknowledgement"}, // can be different from actual file name + { "fileExtension", "html"}, // Source data format. Signed docs are always pdf. + { "documentId", "1"} // a label used to reference the doc }; - - var doc2 = new + Dictionary doc2 = new Dictionary() { - name = "Battle Plan", // can be different from actual file name - fileExtension = "docx", - documentId = "2" + { "name", "Battle Plan"}, // can be different from actual file name + { "fileExtension", "docx" }, + { "documentId", "2" } }; - var doc3 = new + Dictionary doc3 = new Dictionary() { - name = "Lorem Ipsum", // can be different from actual file name - fileExtension = "pdf", - documentId = "3" + { "name", "Lorem Ipsum" }, // can be different from actual file name + { "fileExtension", "pdf" }, + { "documentId", "3" } }; - // create the envelope definition - //env.Documents = new [] { doc1, doc2, doc3 }; - // create a signer recipient to sign the document, identified by name and email // We're setting the parameters via the object creation - Signer signer1 = new Signer + Dictionary signer1 = new Dictionary() { - Email = signerEmail, - Name = signerName, - RecipientId = "1", - RoutingOrder = "1" + { "email", signerEmail }, + { "name", signerName }, + { "recipientId", "1" }, + { "routingOrder", "1" } }; // routingOrder (lower means earlier) determines the order of deliveries // to the recipients. Parallel routing order is supported by using the @@ -259,12 +247,12 @@ private object MakeEnvelope(string signerEmail, string signerName, string ccEmai // create a cc recipient to receive a copy of the documents, identified by name and email // We're setting the parameters via setters - CarbonCopy cc1 = new CarbonCopy + Dictionary cc1 = new Dictionary() { - Email = ccEmail, - Name = ccName, - RoutingOrder = "2", - RecipientId = "2" + { "email", ccEmail }, + { "name", ccName }, + { "routingOrder", "2" }, + { "recipientId", "2" } }; // Create signHere fields (also known as tabs) on the documents, // We're using anchor (autoPlace) positioning @@ -273,49 +261,87 @@ private object MakeEnvelope(string signerEmail, string signerName, string ccEmai // documents for matching anchor strings. So the // signHere2 tab will be used in both document 2 and 3 since they // use the same anchor string for their "signer 1" tabs. - SignHere signHere1 = new SignHere + Dictionary signHere1 = new Dictionary() { - AnchorString = "**signature_1**", - AnchorYOffset = "10", - AnchorUnits = "pixels", - AnchorXOffset = "20" + { "anchorString", "**signature_1**" }, + { "anchorYOffset", "10" }, + { "anchorUnits", "pixels" }, + { "anchorXOffset", "20" } }; - SignHere signHere2 = new SignHere + Dictionary signHere2 = new Dictionary() { - AnchorString = "/sn1/", - AnchorYOffset = "10", - AnchorUnits = "pixels", - AnchorXOffset = "20" + { "anchorString", "/sn1/" }, + { "anchorYOffset", "10" }, + { "anchorUnits", "pixels" }, + { "anchorXOffset", "20" } }; - // Tabs are set per recipient / signer - Tabs signer1Tabs = new Tabs + Dictionary signer1Tabs = new Dictionary() { - SignHereTabs = new List { signHere1, signHere2 } + { "signHereTabs", new dynamic[] { signHere1, signHere2 } } }; - signer1.Tabs = signer1Tabs; + signer1.Add("tabs", signer1Tabs); - // Add the recipients to the envelope object - Recipients recipients = new Recipients + // Recipients holds the different recipient objects as sets of arrays + Dictionary recipients = new Dictionary() { - Signers = new List { signer1 }, - CarbonCopies = new List { cc1 } + { "signers", new dynamic[] { signer1 } }, + { "carbonCopies", new dynamic[] { cc1 } } }; - - dynamic env = new + // create the envelope definition + Dictionary envelopeDefinition = new Dictionary() { - emailSubject = "Please sign this document set", - documents = new[] { doc1, doc2, doc3 }, - recipients = recipients, - // Request that the envelope be sent by setting |status| to "sent". - // To request that the envelope be created as a draft, set to "created" - status = "sent" + { "emailSubject", "Please sign this document set"}, + { "documents", new dynamic[] { doc1, doc2, doc3}}, + { "recipients", recipients }, + { "status", "sent" } }; - //env.Status = "sent"; + return envelopeDefinition; + } + - return env; + [HttpPost] + public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) + { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + (bool statusOk, string envelopeId, string errorCode, string errorMessage) = + DoWork(signerEmail, signerName, ccEmail, ccName, accessToken, basePath, accountId); + + if (statusOk) + { + RequestItemsService.EnvelopeId = envelopeId; + ViewBag.h1 = "Envelope sent"; + ViewBag.message = "The envelope has been created and sent!
Envelope ID " + envelopeId + "."; + return View("example_done"); + } + else + { + ViewBag.errorCode = errorCode; + ViewBag.errorMessage = errorMessage; + return View("error"); + } } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs index ece39e37..d40d25c5 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg011EmbeddedSendingController.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using DocuSign.eSign.Api; using DocuSign.eSign.Client; using DocuSign.eSign.Model; @@ -23,8 +20,9 @@ public Eg011EmbeddedSendingController(DSConfiguration config, IRequestItemsServi public override string EgName => "eg011"; - [HttpPost] - public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string startingView) + private string DoWork(string signerEmail, string signerName, string ccEmail, + string ccName, string accessToken, string basePath, + string accountId, string startingView, string dsReturnUrl) { // Data for this method // signerEmail @@ -32,23 +30,11 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai // ccEmail // ccName // startingView - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - string dsReturnUrl = Config.AppUrl + "/dsReturn"; + // accessToken + // basePath + // accountId + // dsReturnUrl - - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } var config = new Configuration(new ApiClient(basePath)); config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); @@ -69,16 +55,47 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai }; ViewUrl result1 = envelopesApi.CreateSenderView(accountId, envelopeId, viewRequest); // Switch to Recipient and Documents view if requested by the user - String url = result1.Url; + String redirectUrl = result1.Url; Console.WriteLine("startingView: " + startingView); if ("recipient".Equals(startingView)) { - url = url.Replace("send=1", "send=0"); + redirectUrl = redirectUrl.Replace("send=1", "send=0"); + } + return redirectUrl; + } + + [HttpPost] + public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string startingView) + { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // startingView + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + string dsReturnUrl = Config.AppUrl + "/dsReturn"; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); } - Console.WriteLine("Sender view URL: " + url); + string redirectUrl = DoWork(signerEmail, signerName, ccEmail, + ccName, accessToken, basePath, + accountId, startingView, dsReturnUrl); - return Redirect(url); + Console.WriteLine("Sender view URL: " + redirectUrl); + return Redirect(redirectUrl); } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs index 592b30a6..a67b57ca 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg012EmbeddedConsoleController.cs @@ -20,52 +20,37 @@ public Eg012EmbeddedConsoleController(DSConfiguration config, IRequestItemsServi public override string EgName => "eg012"; - [HttpPost] - public IActionResult Create(string startingView) + private string DoWork(string accessToken, string basePath, + string accountId, string startingView, string dsReturnUrl, string envelopeId) { // Data for this method - // signerEmail - // signerName - // ccEmail - // ccName // startingView - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - string dsReturnUrl = Config.AppUrl + "/dsReturn"; - - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } + // accessToken + // basePath + // accountId + // dsReturnUrl + // envelopeId var config = new Configuration(new ApiClient(basePath)); config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); - ConsoleViewRequest viewRequest = MakeConsoleViewRequest(dsReturnUrl, startingView); + ConsoleViewRequest viewRequest = MakeConsoleViewRequest(dsReturnUrl, + startingView, envelopeId); // Step 1. create the NDSE view // Call the CreateSenderView API // Exceptions will be caught by the calling function - ViewUrl results = envelopesApi.CreateConsoleView(accountId, viewRequest); - - Console.WriteLine("NDSE view URL: " + results.Url); - - return Redirect(results.Url); + ViewUrl results = envelopesApi.CreateConsoleView(accountId, viewRequest); + string redirectUrl = results.Url; + return redirectUrl; } - private ConsoleViewRequest MakeConsoleViewRequest(string dsReturnUrl, string startingView) + private ConsoleViewRequest MakeConsoleViewRequest(string dsReturnUrl, string startingView, + string envelopeId) { // Data for this method // dsReturnUrl // startingView - string envelopeId = RequestItemsService.EnvelopeId; + // envelopeId ConsoleViewRequest viewRequest = new ConsoleViewRequest(); // Set the url where you want the recipient to go once they are done @@ -81,5 +66,37 @@ private ConsoleViewRequest MakeConsoleViewRequest(string dsReturnUrl, string sta return viewRequest; } + + + [HttpPost] + public IActionResult Create(string startingView) + { + // Data for this method + // startingView + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + string dsReturnUrl = Config.AppUrl + "/dsReturn"; + string envelopeId = RequestItemsService.EnvelopeId; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + string redirectUrl = DoWork(accessToken, basePath, + accountId, startingView, dsReturnUrl, envelopeId); + + Console.WriteLine("NDSE view URL: " + redirectUrl); + + return Redirect(redirectUrl); + } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs index ca882b5d..51e4f322 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg013AddDocToTemplateController.cs @@ -21,8 +21,9 @@ public Eg013AddDocToTemplateController(DSConfiguration config, IRequestItemsServ public override string EgName => "eg013"; - [HttpPost] - public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) + private string DoWork(string signerEmail, string signerName, string ccEmail, + string ccName, string accessToken, string basePath, + string accountId, string item, string quantity, string dsReturnUrl) { // Data for this method // signerEmail @@ -31,43 +32,30 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai // ccName // item // quantity - // signerClientId -- class global - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - string dsReturnUrl = Config.AppUrl + "/dsReturn"; - - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } + // accessToken + // basePath + // accountId + // dsReturnUrl var config = new Configuration(new ApiClient(basePath)); config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); // Step 1. Make the envelope request body EnvelopeDefinition envelope = MakeEnvelope(signerEmail, signerName, ccEmail, ccName, item, quantity); + // Step 2. call Envelopes::create API method // Exceptions will be caught by the calling function EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelope); - String envelopeId = results.EnvelopeId; - Console.WriteLine("Envelope was created. EnvelopeId " + envelopeId); + // Step 3. create the recipient view, the Signing Ceremony RecipientViewRequest viewRequest = MakeRecipientViewRequest(signerEmail, signerName, dsReturnUrl); ViewUrl results1 = envelopesApi.CreateRecipientView(accountId, envelopeId, viewRequest); - - return Redirect(results1.Url); + return results1.Url; } + private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string signerName, string dsReturnUrl) { @@ -101,7 +89,8 @@ private RecipientViewRequest MakeRecipientViewRequest(string signerEmail, string return viewRequest; } - private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) + private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, + string ccName, string item, string quantity) { // Data for this method // signerEmail @@ -262,5 +251,39 @@ private byte[] document1(string signerEmail, string signerName, string ccEmail, " \n" + " "); } + + + [HttpPost] + public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName, string item, string quantity) + { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + // item + // quantity + // signerClientId -- class global + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + string dsReturnUrl = Config.AppUrl + "/dsReturn"; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + string redirectUrl = DoWork(signerEmail, signerName, ccEmail, + ccName, accessToken, basePath, accountId, item, quantity, dsReturnUrl); + return Redirect(redirectUrl); + } } } \ No newline at end of file diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs index f6fcca83..79ca5457 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/Eg014CollectPaymentController.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; using DocuSign.eSign.Api; using DocuSign.eSign.Client; using DocuSign.eSign.Model; @@ -22,30 +20,17 @@ public Eg014CollectPaymentController(DSConfiguration config, IRequestItemsServic public override string EgName => "eg014"; - [HttpPost] - public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) + private string DoWork(string signerEmail, string signerName, string ccEmail, + string ccName, string accessToken, string basePath, string accountId) { // Data for this method // signerEmail // signerName // ccEmail // ccName - var accessToken = RequestItemsService.User.AccessToken; - var basePath = RequestItemsService.Session.BasePath + "/restapi"; - var accountId = RequestItemsService.Session.AccountId; - - - bool tokenOk = CheckToken(3); - if (!tokenOk) - { - // We could store the parameters of the requested operation - // so it could be restarted automatically. - // But since it should be rare to have a token issue here, - // we'll make the user re-enter the form data after - // authentication. - RequestItemsService.EgName = EgName; - return Redirect("/ds/mustAuthenticate"); - } + // accessToken + // basePath + // accountId var config = new Configuration(new ApiClient(basePath)); config.AddDefaultHeader("Authorization", "Bearer " + accessToken); EnvelopesApi envelopesApi = new EnvelopesApi(config); @@ -56,11 +41,7 @@ public IActionResult Create(string signerEmail, string signerName, string ccEmai // Step 2. call Envelopes::create API method // Exceptions will be caught by the calling function EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelope); - string envelopeId = results.EnvelopeId; - Console.WriteLine("Envelope was created.EnvelopeId " + envelopeId); - ViewBag.h1 = "Envelope sent"; - ViewBag.message = "The envelope has been created and sent!
Envelope ID " + results.EnvelopeId + "."; - return View("example_done"); + return results.EnvelopeId; } private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName) @@ -97,7 +78,7 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s string l1Name = "Harmonica"; int l1Price = 5; string l1Description = $"${l1Price} each" - , l2Name = "Xylophone"; + , l2Name = "Xylophone"; int l2Price = 150; string l2Description = $"${l2Price} each"; int currencyMultiplier = 100; @@ -108,9 +89,9 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s // Substitute values into the HTML // Substitute for: {signerName}, {signerEmail}, {ccName}, {ccEmail} var doc1HTML2 = doc1HTML1.Replace("{signerName}", signerName) - .Replace("{signerEmail}", signerEmail) - .Replace("{ccName}", ccName) - .Replace("{ccEmail}", ccEmail); + .Replace("{signerEmail}", signerEmail) + .Replace("{ccName}", ccName) + .Replace("{ccEmail}", ccEmail); // create the envelope definition EnvelopeDefinition env = new EnvelopeDefinition @@ -312,5 +293,38 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s return env; } + + + [HttpPost] + public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName) + { + // Data for this method + // signerEmail + // signerName + // ccEmail + // ccName + var accessToken = RequestItemsService.User.AccessToken; + var basePath = RequestItemsService.Session.BasePath + "/restapi"; + var accountId = RequestItemsService.Session.AccountId; + + bool tokenOk = CheckToken(3); + if (!tokenOk) + { + // We could store the parameters of the requested operation + // so it could be restarted automatically. + // But since it should be rare to have a token issue here, + // we'll make the user re-enter the form data after + // authentication. + RequestItemsService.EgName = EgName; + return Redirect("/ds/mustAuthenticate"); + } + + string envelopeId = DoWork(signerEmail, signerName, ccEmail, + ccName, accessToken, basePath, accountId); + Console.WriteLine("Envelope was created.EnvelopeId " + envelopeId); + ViewBag.h1 = "Envelope sent"; + ViewBag.message = "The envelope has been created and sent!
Envelope ID " + envelopeId + "."; + return View("example_done"); + } } } diff --git a/eg-03-csharp-auth-code-grant-core/Controllers/HomeController.cs b/eg-03-csharp-auth-code-grant-core/Controllers/HomeController.cs index 49180bc2..fb880b8a 100644 --- a/eg-03-csharp-auth-code-grant-core/Controllers/HomeController.cs +++ b/eg-03-csharp-auth-code-grant-core/Controllers/HomeController.cs @@ -21,6 +21,7 @@ public IActionResult Index() string egName = RequestItemsService.EgName; if (!string.IsNullOrWhiteSpace(egName)) { + RequestItemsService.EgName = null; return Redirect(egName); } From e002e4c6e5145e27eaaae5ef1043d6fc5b1a7f09 Mon Sep 17 00:00:00 2001 From: LarryKlugerDS Date: Mon, 17 Dec 2018 21:59:53 +0200 Subject: [PATCH 9/9] Update to 3.1.3 --- .../eg-03-csharp-auth-code-grant-core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj b/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj index 6ff8f628..e7ae71d8 100644 --- a/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj +++ b/eg-03-csharp-auth-code-grant-core/eg-03-csharp-auth-code-grant-core.csproj @@ -6,7 +6,7 @@ - +