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; }
/** * * 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数, * 微信浏览器调起JSAPI时的输入参数格式如下: * { * "appId" : "wx2421b1c4370ec43b", //公众号名称,由商户传入 * "timeStamp":" 1395712654", //时间戳,自1970年以来的秒数 * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串 * "package" : "prepay_id=u802345jgfjsdfgsdg888", * "signType" : "MD5", //微信签名方式: * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 * } * @return string 微信浏览器调起JSAPI时的输入参数,json格式可以直接做参数用 * 更详细的说明请参考网页端调起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 * */ public string GetJsApiParameters() { Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing..."); WxPayData jsApiParam = new WxPayData(); jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid")); jsApiParam.SetValue("timeStamp", jsApiParam.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", jsApiParam.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); string parameters = jsApiParam.ToJson(); Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters); //new WxPayLog().InsertLog(string.Format("{0}:{1}", "GetJsApiParameters jsApiParam ", parameters), //PayStatus.UnifiedOrderSucces.GetHashCode(), //payTradeNo, openid); return parameters; }