public static void SendIncidentPaymentReceiptEmail(Payment payment, ApiServices Services)
        {
            SendGridMessage receiptMessage = new SendGridMessage();
            IncidentInfo receiptIncident = new IncidentInfo(payment.IncidentGUID);

            receiptMessage.From = SendGridHelper.GetAppFrom();
            receiptMessage.AddTo(receiptIncident.IncidentUserInfo.Email);
            receiptMessage.Html = " ";
            receiptMessage.Text = " ";
            receiptMessage.Subject = " ";

            receiptMessage.EnableTemplateEngine(WebConfigurationManager.AppSettings["RZ_ReceiptTemplateID"]);
            receiptMessage.AddSubstitution("%invoicestub%", new List<string> { receiptIncident.IncidentGUID.Substring(0, 5).ToUpper() });
            receiptMessage.AddSubstitution("%name%", new List<string> { receiptIncident.IncidentUserInfo.Name });
            receiptMessage.AddSubstitution("%jobdescription%", new List<string> { receiptIncident.JobCode });
            receiptMessage.AddSubstitution("%servicefee%", new List<string> { receiptIncident.ServiceFee.ToString() });
            receiptMessage.AddSubstitution("%datesubmitted%", new List<string> { DateTime.Now.ToShortDateString() });
            receiptMessage.AddSubstitution("%paymentmethod%", new List<string> { receiptIncident.PaymentMethod });
            receiptMessage.AddSubstitution("%paymentamount%", new List<string> { receiptIncident.PaymentAmount.ToString() });

            // Create an Web transport for sending email.
            var transportWeb = new Web(SendGridHelper.GetNetCreds());

            // Send the email.
            try
            {
                transportWeb.Deliver(receiptMessage);
                Services.Log.Info("Payment Receipt Email Sent to [" + receiptIncident.IncidentUserInfo.Email + "]");
            }
            catch (InvalidApiRequestException ex)
            {
                for (int i = 0; i < ex.Errors.Length; i++)
                {
                    Services.Log.Error(ex.Errors[i]);
                }
            }
        }
        public async Task<HttpResponseMessage> UpdatePayment(IncidentPaymentRequest paymentRequest)
        {
            Services.Log.Info("Incident Payment Update Request [API]");
            string responseText;

            stranddContext context = new stranddContext();

            //Retrieve Incident
            Incident updateIncident = await (from r in context.Incidents where (r.Id == paymentRequest.IncidentGUID) select r).FirstOrDefaultAsync();

            //Find the Incident to Edit and return Bad Response if not found
            if (updateIncident != null)
            {
                string guidnew = Guid.NewGuid().ToString();
                //Check for Submitted Payment Amount             
                Payment newPayment = new Payment
                {
                    Id = guidnew,
                    PaymentPlatform = paymentRequest.PaymentMethod,
                    PlatformPaymentID = guidnew,
                    Amount = paymentRequest.PaymentAmount,
                    Fees = -1,
                    ProviderUserID = IncidentInfo.GetProviderID(paymentRequest.IncidentGUID),
                    Status = "Admin-Entered",
                    BuyerName = "NONE",
                    BuyerEmail = "NONE",
                    BuyerPhone = "NONE",
                    Currency = "INR",
                    AuthenticationCode = "NONE",
                    IncidentGUID = paymentRequest.IncidentGUID
                };

                if (paymentRequest.PaymentMethod == "PAYMENT-CASH")
                {
                    newPayment.PaymentPlatform = "Cash Payment (Admin)";
                    newPayment.Status = "Paid";
                }

                if (paymentRequest.PaymentMethod == "PAYMENT-FAIL")
                {
                    newPayment.PaymentPlatform = "Payment Failure (Admin)";
                    newPayment.Status = "FAILED";
                }

                if (paymentRequest.PaymentAmount == 0)
                {
                    newPayment.Amount = -1;
                }

                //Save record
                context.Payments.Add(newPayment);
                await context.SaveChangesAsync();
                responseText = "Incident [" + updateIncident.Id + "] Payment Status Updated";
                Services.Log.Info(responseText);

                //Notify Particular Connected User through SignalR
                IHubContext hubContext = Services.GetRealtime<IncidentHub>();
                hubContext.Clients.Group(updateIncident.ProviderUserID).updateMobileClientStatus(newPayment.GetCustomerPushObject());
                Services.Log.Info("Mobile Client [" + updateIncident.ProviderUserID + "] Status Update Payload Sent");

                //Web Client Notifications
                hubContext.Clients.All.saveNewPayment(newPayment);
                Services.Log.Info("Connected Clients Updated");

                //Return Successful Response
                return this.Request.CreateResponse(HttpStatusCode.OK, responseText);
            }
            else
            {
                // Return Failed Response
                responseText = "Incident [" + paymentRequest.IncidentGUID + "] is not found in the system";
                Services.Log.Warn(responseText);
                return this.Request.CreateResponse(HttpStatusCode.BadRequest, responseText);
            }

        }
        public async Task<HttpResponseMessage> InstamojoWebHookReceiver(InstamojoWebhookRequest request)
        {
            Services.Log.Info("Instamojo Webhook Request");

            string incidentPropertyName = WebConfigurationManager.AppSettings["RZ_InstamojoIncidentDataField"];

            //var customFieldsObject = JsonConvert.DeserializeObject<InstamojoCustomFields>(request.Custom_Fields);
            //var parsedIncidentGUID = customFieldsObject.Field_25373.value;

            Services.Log.Info(incidentPropertyName);
            Services.Log.Info(request.Custom_Fields);

            JObject customFieldSetObj = JObject.Parse(request.Custom_Fields);
            Services.Log.Info(customFieldSetObj.ToString());
            JObject customFieldInstanceObj = (JObject) customFieldSetObj[incidentPropertyName];
            Services.Log.Info(customFieldInstanceObj.ToString());
            string parsedIncidentGUID = customFieldInstanceObj["value"].ToString();
            Services.Log.Info(parsedIncidentGUID);

            // Set the Payment Platform.
            string nameInstamojo = "Instamojo";

            Payment newPayment = new Payment()
            {
                Id = Guid.NewGuid().ToString(),
                PlatformPaymentID = request.Payment_ID,
                Status = request.Status,
                BuyerName = request.Buyer_Name,
                BuyerEmail = request.Buyer,
                BuyerPhone = request.Buyer_Phone,
                Currency = request.Currency,
                Amount = request.Amount,
                Fees = request.Fees,
                AuthenticationCode = request.MAC,
                PaymentPlatform = nameInstamojo,
                IncidentGUID = parsedIncidentGUID,
                ProviderUserID = IncidentInfo.GetProviderID(parsedIncidentGUID)

            };

            stranddContext context = new stranddContext();
            context.Payments.Add(newPayment);

            await context.SaveChangesAsync();

            //Initiating Hub Context
            Microsoft.AspNet.SignalR.IHubContext hubContext = Services.GetRealtime<IncidentHub>();

            if (newPayment.ProviderUserID == "NO INCIDENT - NO PROVIDER ID") { Services.Log.Warn("New Instamojo Payment Received - No Incident"); }
            else if (newPayment.ProviderUserID == "INCIDENT NOT FOUND - NO PROVIDER ID") { Services.Log.Warn("New Instamojo Payment Received - No Found Incident"); }
            else if (newPayment.ProviderUserID == "NO ASSOCIATED USER") { Services.Log.Warn("New Instamojo Payment Received - No Associated User"); }
            else
            {
                Services.Log.Info("New Instamojo Payment Received from User [" + newPayment.ProviderUserID + "]");                

                //Notify Particular Connected User through SignalR
                hubContext.Clients.Group(newPayment.ProviderUserID).updateMobileClientStatus(newPayment.GetCustomerPushObject());
                Services.Log.Info("Mobile Client [" + newPayment.ProviderUserID + "] Status Update Payload Sent");

                SendGridController.SendIncidentPaymentReceiptEmail(newPayment, Services);

            }

            //Web Client Notifications
            hubContext.Clients.All.saveNewPayment(newPayment);
            Services.Log.Info("Connected Clients Updated");

            return this.Request.CreateResponse(HttpStatusCode.Created);
        }