Action:整理支付信息,参数,sign。
注意CallbackUrl,这个是支付结果的回调页面。
/// <summary> /// 确认提交 /// </summary> /// <returns></returns> public ActionResult SubmitPay(string orderNo,string rid ,string payState = "0") { if (orderState == (int)EnumatorList.OrderStatus.WaitPay) { //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); //设置package订单参数 packageReqHandler.SetParameter("appid", AppId); //公众账号ID packageReqHandler.SetParameter("body", "商户名称"); packageReqHandler.SetParameter("mch_id", MachId); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr.ToLower()); //随机字符串 packageReqHandler.SetParameter("notify_url", CallbackUrl); //接收财付通通知的URL packageReqHandler.SetParameter("openid", openid); //openid packageReqHandler.SetParameter("out_trade_no", orderNo); //商家订单号 packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("total_fee", actualTotal.ToString()); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("trade_type", "JSAPI"); //交易类型 //获取package包 string sign = packageReqHandler.CreateMd5Sign("key", PaySignkey); packageReqHandler.SetParameter("sign", sign); //交易类型 string data = packageReqHandler.ParseXML(); var result = TenPayV3.Unifiedorder(data); var res = XDocument.Parse(result); string prepayId = ""; try { if (res.Element("xml").Element("return_code").Value == "SUCCESS") prepayId = res.Element("xml").Element("prepay_id").Value; } catch (Exception ex) { return View(); } package = string.Format("prepay_id={0}", prepayId); timeStamp = TenpayUtil.getTimestamp(); //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appId", AppId); paySignReqHandler.SetParameter("timeStamp", timeStamp); paySignReqHandler.SetParameter("nonceStr", nonceStr); paySignReqHandler.SetParameter("package", package); paySignReqHandler.SetParameter("signType", "MD5"); paySign = paySignReqHandler.CreateMd5Sign("key", PaySignkey); } ViewData["myReward"] = myReward; ViewData["rewardList"] = rewardList; ViewData["appId"] = AppId; ViewData["timeStamp"] = timeStamp; ViewData["nonceStr"] = nonceStr; ViewData["package"] = package; ViewData["paySign"] = paySign; return View(); }
cshtml:调用微信支付js
$(function () { //公众号支付 jQuery('a#getBrandWCPayRequest').click(function (e) { WeixinJSBridge.invoke('getBrandWCPayRequest', { "appId": "@ViewData["appId"]", //公众号名称,由商户传入 "timeStamp": "@ViewData["timeStamp"]", //时间戳 "nonceStr": "@ViewData["nonceStr"]", //随机串 "package": "@Html.Raw(ViewData["package"].ToString())",//扩展包 "signType": "MD5", //微信签名方式:1.sha1 "paySign": "@ViewData["paySign"]" //微信签名 }, function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { location.href = "../UserReward_WX/Index?orderId="+orderId; //alert("微信支付成功!"); } else if (res.err_msg == "get_brand_wcpay_request:cancel") { alert("用户取消支付!"); } else { alert(res.err_msg); alert("支付失败!"); } // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。 //因此微信团队建议,当收到ok返回时,向商户后台询问是否收到交易成功的通知,若收到通知,前端展示交易成功的界面;若此时未收到通知,商户后台主动调用查询订单接口,查询订单的当前状态,并反馈给前端展示相应的界面。 }); }); });
微信支付的回调页面(CallbackUrl)
/// <summary> /// 支付回调 /// </summary> /// <returns></returns> public ActionResult GetPayResult() { try { //判断return_code和result_code 是不是 Success } catch (Exception ex) { //记录日志 } return null ; }