/// <summary> /// 传入ProductName,OrderNumber,TotalFee,TimeExpire,OpenId(可选),TradeType,NotifyUrl(可选) /// </summary> /// <param name="param"></param> /// <param name="context"></param> /// <returns></returns> public string GetUnifiedOrder(PayParam param = null, HttpContextBase context = null) { if (param.TotalFee == null || string.IsNullOrWhiteSpace(param.ProductName) || string.IsNullOrWhiteSpace(param.OrderNumber) || string.IsNullOrWhiteSpace(param.TimeExpire) || param.TradeType == null) { return("参数错误"); } var req = new RequestHandler(context); req.SetKey(GetConfig.GetKey(param)); req.SetParameter("appid", GetConfig.GetAppid(param)); req.SetParameter("mch_id", GetConfig.GetMchId(param)); req.SetParameter("nonce_str", GetNoncestr()); req.SetParameter("body", param.ProductName); req.SetParameter("out_trade_no", param.OrderNumber); req.SetParameter("total_fee", param.TotalFee.ToString()); req.SetParameter("spbill_create_ip", IPHelper.GetVisitorIP()); req.SetParameter("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); req.SetParameter("time_expire", param.TimeExpire); req.SetParameter("notify_url", string.IsNullOrWhiteSpace(param.NotifyUrl) ? GetConfig.GetNotify(param) : param.NotifyUrl); req.SetParameter("trade_type", param.TradeType.ToString()); if (!string.IsNullOrWhiteSpace(param.OpenId)) { req.SetParameter("openid", param.OpenId); } req.SetParameter("sign", req.CreateMd5Sign()); var reqXml = req.ParseXml(); //LogHelper.WriteLog("aa_", "reqXml的值是:" + reqXml); var http = new HttpUtil(); http.SetCharset(context == null ? HttpContext.Current.Request.ContentEncoding.BodyName : context.Request.ContentEncoding.BodyName); var result = http.Send(reqXml, ApiList.UnifiedOrderUrl); return(result); }
/// <summary> /// billDate格式 20141212 /// </summary> /// <param name="context"></param> /// <param name="billDate"></param> /// <returns></returns> public BaseResult DownloadBill(HttpContextBase context, string billDate,WeChatParam param) { var packageReq = new RequestHandler(context); packageReq.SetKey(GetConfig.GetKey(param)); packageReq.SetParameter("appid", GetConfig.GetAppid(param)); packageReq.SetParameter("mch_id", GetConfig.GetMchId(param)); packageReq.SetParameter("nonce_str", GetNoncestr()); packageReq.SetParameter("bill_date", billDate); packageReq.SetParameter("bill_type", "ALL"); packageReq.SetParameter("sign", packageReq.CreateMd5Sign()); var reqXml = packageReq.ParseXml(); var httpClient = new HttpUtil(); httpClient.SetCharset(context.Request.ContentEncoding.BodyName); var result = httpClient.Send(reqXml, ApiList.DownloadBillUrl); try { var xe = XElement.Parse(result, LoadOptions.SetLineInfo); var reResult1 = xe.GetElement("return_code") == null ? "" : xe.GetElement("return_code").Value; var reResult2 = xe.GetElement("return_msg") == null ? "" : xe.GetElement("return_msg").Value; return new BaseResult() { IsSuccess = false, Data = "", Message = reResult1 +"_" + reResult2 }; } catch (Exception) { var list = new List<Bill>(); var myList = result.Replace("\r\n", "|").Split('|').Skip(1).ToList<string>(); myList.RemoveAt(myList.Count() - 1); myList.RemoveAt(myList.Count() - 1); myList.RemoveAt(myList.Count() - 1); string[] arr; foreach (var str in myList) { arr = str.Replace("`", "").Split(','); #region 赋值 list.Add(new Bill() { 交易时间 = arr[0], 公众账号ID = arr[1], 商户号 = arr[2], 子商户号 = arr[3], 设备号 = arr[4], 微信订单号 = arr[5], 商户订单号 = arr[6], 用户标识 = arr[7], 交易类型 = arr[8], 交易状态 = arr[9], 付款银行 = arr[10], 货币种类 = arr[11], 总金额 = arr[12], 企业红包金额 = arr[13], 微信退款单号 = arr[14], 商户退款单号 = arr[15], 退款金额 = arr[16], 企业红包退款金额 = arr[17], 退款类型 = arr[18], 退款状态 = arr[19], 商品名称 = arr[20], 商户数据包 = arr[21], 手续费 = arr[22], 费率 = arr[23] }); #endregion } return new BaseResult() { IsSuccess = true, Data = list }; } }
/// <summary> /// 传入ProductName,OrderNumber,TotalFee,TimeExpire,OpenId(可选),TradeType,NotifyUrl(可选) /// </summary> /// <param name="param"></param> /// <param name="context"></param> /// <returns></returns> public string GetUnifiedOrder(PayParam param, HttpContextBase context) { if (param.TotalFee == null || string.IsNullOrWhiteSpace(param.ProductName) || string.IsNullOrWhiteSpace(param.OrderNumber) || string.IsNullOrWhiteSpace(param.TimeExpire) || param.TradeType == null) { return "参数错误"; } var req = new RequestHandler(context); req.SetKey(GetConfig.GetKey(param)); req.SetParameter("appid", GetConfig.GetAppid(param)); req.SetParameter("mch_id", GetConfig.GetMchId(param)); req.SetParameter("nonce_str", GetNoncestr()); req.SetParameter("body", param.ProductName); req.SetParameter("out_trade_no", param.OrderNumber); req.SetParameter("total_fee", param.TotalFee.ToString()); req.SetParameter("spbill_create_ip", IPHelper.GetVisitorIP()); req.SetParameter("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); req.SetParameter("time_expire", param.TimeExpire); req.SetParameter("notify_url", string.IsNullOrWhiteSpace(param.NotifyUrl) ? GetConfig.GetNotify(param) : param.NotifyUrl); req.SetParameter("trade_type", param.TradeType.ToString()); if (!string.IsNullOrWhiteSpace(param.OpenId)) { req.SetParameter("openid", param.OpenId); } req.SetParameter("sign", req.CreateMd5Sign()); var reqXml = req.ParseXml(); //LogHelper.WriteLog("aa_", "reqXml的值是:" + reqXml); var http = new HttpUtil(); http.SetCharset(context.Request.ContentEncoding.BodyName); var result = http.Send(reqXml, ApiList.UnifiedOrderUrl); return result; }
/// <summary> /// 传入订单号OrderNumber,RefundNumber,总金额total_fee(分),RefundFee退款金额(分), /// </summary> /// <param name="context"></param> /// <param name="param"></param> /// <returns></returns> public BaseResult Refund(HttpContextBase context, PayParam param) { if (param.TotalFee == null || param.RefundFee == null || string.IsNullOrWhiteSpace(param.OrderNumber) || string.IsNullOrWhiteSpace(param.RefundNumber)) { return new BaseResult() { IsSuccess = false, Message = "参数错误!" }; } #region 财付通退款,已OK //var packageReq = new RequestHandler(context); //packageReq.SetKey(Key); //packageReq.SetParameter("partner", "1225604801"); //packageReq.SetParameter("out_trade_no", param.OrderNumber); //packageReq.SetParameter("out_refund_no", param.OrderNumber); //packageReq.SetParameter("total_fee", param.TotalFee.Value.ToString(CultureInfo.InvariantCulture)); //packageReq.SetParameter("refund_fee", param.RefundFee.Value.ToString(CultureInfo.InvariantCulture)); //packageReq.SetParameter("op_user_id", "1225604801"); //packageReq.SetParameter("op_user_passwd", "111111"); //packageReq.SetParameter("sign", packageReq.CreateSign()); //var httpClient = new HttpUtil(); ////httpClient.SetCharset(context.Request.ContentEncoding.BodyName); ////这里很神奇,必须要用 GB2312编码,不能通过 context.Request.ContentEncoding.BodyName获取编码 //httpClient.SetCharset("gb2312"); //httpClient.SetCertInfo(WeChatCertPath, WeChatCertPwd); //var reqXml = packageReq.GetRequestURL(); //var result = httpClient.Send(reqXml, "https://mch.tenpay.com/refundapi/gateway/refund.xml"); //var xe = XElement.Parse(result, LoadOptions.SetLineInfo); //return new BaseResult() { IsSuccess = false }; #endregion #region 微信退款 var packageReq = new RequestHandler(context); packageReq.SetKey(GetConfig.GetKey(param)); packageReq.SetParameter("appid", GetConfig.GetAppid(param)); packageReq.SetParameter("mch_id", GetConfig.GetMchId(param)); packageReq.SetParameter("nonce_str", GetNoncestr()); packageReq.SetParameter("out_trade_no", param.OrderNumber); packageReq.SetParameter("out_refund_no", param.RefundNumber); packageReq.SetParameter("total_fee", (param.TotalFee.Value).ToString(CultureInfo.InvariantCulture)); packageReq.SetParameter("refund_fee", param.RefundFee.Value.ToString(CultureInfo.InvariantCulture)); packageReq.SetParameter("op_user_id", GetConfig.GetMchId(param)); packageReq.SetParameter("sign", packageReq.CreateMd5Sign()); var reqXml = packageReq.ParseXml(); var httpClient = new HttpUtil(); httpClient.SetCharset(context.Request.ContentEncoding.BodyName); httpClient.SetCertInfo(GetConfig.GetCertPath(param), GetConfig.GetCertPwd(param)); var result = httpClient.Send(reqXml, "https://api.mch.weixin.qq.com/secapi/pay/refund"); var xe = XElement.Parse(result, LoadOptions.SetLineInfo); var returnCode = xe.GetElement("return_code").Value; //退款成功 if (returnCode.Equals("SUCCESS")) { var resultCode = xe.GetElement("result_code").Value; if (resultCode.Equals("SUCCESS")) { var outTradeNo = xe.GetElement("out_trade_no").Value; //在外面回写订单 return new BaseResult() { IsSuccess = true, Data = new Dictionary<string, string> { {"OrderNumber", outTradeNo} } }; } } var errCodeDes = xe.GetElement("err_code_des") == null ? "" : xe.GetElement("err_code_des").Value; var returnMsg = xe.GetElement("return_msg") == null ? "" : xe.GetElement("return_msg").Value; return new BaseResult() { IsSuccess = false, Message = returnMsg + errCodeDes }; #endregion }
/// <summary> /// billDate格式 20141212 /// </summary> /// <param name="context"></param> /// <param name="billDate"></param> /// <returns></returns> public static BaseResult DownloadBill(string billDate, WeChatParam param) { var packageReq = new RequestHandler(); packageReq.SetKey(GetConfig.GetKey(param)); packageReq.SetParameter("appid", GetConfig.GetAppid(param)); packageReq.SetParameter("mch_id", GetConfig.GetMchId(param)); packageReq.SetParameter("nonce_str", GetNoncestr()); packageReq.SetParameter("bill_date", billDate); packageReq.SetParameter("bill_type", "ALL"); packageReq.SetParameter("sign", packageReq.CreateMd5Sign()); var reqXml = packageReq.ParseXml(); var httpClient = new HttpUtil(); httpClient.SetCharset(HttpContext.Current.Request.ContentEncoding.BodyName); var result = httpClient.Send(reqXml, ApiList.DownloadBillUrl); try { var xe = XElement.Parse(result, LoadOptions.SetLineInfo); var reResult1 = xe.GetElement("return_code") == null ? "" : xe.GetElement("return_code").Value; var reResult2 = xe.GetElement("return_msg") == null ? "" : xe.GetElement("return_msg").Value; return(new BaseResult() { IsSuccess = false, Data = "", Message = reResult1 + "_" + reResult2 }); } catch (Exception) { var list = new List <Bill>(); var myList = result.Replace("\r\n", "|").Split('|').Skip(1).ToList <string>(); myList.RemoveAt(myList.Count() - 1); myList.RemoveAt(myList.Count() - 1); myList.RemoveAt(myList.Count() - 1); string[] arr; foreach (var str in myList) { arr = str.Replace("`", "").Split(','); #region 赋值 list.Add(new Bill() { 交易时间 = arr[0], 公众账号ID = arr[1], 商户号 = arr[2], 子商户号 = arr[3], 设备号 = arr[4], 微信订单号 = arr[5], 商户订单号 = arr[6], 用户标识 = arr[7], 交易类型 = arr[8], 交易状态 = arr[9], 付款银行 = arr[10], 货币种类 = arr[11], 总金额 = arr[12], 企业红包金额 = arr[13], 微信退款单号 = arr[14], 商户退款单号 = arr[15], 退款金额 = arr[16], 企业红包退款金额 = arr[17], 退款类型 = arr[18], 退款状态 = arr[19], 商品名称 = arr[20], 商户数据包 = arr[21], 手续费 = arr[22], 费率 = arr[23] }); #endregion } return(new BaseResult() { IsSuccess = true, Data = list }); } }
/// <summary> /// 传入订单号OrderNumber,RefundNumber,总金额total_fee(分),RefundFee退款金额(分), /// </summary> /// <param name="context"></param> /// <param name="param"></param> /// <returns></returns> public static BaseResult Refund(PayParam param) { if (param.TotalFee == null || param.RefundFee == null || string.IsNullOrWhiteSpace(param.OrderNumber) || string.IsNullOrWhiteSpace(param.RefundNumber)) { return(new BaseResult() { IsSuccess = false, Message = "参数错误!" }); } #region 财付通退款,已OK //var packageReq = new RequestHandler(context); //packageReq.SetKey(Key); //packageReq.SetParameter("partner", "1225604801"); //packageReq.SetParameter("out_trade_no", param.OrderNumber); //packageReq.SetParameter("out_refund_no", param.OrderNumber); //packageReq.SetParameter("total_fee", param.TotalFee.Value.ToString(CultureInfo.InvariantCulture)); //packageReq.SetParameter("refund_fee", param.RefundFee.Value.ToString(CultureInfo.InvariantCulture)); //packageReq.SetParameter("op_user_id", "1225604801"); //packageReq.SetParameter("op_user_passwd", "111111"); //packageReq.SetParameter("sign", packageReq.CreateSign()); //var httpClient = new HttpUtil(); ////httpClient.SetCharset(context.Request.ContentEncoding.BodyName); ////这里很神奇,必须要用 GB2312编码,不能通过 context.Request.ContentEncoding.BodyName获取编码 //httpClient.SetCharset("gb2312"); //httpClient.SetCertInfo(WeChatCertPath, WeChatCertPwd); //var reqXml = packageReq.GetRequestURL(); //var result = httpClient.Send(reqXml, "https://mch.tenpay.com/refundapi/gateway/refund.xml"); //var xe = XElement.Parse(result, LoadOptions.SetLineInfo); //return new BaseResult() { IsSuccess = false }; #endregion #region 微信退款 var packageReq = new RequestHandler(); packageReq.SetKey(GetConfig.GetKey(param)); packageReq.SetParameter("appid", GetConfig.GetAppid(param)); packageReq.SetParameter("mch_id", GetConfig.GetMchId(param)); packageReq.SetParameter("nonce_str", GetNoncestr()); //packageReq.SetParameter("transaction_id", ""); packageReq.SetParameter("out_trade_no", param.OrderNumber); packageReq.SetParameter("out_refund_no", param.RefundNumber); packageReq.SetParameter("total_fee", (param.TotalFee.Value).ToString(CultureInfo.InvariantCulture)); packageReq.SetParameter("refund_fee", param.RefundFee.Value.ToString(CultureInfo.InvariantCulture)); packageReq.SetParameter("op_user_id", GetConfig.GetMchId(param)); packageReq.SetParameter("sign", packageReq.CreateMd5Sign()); var reqXml = packageReq.ParseXml(); var httpClient = new HttpUtil(); httpClient.SetCharset(HttpContext.Current.Request.ContentEncoding.BodyName); httpClient.SetCertInfo(GetConfig.GetCertPath(param), GetConfig.GetCertPwd(param)); var result = httpClient.Send(reqXml, "https://api.mch.weixin.qq.com/secapi/pay/refund"); var xe = XElement.Parse(result, LoadOptions.SetLineInfo); var returnCode = xe.GetElement("return_code").Value; //退款成功 if (returnCode.Equals("SUCCESS")) { var resultCode = xe.GetElement("result_code").Value; if (resultCode.Equals("SUCCESS")) { var outTradeNo = xe.GetElement("out_trade_no").Value; //在外面回写订单 return(new BaseResult() { IsSuccess = true, Data = new Dictionary <string, string> { { "OrderNumber", outTradeNo } } }); } } var errCodeDes = xe.GetElement("err_code_des") == null ? "" : xe.GetElement("err_code_des").Value; var returnMsg = xe.GetElement("return_msg") == null ? "" : xe.GetElement("return_msg").Value; return(new BaseResult() { IsSuccess = false, Message = returnMsg + errCodeDes }); #endregion }