/// <summary> /// This method will process and return Mocked Verification Response for EPC /// </summary> /// <param name="response"></param> /// <param name="loanInformation"></param> private JArray ProcessVerificationResponse(JObject response, JToken loanInformation) { _Logger.LogInformation("[" + _ClassName + "] - ProcessVerificationResponse - [STARTS]"); JToken result = response.SelectToken("$.result"); string orderId = ""; var random = new System.Random(); JToken tokenoptions = loanInformation.SelectToken("$.product.options"); // building payload for Partner API Create Response dynamic responsePayload = new JObject(); dynamic orders = new JArray(); dynamic order = null; //var orderDate = DateTime.Now.ToUniversalTime(); var orderDate = DateTime.Now; if (tokenoptions != null) { foreach (JProperty prop in tokenoptions) { string sProductName = ""; if (!string.IsNullOrEmpty(prop.Name) && _MockRequestHelper.IsValidProduct(prop.Name)) { sProductName = _MockRequestHelper.GetProductDescription(prop.Name); } if (!string.IsNullOrEmpty(sProductName)) { _Logger.LogInformation("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - ProductName is " + sProductName); orderId = random.Next(10000, 80000).ToString(); OrderInformation orderInfo = new OrderInformation() { TransactionId = _WebhookBody.meta != null ? _WebhookBody.meta.resourceId : string.Empty, ProductName = sProductName, ProductCode = prop.Name.ToString(), OrderId = orderId.ToString(), OrderStatus = response.SelectToken("$.status").ToString(), LoanInformation = loanInformation }; TransactionInformationCache.Instance.Add(orderInfo, true); order = new JObject(); order.id = orderId.ToString(); order.orderDateTime = orderDate.ToString("yyyy-MM-ddTHH:mm-ss:ff"); // result["orderDate"]; order.orderStatus = response["status"]; order.orderMessage = result["orderMessage"]; order.product = sProductName; order.documents = new JArray(); orders.Add(order); } } } _Logger.LogInformation("[" + _ClassName + "] - ProcessVerificationResponse - [ENDS]"); return(orders); }
/// <summary> /// This method will build the payload required for Creating Response in the Partner API /// </summary> /// <param name="response"></param> /// <param name="loanInformation"></param> /// <param name="transactionId"></param> private void SubmitAcknowledgementToPartnerAPI(JObject response, JToken loanInformation, string transactionId) { _Logger.LogInformation("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - [STARTS]"); try { if (response != null) { _Logger.LogInformation("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - Submitting Response to Partner API "); if (response.SelectToken("$.result") != null && loanInformation != null) { JToken result = response.SelectToken("$.result");; JToken productName = null; JToken productId = null; JToken existingOrderId = null; string orderId = ""; var random = new System.Random(); // building payload for Partner API Create Response dynamic responsePayload = new JObject(); dynamic orders = new JArray(); dynamic order = null; //var orderDate = DateTime.Now.ToUniversalTime(); var orderDate = DateTime.Now; // assuming the response is of type JToken if (string.Compare(_AppSettings.IntegrationType, "Verification", true) == 0) { JToken tokenoptions = loanInformation.SelectToken("$.product.options"); if (tokenoptions != null) { foreach (JProperty prop in tokenoptions) { string sProductName = ""; if (!string.IsNullOrEmpty(prop.Name) && _MockRequestHelper.IsValidProduct(prop.Name)) { sProductName = _MockRequestHelper.GetProductDescription(prop.Name); } if (!string.IsNullOrEmpty(sProductName)) { _Logger.LogInformation("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - ProductName is " + sProductName); orderId = random.Next(10000, 80000).ToString(); OrderInformation orderInfo = new OrderInformation() { TransactionId = transactionId, ProductName = sProductName, ProductCode = prop.Name.ToString(), OrderId = orderId.ToString(), OrderStatus = response.SelectToken("$.status").ToString(), LoanInformation = loanInformation }; TransactionInformationCache.Instance.Add(orderInfo, true); order = new JObject(); order.id = orderId.ToString(); order.orderDateTime = orderDate.ToString("yyyy-MM-ddTHH:mm-ss:ff"); // result["orderDate"]; order.orderStatus = response["status"]; order.orderMessage = result["orderMessage"]; order.product = sProductName; order.documents = new JArray(); orders.Add(order); } } } } else if (string.Compare(_AppSettings.IntegrationType, "DataDocs", true) == 0) { orderId = random.Next(10000, 80000).ToString(); // responsePayload = new JObject(); //responsePayload.orders = new JArray(); dynamic orderResponse = new JObject(); orderResponse.id = orderId.ToString(); orderResponse.orderDateTime = orderDate.ToString("yyyy-MM-ddTHH:mm-ss:ff"); // result["orderDate"]; orderResponse.orderStatus = "Delivered"; orderResponse.message = result["orderMessage"]; orders.Add(orderResponse); } else { productName = loanInformation.SelectToken("$.product.options.productName"); productId = loanInformation.SelectToken("$.product.options.productId"); existingOrderId = loanInformation.SelectToken("$.product.options.existingOrderId"); // setting the order id to existingOrderId if the payload has it otherwise setting it to a random number orderId = existingOrderId != null?existingOrderId.ToString() : random.Next(10000, 80000).ToString(); var orderInfo = new OrderInformation() { TransactionId = transactionId, ProductName = productName != null?productName.ToString() : string.Empty, ProductCode = productId != null?productId.ToString() : string.Empty, OrderId = existingOrderId != null?existingOrderId.ToString() : orderId.ToString(), // adding existing order id if it exists in the response object, OrderStatus = response.SelectToken("$.status").ToString(), LoanInformation = loanInformation }; // Adding Transaction Information in the local In Memory cache. This would be implemented by the partner in their own way to track transactions and their order statuses TransactionInformationCache.Instance.Add(orderInfo); order = new JObject(); order.id = orderId.ToString(); order.orderDateTime = orderDate.ToString("yyyy-MM-ddTHH:mm-ss:ff"); // result["orderDate"]; order.orderStatus = response["status"]; order.orderMessage = result["orderMessage"]; order.product = productName; order.documents = new JArray(); orders.Add(order); } _Logger.LogInformation("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - Order Date time is " + orderDate.ToString("o")); responsePayload.orders = orders; var responsePayloadString = Convert.ToString(responsePayload); var partnerAPIWrapper = new PartnerAPIWrapper(this._AppSettings); // This is the POST call to the Partner API (Create Response call) | partner/v1/transactions/{{transactionId}}/response var isResponseCreated = partnerAPIWrapper.CreateResponse(responsePayloadString, transactionId); if (isResponseCreated) { _Logger.LogInformation("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - isResponseCreated flag is true"); TransactionStatusCache.Instance.Add(transactionId, response); } } } } catch (Exception ex) { _Logger.LogError("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - Exception - " + ex.Message); _Logger.LogError("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - StackTrace - " + ex.StackTrace); } _Logger.LogInformation("[" + _ClassName + "] - SubmitAcknowledgementToPartnerAPI - [ENDS]"); }