protected void Page_Load(object sender, EventArgs e) { Log.Info(this.GetType().ToString(), "Default page load"); if (!IsPostBack) { JsApiPay jsApiPay = new JsApiPay(this); try { //调用【网页授权获取用户信息】接口获取用户的openid和access_token jsApiPay.GetOpenidAndAccessToken(); //获取收货地址js函数入口参数 wxEditAddrParam = jsApiPay.GetEditAddressParameters(); ViewState["openid"] = jsApiPay.openid; Log.Debug("page load", "request openid: " + jsApiPay.openid); } catch (Exception ex) { Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错,请重试" + "</span>"); Button1.Visible = false; Button2.Visible = false; Label1.Visible = false; Label2.Visible = false; } } }
protected void Page_Load(object sender, EventArgs e) { Log.Info(this.GetType().ToString(), "JsApiPayPage page load"); if (!IsPostBack) { string openid = Request.QueryString["openid"]; string orderNo = Request.QueryString["orderNo"]; string payTradeNo = Request.QueryString["payTradeNo"]; string total_fee = Request.QueryString["total_fee"]; Log.Info(this.GetType().ToString(), "JsApiPayPage page openid:" + openid + " orderNo:" + orderNo + " payTradeNo:" + payTradeNo + " total_fee:" + total_fee); //检测是否给当前页面传递了相关参数 if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(orderNo) || string.IsNullOrEmpty(payTradeNo) || string.IsNullOrEmpty(total_fee)) { ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "JsApiPayPage page load 页面传参出错,请返回重试", " orderNo:" + orderNo + " payTradeNo:" + payTradeNo + " total_fee:" + total_fee + " openid:" + openid), PayStatus.IntitUnifiedOrder.GetHashCode()); Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面传参出错,请返回重试" + "</span>"); Response.End(); Log.Error(this.GetType().ToString(), "This page have not get params, cannot be inited, exit..."); return; } ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "JsApiPayPage page Request ", " orderNo:" + orderNo + " payTradeNo:" + payTradeNo + " total_fee:" + total_fee + " openid:" + openid), PayStatus.IntitUnifiedOrder.GetHashCode(),payTradeNo, openid ); //在页面显示传递参数 this.lblorderNo.Text = orderNo; this.lblTotalFee.Text = total_fee; //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数 JsApiPay jsApiPay = new JsApiPay(this); jsApiPay.openid = openid; jsApiPay.orderNo = orderNo; jsApiPay.payTradeNo = payTradeNo; jsApiPay.total_fee = Convert.ToInt32(Convert.ToDecimal(total_fee)*100);//转换为分 //JSAPI支付预处理 try { WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(); wxJsApiParam = jsApiPay.GetJsApiParameters(); //获取H5调起JS API参数 Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam); //在页面上显示订单信息 //Response.Write("<span style='color:#00CD00;font-size:20px'>订单详情:</span><br/>"); //Response.Write("<span style='color:#00CD00;font-size:20px'>" + unifiedOrderResult.ToPrintStr() + // "</span>"); //Response.End(); } catch (Exception ex) { ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "JsApiPayPage page load 下单失败,请返回重试", " orderNo:" + orderNo + " payTradeNo:" + payTradeNo + " total_fee:" + total_fee + " openid:" + openid), PayStatus.UnifiedOrderFail.GetHashCode(),payTradeNo, openid ); Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试"+"</span>"); Response.End(); } } }
protected void Page_Load(object sender, EventArgs e) { Log.Info(this.GetType().ToString(), "Pay Result Page "); JsApiPay jsApiPay = new JsApiPay(this); WxPayData notifyData = jsApiPay.GetNotifyData(); //检查支付结果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "ResultNotifyPage cheack transaction_id 支付结果中微信订单号不存在", res.ToJson()), PayStatus.IntitPayResultNotify.GetHashCode()); Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml()); this.Response.Write(res.ToXml()); this.Response.End(); } string transaction_id = notifyData.GetValue("transaction_id").ToString(); //查询订单,判断订单真实性 if (!QueryOrder(transaction_id, jsApiPay)) { //若订单查询失败,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "ResultNotifyPage 判断transaction_id订单真实性 订单查询失败", res.ToJson()), PayStatus.PayResultNotifyFail.GetHashCode()); Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml()); this.Response.Write(res.ToXml()); this.Response.End(); } //查询订单成功 else { //将处理结果发送给微信Wap new NotifyResult(this).WapPayProcess(notifyData); //WxPayData res = new WxPayData(); //res.SetValue("return_code", "SUCCESS"); //res.SetValue("return_msg", "OK"); //Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml()); ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "ResultNotifyPage 查询订单成功", res.ToJson()), PayStatus.PayResultNotifySucces.GetHashCode(), notifyData.GetValue("out_trade_no").ToString(), notifyData.GetValue("openid").ToString(), notifyData.GetValue("transaction_id").ToString()); //this.Response.Write(res.ToXml()); //this.Response.End(); } }
//查询订单 private bool QueryOrder(string transaction_id,JsApiPay jsApiPay) { WxPayData req = new WxPayData(); req.SetValue("transaction_id", transaction_id); WxPayData res = jsApiPay.OrderQuery(req); if (res.GetValue("return_code").ToString() == "SUCCESS" && res.GetValue("result_code").ToString() == "SUCCESS") { return true; } else { return false; } }