public static GenerateNonceStr ( ) : string | ||
Результат | string |
/** * * 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数, * 微信浏览器调起JSAPI时的输入参数格式如下: * { * "appId" : "wx2421b1c4370ec43b", //公众号名称,由商户传入 * "timeStamp":" 1395712654", //时间戳,自1970年以来的秒数 * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串 * "package" : "prepay_id=u802345jgfjsdfgsdg888", * "signType" : "MD5", //微信签名方式: * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 * } * @return string 微信浏览器调起JSAPI时的输入参数,json格式可以直接做参数用 * 更详细的说明请参考网页端调起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 * */ public string GetJsApiParameters() { Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing..."); WxPayData jsApiParam = new WxPayData(); jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); string parameters = jsApiParam.ToJson(); Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters); return(parameters); }
/** * 生成扫描支付模式一URL * @param productId 商品ID * @return 模式一URL */ public string GetPrePayUrl(string productId) { Log.Info(this.GetType().ToString(), "Native pay mode 1 url is producing..."); WxPayData data = new WxPayData(); data.SetValue("appid", WxPayConfig.APPID); //公众帐号id data.SetValue("mch_id", WxPayConfig.MCHID); //商户号 data.SetValue("time_stamp", WxPayApi.GenerateTimeStamp()); //时间戳 data.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串 data.SetValue("product_id", productId); //商品ID data.SetValue("sign", data.MakeSign()); //签名 string str = ToUrlParams(data.GetValues()); //转换为URL串 string url = "weixin://wxpay/bizpayurl?" + str; Log.Info(this.GetType().ToString(), "Get native pay mode 1 url : " + url); return(url); }
/** * * 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9 * @return string 共享收货地址js函数需要的参数,json格式可以直接做参数使用 */ public string GetEditAddressParameters() { string parameter = ""; try { //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url string url = "http://host/path/queryString"; //构造需要用SHA1算法加密的数据 WxPayData signData = new WxPayData(); signData.SetValue("appid", WxPayConfig.APPID); signData.SetValue("url", url); signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp()); signData.SetValue("noncestr", WxPayApi.GenerateNonceStr()); signData.SetValue("accesstoken", access_token); string param = signData.ToUrl(); Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param); //SHA1加密 string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign); //获取收货地址js函数入口参数 WxPayData afterData = new WxPayData(); afterData.SetValue("appId", WxPayConfig.APPID); afterData.SetValue("scope", "jsapi_address"); afterData.SetValue("signType", "sha1"); afterData.SetValue("addrSign", addrSign); afterData.SetValue("timeStamp", signData.GetValue("timestamp")); afterData.SetValue("nonceStr", signData.GetValue("noncestr")); //转为json格式 parameter = afterData.ToJson(); Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter); } catch (Exception ex) { Log.Error(this.GetType().ToString(), ex.ToString()); throw new WxPayException(ex.ToString()); } return(parameter); }
/** * * 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数, * 微信浏览器调起JSAPI时的输入参数格式如下: * { * "appId" : "wx2421b1c4370ec43b", //公众号名称,由商户传入 * "timeStamp":" 1395712654", //时间戳,自1970年以来的秒数 * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串 * "package" : "prepay_id=u802345jgfjsdfgsdg888", * "signType" : "MD5", //微信签名方式: * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 * } * @return string 微信浏览器调起JSAPI时的输入参数,json格式可以直接做参数用 * 更详细的说明请参考网页端调起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 * */ private static string GetJsOrder(WxPayData data) { try { WxPayData jsApiParam = new WxPayData(); jsApiParam.SetValue("appId", data.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + data.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); return(jsApiParam.ToJson()); } catch (Exception) { } return(null); }
/** * * 查询订单 * @param WxPayData inputObj 提交给查询订单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回订单查询结果,其他抛异常 */ public static WxPayData OrderQuery(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/orderquery"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!"); } inputObj.SetValue("appid", WxPayConfig.GetConfig().GetAppID()); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID()); //商户号 inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_MD5); //签名类型 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now; Log.Debug("WxPayApi", "OrderQuery request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口提交数据 Log.Debug("WxPayApi", "OrderQuery response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的数据转化为对象以返回 WxPayData result = new WxPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/** * * 查询订单 * @param WxPayData inputObj 提交给查询订单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回订单查询结果,其他抛异常 */ public static WxPayData OrderQuery(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/orderquery"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!"); } inputObj.SetValue("appid", WxPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口提交数据 //将xml格式的数据转化为对象以返回 WxPayData result = new WxPayData(); result.FromXml(response); return(result); }
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)//若在调统一下单接口时抛异常,立即返回结果给微信支付后台 { 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(WxPayConfig.KEY)); Log.Info(this.GetType().ToString(), "UnifiedOrder success , send data to WeChat : " + data.ToXml()); page.Response.Write(data.ToXml()); page.Response.End(); }
public WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WxPayException("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WxPayException("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WxPayException("缺少统一支付接口必填参数trade_type!"); } //关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!inputObj.IsSet("notify_url")) { writeFile("url"); inputObj.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//异步通知url writeFile(inputObj.GetValue("notify_url").ToString()); } inputObj.SetValue("appid", WxPayConfig.APPID); //公众账号ID writeFile(inputObj.GetValue("appid").ToString()); inputObj.SetValue("mch_id", WxPayConfig.MCHID); //商户号 writeFile(inputObj.GetValue("mch_id").ToString()); inputObj.SetValue("spbill_create_ip", WxPayConfig.IP); //终端ip writeFile(inputObj.GetValue("spbill_create_ip").ToString()); inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串 writeFile(inputObj.GetValue("nonce_str").ToString()); //签名 inputObj.SetValue("sign", inputObj.MakeSign()); writeFile(inputObj.GetValue("sign").ToString()); string xml = inputObj.ToXml(); writeFile(xml); var start = DateTime.Now; Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); writeFile(response); WxPayData result = new WxPayData(); result.FromXml(response); //ReportCostTime(url, timeCost, result);//测速上报 return(result); }
public WxPayData ProcessOrder(string openid, string product_id, string out_trade_no) { IList <TextAndValue> tv = new List <TextAndValue>(); tv.Add(new TextAndValue("@uid", openid)); tv.Add(new TextAndValue("@feeid", product_id)); tv.Add(new TextAndValue("@paytype", "2"));//1:支付宝,2:微信支付 tv.Add(new TextAndValue("@orderno", out_trade_no)); BaseModel <DataSet> bs = com.zgyey.sgspay.core.DataProxy.SGS_PAYProxyData.GetList(tv, "fmcapp..jzxx_CreateOrder", "tb1", "tb2"); DataSet ds = bs.result; AlipayOrder fee = new AlipayOrder(); if (int.Parse(ds.Tables[0].Rows[0][0].ToString()) == 0) { fee = ( from x in ds.Tables[1].AsEnumerable() select new AlipayOrder() { orderNo = x["order_no"], feeName = x["fee_name"], fee_des = x["fee_des"], amount = x["fee_money"] } ).ToList()[0]; } //调统一下单接口,获得下单结果 WxPayData unifiedOrderResult = new WxPayData(); try { unifiedOrderResult = UnifiedOrder(fee); } 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()); return(res); } //若下单失败,则立即返回结果给微信支付后台 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()); return(res); } //统一下单成功,则返回成功结果给微信支付后台 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()); return(data); }
public BaseModel <Object> ProcessOrder(string uid, string feeid, string from, string paytype, string key) { //调统一下单接口,获得下单结果 WxPayData unifiedOrderResult = new WxPayData(); BaseModel <Object> resultData = new BaseModel <Object>(); try { AlipayOrder fee = GetFee(uid, feeid, from, paytype, key); unifiedOrderResult = UnifiedOrder(fee); //若下单失败,则立即返回结果给微信支付后台 if (!unifiedOrderResult.IsSet("appid") || !unifiedOrderResult.IsSet("mch_id") || !unifiedOrderResult.IsSet("prepay_id")) { WxPayError err = new WxPayError(); err.return_code = "FAIL"; err.return_msg = "统一下单失败"; resultData.code = 1; resultData.info = "统一下单失败"; resultData.result = err; return(resultData); } //统一下单成功,则返回成功结果给微信支付后台 //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("key", WxPayConfig.KEY);//key //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"); //int amount = 0; //int.TryParse(fee.amount.ToString(), out amount); //data.SetValue("out_trade_no", fee.orderNo); //data.SetValue("fee_money", amount); //data.SetValue("fee_name", fee.feeName); //data.SetValue("fee_des", fee.fee_des); //data.SetValue("paytype", fee.paytype); //data.SetValue("crttime", fee.crttime); //data.SetValue("sign", data.MakeSign()); int amount = 0; if (com.zgyey.sgspay.common.AppSetting.Contains(com.zgyey.sgspay.common.AppSetting.DemoFeeids, feeid)) { amount = 1; } else { if (!int.TryParse(fee.amount.ToString(), out amount)) { amount = 100; } } object prepay_id = unifiedOrderResult.GetValue("prepay_id"); WxPaySuccess wp = new WxPaySuccess() { return_code = "SUCCESS", return_msg = "OK", appid = WxPayConfig.APPID, mch_id = WxPayConfig.MCHID, key = WxPayConfig.KEY,//key nonce_str = WxPayApi.GenerateNonceStr(), prepay_id = prepay_id, result_code = "SUCCESS", err_code_des = "OK", out_trade_no = fee.orderNo.ToString(), fee_money = amount, fee_name = fee.feeName.ToString(), fee_des = fee.fee_des.ToString(), paytype = fee.paytype, crttime = fee.crttime }; resultData.code = 0; resultData.info = "统一下单成功"; resultData.result = wp; Log.Info(this.GetType().ToString(), "UnifiedOrder success , send data to WeChat : " + LitJson.JsonMapper.ToJson(wp)); return(resultData); } catch (Exception ex)//若在调统一下单接口时抛异常,立即返回结果给微信支付后台 { WxPayError err = new WxPayError(); err.return_code = "FAIL"; err.return_msg = "统一下单失败"; resultData.code = 1; resultData.info = "统一下单失败"; resultData.result = err; Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + LitJson.JsonMapper.ToJson(err)); return(resultData); } }