Send E-Signature Link Using SignNow.Net API

About SignNow.Net API

SignNow.Net is .NET 4.5+ and .NET Standard class library for the SignNow API. SignNow allows you to Integrate eSignatures into your apps. It allows you to send documents for signature directly from your system or website, track your request’s status, and download the copies of signed documents in your system.

Create API Keys

Before the start with SignNow.Net, you need to create API keys. So for this register your email here https://www.signnow.com/developers/features/send-a-group-of-documents-for-signing-using-api

Register Your Email Here

After that log in with your credentials and on your dashboard click on the API button.

Click on API button

 

 

 

 

 

 

 

 

Here you can add your app and after clicking on the edit button you can see your client id and client secret.

API Keys

 

 

 

 

 

 

 

 

After getting the API keys you need a Document that you want to send to the customer for the signing.

Here is my Demo Document

Here, In your document you need to add this block of text {{t:s;r:y;o:”Customer”;w:130;h:30;}} to where you want sign. And put this document to your solution under the Document directory.

Demo of Document

The Coding Part

Before starting the coding you need to add the below class file into your system under the Model directory. Here I’m providing the zip file of Model directory

Models

Web.config File

Add the below keys in your web.config file.

<add key="SenderEmailAddress" value="Sender_Emai_Address"/>
<add key="SenderEmailPassword" value="Sender_Email_Password"/>
<add key="SignNowBaseURL" value="https://api-eval.signnow.com" />
<add key="SignNowRedirectUrl" value="https://eval.signnow.com/dispatch?route=fieldinvite" />
<add key="SNEmail" value="Signnow_Email" />
<add key="SNPass" value="Signnow_Password" />
<add key="SignNowUserName" value="Signnow_Email" />
<add key="SignNowPassword" value="Signnow_Password" />
<add key="SNClient" value="Client_Id" />
<add key="SNSecret" value="Client_Secret" />

Index.cshtml View

In this view, I have put the button for sending the sign link. I have made an Ajax call on button click which calls the AddSignature function from the controller.

<div class="jumbotron">
<h1>Signnow Demo</h1>
<p class="lead">Click the below button to send Sign link to the any given email.</p>
<p><button href="#" id="SignDoc" class="btn btn-primary btn-lg">Send Sign Link</button></p>
</div>
Add the below script section in your view
$(document).ready(function () {
  $('#SignDoc').click(function () {

    $.ajax({
       type: "POST",
       url: "/Home/AddSignature",
       dataType: "json",
       success: function (res) {
         if (res.IsSuccess) {
            alert("Check Your Mail.....");
         }
       },
       error: function (error) {
           alert("Something wnt wrong");
       }
    });
  });
});

Email template

I have made a view SendSignLink.cshtml which I’m using to send email to the customer. Add the below code in your SendSignLink.cshtml view. Here I’m replacing {{DocumentLink}} with the SignNow link which will be generated using SignNow API.

<body style="background-color: #F2F2F2;">
<div style="margin-left: auto;margin-right: auto;max-width: 650px;float: none;background-color: #FFF;padding:5%;margin-top:10%">
<table align="center" width="100%" border="0" cellpadding="0" cellspacing="0" style="font-family:Calibri;margin: 0 auto;width: auto;box-shadow: rgba(0, 0, 0, 0.5) 0px 2px 5px;">
<tr style="background: #2f5597; color: white;">
<td valign="top" style="text-align:center; padding: 20px;">
<h2>
Click on the below button<br />
to sign the Document.
</h2>
<br />
<table align="center">
<tr>
<td style="border:1px solid #ffffff;padding: 10px 15px;text-align:center;" align="center">
<a href="{{DocumentLink}}" style="display: inline-block;text-decoration:none;color:#fff;">Sign Doc</a>
<br />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>

SignStatus.cshtml View

Here, in this view, I’m showing the document status like the document is signed or not.

@if (ViewBag.Status == true)
{
<h2>Document signed successfully and saved..</h2>
<p>Check your <b>Document</b> folder</p>
}
else
{
<h2>Something went wrong..</h2>
}

Here is My Controller Code

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web.Mvc;
using static SignnowDemo.Models.Custom;
using static SignnowDemo.Models.Enum;

namespace SignnowDemo.Controllers
{
    public class HomeController : Controller
    {
        string Client = ConfigurationManager.AppSettings["SNClient"].ToString();
        string Secret = ConfigurationManager.AppSettings["SNSecret"].ToString();
        string Email = ConfigurationManager.AppSettings["SNEmail"].ToString();
        string Pass = ConfigurationManager.AppSettings["SNPass"].ToString();
        string SignNowUserName = ConfigurationManager.AppSettings["SignNowUserName"].ToString();
        string SignNowPassword = ConfigurationManager.AppSettings["SignNowPassword"].ToString();
        string SignNowRedirectUrl = ConfigurationManager.AppSettings["SignNowRedirectUrl"].ToString();
        string SignNowBaseURL = ConfigurationManager.AppSettings["SignNowBaseURL"].ToString();
        
