官方API:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
首先要修改/lib/HttpService.cs
把Get和Post中的的设置代理服务器代码进行隐藏:
修改前:
WebProxy proxy = new WebProxy();
proxy.Address = new Uri(WxPayConfig.PROXY_URL);
request.Proxy = proxy;
修改后:
//WebProxy proxy = new WebProxy();
//proxy.Address = new Uri(WxPayConfig.PROXY_URL);
//request.Proxy = proxy;
正文:
(注:用户必须把项目放到外网连接上才可以进行回调,生成订单。例如:http://www.xxxx.com/Wx/example/NativeNotifyPage.aspx---微信公众平台配置的回调)
用户扫码微信二维码后,微信服务器端会回调到用户在微信公众平台配置的回调url:/example/NativeNotifyPage.aspx
NativeNotify nativeNatify = new NativeNotify(this);
nativeNatify.ProcessNotify();
ProcessNotify:(检查回调数据是否正常,正常就进行统一下单,统一成功下单后,用户可以加入自己的逻辑,最后返回结果给微信服务器确定下单)
public override void ProcessNotify() { WxPayData notifyData = GetNotifyData(); //检查openid和product_id是否返回 if (!notifyData.IsSet("openid") || !notifyData.IsSet("product_id")) { WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "回调数据异常"); Log.Info(this.GetType().ToString(), "The data WeChat post is error : " + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } //调统一下单接口,获得下单结果 string openid = notifyData.GetValue("openid").ToString(); string product_id = notifyData.GetValue("product_id").ToString(); WxPayData unifiedOrderResult = new WxPayData(); try { unifiedOrderResult = UnifiedOrder(openid, product_id); } catch(Exception ex)//若在调统一下单接口时抛异常,立即返回结果给微信支付后台 { WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "统一下单失败"); Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } //若下单失败,则立即返回结果给微信支付后台 if (!unifiedOrderResult.IsSet("appid") || !unifiedOrderResult.IsSet("mch_id") || !unifiedOrderResult.IsSet("prepay_id")) { WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "统一下单失败"); Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } //用户可以加入自己的逻辑 //统一下单成功,则返回成功结果给微信支付后台 WxPayData data = new WxPayData(); data.SetValue("return_code", "SUCCESS"); data.SetValue("return_msg", "OK"); data.SetValue("appid", WxPayConfig.APPID); data.SetValue("mch_id", WxPayConfig.MCHID); data.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); data.SetValue("prepay_id", unifiedOrderResult.GetValue("prepay_id")); data.SetValue("result_code", "SUCCESS"); data.SetValue("err_code_des", "OK"); data.SetValue("sign", data.MakeSign()); Log.Info(this.GetType().ToString(), "UnifiedOrder success , send data to WeChat : " + data.ToXml()); page.Response.Write(data.ToXml()); page.Response.End(); }
统一下单(用户可以根据自己的需要来修改参数,生成自己的订单):
private WxPayData UnifiedOrder(string openId,string productId) { //统一下单 WxPayData req = new WxPayData(); req.SetValue("body", "test");//这个是内容,显示在订单上的,这个内容用户可以自行修改 req.SetValue("attach", "test"); req.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); req.SetValue("total_fee", 1);//这个是金额,1代表1分钱,钱数用户也可以修改 req.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); req.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); req.SetValue("goods_tag", "test"); req.SetValue("trade_type", "NATIVE"); req.SetValue("openid", openId); req.SetValue("product_id", productId);//这个是商品的ID WxPayData result = WxPayApi.UnifiedOrder(req); return result; }
统一下单成功后(生成类似这样的订单):