Example #1
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            RockContext rockContext = new RockContext();

            Guid   documentGuid      = "5bb17a5b-1eaa-4e41-82e1-2ee861d26658".AsGuid();
            string signNowInviteLink = "";

            PersonAliasService personAliasService = new PersonAliasService(rockContext);
            BinaryFileService  binaryfileService  = new BinaryFileService(rockContext);
            Person             person             = CurrentPerson;

            string signedDocumentId = System.Web.HttpContext.Current.Request.QueryString["document_id"];

            if (string.IsNullOrEmpty(signNowInviteLink))
            {
                BinaryFile renderedPDF = binaryfileService.Get(documentGuid);

                // Save the file to a temporary place
                string tempFile = Path.GetTempPath() + "VolunteerApplication_" + person.FirstName + person.LastName + ".pdf";

                // Open a FileStream to write to the file:
                using (Stream fileStream = File.OpenWrite(tempFile))
                {
                    renderedPDF.ContentStream.CopyTo(fileStream);
                }

                SignNow signNow        = new SignNow();
                string  snErrorMessage = "";
                String  token          = signNow.GetAccessToken(false, out snErrorMessage);

                JObject result     = SignNowSDK.Document.Create(token, tempFile, false); //Changed from true
                string  documentId = result.Value <string>("id");

                // Get the invite link
                var error = new List <string>();
                signNowInviteLink = signNow.GetInviteLink(documentId, out error);
                string url           = "";
                string newDocumentId = "";
                using (var client = new HttpClient(new HttpClientHandler {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                }))
                {
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", Context.Request.AcceptTypes);
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Context.Request.UserAgent);
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");

                    client.BaseAddress = new Uri(signNowInviteLink);
                    HttpResponseMessage response = client.GetAsync("").Result;
                    url = response.RequestMessage.RequestUri.AbsoluteUri;
                    MatchCollection mc = Regex.Matches(url, "document_id%253D([0-9,a-f]{40})");
                    newDocumentId = mc[0].Groups[1].Value;
                }

                // Delete the file when we are done:
                File.Delete(tempFile);
                JObject createLinkRes = SignNowSDK.Link.Create(token, documentId);
            }
        }
Example #2
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            // If this request isn't coming from a browser it can't be completed).
            if (System.Web.HttpContext.Current == null)
            {
                return(false);
            }

            Guid documentGuid = action.GetWorkflowAttributeValue(GetActionAttributeValue(action, "Document").AsGuid()).AsGuid();
            BinaryFileService binaryfileService = new BinaryFileService(rockContext);

            BinaryFile renderedPDF = binaryfileService.Get(documentGuid);

            // Save the file to a temporary place
            string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            Directory.CreateDirectory(tempDirectory);
            string tempFile = tempDirectory + Path.DirectorySeparatorChar + renderedPDF.FileName;

            // Open a FileStream to write to the file:
            using (Stream fileStream = File.OpenWrite(tempFile))
            {
                renderedPDF.ContentStream.CopyTo(fileStream);
            }

            SignNow signNow        = new SignNow();
            string  snErrorMessage = "";
            String  token          = signNow.GetAccessToken(false, out snErrorMessage);

            if (!string.IsNullOrEmpty(snErrorMessage))
            {
                errorMessages.Add(snErrorMessage);
                return(false);
            }
            JObject result     = SignNowSDK.Document.Create(token, tempFile, true);
            string  documentId = result.Value <string>("id");

            if (string.IsNullOrWhiteSpace(documentId))
            {
                errorMessages.Add("SignNow Document Creation Error: " + result.ToString());
                return(false);
            }
            // Clean up the temporary directory
            Directory.Delete(tempDirectory, true);

            SetWorkflowAttributeValue(action, GetActionAttributeValue(action, "SignNowDocumentId").AsGuid(), documentId);

            var signerEmail    = "guest_signer_" + Guid.NewGuid().ToString() + "@no.reply";
            var signerPassword = Guid.NewGuid().ToString();

            var user = SignNowSDK.User.Create(signerEmail, signerPassword);

            JObject OAuthRes        = SignNowSDK.OAuth2.RequestToken(signerEmail, signerPassword);
            var     userAccessToken = OAuthRes.Value <string>("access_token");

            dynamic dataobject = new
            {
                to = new[]  {
                    new {
                        email   = signerEmail,
                        role    = GetAttributeValue(action, "SignerRole"),
                        role_id = "",
                        order   = 1
                    }
                },
                from = "*****@*****.**"
            };


            // Get the invite link
            var generated = SignNowSDK.Document.Invite(token, documentId, dataobject, DisableEmail: true);

            var signNowInviteLink = string.Format(
                "https://signnow.com/dispatch?route=fieldinvite&document_id={0}&access_token={1}&mobileweb=mobileweb_only",
                documentId,
                userAccessToken);
            var mergeFields = GetMergeFields(action);
            var redirectUri = GetAttributeValue(action, "RedirectUri").ResolveMergeFields(mergeFields);

            if (!string.IsNullOrWhiteSpace(redirectUri))
            {
                redirectUri       += string.Format("{0}document_id={1}", redirectUri.Contains("?") ? "&" : "?", documentId);
                signNowInviteLink += "&redirect_uri=" + HttpUtility.UrlEncode(redirectUri);
            }

            SetWorkflowAttributeValue(action, GetActionAttributeValue(action, "SignNowInviteLink").AsGuid(), signNowInviteLink);
            SetWorkflowAttributeValue(action, GetActionAttributeValue(action, "SignNowDocumentId").AsGuid(), documentId);
            return(true);
        }
