//获取返回值 public Boolean Getresult(int total_fee, string productId, string productName, string flag, string remark) { Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing..."); WxPayData data = new WxPayData(); data.SetValue("body", productName); //商品描述 data.SetValue("attach", remark); //附加数据 data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); //随机字符串 data.SetValue("total_fee", total_fee); //总金额 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); //交易起始时间 data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); //交易结束时间 data.SetValue("goods_tag", flag); //商品标记 data.SetValue("trade_type", "NATIVE"); //交易类型 data.SetValue("product_id", productId); //商品ID WxPayData result = WxPayApi.UnifiedOrder(data); //调用统一下单接口 if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS") { return(true); } else { return(false); } }
public WxPayData GetUPay(string openid, int total_fee, string city, string trade_nos) { string productId = "200"; WxPayData data = new WxPayData(city); string notify_url = "https://wx.hzgolong.com/wechat/ClearDebtCallback"; data.SetValue("body", "结清欠款"); //商品描述 data.SetValue("attach", "结清欠款,单号" + trade_nos); //附加数据 string out_trade_no = WxPayApi.GenerateOutTradeNo(city); data.SetValue("openid", openid); //总金额 data.SetValue("out_trade_no", out_trade_no); data.SetValue("total_fee", total_fee); //总金额 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); //交易起始时间 data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); //交易结束时间 data.SetValue("goods_tag", "realNameCheck"); //商品标记 data.SetValue("trade_type", "JSAPI"); //交易类型 data.SetValue("product_id", productId); //商品ID data.SetValue("notify_url", notify_url); WxPayData result = WxPayApi.UnifiedOrder(data, city);//调用统一下单接口 //string url = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接 result.SetValue("out_trade_no", out_trade_no); return(result); }
/** * 调用统一下单,获得下单结果 * @return 统一下单结果 * @失败时抛异常WxPayException */ public WxPayData GetUnifiedOrderResult() { //统一下单 WxPayData data = new WxPayData(); data.SetValue("body", "test"); data.SetValue("attach", "test"); data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); data.SetValue("total_fee", total_fee); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); data.SetValue("goods_tag", "test"); data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", openid); WxPayData result = WxPayApi.UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxPayException("UnifiedOrder response error!"); } unifiedOrderResult = result; return(result); }
/*** * 申请退款完整业务流程逻辑 * @param transaction_id 微信订单号(优先使用) * @param out_trade_no 商户订单号 * @param total_fee 订单总金额 * @param refund_fee 退款金额 * @return 退款结果(xml格式) */ public static WxPayData Run(string transaction_id, string out_trade_no, string total_fee, string refund_fee) { Log.Info("Refund", "Refund is processing..."); WxPayData data = new WxPayData(); if (!string.IsNullOrEmpty(transaction_id))//微信订单号存在的条件下,则已微信订单号为准 { data.SetValue("transaction_id", transaction_id); } else//微信订单号不存在,才根据商户订单号去退款 { data.SetValue("out_trade_no", out_trade_no); } data.SetValue("total_fee", int.Parse(total_fee)); //订单总金额 data.SetValue("refund_fee", int.Parse(refund_fee)); //退款金额 data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo()); //随机生成商户退款单号 data.SetValue("op_user_id", WxPayConfig.MCHID); //操作员,默认为商户号 WxPayData result = WxPayApi.Refund(data); //提交退款申请给API,接收返回数据 Log.Info("Refund", "Refund process complete, result : " + result.ToXml()); return(result); }
protected void Page_Load(object sender, EventArgs e) { Log.Info(this.GetType().ToString(), "page load"); NativePay nativePay = new NativePay(); OrderId = WxPayApi.GenerateOutTradeNo(); //生成扫码支付模式一url string url1 = nativePay.GetPrePayUrl("123456789"); //生成扫码支付模式二url string url2 = nativePay.GetPayUrl("123456789", OrderId); //将url生成二维码图片 Image1.ImageUrl = "MakeQRCode.aspx?data=" + HttpUtility.UrlEncode(url1); Image2.ImageUrl = "MakeQRCode.aspx?data=" + HttpUtility.UrlEncode(url2); }
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); 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); WxPayData result = WxPayApi.UnifiedOrder(req); return(result); }
/*** * 申请退款完整业务流程逻辑 * @param transaction_id 微信订单号(优先使用) * @param out_trade_no 商户订单号 * @param total_fee 订单总金额 * @param refund_fee 退款金额 * @return 退款结果(xml格式) */ public static WxPayData Run(string transaction_id, string out_trade_no, string total_fee, string refund_fee) { WxPayData data = new WxPayData(); if (!string.IsNullOrEmpty(transaction_id))//微信订单号存在的条件下,则已微信订单号为准 { data.SetValue("transaction_id", transaction_id); } else//微信订单号不存在,才根据商户订单号去退款 { data.SetValue("out_trade_no", out_trade_no); } data.SetValue("total_fee", int.Parse(total_fee)); //订单总金额 data.SetValue("refund_fee", int.Parse(refund_fee)); //退款金额 data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo()); //随机生成商户退款单号 data.SetValue("op_user_id", WxPayConfig.MCHID); //操作员,默认为商户号 return(WxPayApi.Refund(data)); //提交退款申请给API,接收返回数据 }
private AlipayOrder GetFee(string uid, string feeid, string from, string paytype, string key) { AlipayOrder fee = null; DataMsg m = new DataMsg(); BaseModel <DataSet> bs = null; //创建订单 IList <TextAndValue> tv = new List <TextAndValue>(); tv.Add(new TextAndValue("@uid", uid)); tv.Add(new TextAndValue("@feeid", feeid)); tv.Add(new TextAndValue("@from", from)); isFrom(tv, key); tv.Add(new TextAndValue("@paytype", paytype)); //1:支付宝,2:微信支付 string out_trade_no = WxPayApi.GenerateOutTradeNo(from); //请与贵网站订单系统中的唯一订单号匹配 tv.Add(new TextAndValue("@orderno", out_trade_no)); bs = com.zgyey.sgspay.core.DataProxy.SGS_PAYProxyData.GetList(tv, "payapp..sgs_CreateOrder", "tb1", "tb2"); DataSet ds = bs.result; m.code = int.Parse(ds.Tables[0].Rows[0][0].ToString()); m.info = ds.Tables[0].Rows[0][1].ToString(); if (m.code == 0) { fee = ( from x in ds.Tables[1].AsEnumerable() select new AlipayOrder() { feeid = x["fee_id"], orderNo = x["order_no"], feeName = x["fee_name"], fee_des = x["fee_des"], amount = x["fee_money"], crttime = DateTime.Parse(x["crttime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"), paytype = paytype } ).ToList()[0]; } return(fee); }
} //H5调起JS API参数 protected void Page_Load(object sender, EventArgs e) { Log.Info(this.GetType().ToString(), "page load"); if (!IsPostBack) { string openid = Request.QueryString["openid"]; string total_fee = Request.QueryString["total_fee"]; //检测是否给当前页面传递了相关参数 if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee)) { Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面传参出错,请返回重试" + "</span>"); Log.Error(this.GetType().ToString(), "This page have not get params, cannot be inited, exit..."); submit.Visible = false; return; } //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数 JsApiPay jsApiPay = new JsApiPay(this); jsApiPay.openid = openid; jsApiPay.total_fee = int.Parse(total_fee); //JSAPI支付预处理 try { //统一下单 WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(WxPayApi.GenerateOutTradeNo()); //获取H5调起JS API参数 wxJsApiParam = jsApiPay.GetJsApiParameters(WxPayConfig.KEY);// 用于前端js调用 Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam); //在页面上显示订单信息 Response.Write("<span style='color:#00CD00;font-size:20px'>订单详情:</span><br/>"); Response.Write("<span style='color:#00CD00;font-size:20px'>" + unifiedOrderResult.ToPrintStr() + "</span>"); } catch (Exception ex) { Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试" + "</span>"); submit.Visible = false; } } }
/** * 生成直接支付url,支付url有效期为2小时,模式二 * @param productId 商品ID * @return 模式二URL */ public string GetPayUrl(string productId) { Log.Info(this.GetType().ToString(), "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", 10000000); //总金额 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(); //获得统一下单接口返回的二维码链接 Log.Info(this.GetType().ToString(), "Get native pay mode 2 url : " + url); return(url); }
/** * 生成直接支付url,支付url有效期为2小时,模式二 * @param productId 商品ID * @return 模式二URL */ public string GetPayUrl(PaperInfo paperInfo, out string _orderNumber) { _orderNumber = WxPayApi.GenerateOutTradeNo(); Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing..."); int price = Convert.ToInt32(paperInfo.Price * 100); WxPayData data = new WxPayData(); data.SetValue("body", "启源论文网"); //商品描述 data.SetValue("attach", "QYLW"); //附加数据 data.SetValue("out_trade_no", _orderNumber); //随机字符串 data.SetValue("total_fee", price); //总金额 data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); //交易起始时间 data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); //交易结束时间 data.SetValue("goods_tag", "QY"); //商品标记 data.SetValue("trade_type", "NATIVE"); //交易类型 data.SetValue("product_id", paperInfo.Code); //商品ID WxPayData result = WxPayApi.UnifiedOrder(data); //调用统一下单接口 string url = result.GetValue("code_url").ToString(); //获得统一下单接口返回的二维码链接 Log.Info(this.GetType().ToString(), "Get native pay mode 2 url : " + url); return(url); }
//public WxPayData unifiedOrderResult { get; set; } public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; writeFile("coming2"); string openid = HttpContext.Current.Request.QueryString["openid"]; string total_fee = HttpContext.Current.Request.QueryString["jiage"]; string goods = HttpContext.Current.Request.QueryString["wupinglx"]; string huowuguizhongF = HttpContext.Current.Request.QueryString["huowuguizhongF"]; string qujianhao = HttpContext.Current.Request.QueryString["qujianhao"]; string huojiahao = HttpContext.Current.Request.QueryString["huojiahao"]; string zhongliangleibie = HttpContext.Current.Request.QueryString["zhongliangleibie"]; string name = HttpContext.Current.Request.QueryString["shouhuoren"]; string tel = HttpContext.Current.Request.QueryString["tel"]; string kuaididizhi = HttpContext.Current.Request.QueryString["kuaididizhi"]; JsApiPay jsApiPay = new JsApiPay(context); jsApiPay.openid = openid; writeFile(jsApiPay.openid); jsApiPay.goods = goods; writeFile(jsApiPay.goods.ToString()); jsApiPay.total_fee = int.Parse(total_fee); double price = jsApiPay.total_fee / 100.0; writeFile(jsApiPay.total_fee.ToString()); //统一下单 WxPayData data = new WxPayData(); data.SetValue("body", goods); writeFile(data.GetValue("body").ToString()); data.SetValue("attach", goods); writeFile(data.GetValue("attach").ToString()); data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); string out_trade_no = data.GetValue("out_trade_no").ToString(); writeFile(data.GetValue("out_trade_no").ToString()); data.SetValue("total_fee", total_fee); writeFile(data.GetValue("total_fee").ToString()); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); writeFile(data.GetValue("time_start").ToString()); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); writeFile(data.GetValue("time_expire").ToString()); data.SetValue("goods_tag", goods); writeFile(data.GetValue("goods_tag").ToString()); data.SetValue("trade_type", "JSAPI"); writeFile(data.GetValue("trade_type").ToString()); data.SetValue("openid", openid); writeFile(data.GetValue("openid").ToString()); WxPayData result = UnifiedOrder(data); writeFile("over..."); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxPayException("UnifiedOrder response error!"); } writeFile("overing"); kyfly.BLL.yewu_dingdanxinxiB bll = new kyfly.BLL.yewu_dingdanxinxiB(context); string jiedanshijian = DateTime.Now.Date.ToString(); string peisongshijian = DateTime.Now.Date.ToString(); string xiuzhengshijian = DateTime.Now.Date.ToString(); WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(); //此处有问题! context.Response.Write(jsApiPay.GetJsApiParameters()); //获取H5调起JS API参数 bll.Add(name, tel, goods, "", kuaididizhi, "待付款", jiedanshijian, qujianhao, huojiahao, huowuguizhongF, zhongliangleibie, price.ToString(), "", peisongshijian, "", "", xiuzhengshijian, out_trade_no, "", "", ""); }
/// <summary> /// 刷卡支付完整业务流程逻辑 /// </summary> /// <param name="body">商品描述</param> /// <param name="total_fee">总金额</param> /// <param name="auth_code">支付授权码</param> /// <returns>刷卡支付结果</returns> public static string Run(string body, string total_fee, string auth_code) { Log.Info("MicroPay", "Micropay is processing..."); WxPayData data = new WxPayData(); data.SetValue("auth_code", auth_code); //授权码 data.SetValue("body", body); //商品描述 data.SetValue("total_fee", int.Parse(total_fee)); //总金额 data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); //产生随机的商户订单号 WxPayData result = WxPayApi.Micropay(data, 10); //提交被扫支付,接收返回结果 //如果提交被扫支付接口调用失败,则抛异常 if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL") { string returnMsg = result.IsSet("return_msg") ? result.GetValue("return_msg").ToString() : ""; Log.Error("MicroPay", "Micropay API interface call failure, result : " + result.ToXml()); throw new WxPayException("Micropay API interface call failure, return_msg : " + returnMsg); } //签名验证 result.CheckSign(); Log.Debug("MicroPay", "Micropay response check sign success"); //刷卡支付直接成功 if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS") { Log.Debug("MicroPay", "Micropay business success, result : " + result.ToXml()); return(result.ToPrintStr()); } /**************************************************************** * 剩下的都是接口调用成功,业务失败的情况 * ****************************************************************/ //1)业务结果明确失败 if (result.GetValue("err_code").ToString() != "USERPAYING" && result.GetValue("err_code").ToString() != "SYSTEMERROR") { Log.Error("MicroPay", "micropay API interface call success, business failure, result : " + result.ToXml()); return(result.ToPrintStr()); } //2)不能确定是否失败,需查单 //用商户订单号去查单 string out_trade_no = data.GetValue("out_trade_no").ToString(); //确认支付是否成功,每隔一段时间查询一次订单,共查询10次 int queryTimes = 10;//查询次数计数器 while (queryTimes-- > 0) { //查询结果 WxPayData queryResult = Query(out_trade_no, out int succResult); //如果需要继续查询,则等待2s后继续 if (succResult == 2) { Thread.Sleep(2000); continue; } //查询成功,返回订单查询接口返回的数据 else if (succResult == 1) { Log.Debug("MicroPay", "Mircopay success, return order query result : " + queryResult.ToXml()); return(queryResult.ToPrintStr()); } //订单交易失败,直接返回刷卡支付接口返回的结果,失败原因会在err_code中描述 else { Log.Error("MicroPay", "Micropay failure, return micropay result : " + result.ToXml()); return(result.ToPrintStr()); } } //确认失败,则撤销订单 Log.Error("MicroPay", "Micropay failure, Reverse order is processing..."); if (!Cancel(out_trade_no)) { Log.Error("MicroPay", "Reverse order failure"); throw new WxPayException("Reverse order failure!"); } return(result.ToPrintStr()); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; writeFile("enter"); string code = ""; //string iv = ""; //string encryptedData = ""; string threedsession = ""; try { code = HttpContext.Current.Request.QueryString["code"].ToString(); //iv = HttpContext.Current.Request.QueryString["iv"].ToString(); //encryptedData = HttpContext.Current.Request.QueryString["encryptedData"].ToString(); //threedsession = HttpContext.Current.Request.QueryString["threedsession"].ToString(); } catch (Exception ex) { context.Response.Write(ex.ToString()); } writeFile(code); string Appid = "wx2e67c9b918d371a4"; string Secret = "84060efaf50e9fa85a62963a36dbb4b2"; string grant_type = "authorization_code"; //向微信服务端 使用登录凭证 code 获取 session_key 和 openid string url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid + "&secret=" + Secret + "&js_code=" + code + "&grant_type=" + grant_type; string type = "utf-8"; writeFile(url); GetUsersHelper GetUsersHelper = new GetUsersHelper(); string j = GetUsersHelper.GetUrltoHtml(url, type);//获取微信服务器返回字符串 writeFile(j); //将字符串转换为json格式 JObject jo = (JObject)JsonConvert.DeserializeObject(j); writeFile(jo.ToString()); result res = new result(); try { //微信服务器验证成功 res.openid = jo["openid"].ToString(); res.session_key = jo["session_key"].ToString(); threedsession = WxPayApi.GenerateOutTradeNo(); writeFile(res.openid); writeFile(res.session_key); writeFile(threedsession); SetCookie("threedsession", res.openid + res.session_key, 1); //写code 保存到cookies //context.Response.Write(res.openid); context.Response.Write(threedsession); } catch (Exception) { //微信服务器验证失败 res.errcode = jo["errcode"].ToString(); res.errmsg = jo["errmsg"].ToString(); } //if (!string.IsNullOrEmpty(res.openid)) //{ // //用户数据解密 // GetUsersHelper.AesIV = iv; // GetUsersHelper.AesKey = res.session_key; // string result = GetUsersHelper.AESDecrypt(encryptedData); // //存储用户数据 // JObject _usrInfo = (JObject)JsonConvert.DeserializeObject(result); // userInfo userInfo = new userInfo(); // userInfo.openId = _usrInfo["openId"].ToString(); // try //部分验证返回值中没有unionId // { // userInfo.unionId = _usrInfo["unionId"].ToString(); // } // catch (Exception) // { // userInfo.unionId = "unionId"; // } // userInfo.nickName = _usrInfo["nickName"].ToString(); // userInfo.gender = _usrInfo["gender"].ToString(); // userInfo.city = _usrInfo["city"].ToString(); // userInfo.province = _usrInfo["province"].ToString(); // userInfo.country = _usrInfo["country"].ToString(); // userInfo.avatarUrl = _usrInfo["avatarUrl"].ToString(); // object watermark = _usrInfo["watermark"].ToString(); // object appid = _usrInfo["watermark"]["appid"].ToString(); // object timestamp = _usrInfo["watermark"]["timestamp"].ToString(); // #region // //创建连接池对象(与数据库服务器进行连接) // SqlConnection conn = new SqlConnection("server=127.0.0.1;database=Test;uid=sa;pwd=1"); // //打开连接池 // conn.Open(); // //创建命令对象 // string Qrystr = "SELECT * FROM WeChatUsers WHERE openId='" + userInfo.openId + "'"; // SqlCommand cmdQry = new SqlCommand(Qrystr, conn); // object obj = cmdQry.ExecuteScalar(); // if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) // { // string str = "INSERT INTO WeChatUsers ([UnionId] ,[OpenId],[NickName],[Gender],[City],[Province],[Country],[AvatarUrl],[Appid],[Timestamp],[Memo],[counts])VALUES('" + userInfo.unionId + "','" + userInfo.openId + "','" + userInfo.nickName + "','" + userInfo.gender + "','" + userInfo.city + "','" + userInfo.province + "','" + userInfo.country + "','" + userInfo.avatarUrl + "','" + appid.ToString() + "','" + timestamp.ToString() + "','来自微信小程序','1')"; // SqlCommand cmdUp = new SqlCommand(str, conn); // // 执行操作 // try // { // int row = cmdUp.ExecuteNonQuery(); // } // catch (Exception ex) // { // context.Response.Write(ex.ToString()); // } // } // else // { // //多次访问,记录访问次数counts 更新unionId是预防最初没有,后期关联后却仍未记录 // string str = "UPDATE dbo.WeChatUsers SET counts = counts+1,UnionId = '" + userInfo.unionId + "' WHERE OpenId='" + userInfo.openId + "'"; // SqlCommand cmdUp = new SqlCommand(str, conn); // int row = cmdUp.ExecuteNonQuery(); // } // //关闭连接池 // conn.Close(); // #endregion // //返回解密后的用户数据 // context.Response.Write(result); //} //else //{ // context.Response.Write(j); //} }