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(); } }
public void WapPayProcess(WxPayData notifyData, int timeOut = 6) { string url = "http://123.57.211.198/api/pay/NotifyResultOp.ashx"; WxPayData inputObj = new WxPayData(); inputObj.SetValue("attach",notifyData.GetValue("attach"));//业务订单号 inputObj.SetValue("out_trade_no",notifyData.GetValue("out_trade_no"));//业务支付订单号 inputObj.SetValue("total_fee",notifyData.GetValue("total_fee"));//支付金额 inputObj.SetValue("transaction_id",notifyData.GetValue("transaction_id"));//微信支付订单号 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); if (!WxPayConfig.CURL_TIMEOUT.Equals(0)) { timeOut = WxPayConfig.CURL_TIMEOUT; } Log.Debug("NotifyResult", "WapPayProcess request : " + xml); try { string response = HttpService.Post(xml, url, false, timeOut); //调用HTTP通信接口提交数据 Log.Debug("NotifyResult", "WapPayProcess response : " + response); ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "NotifyResult WapPayProcess response : ", response), PayStatus.GetOpenidSucces.GetHashCode()); WxPayData res = new WxPayData(); res.FromXml(response); page.Response.Write(res.ToXml()); page.Response.End(); } catch (WxPayException ex) { WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "业务处理发生错误"); ////new WxPayLog().InsertLog(string.Format("{0}:{1}", "NotifyResult WapPayProcess处理发生错误", res.ToJson()), PayStatus.PayResultNotifyFail.GetHashCode()); Log.Error(this.GetType().ToString(), "NotifyResult WapPayProcess failure : " + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } }
/** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WxPayException("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WxPayException("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WxPayException("缺少统一支付接口必填参数trade_type!"); } //关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!inputObj.IsSet("notify_url")) { inputObj.SetValue("notify_url", WxPayConfig.NOTIFY_URL); //异步通知url } inputObj.SetValue("appid", WxPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID); //商户号 inputObj.SetValue("spbill_create_ip", WxPayConfig.IP); //终端ip inputObj.SetValue("nonce_str", inputObj.GenerateNonceStr()); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); Log.Debug("WxPayApi", "UnfiedOrder request json : " + inputObj.ToJson()); //new WxPayLog().InsertLog(string.Format("{0}:{1}", "UnifiedOrder request json ", inputObj.ToJson()), //PayStatus.IntitUnifiedOrder.GetHashCode(), // inputObj.GetValue("out_trade_no").ToString(), inputObj.GetValue("openid").ToString()); if (!WxPayConfig.CURL_TIMEOUT.Equals(0)) { timeOut = WxPayConfig.CURL_TIMEOUT; } string response = HttpService.Post(xml, url, false, timeOut); //string response = string.Empty; Log.Debug("WxPayApi", "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int) ((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); //new WxPayLog().InsertLog(string.Format("{0}:{1}", "UnifiedOrder response ", result.ToJson()), //PayStatus.UnifiedOrderSucces.GetHashCode(), //inputObj.GetValue("out_trade_no").ToString(), inputObj.GetValue("openid").ToString()); //ReportCostTime(url, timeCost, result);//测速上报 return result; }
/** * * 查询订单 * @param WxPayData inputObj 提交给查询订单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回订单查询结果,其他抛异常 */ public WxPayData OrderQuery(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/orderquery"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str", inputObj.GenerateNonceStr());//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "OrderQuery request : " + xml); if (!WxPayConfig.CURL_TIMEOUT.Equals(0)) { timeOut = WxPayConfig.CURL_TIMEOUT; } string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口提交数据 Log.Debug("WxPayApi", "OrderQuery response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的数据转化为对象以返回 WxPayData result = new WxPayData(); result.FromXml(response); //ReportCostTime(url, timeCost, result);//测速上报 return result; }
/// <summary> /// 接收从微信支付后台发送过来的数据并验证签名 /// </summary> /// <returns>微信支付后台返回的数据</returns> public WxPayData GetNotifyData() { //接收从微信后台POST过来的数据 System.IO.Stream s = page.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); Log.Info(this.GetType().ToString(), "Receive data from WeChat : " + builder.ToString()); //转换数据格式并验证签名 WxPayData data = new WxPayData(); try { data.FromXml(builder.ToString()); } catch (WxPayException ex) { //若签名错误,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); Log.Error(this.GetType().ToString(), "Sign check error : " + res.ToXml()); //new WxPayLog().InsertLog(string.Format("{0}:{1}", "GetNotifyData Sign check error", data.ToJson()), //PayStatus.IntitPayResultNotify.GetHashCode()); page.Response.Write(res.ToXml()); page.Response.End(); } //new WxPayLog().InsertLog(string.Format("{0}:{1}", "GetNotifyData Receive data from WeChat and Check sign success", data.ToJson()), PayStatus.IntitPayResultNotify.GetHashCode()); Log.Info(this.GetType().ToString(), "Check sign success"); return data; }