forked from docusign/code-examples-csharp
/
Eg03CreateNewClickwrapVersionController.cs
110 lines (96 loc) · 4.05 KB
/
Eg03CreateNewClickwrapVersionController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using DocuSign.Click.Api;
using DocuSign.Click.Client;
using DocuSign.Click.Model;
using DocuSign.CodeExamples.Common;
using DocuSign.CodeExamples.Controllers;
using DocuSign.CodeExamples.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace DocuSign.CodeExamples.Click.Controllers
{
[Area("Click")]
[Route("[area]/Eg05")]
public class Eg05CreateNewClickwrapVersionController : EgController
{
public Eg05CreateNewClickwrapVersionController(
DSConfiguration dsConfig,
IRequestItemsService requestItemsService)
: base(dsConfig, requestItemsService)
{
}
public override string EgName => "Eg05";
[MustAuthenticate]
[Route("Create")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create()
{
// Step 1. Obtain your OAuth token
var accessToken = RequestItemsService.User.AccessToken;
var basePath = $"{RequestItemsService.Session.BasePath}/clickapi"; // Base API path
// Step 2: Construct your API headers
var apiClient = new ApiClient(basePath);
apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
var clickAccountApi = new AccountsApi(apiClient);
var accountId = RequestItemsService.Session.AccountId;
try
{
if (string.IsNullOrEmpty(RequestItemsService.ClickwrapId))
{
ViewBag.errorCode = 400;
ViewBag.errorMessage = "Cannot find any clickwrap. Please first create a clickwrap using the first example.";
return View("Error");
}
// Step 3: Construct the request body for your new clickwrap version
ClickwrapRequest clickwrapRequest = BuildUpdateClickwrapVersionRequest();
var clickwrapId = RequestItemsService.ClickwrapId;
// Step 4: Call the Click API to create the clickwrap version
var clickWrap = clickAccountApi.CreateClickwrapVersion(accountId, clickwrapId, clickwrapRequest);
//Show results
ViewBag.h1 = "A new clickwrap version was successfully created";
ViewBag.message = $"The new clickwrap version was created! Clickwrap ID: {clickWrap.ClickwrapId}, Name: {clickWrap.ClickwrapName}.";
ViewBag.Locals.Json = JsonConvert.SerializeObject(clickWrap, Formatting.Indented);
return View("example_done");
}
catch (ApiException apiException)
{
ViewBag.errorCode = apiException.ErrorCode;
ViewBag.errorMessage = apiException.Message;
return View("Error");
}
}
private static ClickwrapRequest BuildUpdateClickwrapVersionRequest()
{
var clickwrapRequest = new ClickwrapRequest
{
Name = "Terms of Service",
DisplaySettings = new DisplaySettings()
{
DisplayName = "Terms of Service v2",
MustRead = true,
MustView = false,
RequireAccept = false,
Downloadable = false,
SendToEmail = false,
ConsentButtonText = "I Agree",
Format = "modal",
DocumentDisplay = "document",
},
Documents = new List<Document>(){
new Document()
{
DocumentBase64=Convert.ToBase64String(System.IO.File.ReadAllBytes("Terms_of_service.pdf")),
DocumentName="Terms of Service",
FileExtension="pdf",
Order= 0
}
},
Status = "active",
RequireReacceptance = true
};
return clickwrapRequest;
}
}
}