public async Task <ActionResult> TransactionComplete(string PAY_REQUEST_ID, int TRANSACTION_STATUS, string CHECKSUM)
        {
            using (FreeMarketEntities db = new FreeMarketEntities())
            {
                ThankYouViewModel model;
                ShoppingCart      cart = GetCartFromSession(User.Identity.GetUserId());
                int orderNumber        = cart.Order.OrderNumber;

                PaymentGatewayParameter parameters = PaymentGatewayIntegration.GetParameters();

                string checkSource = string.Format("{0}{1}{2}{3}{4}",
                                                   parameters.PaymentGatewayID, PAY_REQUEST_ID, TRANSACTION_STATUS, cart.Order.OrderNumber.ToString(), parameters.Key);
                string checkSum = Extensions.CreateMD5(checkSource);

                if (checkSum == CHECKSUM)
                {
                    if (TRANSACTION_STATUS == 1)
                    {
                        // Get the order from the database, not the session.
                        OrderHeader order = db.OrderHeaders.Find(orderNumber);

                        if (order == null)
                        {
                        }
                        else
                        {
                            // This will be executed if the notify url did not receive a response yet.
                            if (order.OrderStatus == "Locked")
                            {
                                cart.SetOrderConfirmed(User.Identity.GetUserId());
                                OrderHeader.SendConfirmationMessages(User.Identity.GetUserId(), orderNumber);
                            }
                            else
                            {
                                cart.Initialize(User.Identity.GetUserId());
                            }
                        }

                        AuditUser.LogAudit(33, string.Format("Order Number: {0} | Transaction Status: {1} | Pay Request Id: {2}", orderNumber, TRANSACTION_STATUS, PAY_REQUEST_ID), User.Identity.GetUserId());
                    }
                    else
                    {
                        AuditUser.LogAudit(39, string.Format("Order Number: {0} | Transaction Status: {1} | Pay Request Id: {2}", orderNumber, TRANSACTION_STATUS, PAY_REQUEST_ID), User.Identity.GetUserId());
                    }
                }
                else
                {
                    OrderHeader.SendWarningEmail(orderNumber);
                    AuditUser.LogAudit(34, string.Format("Order Number: {0}", orderNumber), User.Identity.GetUserId());
                    model = new ThankYouViewModel {
                        TransactionStatus = 999
                    };
                    return(View("ThankYou", model));
                }

                model = new ThankYouViewModel {
                    TransactionStatus = TRANSACTION_STATUS
                };
                return(View("ThankYou", model));
            }
        }
        public async Task <ActionResult> Notify(int?PAYGATE_ID, string PAY_REQUEST_ID, string REFERENCE, int TRANSACTION_STATUS,
                                                int RESULT_CODE, string AUTH_CODE, string CURRENCY, int AMOUNT, string RESULT_DESC, int TRANSACTION_ID,
                                                string RISK_INDICATOR, string PAY_METHOD, string PAY_METHOD_DETAIL, string USER1, string USER2, string USER3,
                                                string VAULT_ID, string PAYVAULT_DATA_1, string PAYVAULT_DATA_2, string CHECKSUM)
        {
            bool checksumPassed     = false;
            bool priceSameAsRequest = false;

            PaymentGatewayParameter param = PaymentGatewayIntegration.GetParameters();
            string id = "";

            if (PAYGATE_ID == null)
            {
                id = param.PaymentGatewayID.ToString();
            }
            else
            {
                id = PAYGATE_ID.ToString();
            }

            string check = id + PAY_REQUEST_ID + REFERENCE + TRANSACTION_STATUS.ToString()
                           + RESULT_CODE.ToString() + AUTH_CODE + CURRENCY + AMOUNT + RESULT_DESC + TRANSACTION_ID
                           + RISK_INDICATOR + PAY_METHOD + PAY_METHOD_DETAIL + USER1 + USER2 + USER3
                           + VAULT_ID + PAYVAULT_DATA_1 + PAYVAULT_DATA_2 + param.Key;

            string checksum = Extensions.CreateMD5(check);

            if (CHECKSUM == checksum)
            {
                checksumPassed = true;
                using (FreeMarketEntities db = new FreeMarketEntities())
                {
                    if (!string.IsNullOrEmpty(REFERENCE))
                    {
                        ValidatePaymentAmount_Result request = db.ValidatePaymentAmount(REFERENCE).FirstOrDefault();

                        if (request != null)
                        {
                            string requestedAmount = request.Amount.ToString();
                            if (requestedAmount == AMOUNT.ToString())
                            {
                                priceSameAsRequest = true;

                                string orderNumberString = REFERENCE.ToString();

                                if (db.PaymentGatewayMessages
                                    .Where(c => c.Reference == orderNumberString && c.TransactionStatus == TRANSACTION_STATUS)
                                    .FirstOrDefault() == null)
                                {
                                    PaymentGatewayMessage message = new PaymentGatewayMessage
                                    {
                                        PayGate_ID         = decimal.Parse(id),
                                        Pay_Request_ID     = PAY_REQUEST_ID,
                                        Reference          = REFERENCE,
                                        TransactionStatus  = TRANSACTION_STATUS,
                                        Transaction_Date   = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                        Result_Code        = RESULT_CODE,
                                        Auth_Code          = AUTH_CODE,
                                        Currency           = CURRENCY,
                                        Amount             = AMOUNT,
                                        Result_Desc        = RESULT_DESC,
                                        Transaction_ID     = TRANSACTION_ID,
                                        Risk_Indicator     = RISK_INDICATOR,
                                        Pay_Method         = PAY_METHOD,
                                        Pay_Method_Detail  = PAY_METHOD_DETAIL,
                                        User1              = USER1,
                                        User2              = USER2,
                                        User3              = USER3,
                                        Vault_ID           = VAULT_ID,
                                        Pay_Vault_Data1    = PAYVAULT_DATA_1,
                                        Pay_Vault_Data2    = PAYVAULT_DATA_2,
                                        Checksum_Passed    = checksumPassed,
                                        PriceSameAsRequest = priceSameAsRequest
                                    };

                                    db.PaymentGatewayMessages.Add(message);
                                    db.SaveChanges();
                                }

                                try
                                {
                                    if (TRANSACTION_STATUS == 1)
                                    {
                                        int         orderNumber = int.Parse(REFERENCE);
                                        OrderHeader order       = db.OrderHeaders.Find(orderNumber);
                                        if (order == null)
                                        {
                                        }
                                        else
                                        {
                                            if (order.OrderStatus == "Locked")
                                            {
                                                ShoppingCart.SetOrderConfirmedFromNotify(orderNumber);
                                                OrderHeader.SendConfirmationMessages(order.CustomerNumber, orderNumber);
                                            }
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    ExceptionLogging.LogException(e);
                                }

                                AuditUser.LogAudit(35, string.Format("Order Number: {0}", REFERENCE));
                            }
                            else
                            {
                                priceSameAsRequest = false;

                                string orderNumberString = REFERENCE.ToString();

                                if (db.PaymentGatewayMessages
                                    .Where(c => c.Reference == orderNumberString && c.TransactionStatus == TRANSACTION_STATUS)
                                    .FirstOrDefault() == null)
                                {
                                    PaymentGatewayMessage message = new PaymentGatewayMessage
                                    {
                                        PayGate_ID         = decimal.Parse(id),
                                        Pay_Request_ID     = PAY_REQUEST_ID,
                                        Reference          = REFERENCE,
                                        TransactionStatus  = TRANSACTION_STATUS,
                                        Transaction_Date   = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                        Result_Code        = RESULT_CODE,
                                        Auth_Code          = AUTH_CODE,
                                        Currency           = CURRENCY,
                                        Amount             = AMOUNT,
                                        Result_Desc        = RESULT_DESC,
                                        Transaction_ID     = TRANSACTION_ID,
                                        Risk_Indicator     = RISK_INDICATOR,
                                        Pay_Method         = PAY_METHOD,
                                        Pay_Method_Detail  = PAY_METHOD_DETAIL,
                                        User1              = USER1,
                                        User2              = USER2,
                                        User3              = USER3,
                                        Vault_ID           = VAULT_ID,
                                        Pay_Vault_Data1    = PAYVAULT_DATA_1,
                                        Pay_Vault_Data2    = PAYVAULT_DATA_2,
                                        Checksum_Passed    = checksumPassed,
                                        PriceSameAsRequest = priceSameAsRequest
                                    };

                                    db.PaymentGatewayMessages.Add(message);
                                    db.SaveChanges();
                                }

                                AuditUser.LogAudit(34, string.Format("Order Number: {0}. Request Amount: {1}. Notification Amount: {2}", REFERENCE, requestedAmount, AMOUNT));

                                try
                                {
                                    int orderNumber = int.Parse(REFERENCE);
                                    OrderHeader.SendWarningEmail(orderNumber);
                                }
                                catch (Exception e)
                                {
                                    ExceptionLogging.LogException(e);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                checksumPassed = false;
                using (FreeMarketEntities db = new FreeMarketEntities())
                {
                    string orderNumberString = REFERENCE.ToString();

                    if (db.PaymentGatewayMessages
                        .Where(c => c.Reference == orderNumberString && c.TransactionStatus == TRANSACTION_STATUS)
                        .FirstOrDefault() == null)
                    {
                        PaymentGatewayMessage message = new PaymentGatewayMessage
                        {
                            PayGate_ID        = PAYGATE_ID,
                            Pay_Request_ID    = PAY_REQUEST_ID,
                            Reference         = REFERENCE,
                            TransactionStatus = TRANSACTION_STATUS,
                            Result_Code       = RESULT_CODE,
                            Auth_Code         = AUTH_CODE,
                            Currency          = CURRENCY,
                            Amount            = AMOUNT,
                            Result_Desc       = RESULT_DESC,
                            Transaction_ID    = TRANSACTION_ID,
                            Risk_Indicator    = RISK_INDICATOR,
                            Pay_Method        = PAY_METHOD,
                            Pay_Method_Detail = PAY_METHOD_DETAIL,
                            User1             = USER1,
                            User2             = USER2,
                            User3             = USER3,
                            Vault_ID          = VAULT_ID,
                            Pay_Vault_Data1   = PAYVAULT_DATA_1,
                            Pay_Vault_Data2   = PAYVAULT_DATA_2,
                            Checksum_Passed   = checksumPassed
                        };

                        db.PaymentGatewayMessages.Add(message);
                        db.SaveChanges();
                    }

                    AuditUser.LogAudit(34, string.Format("Order Number: {0}. Checksum failed.", REFERENCE));

                    try
                    {
                        int orderNumber = int.Parse(REFERENCE);
                        OrderHeader.SendWarningEmail(orderNumber);
                    }
                    catch (Exception e)
                    {
                        ExceptionLogging.LogException(e);
                    }
                }
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }