/// <summary> /// 生成直接支付url,支付url有效期为2小时,模式二 /// NativePay.GetPayUrl /// </summary> /// <param name="productId">商品ID</param> /// <returns>模式二URL</returns> public static string GetNativePayUrl(string productId) { var methodString = TypeString + ".GetNativePrePayUrl"; WxPayLog.Info(methodString, "Native pay mode 2 url is producing..."); WxPayData data = new WxPayData(); data.SetValue("body", "test"); //商品描述 data.SetValue("attach", "test"); //附加数据 data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); //随机字符串 data.SetValue("total_fee", 1); //总金额 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); //交易起始时间 data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); //交易结束时间 data.SetValue("goods_tag", "jjj"); //商品标记 data.SetValue("trade_type", "NATIVE"); //交易类型 data.SetValue("product_id", productId); //商品ID WxPayData result = WxPayApi.UnifiedOrder(data); //调用统一下单接口 string url = result.GetValue("code_url").ToString(); //获得统一下单接口返回的二维码链接 WxPayLog.Info(methodString, "Get native pay mode 2 url : " + url); return(url); }
public static void ProcessNativeNotify(HttpContextBase httpContext) { var methodString = TypeString + ".ProcessNativeNotify"; WxPayData notifyData = GetNotifyData(httpContext); //检查openid和product_id是否返回 if (!notifyData.IsSet("openid") || !notifyData.IsSet("product_id")) { WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "回调数据异常"); WxPayLog.Info(methodString, "The data WeChat post is error : " + res.ToXml()); httpContext.Response.Write(res.ToXml()); httpContext.Response.End(); } //调统一下单接口,获得下单结果 string openid = notifyData.GetValue("openid").ToString(); string product_id = notifyData.GetValue("product_id").ToString(); WxPayData unifiedOrderResult = new WxPayData(); try { //统一下单 WxPayData req = new WxPayData(); req.SetValue("body", "test"); req.SetValue("attach", "test"); req.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); req.SetValue("total_fee", 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", product_id); unifiedOrderResult = WxPayApi.UnifiedOrder(req); } catch (Exception ex)//若在调统一下单接口时抛异常,立即返回结果给微信支付后台 { WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "统一下单失败"); WxPayLog.Error(methodString, "UnifiedOrder failure : " + res.ToXml()); httpContext.Response.Write(res.ToXml()); httpContext.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", "统一下单失败"); WxPayLog.Error(methodString, "UnifiedOrder failure : " + res.ToXml()); httpContext.Response.Write(res.ToXml()); httpContext.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()); WxPayLog.Info(methodString, "UnifiedOrder success , send data to WeChat : " + data.ToXml()); httpContext.Response.Write(data.ToXml()); httpContext.Response.End(); }