public void Post([FromBody] JObject data)
        {
            _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - [STARTS]");

            // Checking if the request payload is null
            if (data != null)
            {
                _Logger.LogInformation("[" + _ClassName + "]  - POST - api/webhook -  Received Webhook Notification at ");

                var webhookSecret      = _AppSettings.WebhookSecret;
                var requestSignature   = Request.Headers["Elli-Signature"].ToString();
                var requestEnvironment = Request.Headers["Elli-Environment"].ToString();

                _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - Request Elli-Signature - " + requestSignature);
                _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - Request Elli-Environment - " + requestEnvironment);

                // generate the webhook token from the payload and secret using HMACSHA
                var webHookToken = WebHookHelper.GetWebhookNotificationToken(data.ToString(Formatting.None), webhookSecret);

                _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - WebHook Data - " + data.ToString(Formatting.Indented));
                _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - WebHook Token - " + webHookToken);

                // Check if the generated WebHook token is similar to the request signature received in the header.
                if (WebHookHelper.IsValidWebhookToken(requestSignature, webHookToken))
                {
                    var webHookBody = new WebhookNotificationBody()
                    {
                        eventId   = data.GetValue <string>("eventId"),
                        eventTime = data.GetValue <DateTime>("eventTime"),
                        eventType = data.GetValue <string>("eventType"),
                        meta      = data.GetValue <Meta>("meta")
                    };

                    var transactionId = webHookBody.meta != null ? webHookBody.meta.resourceId : string.Empty;

                    _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - Transaction ID - " + transactionId);

                    var partnerAPIWrapper = new PartnerAPIWrapper(this._AppSettings);

                    // executing the Get Request Partner API Call here
                    var requestData = partnerAPIWrapper.GetRequest(transactionId);

                    if (requestData != null)
                    {
                        _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - Get Request Data is not null ");

                        var loanInformation = requestData;

                        // if the requestData is not null then the Partner will validate it against their business rules and if it is valid then they have to build their request here and submit it
                        // the response that they receive from their request will go back into the Partner API as a CreateResponse Partner API call

                        var validationInfo = MockResponseHelper.ValidateLoanData(requestData);

                        if (validationInfo != null && validationInfo["success"] != null)
                        {
                            var response = MockRequestHelper.SubmitToPartner(requestData, transactionId);

                            // This method will build the payload required for Creating Response in the Partner API
                            SubmitAcknowledgementToPartnerAPI(response, loanInformation, transactionId);
                        }
                        else
                        {
                            TransactionStatusCache.Instance.Add(transactionId, validationInfo);
                        }
                    }
                }
                else
                {
                    _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - WebHook Token is Invalid ");
                }
            }

            _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - [ENDS]");
        }
Example #2
0
        /// <summary>
        /// This method will process the webhook request
        /// </summary>
        public void ProcessWebhookRequest()
        {
            var transactionId     = this._WebhookBody.meta != null ? _WebhookBody.meta.resourceId : string.Empty;
            var partnerAPIWrapper = new PartnerAPIWrapper(this._AppSettings);

            _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - Transaction ID - " + transactionId);

            // This is for the Transaction messaging flow. if the eventType is NewMessage then the Lender has sent a message to the Partner and the partner has to retreive it
            if (_WebhookBody.eventType == "NewMessage")
            {
                //Dump message body into cache.

                var resourceRefURL = _WebhookBody.meta.resourceRef;
                var messageId      = resourceRefURL.Substring(resourceRefURL.LastIndexOf('/') + 1, resourceRefURL.Length - resourceRefURL.LastIndexOf('/') - 1);

                var messageBody = partnerAPIWrapper.GetMessage(transactionId, messageId);
                if (messageBody != null)
                {
                    _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - Message is not null ");
                    MessageCache.Instance.Add(transactionId, messageBody.ToString());
                }
            }
            else if (_WebhookBody.eventType == "CreateRequest")
            {
                // executing the Get Request Partner API Call here
                var requestData = partnerAPIWrapper.GetRequest(transactionId);

                if (requestData != null)
                {
                    _Logger.LogInformation("[" + _ClassName + "] - POST - api/webhook - Get Request Data is not null ");

                    var loanInformation = requestData;

                    // if the requestData is not null then the Partner will validate it against their business rules and if it is valid then they have to build their request here and submit it
                    // the response that they receive from their request will go back into the Partner API as a CreateResponse Partner API call

                    var mockResponseHelper = new MockResponseHelper(_AppSettings);
                    var validationInfo     = mockResponseHelper.ValidateLoanData(requestData);

                    if (validationInfo != null && validationInfo["success"] != null)
                    {
                        // This will get the type of Integration (e.g. Appraisal, Flood, Verification etc.)
                        var integrationCategory = _AppSettings.IntegrationType == null ? "Appraisal" : _AppSettings.IntegrationType;

                        var response = _MockRequestHelper.SubmitToPartner(requestData, transactionId);

                        // This method will build and submit the payload required for Creating the response in EPC
                        var responseParser = new ResponseProcessor(_WebhookBody, _Logger, _AppSettings);

                        // For Data & Docs flow, validating the credentials.
                        if (string.Compare(_AppSettings.IntegrationType, "DataDocs", true) == 0)
                        {
                            // use password manager and do validation.
                            if (requestData.SelectToken("$.credentials") != null)
                            {
                                var userName = requestData.GetValueByPath <string>("$.credentials.userName");
                                var password = requestData.GetValueByPath <string>("$.credentials.password");

                                // partner will validate the credentials returned from GetRequest with their system and submit the acknowledgement. here we're validating against a mock username and password.
                                if (userName == "datadocs" && password == "#######")
                                {
                                    responseParser.SubmitAcknowledgementToEPC(response, loanInformation, "Delivered");
                                }
                                else
                                {
                                    responseParser.SubmitAcknowledgementToEPC(response, loanInformation, "Rejected"); // if the credentials are invalid or null then send Rejected status
                                }
                            }
                            else
                            {
                                responseParser.SubmitAcknowledgementToEPC(response, loanInformation, "Rejected"); // if the credentials are invalid or null then send Rejected status
                            }
                        }
                        else
                        {
                            responseParser.SubmitAcknowledgementToEPC(response, loanInformation); // This is for the other categories.
                        }
                    }
                    else
                    {
                        TransactionStatusCache.Instance.Add(transactionId, validationInfo);
                    }
                }
            }
        }