Example #3
0
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            // Check to see if the action's activity does not yet have the the 'InviteLink' attribute.
            string signNowDocumentId = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "SignNowDocumentId").AsGuid());

            if (string.IsNullOrEmpty(signNowDocumentId))
            {
                errorMessages.Add("A sign now document is required to complete this action");
                return(false);
            }

            Guid documentGuid = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "Document").AsGuid()).AsGuid();


            PersonAliasService personAliasService = new PersonAliasService(rockContext);
            BinaryFileService  binaryfileService  = new BinaryFileService(rockContext);


            SignNow signNow        = new SignNow();
            string  snErrorMessage = "";
            string  token          = signNow.GetAccessToken(false, out snErrorMessage);

            if (!string.IsNullOrEmpty(snErrorMessage))
            {
                errorMessages.Add(snErrorMessage);
                return(false);
            }

            //Check if document is signed.
            JObject document = SignNowSDK.Document.Get(token, signNowDocumentId);

            if ((( JArray )document["signatures"]).Count() > 0)
            {
                // Download the file
                string tempPath     = Path.GetTempPath();
                string tempFileName = (String)document["document_name"];
                var    result       = SignNowSDK.Document.Download(token, signNowDocumentId, tempPath, tempFileName);

                // Put it into the workflow attribute
                BinaryFile signedPDF = binaryfileService.Get(documentGuid);
                if (signedPDF == null)
                {
                    signedPDF = new BinaryFile();
                    // TODO: This probably shouldn't be hardcoded
                    signedPDF.MimeType    = "application/pdf";
                    signedPDF.FileName    = tempFileName;
                    signedPDF.IsTemporary = false;
                    binaryfileService.Add(signedPDF);

                    // Update the file type if necessary
                    Guid binaryFileTypeGuid = Guid.Empty;

                    var destinationAttribute    = AttributeCache.Get(GetActionAttributeValue(action, "Document").AsGuid(), rockContext);
                    var binaryFileTypeQualifier = destinationAttribute.QualifierValues["binaryFileType"];
                    if (!String.IsNullOrWhiteSpace(binaryFileTypeQualifier.Value))
                    {
                        if (binaryFileTypeQualifier.Value != null)
                        {
                            binaryFileTypeGuid = binaryFileTypeQualifier.Value.AsGuid();

                            signedPDF.BinaryFileTypeId = new BinaryFileTypeService(rockContext).Get(binaryFileTypeGuid).Id;
                        }
                    }
                    signedPDF.DatabaseData         = new BinaryFileData();
                    signedPDF.DatabaseData.Content = File.ReadAllBytes(tempPath + tempFileName + ".pdf");

                    rockContext.SaveChanges();

                    // Now store the attribute
                    if (destinationAttribute.EntityTypeId == new Workflow().TypeId)
                    {
                        action.Activity.Workflow.SetAttributeValue(destinationAttribute.Key, signedPDF.Guid.ToString());
                    }
                    else if (destinationAttribute.EntityTypeId == new WorkflowActivity().TypeId)
                    {
                        action.Activity.SetAttributeValue(destinationAttribute.Key, signedPDF.Guid.ToString());
                    }
                }
                else
                {
                    signedPDF.FileName             = tempFileName;
                    signedPDF.DatabaseData.Content = File.ReadAllBytes(tempPath + tempFileName + ".pdf");
                }

                // Delete the file when we are done:
                File.Delete(tempPath + tempFileName);

                // We have a signed copy
                SetWorkflowAttributeValue(action, GetActionAttributeValue(action, "PDFSigned").AsGuid(), "True");
            }
            else
            {
                // Not signed yet.
                SetWorkflowAttributeValue(action, GetActionAttributeValue(action, "PDFSigned").AsGuid(), "False");
            }
            return(true);
        }