微信公众号支付

步骤:

1、授权目录设置

2、配置商户号、apikey、appid.

3、前台代码。

   

<a href="javascript:void(0);" class="btn" id="getBrandWCPayRequest">确认支付</a>
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            //公众号支付
            jQuery('a#getBrandWCPayRequest').click(function (e) {
             
                WeixinJSBridge.invoke('getBrandWCPayRequest', {
                   <%=packageValue%>
                }, function (res) {
                    var newurl = "/api/payment/wxpay/payResult.aspx?wid=" + <%=wid%> + "&otid=" + '<%=otid_str%>' + "&openid=" + '<%=openid%>' + "";
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        //支付成功后清空我的订单
                        $.post("/shop/shopmgr.ashx?myact=clearMyOrder", { wid: '<%=wid%>', openid: '<%=openid%>' }, function (data) { }, "json")
 
                        alert("微信支付成功,点击右上角返回!");
                        window.location.href = newurl;
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        alert("您取消了支付!");
                    } else {
                        alert(res.err_msg);
                        alert("您支付失败了!");
                    }
                });
 
            });
 
            WeixinJSBridge.log('yo~ ready.');
 
        }, false);
View Code

   https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7

4、后台代码

  主要是生成前台所需要的 packageValue :

 

/先设置基本信息
           string MchId = paymentInfo.partnerId; // "1218574001";//  
 
           string partnerKey = paymentInfo.partnerKey;// 商户支付密钥Key。登录微信商户后台,进入栏目【账户设置】【密码安全】【API 安全】【API 密钥】
 
           string notify_url = "http://" + HttpContext.Current.Request.Url.Authority + "/api/payment/wxpay/notify_url.aspx";
 
 
           string timeStamp = "";
           string nonceStr = "";
           string paySign = "";
 
           string sp_billno = out_trade_no;
           //当前时间 yyyyMMdd
           string date = DateTime.Now.ToString("yyyyMMdd");
 
           if (null == sp_billno)
           {
               //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一
               sp_billno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28);
           }
 
 
           //创建支付应答对象
           Senparc.Weixin.MP.TenPayLibV3.RequestHandler packageReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null);
           //初始化
           packageReqHandler.Init();
           //packageReqHandler.SetKey(""/*TenPayV3Info.Key*/);
 
           timeStamp = TenPayV3Util.GetTimestamp();
           nonceStr = TenPayV3Util.GetNoncestr();
 
           //设置package订单参数
           packageReqHandler.SetParameter("appid", uwEntity.AppId);          //公众账号ID
           packageReqHandler.SetParameter("mch_id", MchId);          //商户号
           packageReqHandler.SetParameter("nonce_str", nonceStr);                    //随机字符串
           packageReqHandler.SetParameter("body", busiBody);  //商品描述
           packageReqHandler.SetParameter("attach", wid + "|" + busiBody);
           packageReqHandler.SetParameter("out_trade_no", sp_billno);        //商家订单号
           packageReqHandler.SetParameter("total_fee", ((int)(ttFee * 100)).ToString());                    //商品金额,以分为单位(money * 100).ToString()
           packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress);   //用户的公网ip,不是商户服务器IP
           packageReqHandler.SetParameter("notify_url", notify_url);            //接收财付通通知的URL
           packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString());//交易类型
           packageReqHandler.SetParameter("openid", openid);                        //用户的openId
 
           string sign = packageReqHandler.CreateMd5Sign("key", partnerKey);
           packageReqHandler.SetParameter("sign", sign);                        //签名
 
           string data = packageReqHandler.ParseXML();
    
           var result = TenPayV3.Unifiedorder(data);
 
           var res = XDocument.Parse(result);
           prepayId = res.Element("xml").Element("prepay_id").Value;
     
           //设置支付参数
           Senparc.Weixin.MP.TenPayLibV3.RequestHandler paySignReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null);
           paySignReqHandler.SetParameter("appId", uwEntity.AppId);
           paySignReqHandler.SetParameter("timeStamp", timeStamp);
           paySignReqHandler.SetParameter("nonceStr", nonceStr);
           paySignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId));
           paySignReqHandler.SetParameter("signType", "MD5");
           paySign = paySignReqHandler.CreateMd5Sign("key", partnerKey);
 
           packageValue = "";
           packageValue += " \"appId\": \"" + uwEntity.AppId + "\", ";
           packageValue += " \"timeStamp\": \"" + timeStamp + "\", ";
           packageValue += " \"nonceStr\": \"" + nonceStr + "\", ";
           packageValue += " \"package\": \"" + string.Format("prepay_id={0}", prepayId) + "\", ";
           packageValue += " \"signType\": \"MD5\", ";
           packageValue += " \"paySign\": \"" + paySign + "\"";
View Code

 

posted @ 2015-11-30 10:50  每天进步一点点!  阅读(468)  评论(0编辑  收藏  举报