一.设置基本信息
1 //=======【基本信息设置】=====================================
2 /* 微信公众号信息配置
3 * APPID:绑定支付的APPID(必须配置)
4 * MCHID:商户号(必须配置)
5 * KEY:商户支付密钥,参考开户邮件设置(必须配置)
6 * APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置)
7 */
8 public const string APPID = "xx";
9 public const string MCHID = "xx";
10 public const string KEY = "xx";
11 public const string APPSECRET = "xx";
12
13 //=======【证书路径设置】=====================================
14 /* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要)
15 */
16 public const string SSLCERT_PATH = "cert/apiclient_cert.p12";
17 public const string SSLCERT_PASSWORD = "1233410002";
18
19
20
21 //=======【支付结果通知url】=====================================
22 /* 支付结果通知回调url,用于商户接收支付结果
23 */
24 public static string NOTIFY_URL = "http://" + HttpContext.Current.Request.Url.Authority.ToLower()+"/api/wx/notify_page.aspx";
二.传入参数设置
1 WxPayData data = new WxPayData();
2 int total=Convert.ToInt32(model.payfee)*100; //金额以分为单位 Int型
3 data.SetValue("body", username + "购买订单");//商品描述
4 data.SetValue("attach", username + "购买订单");//附加数据
5 data.SetValue("out_trade_no", orderno);//随机字符串
6 data.SetValue("total_fee",total.ToString());//总金额
7 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
8 data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
9 data.SetValue("goods_tag", username + "购买订单");//商品标记
10 data.SetValue("trade_type", "NATIVE");//交易类型 扫码
11 data.SetValue("product_id", model.orderid);//商品ID
WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
string code = result.GetValue("return_code").ToString(); //返回结果
if (code != "SUCCESS")
{
string msg = result.GetValue("return_msg").ToString();
Response.Redirect("~/error.aspx?msg=" + Server.UrlEncode(msg));
}
string url = result.GetValue("code_url").ToString();// 获得统一下单接口返回的二维码链接
return url;
四.生成二维码
1.引用 ThoughtWorks.QRCode.Codec.dll
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
qrCodeEncoder.QRCodeVersion = 0;
qrCodeEncoder.QRCodeScale = 4;
//将字符串生成二维码图片
Bitmap image = qrCodeEncoder.Encode(str, Encoding.Default);
//保存为PNG到内存流
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Png);
//输出二维码图片
Response.BinaryWrite(ms.GetBuffer());
Response.End();
三. 发送支付信息
1 public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6)
2 {
3 string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
4 //检测必填参数
5 if (!inputObj.IsSet("out_trade_no"))
6 {
7 throw new Exception("缺少统一支付接口必填参数out_trade_no!");
8 }
9 else if (!inputObj.IsSet("body"))
10 {
11 throw new Exception("缺少统一支付接口必填参数body!");
12 }
13 else if (!inputObj.IsSet("total_fee"))
14 {
15 throw new Exception("缺少统一支付接口必填参数total_fee!");
16 }
17 else if (!inputObj.IsSet("trade_type"))
18 {
19 throw new Exception("缺少统一支付接口必填参数trade_type!");
20 }
21
22 //关联参数
23 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
24 {
25 throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
26 }
27 if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
28 {
29 throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
30 }
31
32 //异步通知url未设置,则使用配置文件中的url
33 if (!inputObj.IsSet("notify_url"))
34 {
35 inputObj.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//异步通知url
36 }
37
38 inputObj.SetValue("appid", WxPayConfig.APPID);//公众账号ID
39 inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商户号
40 inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//终端ip
41 inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
42
43 //签名
44 inputObj.SetValue("sign", inputObj.MakeSign());
45 string xml = inputObj.ToXml();
46
47 var start = DateTime.Now;
48
49 Common.WebClient web = new Common.WebClient();
50 string response = HttpService.Post(xml, url, false, timeOut);
51 var end = DateTime.Now;
54 int timeCost = (int)((end - start).TotalMilliseconds);
55 WxPayData result = new WxPayData();
57 result.FromXml(response);
59 return result;
63 }