protected void PayRequestButton_Click(object sender, EventArgs e)
    {
        try
        {
            string result;

            BypassCertificateError();

            ir.shaparak.bpm.PaymentGatewayImplService bpService = new ir.shaparak.bpm.PaymentGatewayImplService();
            result = bpService.bpPayRequest(Int64.Parse(TerminalIdTextBox.Text),
                                            UserNameTextBox.Text,
                                            UserPasswordTextBox.Text,
                                            Int64.Parse(PayOrderIdTextBox.Text),
                                            Int64.Parse(PayAmountTextBox.Text),
                                            PayDateTextBox.Text,
                                            PayTimeTextBox.Text,
                                            PayAdditionalDataTextBox.Text,
                                            PayCallBackUrlTextBox.Text,
                                            Int64.Parse(PayPayerIdTextBox.Text));

            PayOutputLabel.Text = result;

            String[] resultArray = result.Split(',');
            if (resultArray[0] == "0")
            {
                ClientScript.RegisterStartupScript(typeof(Page), "ClientScript", "<script language='javascript' type='text/javascript'> postRefId('" + resultArray[1] + "');</script> ", false);
            }
        }
        catch (Exception exp)
        {
            PayOutputLabel.Text = "Error: " + exp.Message;
        }
    }
        /// <summary>
        /// Post process payment (used by payment gateways that require redirecting to a third-party URL)
        /// </summary>
        /// <param name="postProcessPaymentRequest">Payment info required for an order processing</param>
        /// public void
        public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
        {
            //Todo: اگر مشخصات اولیه درگاه ثبت نشده باشد، نباید خطا داشته باشیم.
            var order = postProcessPaymentRequest.Order;
            #region [Call bpMellat Web Service]

            var orderTotal = Math.Round(order.OrderTotal, 2);
            var mellatOrderId = _mellatPaymentSettings.OrderId;
            var terminalId = _mellatPaymentSettings.TerminalId;
            var userName = _mellatPaymentSettings.UserName;
            var userPassword = _mellatPaymentSettings.UserPassword;
            var price = Convert.ToInt64(orderTotal);
            const string description = "خرید از بازار اینترنتی";
            var callBackUrl = _webHelper.GetStoreLocation(false)
                              + "Plugins/PaymentMellat/CallBack?OrderId="
                              + order.Id;
            do
            {
                BypassCertificateError();
                var payDate = DateTime.Now.Year +
                              DateTime.Now.Month.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0') +
                              DateTime.Now.Day.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0');
                var payTime = DateTime.Now.Hour.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0') +
                              DateTime.Now.Minute.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0') +
                              DateTime.Now.Second.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0');

                var bpService = new ir.shaparak.bpm.PaymentGatewayImplService();
                var result = bpService.bpPayRequest(Int64.Parse(terminalId),
                    userName,
                    userPassword,
                    mellatOrderId,
                    price,
                    payDate,
                    payTime,
                    description,
                    callBackUrl,
                    0);

                string url, comment;
                var resultArray = result.Split(',');
                if (resultArray[0] == "0") // آماده برای انتقال به بانک
                {
                    order.CaptureTransactionId = mellatOrderId.ToString(CultureInfo.InvariantCulture);
                    _orderService.UpdateOrder(order);
                    UpdateOrderId(++mellatOrderId, null);
                    url = (_webHelper.GetStoreLocation(false)
                                             + "Plugins/PaymentMellat/Pay?result="
                                             + resultArray[1]);
                    _httpContext.Response.Redirect(url, true);
                }
                switch (result)
                {
                    case "41": // Order Id Already Exist
                        mellatOrderId++;
                        break;
                    case "421":// Invalid IP
                        comment = Utility.ErrorCode("bpPayRequest", result);
                        url = (_webHelper.GetStoreLocation(false)
                                                 + string.Format("Plugins/PaymentMellat/Error?result={0}&cashRefId={1}&insRefId={2}&cachOrder={3}&insOrder={4}&id={5}"
                                                     , comment, "", "", mellatOrderId, "", order.Id));
                        _httpContext.Response.Redirect(url, true);
                        break;
                    default:   // Other Errors
                        comment = Utility.ErrorCode("bpPayRequest");
                        url = (_webHelper.GetStoreLocation(false)
                                                 + string.Format("Plugins/PaymentMellat/Error?result={0}&cashRefId={1}&insRefId={2}&cachOrder={3}&insOrder={4}&id={5}"
                                                     , comment, "", "", mellatOrderId, "", order.Id));
                        _httpContext.Response.Redirect(url, true);
                        break;
                }
            }
            while (true);
            #endregion
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Read Fact Number
        if ((Request.QueryString["n"] != null))
        {
            code_s          = Request.QueryString["n"];
            Session["Fact"] = code_s;
        }
        else
        {
            //Response.Redirect("perfact.aspx");
        }

        //Select Data Value
        tblFactOnCollection list_fact = new tblFactOnCollection();

        list_fact.ReadList(Criteria.NewCriteria(tblFactOn.Columns.id_fact, CriteriaOperators.Equal, Convert.ToInt32(code_s)));

        string new_content = "";
        int    count_stuff = 0, Sum_price = 0;
        tblDetailStuffCollection list_detail = new tblDetailStuffCollection();

        list_detail.ReadList(Criteria.NewCriteria(tblDetailStuff.Columns.id_fact, CriteriaOperators.Equal, Convert.ToInt32(code_s)));

        for (int i = 0; i < list_detail.Count; i++)
        {
            count_stuff += 1;
            string stuff_id = list_detail[i].id_stuff.ToString();
            tblPorfolioCollection info_stuff = new tblPorfolioCollection();
            info_stuff.ReadList(Criteria.NewCriteria(tblPorfolio.Columns.id_profolio, CriteriaOperators.Like, stuff_id));

            new_content += info_stuff[0].id_profolio;

            Sum_price += Convert.ToInt32((list_detail[i].pay_one * list_detail[i].no_stuff).ToString());
        }

        PayAmountTextBox.Text  = Sum_price.ToString();
        PayOrderIdTextBox.Text = code_s;
        if (!IsPostBack)
        {
            SetDefaultDateTime();
            PayCallBackUrlTextBox.Text = "http://shahdforoush.ir/resultbuy.aspx";
            TerminalIdTextBox.Text     = "1309976";
            UserNameTextBox.Text       = "shfd";
            UserPasswordTextBox.Text   = "shfd47";
        }
        //send data to the bank
        try
        {
            string result;

            BypassCertificateError();

            //BPService.PaymentGatewayImplService bpService = new BPService.PaymentGatewayImplService();
            ir.shaparak.bpm.PaymentGatewayImplService bpService = new ir.shaparak.bpm.PaymentGatewayImplService();
            result = bpService.bpPayRequest(Int64.Parse(TerminalIdTextBox.Text),
                                            UserNameTextBox.Text,
                                            UserPasswordTextBox.Text,
                                            Int64.Parse(PayOrderIdTextBox.Text),
                                            Int64.Parse(PayAmountTextBox.Text),
                                            PayDateTextBox.Text,
                                            PayTimeTextBox.Text,
                                            PayAdditionalDataTextBox.Text,
                                            PayCallBackUrlTextBox.Text,
                                            Int64.Parse(PayPayerIdTextBox.Text));

            PayOutputLabel.Text = result;

            String[] resultArray = result.Split(',');
            if (resultArray[0] == "0")
            {
                ClientScript.RegisterStartupScript(typeof(Page), "ClientScript", "<script language='javascript' type='text/javascript'> postRefId('" + resultArray[1] + "');</script> ", false);
            }
        }
        catch (Exception exp)
        {
            PayOutputLabel.Text = "Error: " + exp.Message;
        }
    }
        /// <summary>
        /// Post process payment (used by payment gateways that require redirecting to a third-party URL)
        /// </summary>
        /// <param name="postProcessPaymentRequest">Payment info required for an order processing</param>
        /// public void
        public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
        {
            //Todo: اگر مشخصات اولیه درگاه ثبت نشده باشد، نباید خطا داشته باشیم.
            var order = postProcessPaymentRequest.Order;

            #region [Call bpMellat Web Service]

            var          orderTotal    = Math.Round(order.OrderTotal, 2);
            var          mellatOrderId = _mellatPaymentSettings.OrderId;
            var          terminalId    = _mellatPaymentSettings.TerminalId;
            var          userName      = _mellatPaymentSettings.UserName;
            var          userPassword  = _mellatPaymentSettings.UserPassword;
            var          price         = Convert.ToInt64(orderTotal);
            const string description   = "خرید از بازار اینترنتی";
            var          callBackUrl   = _webHelper.GetStoreLocation(false)
                                         + "Plugins/PaymentMellat/CallBack?OrderId="
                                         + order.Id;
            do
            {
                BypassCertificateError();
                var payDate = DateTime.Now.Year +
                              DateTime.Now.Month.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0') +
                              DateTime.Now.Day.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0');
                var payTime = DateTime.Now.Hour.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0') +
                              DateTime.Now.Minute.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0') +
                              DateTime.Now.Second.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0');


                var bpService = new ir.shaparak.bpm.PaymentGatewayImplService();
                var result    = bpService.bpPayRequest(Int64.Parse(terminalId),
                                                       userName,
                                                       userPassword,
                                                       mellatOrderId,
                                                       price,
                                                       payDate,
                                                       payTime,
                                                       description,
                                                       callBackUrl,
                                                       0);

                string url, comment;
                var    resultArray = result.Split(',');
                if (resultArray[0] == "0") // آماده برای انتقال به بانک
                {
                    order.CaptureTransactionId = mellatOrderId.ToString(CultureInfo.InvariantCulture);
                    _orderService.UpdateOrder(order);
                    UpdateOrderId(++mellatOrderId, null);
                    url = (_webHelper.GetStoreLocation(false)
                           + "Plugins/PaymentMellat/Pay?result="
                           + resultArray[1]);
                    _httpContext.Response.Redirect(url, true);
                }
                switch (result)
                {
                case "41":     // Order Id Already Exist
                    mellatOrderId++;
                    break;

                case "421":    // Invalid IP
                    comment = Utility.ErrorCode("bpPayRequest", result);
                    url     = (_webHelper.GetStoreLocation(false)
                               + string.Format("Plugins/PaymentMellat/Error?result={0}&cashRefId={1}&insRefId={2}&cachOrder={3}&insOrder={4}&id={5}"
                                               , comment, "", "", mellatOrderId, "", order.Id));
                    _httpContext.Response.Redirect(url, true);
                    break;

                default:       // Other Errors
                    comment = Utility.ErrorCode("bpPayRequest");
                    url     = (_webHelper.GetStoreLocation(false)
                               + string.Format("Plugins/PaymentMellat/Error?result={0}&cashRefId={1}&insRefId={2}&cachOrder={3}&insOrder={4}&id={5}"
                                               , comment, "", "", mellatOrderId, "", order.Id));
                    _httpContext.Response.Redirect(url, true);
                    break;
                }
            }while (true);
            #endregion
        }