微信支付动态设置支付金额 ASP.NET 10C

.aspx文件

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <meta name="viewport" content="width=640px, maximum-scale=1.0, user-scalable=no"/>
    <link href="/images/payStyle.css" type="text/css" rel="Stylesheet" />
    <script type="text/javascript">
        $(function () {
            $(window).bind('resize load', function() {
                $("body").css("zoom", $(window).width() / 640);
                $("body").css("display", "block");
                $("body").css("zoom", $(window).width() / 640);
                $("input").attr("placeholder", "请填写");
            });
        })
    </script>
<script type="text/javascript">
    //调用微信JS api 支付
    function jsApiCall()
    {
        WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        <%#wxJsApiParam%>,//josn串
                    function (res)
                    {
                        if (res.err_msg == "get_brand_wcpay_request:ok")
                        {
                            var OrderId=$("#OrderId").val();
                            var orderProductName=$("#orderProductName").val();
                            var orderMoneySum=$("#orderMoneySum").val();

                            window.location.href="http://11.com/fk.aspx;//支付成功后的跳转页面

                        }else
                        {
                            WeixinJSBridge.call('closeWindow');
                        }

                    }
                    );
    }

    function callpay()
    {
        if (typeof WeixinJSBridge == "undefined")
        {
            if (document.addEventListener)
            {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }
            else if (document.attachEvent)
            {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }
        else
        {
            jsApiCall();
        }
    }

           </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="bodydiv">
        <div id="payhead"></div>
        <div id="middlebody">
            <table class="tablestyle">
                <tr class="tablestyletr">
                    <td colspan="2" class="ttdstyle">企业名称</td>
                </tr>
                <tr class="tablestyletr">
                    <td colspan="2" class="tdstyle"><asp:TextBox runat="server" ID="companyNameText" BorderWidth="0px" Width="100%" Font-Size="25px"></asp:TextBox></td>
                </tr>
                <tr class="backcolor">
                    <td colspan="2" class="backcolor"></td>
                </tr>
                <tr class="tablestyletr">
                    <td class="ttdstyle">企业税号</td>
                    <td class="tdstyle"><asp:TextBox runat="server" ID="noText" BorderWidth="0px" Width="100%"></asp:TextBox></td>
                </tr>
                <tr class="backcolor">
                    <td colspan="2" class="backcolor"></td>
                </tr>
                <tr class="tablestyletr">
                    <td class="ttdstyle">所属区县</td>
                    <td class="tdstyle">
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem>5</asp:ListItem>
                            <asp:ListItem>8</asp:ListItem>
                            <asp:ListItem>6</asp:ListItem>
                            </asp:DropDownList></td>
                </tr>
                <tr class="backcolor">
                    <td colspan="2" class="backcolor"></td>
                </tr>
                <tr class="tablestyletr">
                    <td class="ttdstyle">座机电话</td>
                    <td class="tdstyle"><asp:TextBox runat="server" ID="phoneText" BorderWidth="0px" Width="100%"></asp:TextBox></td>
                </tr>
                <tr class="backcolor">
                    <td colspan="2" class="backcolor"></td>
                </tr>
                <tr class="tablestyletr">
                    <td class="ttdstyle">联系人</td>
                    <td class="tdstyle"><asp:TextBox runat="server" ID="menText" BorderWidth="0px" Width="100%"></asp:TextBox></td>
                </tr>
                <tr class="backcolor">
                    <td colspan="2" class="backcolor"></td>
                </tr>
                <tr class="tablestyletr">
                    <td class="ttdstyle">手机号码</td>
                    <td class="tdstyle"><asp:TextBox runat="server" ID="menphoneText" BorderWidth="0px" Width="100%"></asp:TextBox></td>
                               </tr>
            </table>
        </div>
        <div id="middlehander"></div>
        <div id="jediv">
            <table class="tablestyle">
                <tr class="tablestyletr">
                    <td class="ttdstyle">续费金额</td>
                    <td class="tdstyle"><asp:TextBox runat="server" ID="jeText" BorderWidth="0px" Width="100%"></asp:TextBox></td>
                </tr>
            </table>
        </div>
        <div id="footdiv">

            <div style="width:100%; text-align:center;">
                <asp:ImageButton runat="server" ID="submitBtn" ImageUrl="~/images/submitbtn.jpg" OnClick="submitBtn_Click" />

            </div>
        </div>
    </div>
    </form>
</body>
</html>

.cs文件


    public partial class pay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                JsApiPay jsApiPay = new JsApiPay(this);
                try
                {
                    string total_fee = Money;// Request["total_fee"];
                    orderMoneySum = total_fee;
                    string ParkName = "服务费";// Request["ParkName"];
                    orderProductName = ParkName + "停车费用";
                    OrderId = "145448545";// Request["OrderId"];
                    //if (string.IsNullOrWhiteSpace(total_fee) || total_fee == "0")
                    if (total_fee == "0")
                    {
                        throw new WxPayException("<span style='color:#FF0000;font-size:20px'>" + "费用为零,请求参数错误" + "</span>");
                    }

                    jsApiPay.total_fee = int.Parse((Convert.ToDouble(total_fee) * 100).ToString());
                    Money = (Convert.ToDouble(jsApiPay.total_fee) / 100).ToString();
                    jsApiPay.orderid = OrderId;
                    //JSAPI支付预处理
                    try
                    {
                        Common common = new Common(Context);
                        jsApiPay.openid = common.GetOpenId();
                        if (Common.OpenId == "Openid")
                        {
                            throw new WxPayException("OpenId为空无法下单!");
                        }
                        jsApiPay.access_token = Common.access_token;

                        WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(ParkName);//ParkName不能是空,这是一个大坑
                        wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数 
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试:" + ex.InnerException.Message + "</span>");
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错,请重试:" + ex.Message + "</span>");
                }
                this.DataBind();
            }
        }

        public string wxJsApiParam { get; set; } //H5调起JS API参数
        public string Money = "1";
        public string OrderId { get; set; }
        public string orderMoneySum { get; set; }//商品金额
        public string orderProductName { get; set; }//商品名称

        protected void submitBtn_Click(object sender, ImageClickEventArgs e)
        {
            Session["companyVar"] = this.companyNameText.Text.ToString();//企业名称
            Session["noVar"] = noText.Text.ToString();//统一社会信用代码
            Session["menVar"] = menText.Text.ToString();//联系人
            Session["menphoneVar"] = menphoneText.Text.ToString();//联系人手机号
            Session["phoneVar"] = this.phoneText.Text.ToString();//联系电话
            Session["address"] = this.DropDownList1.SelectedValue.ToString();
            Session["dateVar"] = DateTime.Now.ToString();//缴费日期
            Session["moneyVar"] = this.jeText.Text.ToString();//缴费金额
            Money = Convert.ToDouble(this.jeText.Text.ToString()).ToString();
            ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>callpay();</script>");
        }
    }

