Пример #1
0
 public DocuSignHandlerService(
     IOptionsMonitor <ILog> optionsMonitor,
     TenantExtra tenantExtra,
     DocuSignHelper docuSignHelper,
     SecurityContext securityContext,
     NotifyClient notifyClient)
 {
     TenantExtra     = tenantExtra;
     DocuSignHelper  = docuSignHelper;
     SecurityContext = securityContext;
     NotifyClient    = notifyClient;
     Log             = optionsMonitor.CurrentValue;
 }
Пример #2
0
 public DocuSignHandler(
     RequestDelegate next,
     IOptionsMonitor <ILog> optionsMonitor,
     TenantExtra tenantExtra,
     DocuSignHelper docuSignHelper,
     SecurityContext securityContext,
     NotifyClient notifyClient)
 {
     Next            = next;
     TenantExtra     = tenantExtra;
     DocuSignHelper  = docuSignHelper;
     SecurityContext = securityContext;
     NotifyClient    = notifyClient;
     Log             = optionsMonitor.CurrentValue;
 }
Пример #3
0
        private void Webhook(HttpContext context)
        {
            Log.Info("DocuSign webhook: " + context.Request.QueryString);
            try
            {
                var xmldoc = new XmlDocument();
                xmldoc.Load(context.Request.Body);
                Log.Info("DocuSign webhook outerXml: " + xmldoc.OuterXml);

                var mgr = new XmlNamespaceManager(xmldoc.NameTable);
                mgr.AddNamespace(XmlPrefix, "http://www.docusign.net/API/3.0");

                var envelopeStatusNode = GetSingleNode(xmldoc, "DocuSignEnvelopeInformation/" + XmlPrefix + ":EnvelopeStatus", mgr);
                var envelopeId         = GetSingleNode(envelopeStatusNode, "EnvelopeID", mgr).InnerText;
                var subject            = GetSingleNode(envelopeStatusNode, "Subject", mgr).InnerText;

                var statusString = GetSingleNode(envelopeStatusNode, "Status", mgr).InnerText;
                if (!Enum.TryParse(statusString, true, out DocuSignStatus status))
                {
                    throw new Exception("DocuSign webhook unknown status: " + statusString);
                }

                Log.Info("DocuSign webhook: " + envelopeId + " " + subject + " " + status);

                var customFieldUserIdNode = GetSingleNode(envelopeStatusNode, "CustomFields/" + XmlPrefix + ":CustomField[" + XmlPrefix + ":Name='" + DocuSignHelper.UserField + "']", mgr);
                var userIdString          = GetSingleNode(customFieldUserIdNode, "Value", mgr).InnerText;
                Auth(userIdString);

                switch (status)
                {
                case DocuSignStatus.Completed:

                    var documentStatuses = GetSingleNode(envelopeStatusNode, "DocumentStatuses", mgr);
                    foreach (XmlNode documentStatus in documentStatuses.ChildNodes)
                    {
                        try
                        {
                            var documentId   = GetSingleNode(documentStatus, "ID", mgr).InnerText;
                            var documentName = GetSingleNode(documentStatus, "Name", mgr).InnerText;

                            string folderId    = null;
                            string sourceTitle = null;

                            var documentFiels = GetSingleNode(documentStatus, "DocumentFields", mgr, true);
                            if (documentFiels != null)
                            {
                                var documentFieldFolderNode = GetSingleNode(documentFiels, "DocumentField[" + XmlPrefix + ":Name='" + FilesLinkUtility.FolderId + "']", mgr, true);
                                if (documentFieldFolderNode != null)
                                {
                                    folderId = GetSingleNode(documentFieldFolderNode, "Value", mgr).InnerText;
                                }
                                var documentFieldTitleNode = GetSingleNode(documentFiels, "DocumentField[" + XmlPrefix + ":Name='" + FilesLinkUtility.FileTitle + "']", mgr, true);
                                if (documentFieldTitleNode != null)
                                {
                                    sourceTitle = GetSingleNode(documentFieldTitleNode, "Value", mgr).InnerText;
                                }
                            }

                            var file = DocuSignHelper.SaveDocument(envelopeId, documentId, documentName, folderId);

                            NotifyClient.SendDocuSignComplete(file, sourceTitle ?? documentName);
                        }
                        catch (Exception ex)
                        {
                            Log.Error("DocuSign webhook save document: " + documentStatus.InnerText, ex);
                        }
                    }
                    break;

                case DocuSignStatus.Declined:
                case DocuSignStatus.Voided:
                    var statusFromResource = status == DocuSignStatus.Declined
                                                     ? FilesCommonResource.DocuSignStatusDeclined
                                                     : FilesCommonResource.DocuSignStatusVoided;
                    NotifyClient.SendDocuSignStatus(subject, statusFromResource);
                    break;
                }
            }
            catch (Exception e)
            {
                Log.Error("DocuSign webhook", e);

                throw new HttpException((int)HttpStatusCode.BadRequest, e.Message);
            }
        }