        public ActionResult Index()
        {
            return View();
        }
        //Generate Sign link
        public JsonResult AddSignature()
        {
            bool Status = false;
            string httpslink = string.Empty;
            try
            {
                SignNow.Config.init(Client, Secret, SignNowBaseURL);
                JObject OAuthRes = SignNow.OAuth2.RequestToken(Email, Pass);
                string AccessToken = OAuthRes.GetValue("access_token").ToString();
                JObject OAuthResSigner = SignNow.OAuth2.RequestToken(SignNowUserName, SignNowPassword);
                string AccessTokenSigner = OAuthResSigner.GetValue("access_token").ToString();
                string fullname = "Chand Dakhara";
                JObject SetSignerName = SignNow.User.Update(AccessTokenSigner, fullname, ""); //TODO PUT THE CUSTOMERS'S REAL NAME HERE
                JObject result2 = SignNow.User.TurnOffReusableSignatures(AccessTokenSigner);
                // CREATE DOCUMENT
                JObject newDocRes = SignNow.Document.Create(AccessToken, Server.MapPath("~/Document/SignNowDemoPDF.pdf"), true);
                string docId = newDocRes.GetValue("id").ToString();

                // CREATE INVITE TO SIGN UPLOADED DOCUMENT
                SignNowInviteObject signNowInviteObject = new SignNowInviteObject();

                List<SignNowTo> signNowTos = new List<SignNowTo>();
                SignNowTo signNow = new SignNowTo();
                signNow.email = SignNowUserName;
                signNow.role = "Customer";
                signNow.order = 1;
                signNow.role_id = "";

                signNowTos.Add(signNow);

                signNowInviteObject.to = signNowTos;
                signNowInviteObject.from = "Your_Signnow_Email";
                signNowInviteObject.cc = new List<string>();
                signNowInviteObject.subject = "Please Sign";
                signNowInviteObject.message = "Please Sign";

                var result = JsonConvert.SerializeObject(signNowInviteObject);
                JObject jobj = JObject.Parse(result);
                JObject inviteRes = SignNow.Document.Invite(AccessToken, docId, jobj, "JSON", true);

                JObject OAuthResSigner2 = SignNow.OAuth2.RequestTokenLimitedScopeDocument(SignNowUserName, SignNowPassword, docId);

                string AccessTokenSignerRestricted = OAuthResSigner2.GetValue("access_token").ToString();

                string PageToGoToAftersigning = $"http://{HttpContext.Request.Url.Authority}/Home/SignNowResponse?param={docId}";

                httpslink = $"{SignNowRedirectUrl}&document_id={docId}&access_token={AccessTokenSignerRestricted}&disable_email=true&redirect_uri={WebUtility.UrlEncode(PageToGoToAftersigning)}&mobileweb=mobileweb_only";

                JObject createWebhookRes = SignNow.Webhook.Create(AccessToken, "invite.update", $"{HttpContext.Request.Url.Host}/Home/SignNowResponse");
                JObject createLinkRes = SignNow.Link.Create(AccessToken, docId);
                JObject downloadLinkRes = SignNow.Document.Share(AccessToken, docId);

                StreamReader objStreamReader;
                string path = Server.MapPath("~/Templates/SendSignLink.html");
                if (!string.IsNullOrEmpty(path))
                {
                    objStreamReader = System.IO.File.OpenText(path);
                    string emailText = objStreamReader.ReadToEnd();
                    objStreamReader.Close();
                    objStreamReader = null;

                    emailText = emailText.Replace("{{DocumentLink}}", httpslink);

                    SendMail("chanddakhara.vision@gmail.com", "Sign The Doc", emailText);
                }
                Status = true;
            }
            catch (Exception ex)
            {
                Status = false;
            }
            return Json(new { IsSuccess = Status }, JsonRequestBehavior.AllowGet);
        }
        //Send Email
        public static void SendMail(string to, string subject, string msg)
        {
            try
            {
                string SenderEmailAddress = System.Configuration.ConfigurationManager.AppSettings["SenderEmailAddress"];
                string SenderEmailPassword = System.Configuration.ConfigurationManager.AppSettings["SenderEmailPassword"];

                ServicePointManager.Expect100Continue = true;
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

                MailMessage message = new MailMessage();
                message.To.Add(new MailAddress(to));
                message.From = new MailAddress(SenderEmailAddress, "Chand Dakhara");
                message.Subject = subject;
                message.Body = msg;
                message.IsBodyHtml = true;

                SmtpClient client = new SmtpClient();
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                System.Net.NetworkCredential nc = new System.Net.NetworkCredential(SenderEmailAddress, SenderEmailPassword);
                client.EnableSsl = true;
                client.Credentials = nc;
                client.Send(message);
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }
        //Signnow response 
        public ActionResult SignNowResponse(string param)
        {
            bool Status = false;
            try
            {
                string document_id = param;

                SignNow.Config.init(Client, Secret, SignNowBaseURL);

                JObject OAuthRes = SignNow.OAuth2.RequestToken(Email, Pass);
                string AccessToken = OAuthRes.GetValue("access_token").ToString();
                JObject OAuthResSigner = SignNow.OAuth2.RequestToken(SignNowUserName, SignNowPassword);
                string AccessTokenSigner = OAuthResSigner.GetValue("access_token").ToString();

                // DOWNLOAD SIGNED DOCUMENT
                var RequestDocument = SignNow.Document.Get(AccessToken, document_id);
                bool IsSignDoc = RequestDocument["field_invites"][0]["status"] == DocumentSignStatus.pending.ToString();

                if (IsSignDoc)
                {
                    Status = false;
                }
                else
                {
                    Status = true;
                    JObject downloadDocRes = SignNow.Document.Download(AccessToken, document_id, Server.MapPath("~/Document/"), "SignedDoc.pdf");
                }
            }
            catch (Exception ex)
            {
                Status = false;
            }
            return RedirectToAction("SignStatus", new { Status = Status });
        }
        public ActionResult SignStatus(bool Status)
        {
            ViewBag.Status = Status;
            return View();
        }
    }
}

Final Output

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories