请先下载官方的DEMO,下载地址:https://pay.weixin.qq.com/wiki/doc/api/download/WxPayAPI_CS.zip
然后做如下修改,先修改Config.cs为自己的商户号,密钥,公众号等信息
在Data.cs代码文件增加一个方法用于,生成沙箱测试的签名
/// <summary> /// 获取沙箱密钥 /// </summary> /// <returns></returns> public string MakeSandboxSign() { //转url格式 string str = ToUrl(); //在string后加入API KEY str += "&key=" + WxPayApi.SandboxGetSignKey().GetValue("sandbox_signkey"); //MD5加密 var md5 = MD5.Create(); var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); var sb = new StringBuilder(); foreach (byte b in bs) { sb.Append(b.ToString("x2")); } //所有字符转为大写 return sb.ToString().ToUpper(); }
在WxPayApi.cs文件中增加下列几个方法,完成业务逻辑,这里采用的是1003+1004+1005示例验收方式,
/// <summary> /// 获取沙箱密钥 /// </summary> /// <returns></returns> public static WxPayData SandboxGetSignKey() { string url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey"; //沙箱密钥获取api WxPayData wpd = new WxPayData(); wpd.SetValue("mch_id", WxPayConfig.MCHID); wpd.SetValue("nonce_str", GenerateNonceStr()); wpd.SetValue("sign", wpd.MakeSign()); string xml = wpd.ToXml(); var start = DateTime.Now; //请求开始时间 string response = HttpService.Post(xml, url, false, 6); 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> /// <param name="OutTradeNo"></param> /// <returns></returns> public static WxPayData SandboxCreateOrder(string OutTradeNo) { string url = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"; WxPayData wpd = new WxPayData(); wpd.SetValue("appid", WxPayConfig.APPID); wpd.SetValue("mch_id", WxPayConfig.MCHID); wpd.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); wpd.SetValue("body", "验收测试"); wpd.SetValue("out_trade_no", OutTradeNo); wpd.SetValue("total_fee", "552");//1003测试此处为551,1004测试完毕要改为552 wpd.SetValue("spbill_create_ip", "127.0.0.1"); wpd.SetValue("notify_url", "http://www.weixin.qq.com/wxpay/pay.php"); wpd.SetValue("trade_type", "NATIVE"); wpd.SetValue("sign", wpd.MakeSandboxSign()); string xml = wpd.ToXml(); var start = DateTime.Now; //请求开始时间 string response = HttpService.Post(xml, url, false, 6); 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> /// <param name="OutTradeNo"></param> /// <returns></returns> public static WxPayData SandboxQuery(string OutTradeNo) { string url = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery"; WxPayData wpd = new WxPayData(); wpd.SetValue("appid", WxPayConfig.APPID); wpd.SetValue("mch_id", WxPayConfig.MCHID); wpd.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); wpd.SetValue("out_trade_no", OutTradeNo); wpd.SetValue("sign", wpd.MakeSandboxSign()); string xml = wpd.ToXml(); var start = DateTime.Now; //请求开始时间 string response = HttpService.Post(xml, url, false, 6); 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> /// <param name="OutTradeNo"></param> /// <param name="OutRefundNo">退款单号</param> /// <returns></returns> public static WxPayData SandboxRefund(string OutTradeNo, string OutRefundNo) { string url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refund"; WxPayData wpd = new WxPayData(); wpd.SetValue("appid", WxPayConfig.APPID); wpd.SetValue("mch_id", WxPayConfig.MCHID); wpd.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); wpd.SetValue("out_trade_no", OutTradeNo); wpd.SetValue("out_refund_no", OutRefundNo); wpd.SetValue("total_fee", "552"); wpd.SetValue("refund_fee", "551"); wpd.SetValue("sign", wpd.MakeSandboxSign()); string xml = wpd.ToXml(); var start = DateTime.Now; //请求开始时间 string response = HttpService.Post(xml, url, false, 6); 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> /// <param name="OutTradeNo"></param> /// <returns></returns> public static WxPayData SandboxRefundQuery(string OutTradeNo) { string url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery"; WxPayData wpd = new WxPayData(); wpd.SetValue("appid", WxPayConfig.APPID); wpd.SetValue("mch_id", WxPayConfig.MCHID); wpd.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); wpd.SetValue("out_trade_no", OutTradeNo); wpd.SetValue("sign", wpd.MakeSandboxSign()); string xml = wpd.ToXml(); var start = DateTime.Now; //请求开始时间 string response = HttpService.Post(xml, url, false, 6); 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> /// <param name="BillDate"></param> /// <param name="BillType"></param> /// <returns></returns> public static WxPayData SandboxDownBill(string BillDate, string BillType) { string url = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill"; WxPayData wpd = new WxPayData(); wpd.SetValue("appid", WxPayConfig.APPID); wpd.SetValue("mch_id", WxPayConfig.MCHID); wpd.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); wpd.SetValue("bill_date", BillDate); wpd.SetValue("bill_type", BillType); wpd.SetValue("sign", wpd.MakeSandboxSign()); string xml = wpd.ToXml(); var start = DateTime.Now; //请求开始时间 string response = HttpService.Post(xml, url, false, 6); var end = DateTime.Now; int timeCost = (int) ((end - start).TotalMilliseconds); WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return result; }
前台测试页面代码
public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string outTradeNo = DateTime.Now.ToString("yyyyMMddHHMMssfff"); Label1.Text = outTradeNo; WxPayData wpd = WxPayApi.SandboxCreateOrder(outTradeNo); string code_url = wpd.GetValue("code_url").ToString(); Image1.ImageUrl = "MakeQRCode.aspx?data=" + HttpUtility.UrlEncode(code_url); Response.Write(wpd.ToXml()); } } protected void Button1_Click(object sender, EventArgs e) { WxPayData wpd = WxPayApi.SandboxQuery(Label1.Text); Label2.Text = wpd.ToPrintStr(); } protected void Button2_Click(object sender, EventArgs e) { WxPayData wpd = WxPayApi.SandboxRefund(Label1.Text, Label1.Text); Label2.Text = wpd.ToPrintStr(); } protected void Button3_Click(object sender, EventArgs e) { WxPayData wpd = WxPayApi.SandboxRefundQuery(Label1.Text); Label2.Text = Unicode2String(wpd.ToPrintStr()); } /// <summary> /// Unicode转字符串 /// </summary> /// <param name="source">经过Unicode编码的字符串</param> /// <returns>正常字符串</returns> public static string Unicode2String(string source) { return new Regex(@"\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace( source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16))); } protected void Button4_Click(object sender, EventArgs e) { WxPayData wpd = WxPayApi.SandboxDownBill("20190507", "ALL"); Label2.Text = Unicode2String(wpd.ToXml()); } protected void Button5_Click(object sender, EventArgs e) { WxPayData wpd = WxPayApi.SandboxDownBill("20190507", "SUCCESS"); Label2.Text = Unicode2String(wpd.ToXml()); } protected void Button6_Click(object sender, EventArgs e) { WxPayData wpd = WxPayApi.SandboxDownBill("20190507", "REFUND"); Label2.Text = Unicode2String(wpd.ToXml()); } }
希望对大家有帮助~