**我在.cs文件里面Money设置为1。但是在填写金额为2后,提交后。跳转到支付,金额仍然是1.。。请问如何设置。。。

**

4个回答

Page_load先执行才到按钮的click事件,而且你判断了!Page.IsPostBack,点击按钮不会再执行if(!Page.IsPostBack)里面的代码,改成下面的

     protected void SetWxPayParams()
    {
        JsApiPay jsApiPay = new JsApiPay(this);
        try
        {
            string total_fee = Money;// Request["total_fee"];
            orderMoneySum = total_fee;
            string ParkName = "服务费";// Request["ParkName"];
            orderProductName = ParkName + "停车费用";
            OrderId = "145448545";// Request["OrderId"];
            //if (string.IsNullOrWhiteSpace(total_fee) || total_fee == "0")
            if (total_fee == "0")
            {
                throw new WxPayException("<span style='color:#FF0000;font-size:20px'>" + "费用为零,请求参数错误" + "</span>");
            }

            jsApiPay.total_fee = int.Parse((Convert.ToDouble(total_fee) * 100).ToString());
            Money = (Convert.ToDouble(jsApiPay.total_fee) / 100).ToString();
            jsApiPay.orderid = OrderId;
            //JSAPI支付预处理
            try
            {
                Common common = new Common(Context);
                jsApiPay.openid = common.GetOpenId();
                if (Common.OpenId == "Openid")
                {
                    throw new WxPayException("OpenId为空无法下单!");
                }
                jsApiPay.access_token = Common.access_token;

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(ParkName);//ParkName不能是空,这是一个大坑
                wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数 
            }
            catch (Exception ex)
            {
                Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试:" + ex.InnerException.Message + "</span>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错,请重试:" + ex.Message + "</span>");
        }
        this.DataBind(); 
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Page_load里面的代码单独出一个函数,而且不需要再page_load里面设置微信支付参数,放到按钮里面
    }


    protected void submitBtn_Click(object sender, ImageClickEventArgs e)
    {
        Session["companyVar"] = this.companyNameText.Text.ToString();//企业名称
        Session["noVar"] = noText.Text.ToString();//统一社会信用代码
        Session["menVar"] = menText.Text.ToString();//联系人
        Session["menphoneVar"] = menphoneText.Text.ToString();//联系人手机号
        Session["phoneVar"] = this.phoneText.Text.ToString();//联系电话
        Session["address"] = this.DropDownList1.SelectedValue.ToString();
        Session["dateVar"] = DateTime.Now.ToString();//缴费日期
        Session["moneyVar"] = this.jeText.Text.ToString();//缴费金额
        Money = Convert.ToDouble(this.jeText.Text.ToString()).ToString();
        SetWxPayParams();////////////////////////////////////放这里来获取微信支付参数并且重新绑定参数值,注意位置,一定要在上面这句执行后再执行获取微信支付参数
        ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>callpay();</script>");
    }

webform的运行机制没搞清楚

用户输入信息后,先跳转到订单添加界面,然后再跳转到支付界面,就不会出这个问题。我暂时是这样解决的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问