/** * 下载对账单 * @param WxPayData inputObj 提交给下载对账单API的参数 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData DownloadBill(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/downloadbill"; //检测必填参数 if (!inputObj.IsSet("bill_date")) { throw new WxPayException("对账单接口中,缺少必填参数bill_date!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); Log.Debug("WxPayApi", "DownloadBill request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "DownloadBill result : " + response); WxPayData result = new WxPayData(); //若接口调用失败会返回xml格式的结果 if (response.Substring(0, 5) == "<xml>") { result.FromXml(response); } //接口调用成功则返回非xml格式的数据 else result.SetValue("result", response); return result; }
/** * * 关闭订单 * @param WxPayData inputObj 提交给关闭订单API的参数 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData CloseOrder(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/closeorder"; //检测必填参数 if(!inputObj.IsSet("out_trade_no")) { throw new WxPayException("关闭订单接口中,out_trade_no必填!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串 inputObj.SetValue("sign",inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 string response = HttpService.Post(xml, url, false, timeOut); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return result; }
/** * 提交被扫支付API * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台, * 由商户收银台或者商户后台调用该接口发起支付。 * @param WxPayData inputObj 提交给被扫支付API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回调用结果,其他抛异常 */ public static WxPayData Micropay(WxPayData inputObj, int timeOut = 10) { string url = "https://api.mch.weixin.qq.com/pay/micropay"; //检测必填参数 if (!inputObj.IsSet("body")) { throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!"); } else if (!inputObj.IsSet("out_trade_no")) { throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!"); } else if (!inputObj.IsSet("auth_code")) { throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!"); } inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 Log.Debug("WxPayApi", "MicroPay request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "MicroPay 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 = httpContext.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()); httpContext.Response.Write(res.ToXml()); httpContext.Response.End(); } Log.Info(this.GetType().ToString(), "Check sign success"); return data; }
/** * * 查询订单 * @param WxPayData inputObj 提交给查询订单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回订单查询结果,其他抛异常 */ public static 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", WxPayApi.GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); DateTime start = DateTime.Now; Log.Debug("WxPayApi", "OrderQuery request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口提交数据 Log.Debug("WxPayApi", "OrderQuery response : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的数据转化为对象以返回 WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/** * 下载对账单 * @param WxPayData inputObj 提交给下载对账单API的参数 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData DownloadBill(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/downloadbill"; //检测必填参数 if (!inputObj.IsSet("bill_date")) { throw new WxPayException("对账单接口中,缺少必填参数bill_date!"); } inputObj.SetValue("appid", WxPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); Log.Debug("WxPayApi", "DownloadBill request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "DownloadBill result : " + response); WxPayData result = new WxPayData(); //若接口调用失败会返回xml格式的结果 if (response.Substring(0, 5) == "<xml>") { result.FromXml(response); } //接口调用成功则返回非xml格式的数据 else { result.SetValue("result", response); } return(result); }
/** * * 查询退款 * 提交退款申请后,通过该接口查询退款状态。退款有一定延时, * 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。 * out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个 * @param WxPayData inputObj 提交给查询退款API的参数 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData RefundQuery(WeChatConfig wcf, WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/refundquery"; //检测必填参数 if (!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id")) { throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"); } inputObj.SetValue("appid", wcf.appId); //公众账号ID inputObj.SetValue("mch_id", wcf.mchId); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 Log.Debug("WxPayApi", "RefundQuery request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "RefundQuery response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的结果转换为对象以返回 WxPayData result = new WxPayData(wcf); result.FromXml(response); ReportCostTime(wcf, url, timeCost, result);//测速上报 return(result); }
/** * * 撤销订单API接口 * @param WxPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回API调用结果,其他抛异常 */ public static WxPayData Reverse(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!"); } inputObj.SetValue("appid", WxPayConfig.GetConfig().GetAppID()); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID()); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_MD5); //签名类型 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 Log.Debug("WxPayApi", "Reverse request : " + xml); string response = HttpService.Post(xml, url, true, timeOut); Log.Debug("WxPayApi", "Reverse response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/** * * 统一下单 * @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")) { LogHelper.Error("out_trade_no 为空"); throw new WxPayException("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { LogHelper.Error("body 为空"); throw new WxPayException("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { LogHelper.Error("total_fee 为空"); throw new WxPayException("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { LogHelper.Error("trade_type 为空"); throw new WxPayException("缺少统一支付接口必填参数trade_type!"); } //LogHelper.Error("openid " + inputObj.IsSet("openid")); //关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { LogHelper.Error("openid 为空"); throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { LogHelper.Error("trade_type 为空"); 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 } //LogHelper.Error("获取result"); inputObj.SetValue("appid", WxPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID); //商户号 string ip = ""; System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; for (int i = 0; i < addressList.Length; i++) { ip = addressList[i].ToString(); } inputObj.SetValue("spbill_create_ip", ip); //终端ip inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); var start = DateTime.Now; string response = HttpService.Post(xml, url, false, timeOut); //LogHelper.Error("---UnfiedOrder response : " + response + "---UnfiedOrder request : " + xml + "---获取nonce_str" + GenerateNonceStr()); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); //LogHelper.Error("UnfiedOrder request : " + result); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/** * * 统一下单 * @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", GenerateNonceStr());//随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return result; }
/** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws Exception * @return 成功时返回,其他抛异常 */ public static async Task <WxPayData> UnifiedOrder(WxPayData inputObj, int timeOut = 6) { var url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new Exception("缺少统一支付接口必填参数out_trade_no!"); } if (!inputObj.IsSet("body")) { throw new Exception("缺少统一支付接口必填参数body!"); } if (!inputObj.IsSet("total_fee")) { throw new Exception("缺少统一支付接口必填参数total_fee!"); } if (!inputObj.IsSet("trade_type")) { throw new Exception("缺少统一支付接口必填参数trade_type!"); } //关联参数 if ((inputObj.GetValue("trade_type").ToString() == "JSAPI") && !inputObj.IsSet("openid")) { throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if ((inputObj.GetValue("trade_type").ToString() == "NATIVE") && !inputObj.IsSet("product_id")) { throw new Exception("统一支付接口中,缺少必填参数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", GenerateNonceStr()); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); var xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); var response = await HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); var end = DateTime.Now; var timeCost = (int)(end - start).TotalMilliseconds; var result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/** * * 撤销订单API接口 * @param WxPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回API调用结果,其他抛异常 */ public static WxPayData Reverse(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 Log.Debug("WxPayApi", "Reverse request : " + xml); string response = HttpService.Post(xml, url, true, timeOut); Log.Debug("WxPayApi", "Reverse response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return result; }
/** * * 转换短链接 * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX), * 减小二维码数据量,提升扫描速度和精确度。 * @param WxPayData inputObj 提交给转换短连接API的参数 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData ShortUrl(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/tools/shorturl"; //检测必填参数 if(!inputObj.IsSet("long_url")) { throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串 inputObj.SetValue("sign",inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 Log.Debug("WxPayApi", "ShortUrl request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "ShortUrl response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return result; }
/** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData UnifiedOrder(WxPayData inputObj, string city, 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 } string nonce_str = GenerateNonceStr(); inputObj.SetValue("appid", WxPayConfig.APPID(city)); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.getMCHID(city)); //商户号 inputObj.SetValue("spbill_create_ip", WxPayConfig.IP); //终端ip inputObj.SetValue("nonce_str", nonce_str); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); //ReportCostTime(url, timeCost, result);//测速上报 WxPayData tmp = new WxPayData(city); tmp.SetValue("appId", WxPayConfig.APPID(city)); //公众账号ID tmp.SetValue("nonceStr", nonce_str); //随机字符串 tmp.SetValue("package", "prepay_id=" + result.GetValue("prepay_id")); tmp.SetValue("signType", "MD5"); tmp.SetValue("timeStamp", GenerateTimeStamp()); tmp.SetValue("paySign", tmp.MakeSign()); return(tmp); }
/** * * 测速上报接口实现 * @param WxPayData inputObj 提交给测速上报接口的参数 * @param int timeOut 测速上报接口超时时间 * @throws WxPayException * @return 成功时返回测速上报接口返回的结果,其他抛异常 */ public static WxPayData Report(WxPayData inputObj, int timeOut = 1) { string url = "https://api.mch.weixin.qq.com/payitil/report"; //检测必填参数 if(!inputObj.IsSet("interface_url")) { throw new WxPayException("接口URL,缺少必填参数interface_url!"); } if(!inputObj.IsSet("return_code")) { throw new WxPayException("返回状态码,缺少必填参数return_code!"); } if(!inputObj.IsSet("result_code")) { throw new WxPayException("业务结果,缺少必填参数result_code!"); } if(!inputObj.IsSet("user_ip")) { throw new WxPayException("访问接口IP,缺少必填参数user_ip!"); } if(!inputObj.IsSet("execute_time_")) { throw new WxPayException("接口耗时,缺少必填参数execute_time_!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号 inputObj.SetValue("user_ip",WxPayConfig.IP);//终端ip inputObj.SetValue("time",DateTime.Now.ToString("yyyyMMddHHmmss"));//商户上报时间 inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串 inputObj.SetValue("sign",inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); Log.Info("WxPayApi", "Report request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Info("WxPayApi", "Report response : " + response); WxPayData result = new WxPayData(); result.FromXml(response); return result; }
public ActionResult RechargePay(double money, string ip) { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; string orderNo = Guid.NewGuid().ToString().Replace("-", "").ToUpper().Substring(0, 6) + TimeManager.GetCurrentTimestamp(); ViewBag.OrderNo = orderNo; ViewBag.TotalPrice = money; OrderResultModel orm = new OrderResultModel(); orm.openid = System.Web.HttpContext.Current.Session["member"].ToString(); orm.total_fee = money * 100; orm.trade_type = "JSAPI"; orm.spbill_create_ip = ip; orm.out_trade_no = orderNo; orm.appid = WxPayAPI.WxPayConfig.APPID; orm.body = "捷诚宝个人中心充值"; orm.mch_id = WxPayAPI.WxPayConfig.MCHID; orm.nonce_str = WxPayAPI.WxPayApi.GenerateNonceStr(); orm.notify_url = HttpContext.Request.Url.Scheme + "://" + HttpContext.Request.Url.Host + ":" + HttpContext.Request.Url.Port + "/Pay/RechargePayResult"; LogHelper.Log.Write(orm.notify_url); WxPayAPI.WxPayData data = new WxPayAPI.WxPayData(); data.SetValue("openid", orm.openid); data.SetValue("total_fee", orm.total_fee); data.SetValue("trade_type", orm.trade_type); data.SetValue("spbill_create_ip", orm.spbill_create_ip); data.SetValue("out_trade_no", orm.out_trade_no); data.SetValue("appid", orm.appid); data.SetValue("body", orm.body); data.SetValue("mch_id", orm.mch_id); data.SetValue("nonce_str", orm.nonce_str); data.SetValue("notify_url", orm.notify_url); orm.sign = data.MakeSign(); data.SetValue("sign", orm.sign); //LogHelper.Log.Write("openid:" + data.GetValue("openid")); //LogHelper.Log.Write("total_fee:" + data.GetValue("total_fee")); //LogHelper.Log.Write("appid:" + data.GetValue("appid")); //LogHelper.Log.Write("notify_url:" + data.GetValue("notify_url")); string xml = data.ToXml(); string response = WxPayAPI.HttpService.Post(xml, url, false, 5); WxPayAPI.WxPayData result = new WxPayAPI.WxPayData(); result.FromXml(response); WxPayAPI.WxPayData jsApiParam = new WxPayAPI.WxPayData(); jsApiParam.SetValue("appId", result.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayAPI.WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayAPI.WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + result.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); string jsonParam = jsApiParam.ToJson(); ViewData["Result"] = result; ViewData["JsonResult"] = jsonParam; return(View()); }
/** * * 申请退款 * @param WxPayData inputObj 提交给申请退款API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回接口调用结果,其他抛异常 */ public static WxPayData Refund(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!"); } else if (!inputObj.IsSet("out_refund_no")) { throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("退款申请接口中,缺少必填参数total_fee!"); } else if (!inputObj.IsSet("refund_fee")) { throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!"); } else if (!inputObj.IsSet("op_user_id")) { throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!"); } inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "Refund request : " + xml); string response = HttpService.Post(xml, url, true, timeOut);//调用HTTP通信接口提交数据到API Log.Debug("WxPayApi", "Refund 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; }
/** * * 统一下单 * @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 } HttpRequest requst = HttpContext.Current.Request; inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号 string ip = "58.247.11.229";// GetIP();// requst.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(ip)) { //ip = requst.ServerVariables["REMOTE_ADDR"]; } System.IO.File.AppendAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), DateTime.Now.ToString() + ":ip = " + ip + Environment.NewLine); inputObj.SetValue("spbill_create_ip", requst.UserHostAddress);//WxPayConfig.IP);//终端ip requst.UserHostAddress);// inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); System.IO.File.AppendAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), DateTime.Now.ToString() + ":组装xml = " + xml + Environment.NewLine); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); ////发起请求 string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); System.IO.File.AppendAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), DateTime.Now.ToString() + ":返回数据: = " + response + Environment.NewLine); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return result; }
/** * * 统一下单 * @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.GetConfig().GetNotifyUrl());//异步通知url } inputObj.SetValue("appid", WxPayConfig.GetConfig().GetAppID()); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID()); //商户号 inputObj.SetValue("spbill_create_ip", WxPayConfig.GetConfig().GetIp()); //终端ip inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_MD5); //签名类型 //签名 inputObj.SetValue("sign", inputObj.MakeSign(WxPayData.SIGN_TYPE_MD5)); string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); 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()); string sql = "select support_to_openid,support_money,support_by_nickname,is_send_to_sopport,is_send_to_join,is_pay_successed,support_by_openid from action_support_people where inner_order_num = '" + data.GetValue("out_trade_no").ToString() + "'"; DataTable dt = DTcms.DBUtility.DbHelperSQL.Query(sql).Tables[0]; string support_money = "0.00", support_name = "", support_to_name = ""; string join_id = "-1"; string joiner_openid = "", support_openid = ""; bool is_send_to_sopport = false, is_send_to_join = false, is_pay_successed = false; if (dt != null && dt.Rows.Count > 0) { joiner_openid = dt.Rows[0][0].ToString(); support_money = dt.Rows[0][1].ToString(); support_name = dt.Rows[0][2].ToString(); if (dt.Rows[0][3].ToString() == "1") { is_send_to_sopport = true; } if (dt.Rows[0][4].ToString() == "1") { is_send_to_join = true; } if (dt.Rows[0][5].ToString() == "1") { is_pay_successed = true; } support_openid = dt.Rows[0][6].ToString(); } if (!is_pay_successed) { sql = "update action_support_people set is_pay_successed = 1 where inner_order_num = '" + data.GetValue("out_trade_no").ToString() + "' and is_pay_successed=0"; DTcms.DBUtility.DbHelperSQL.ExecuteSql(sql); sql = "select sum(support_money) from action_support_people where support_to_openid = '" + joiner_openid + "' and is_pay_successed=1"; dt = DTcms.DBUtility.DbHelperSQL.Query(sql).Tables[0]; if (dt != null && dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0][0].ToString()) && Convert.ToDecimal(dt.Rows[0][0].ToString()) >= globalField.target_money) { sql = "update action_join_people set is_done = 1,done_date = getdate() where people_openid = '" + joiner_openid + "'"; DTcms.DBUtility.DbHelperSQL.ExecuteSql(sql); } sql = "select people_name,id from action_join_people where people_openid = '" + joiner_openid + "'"; dt = DTcms.DBUtility.DbHelperSQL.Query(sql).Tables[0]; if (dt != null && dt.Rows.Count > 0) { support_to_name = dt.Rows[0][0].ToString(); join_id = dt.Rows[0][1].ToString(); } var accessToken = AccessTokenContainer.GetAccessToken("wx9511a139072c5d5e"); //发送给支持者 if (!is_send_to_sopport) { var testData = new //TestTemplateData() { first = new TemplateDataItem("感谢您的支持!"), keyword1 = new TemplateDataItem(data.GetValue("out_trade_no").ToString()), keyword2 = new TemplateDataItem("“觉醒之路”首届商界精英茶马古道挑战赛"), keyword3 = new TemplateDataItem("被支持人:" + support_to_name), keyword4 = new TemplateDataItem(support_money + "元"), keyword5 = new TemplateDataItem(DateTime.Now.ToString("yyyy-MM-dd")), remark = new TemplateDataItem("感谢您的支持!") }; string templateid = "K2WtNl3zArzx3KsKGT2e3Ph7Fud64oIhMHC4XvUFoVA"; string redirect_url = "http://" + "www.suzhoulvtu.cn" + "/mobile/index.aspx?id=" + join_id; var result = TemplateApi.SendTemplateMessage(accessToken, support_openid, templateid, "#FF0000", redirect_url, testData); sql = "update action_support_people set is_send_to_sopport = 1 where inner_order_num = '" + data.GetValue("out_trade_no").ToString() + "'"; DTcms.DBUtility.DbHelperSQL.ExecuteSql(sql); } //发送给被支持人 if (!is_send_to_join) { var testData = new //TestTemplateData() { first = new TemplateDataItem("您收到了他人的支持!"), keyword1 = new TemplateDataItem(data.GetValue("out_trade_no").ToString()), keyword2 = new TemplateDataItem("“觉醒之路”首届商界精英茶马古道挑战赛"), keyword3 = new TemplateDataItem("支持人:" + support_name), keyword4 = new TemplateDataItem(support_money + "元"), keyword5 = new TemplateDataItem(DateTime.Now.ToString("yyyy-MM-dd")), remark = new TemplateDataItem("感谢您的支持!") }; string templateid = "K2WtNl3zArzx3KsKGT2e3Ph7Fud64oIhMHC4XvUFoVA"; string redirect_url = "http://" + "www.suzhoulvtu.cn" + "/mobile/index.aspx?id=" + join_id; var result = TemplateApi.SendTemplateMessage(accessToken, joiner_openid, templateid, "#FF0000", redirect_url, testData); sql = "update action_support_people set is_send_to_join = 1 where inner_order_num = '" + data.GetValue("out_trade_no").ToString() + "'"; DTcms.DBUtility.DbHelperSQL.ExecuteSql(sql); } } } catch (Exception 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()); page.Response.Write(res.ToXml()); page.Response.End(); } Log.Info(this.GetType().ToString(), "Check sign success"); return(data); }
/// <summary> /// 微信支付专用逻辑 /// </summary> /// <returns></returns> public ActionResult WxPay() { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; ViewBag.OrderNo = Request.QueryString["orderNo"].ToString(); ViewBag.TotalPrice = double.Parse(Request.QueryString["totalprice"].ToString()); OrderResultModel orm = new OrderResultModel(); orm.openid = System.Web.HttpContext.Current.Session["member"].ToString(); orm.total_fee = double.Parse(Request.QueryString["totalprice"].ToString()) * 100; orm.trade_type = "JSAPI"; orm.spbill_create_ip = Request.QueryString["ip"].ToString(); orm.out_trade_no = Request.QueryString["orderNo"].ToString(); orm.appid = WxPayAPI.WxPayConfig.APPID; orm.body = "捷诚宝商城"; orm.mch_id = WxPayAPI.WxPayConfig.MCHID; orm.nonce_str = WxPayAPI.WxPayApi.GenerateNonceStr(); orm.notify_url = HttpContext.Request.Url.Scheme + "://" + HttpContext.Request.Url.Host + ":" + HttpContext.Request.Url.Port + "/Pay/WxPayResult"; WxPayAPI.WxPayData data = new WxPayAPI.WxPayData(); data.SetValue("openid", orm.openid); data.SetValue("total_fee", orm.total_fee); data.SetValue("trade_type", orm.trade_type); data.SetValue("spbill_create_ip", orm.spbill_create_ip); data.SetValue("out_trade_no", orm.out_trade_no); data.SetValue("appid", orm.appid); data.SetValue("body", orm.body); data.SetValue("mch_id", orm.mch_id); data.SetValue("nonce_str", orm.nonce_str); data.SetValue("notify_url", orm.notify_url); orm.sign = data.MakeSign(); data.SetValue("sign", orm.sign); foreach (var item in data.GetValues()) { LogHelper.Log.Write(item.Key + ":" + item.Value); } string xml = data.ToXml(); string response = WxPayAPI.HttpService.Post(xml, url, false, 5); WxPayAPI.WxPayData result = new WxPayAPI.WxPayData(); result.FromXml(response); WxPayAPI.WxPayData jsApiParam = new WxPayAPI.WxPayData(); jsApiParam.SetValue("appId", result.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayAPI.WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayAPI.WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + result.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); string jsonParam = jsApiParam.ToJson(); ViewData["Result"] = result; ViewData["JsonResult"] = jsonParam; return(View()); }
/** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData UnifiedOrder(WxPayData inputObj, WxPayConfig config, 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 inputObj.SetValue("notify_url", config.NOTIFY_URL); //异步通知url inputObj.SetValue("appid", config.APPID); //公众账号ID inputObj.SetValue("mch_id", config.MCHID); //商户号 //inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip string strNonce_str = GenerateNonceStr(); inputObj.SetValue("nonce_str", strNonce_str);//随机字符串 //签名 string strSign = inputObj.MakeSign(config.KEY); inputObj.SetValue("sign", strSign); string xml = inputObj.ToXml(); Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); WxPayData result = new WxPayData(); result.FromXml(response); result.SetValue("nonce_str", strNonce_str); result.SetValue("sign", strSign); return(result); }
/** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData UnifiedOrder(WxPayData inputObj, WxPayConfig config, int timeOut = 12) { 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", config.NOTIFY_URL);//异步通知url } inputObj.SetValue("appid", config.APPID); //公众账号ID inputObj.SetValue("mch_id", config.MCHID); //商户号 inputObj.SetValue("spbill_create_ip", config.IP); //终端ip inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign(config)); string xml = inputObj.ToXml(); var start = DateTime.Now; string response = HttpService.Post(config, xml, url, false, timeOut); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); result.CheckSign(config); //ReportCostTime(config, url, timeCost, result);//测速上报 return(result); }
/** * * 查询退款 * 提交退款申请后,通过该接口查询退款状态。退款有一定延时, * 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。 * out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个 * @param WxPayData inputObj 提交给查询退款API的参数 * @param int timeOut 接口超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData RefundQuery(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/refundquery"; //检测必填参数 if(!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id")) { throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"); } inputObj.SetValue("appid",WxPayConfig.APPID);//公众账号ID inputObj.SetValue("mch_id",WxPayConfig.MCHID);//商户号 inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串 inputObj.SetValue("sign",inputObj.MakeSign());//签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 Log.Debug("WxPayApi", "RefundQuery request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API Log.Debug("WxPayApi", "RefundQuery 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; }
/** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 100) { 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 string ip = HttpContext.Current.Request.UserHostAddress; inputObj.SetValue("spbill_create_ip", ip); //终端ip inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); //throw new WxPayException("<span style='color:#00CD00;font-size:20px'>" + inputObj.ToPrintStr() + "</span>"); string xml = inputObj.ToXml(); // throw new WxPayException("<span style='color:#00CD00;font-size:20px'>" + xml+ "</span>"); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); //throw new WxPayException("<span style='color:#00CD00;font-size:20px'>123</span>"); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); // throw new WxPayException("<span style='color:#00CD00;font-size:20px'>" + result.ToPrintStr() + "</span>"); ReportCostTime(url, timeCost, result);//测速上报 // throw new WxPayException("成功!"+result); return(result); }
public static WxPayData UnifiedTransfer(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; //检测必填参数 if (!inputObj.IsSet("partner_trade_no")) { throw new WxPayException("缺少统一支付接口必填参数partner_trade_no!"); } else if (!inputObj.IsSet("openid")) { throw new WxPayException("缺少统一支付接口必填参数openid!"); } else if (!inputObj.IsSet("amount")) { throw new WxPayException("缺少统一支付接口必填参数amount!"); } else if (!inputObj.IsSet("desc")) { throw new WxPayException("缺少统一支付接口必填参数desc!"); } //关联参数 //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("mch_appid", WxPayConfig.APPID); //公众账号ID inputObj.SetValue("mchid", WxPayConfig.MCHID); //商户号 inputObj.SetValue("spbill_create_ip", WxPayConfig.IP); //终端ip inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 /* * NO_CHECK:不校验真实姓名 * FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账) * OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功) */ inputObj.SetValue("check_name", "NO_CHECK");//校验用户姓名选项 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "UnifiedTransfer request : " + xml); string response = HttpService.Post(xml, url, true, timeOut); Log.Debug("WxPayApi", "UnifiedTransfer response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); //ReportCostTime(url, timeCost, result);//测速上报 return(result); }