Skip to content

Commit

Permalink
Merge pull request #2 from docusign/pr1
Browse files Browse the repository at this point in the history
SDK v3.1.3, DoWork methods, and create SDK obj for each example
  • Loading branch information
LarryKlugerDS authored Dec 18, 2018
2 parents 0c28003 + e002e4c commit 0c2f258
Show file tree
Hide file tree
Showing 19 changed files with 1,065 additions and 478 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
namespace eg_03_csharp_auth_code_grant_core
{
public interface IRequestItemsService
{
ApiClient DefaultApiClient { get; }

Configuration DefaultConfiguration { get; }
{
string EgName { get; set; }

Session Session { get; set; }
Expand Down
39 changes: 2 additions & 37 deletions eg-03-csharp-auth-code-grant-core/Common/RequestItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +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 CONFIG_KEY = "{0}_DocusignConfig";
private string BASE_URI = "https://demo.docusign.net/restapi";
private readonly string _id;
private string _accessToken;

public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache)
Expand All @@ -30,38 +27,6 @@ public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCach
}
}

public ApiClient DefaultApiClient
{
get
{
var key = string.Format(API_CLIENT_KEY, _id);
ApiClient apiClient = _cache.Get<ApiClient>("apiClient");
if (apiClient == null)
{
apiClient = new ApiClient(BASE_URI);
_cache.Set(key, apiClient);
}

return apiClient;
}
}

public Configuration DefaultConfiguration
{
get
{
var key = string.Format(CONFIG_KEY, _id);
var docuSignConfig = _cache.Get<Configuration>(key);

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);
Expand All @@ -70,7 +35,7 @@ public string EgName {
get => _cache.Get<string>(GetKey("EgName"));
set => _cache.Set(GetKey("EgName"), value);
}

public Session Session {
get => _cache.Get<Session>(GetKey("Session"));
set => _cache.Set(GetKey("Session"), value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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;
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
Expand All @@ -25,19 +24,28 @@ 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)
{
var session = RequestItemsService.Session;
var user = RequestItemsService.User;
// Data for this method
// signerEmail
// signerName
// accessToken
// basePath
// accountId

// dsPingUrl -- class global
// signerClientId -- class global
// dsReturnUrl -- class global

// 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);
EnvelopeSummary results = envelopesApi.CreateEnvelope(session.AccountId, envelope);

// Step 2. Call DocuSign to create the envelope
var config = new Configuration(new ApiClient(basePath));
config.AddDefaultHeader("Authorization", "Bearer " + accessToken);
EnvelopesApi envelopesApi = new EnvelopesApi(config);
EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelope);
string envelopeId = results.EnvelopeId;

// Save for future use within the example launcher
Expand All @@ -46,17 +54,26 @@ 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!
// 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)
{
// 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.
Expand Down Expand Up @@ -93,8 +110,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();
Expand Down Expand Up @@ -153,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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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;

namespace eg_03_csharp_auth_code_grant_core.Controllers
{
Expand All @@ -22,21 +20,38 @@ public Eg002SigningViaEmailController(DSConfiguration config, IRequestItemsServi

public override string EgName => "eg002";

[HttpPost]
public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName)
{
public EnvelopeSummary DoWork(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;

EnvelopeDefinition env = MakeEnvelope(signerEmail, signerName, ccEmail, ccName);
EnvelopesApi envelopesApi = new EnvelopesApi(RequestItemsService.DefaultConfiguration);
EnvelopeSummary results = envelopesApi.CreateEnvelope(RequestItemsService.Session.AccountId, env);
var config = new Configuration(new ApiClient(basePath));
config.AddDefaultHeader("Authorization", "Bearer " + accessToken);
EnvelopesApi envelopesApi = new EnvelopesApi(config);
EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, env);
RequestItemsService.EnvelopeId = results.EnvelopeId;
ViewBag.h1 = "Envelope sent";
ViewBag.message = "The envelope has been created and sent!<br />Envelope ID " + results.EnvelopeId + ".";
//return results;
return View("example_done");
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/
Expand All @@ -53,6 +68,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;
Expand All @@ -65,16 +82,13 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
FileExtension = "docx",
DocumentId = "2"
};

Document doc3 = new Document
{
DocumentBase64 = doc3PdfBytes,
Name = "Lorem Ipsum", // can be different from actual file name
FileExtension = "pdf",
DocumentId = "3"
};


// The order in the docs array determines the order in the envelope
env.Documents = new List<Document> { doc1, doc2, doc3};

Expand Down Expand Up @@ -124,12 +138,10 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
AnchorXOffset = "20"
};


// Tabs are set per recipient / signer
Tabs signer1Tabs = new Tabs {
SignHereTabs = new List<SignHere> { signHere1, signHere2}
};

signer1.Tabs = signer1Tabs;

// Add the recipients to the envelope object
Expand All @@ -138,9 +150,7 @@ private EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, s
Signers = new List<Signer> { signer1 },
CarbonCopies = new List<CarbonCopy> { 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;
Expand All @@ -150,6 +160,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(
" <!DOCTYPE html>\n" +
" <html>\n" +
Expand All @@ -174,5 +190,26 @@ private byte[] document1(string signerEmail, string signerName, string ccEmail,
" </html>"
);
}

[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!<br />Envelope ID " + results.EnvelopeId + ".";
return View("example_done");
}
}
}
Loading

0 comments on commit 0c2f258

Please sign in to comment.