/// <summary> /// 撤销订单API接口 /// @param WeChatPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个 /// @param int timeOut 接口超时时间 /// @throws WeChatPayException /// @return 成功时返回API调用结果,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData Reverse(WeChatPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/secapi/pay/reverse"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WeChatPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!"); } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 LogHelper.Debug("WeChatPayAPI", "Reverse request : " + xml); string response = HttpService.Post(xml, url, true, timeOut); LogHelper.Debug("WeChatPayAPI", "Reverse response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WeChatPayData result = new WeChatPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/// <summary> /// JS-SDK使用权限签名 /// 签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分) 。对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。 /// API参考:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95 /// 签名算法验证工具:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign /// </summary> /// <param name="jsapi_ticket"></param> /// <param name="url"></param> /// <param name="noncestr"></param> /// <param name="timestamp"></param> /// <returns></returns> public static string MakeJsAPISign(string jsapi_ticket, string url, out string noncestr, out string timestamp) { string jsSign = string.Empty; try { noncestr = WeChatPayData.MakeNonceStr(); timestamp = WeChatPayData.MakeTimeStamp(); //参与加密的参数key全部小写 WeChatPayData signData = new WeChatPayData(); signData.SetValue("jsapi_ticket", jsapi_ticket); signData.SetValue("timestamp", timestamp); signData.SetValue("noncestr", noncestr); signData.SetValue("url", url); string param = signData.ToSignStr(); Log.Debug("MakeJsAPISign", "SHA1 encrypt param : " + param); //SHA1加密 jsSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug("MakeJsAPISign", "SHA1 encrypt result : " + jsSign); } catch (Exception ex) { Log.Error("MakeJsAPISign", ex.ToString()); throw ex; } return(jsSign); }
public void GetPreCode() { string url = string.Empty; try { string host = "hexiaodong.top/"; string path = "api/pay/code"; string redirect_uri = HttpUtility.UrlEncode("http://" + host + path); WeChatPayData data = new WeChatPayData(); data.SetValue("appid", WeChatConfig.APP_ID); data.SetValue("redirect_uri", redirect_uri); data.SetValue("response_type", "code"); data.SetValue("scope", "snsapi_userinfo"); //data.SetValue("connect_redirect", "1"); data.SetValue("state", "1" + "#wechat_redirect"); url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl(); _logger.Info("url地址为:" + url); } catch (Exception ex) { _logger.Info("错误信息为:" + ex); } ControllerContext.HttpContext.Response.Redirect(url); //var client = new RestClient(url) //{ // UserAgent = "Mozilla/5.0 (Linux; Android 6.0; 1503-M02 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.2 TBS/036558 Safari/537.36 MicroMessenger/6.3.25.861 NetType/WIFI Language/zh_CN", // Encoding = Encoding.UTF8 //}; }
/// <summary> /// 转换短链接 /// 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX), /// 减小二维码数据量,提升扫描速度和精确度。 /// @param WeChatPayData inputObj 提交给转换短连接API的参数 /// @param int timeOut 接口超时时间 /// @throws WeChatPayException /// @return 成功时返回,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData ShortUrl(WeChatPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/tools/shorturl"; //检测必填参数 if (!inputObj.IsSet("long_url")) { throw new WeChatPayException("需要转换的URL,签名用原串,传输需URL encode!"); } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 LogHelper.Debug("WeChatPayAPI", "ShortUrl request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); LogHelper.Debug("WeChatPayAPI", "ShortUrl response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WeChatPayData result = new WeChatPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/// <summary> /// 关闭订单 /// @param WeChatPayData inputObj 提交给关闭订单API的参数 /// @param int timeOut 接口超时时间 /// @throws WeChatPayException /// @return 成功时返回,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData CloseOrder(WeChatPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/closeorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WeChatPayException("关闭订单接口中,out_trade_no必填!"); } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 string response = HttpService.Post(xml, url, false, timeOut); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WeChatPayData result = new WeChatPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/// <summary> /// 卡券签名 /// 1.将 api_ticket(特别说明:api_ticket 相较 appsecret 安全性更高,同时兼容老版本文档中使用的 appsecret 作为签名凭证。)、app_id、location_id、times_tamp、nonce_str、card_id、card_type的value值进行字符串的字典序排序。 /// 2.将所有参数字符串拼接成一个字符串进行sha1加密,得到cardSign。 /// </summary> /// <param name="apiTicket"></param> /// <param name="noncestr"></param> /// <param name="timestamp"></param> /// <returns></returns> public static string MakeCardSign(string apiTicket, out string noncestr, out string timestamp) { string cardSign = string.Empty; try { string param = string.Empty; noncestr = WeChatPayData.MakeNonceStr(); timestamp = WeChatPayData.MakeTimeStamp(); WeChatPayData signData = new WeChatPayData(); signData.SetValue(apiTicket, apiTicket); signData.SetValue(Config.APPID, Config.APPID); signData.SetValue(timestamp, timestamp); signData.SetValue(noncestr, noncestr); //参与加密的参数value连接成字符串 foreach (KeyValuePair <string, object> kvp in signData.GetValues()) { param += kvp.Key; } Log.Debug("MakeJsAPISign", "SHA1 encrypt param : " + param); //SHA1加密 cardSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug("MakeJsAPISign", "SHA1 encrypt result : " + cardSign); } catch (Exception ex) { Log.Error("MakeJsAPISign", ex.ToString()); throw ex; } return(cardSign); }
/// <summary> /// 调用统一下单,获得下单结果 /// @return 统一下单结果 /// @失败时抛异常WeChatPayException /// </summary> /// <returns></returns> public WeChatPayData GetUnifiedOrderResult(int total_fee, string openid) { //统一下单 WeChatPayData data = new WeChatPayData(); string str = GenerateOutTradeNo(); data.SetValue("body", ""); data.SetValue("attach", ""); data.SetValue("out_trade_no", str); 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", "WXG"); data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", openid); WeChatPayData result = UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { LogHelper.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WeChatPayException("UnifiedOrder response error!"); } return(result); }
public ResultModel <WeChatPayData> WeChatPay(PayParamModel model) { //统一下单 WeChatPayData data = new WeChatPayData(); data.SetValue("body", model.title); data.SetValue("attach", model.title); //data.SetValue("detail", model.title); data.SetValue("out_trade_no", model.out_trade_osn); data.SetValue("total_fee", ((int)(model.price * 100)).ToString()); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); data.SetValue("goods_tag", model.title); data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", model.openid); WeChatPayData result = UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { throw new Exception("UnifiedOrder response error!"); } return(new ResultModel <WeChatPayData>() { code = 1, msg = "微信支付平台接口调用成功", data = result }); }
/// <summary> /// 申请退款 /// @param WeChatPayData inputObj 提交给申请退款API的参数 /// @param int timeOut 超时时间 /// @throws WeChatPayException /// @return 成功时返回接口调用结果,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData Refund(WeChatPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WeChatPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!"); } else if (!inputObj.IsSet("out_refund_no")) { throw new WeChatPayException("退款申请接口中,缺少必填参数out_refund_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WeChatPayException("退款申请接口中,缺少必填参数total_fee!"); } else if (!inputObj.IsSet("refund_fee")) { throw new WeChatPayException("退款申请接口中,缺少必填参数refund_fee!"); } else if (!inputObj.IsSet("op_user_id")) { throw new WeChatPayException("退款申请接口中,缺少必填参数op_user_id!"); } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now; LogHelper.Debug("WeChatPayAPI", "Refund request : " + xml); string response = HttpService.Post(xml, url, true, timeOut);//调用HTTP通信接口提交数据到API LogHelper.Debug("WeChatPayAPI", "Refund response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的结果转换为对象以返回 WeChatPayData result = new WeChatPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/// <summary> /// 提交被扫支付API /// 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台, /// 由商户收银台或者商户后台调用该接口发起支付。 /// @param WeChatPayData inputObj 提交给被扫支付API的参数 /// @param int timeOut 超时时间 /// @throws WeChatPayException /// @return 成功时返回调用结果,其他抛异常/ /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData Micropay(WeChatPayData inputObj, int timeOut = 10) { string url = "https://api.mch.weixin.qq.com/pay/micropay"; //检测必填参数 if (!inputObj.IsSet("body")) { throw new WeChatPayException("提交被扫支付API接口中,缺少必填参数body!"); } else if (!inputObj.IsSet("out_trade_no")) { throw new WeChatPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!"); } else if (!inputObj.IsSet("total_fee")) { throw new WeChatPayException("提交被扫支付API接口中,缺少必填参数total_fee!"); } else if (!inputObj.IsSet("auth_code")) { throw new WeChatPayException("提交被扫支付API接口中,缺少必填参数auth_code!"); } inputObj.SetValue("spbill_create_ip", WeChatPayConfig.IP); //终端ip inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 LogHelper.Debug("WeChatPayAPI", "MicroPay request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API LogHelper.Debug("WeChatPayAPI", "MicroPay response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的结果转换为对象以返回 WeChatPayData result = new WeChatPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/// <summary> /// 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9 /// </summary> /// <param name="accessToken"></param> /// <param name="url"></param> /// <returns>共享收货地址js函数需要的参数,json格式可以直接做参数使用</returns> public static string MakeEditAddressJsParam(string accessToken, string url) { string editAddrParam = string.Empty; try { //参与加密的参数key全部小写 WeChatPayData signData = new WeChatPayData(); signData.SetValue("appid", Config.APPID); signData.SetValue("url", url); signData.SetValue("timestamp", WeChatPayData.MakeTimeStamp()); signData.SetValue("noncestr", WeChatPayData.MakeNonceStr()); signData.SetValue("accesstoken", accessToken); string param = signData.ToSignStr(); Log.Debug("MakeEditAddressJsParam", "SHA1 encrypt param : " + param); //SHA1加密 string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); Log.Debug("MakeEditAddressJsParam", "SHA1 encrypt result : " + addrSign); //构造收货地址js函数入口参数 WeChatPayData paramsData = new WeChatPayData(); paramsData.SetValue("appId", Config.APPID); paramsData.SetValue("scope", "jsapi_address"); paramsData.SetValue("signType", "sha1"); paramsData.SetValue("addrSign", addrSign); paramsData.SetValue("timeStamp", signData.GetValue("timestamp")); paramsData.SetValue("nonceStr", signData.GetValue("noncestr")); //转为json格式 editAddrParam = paramsData.ToJson(); Log.Debug("MakeEditAddressJsParam", editAddrParam); } catch (Exception ex) { Log.Error("MakeEditAddressJsParam", ex.ToString()); throw ex; } return(editAddrParam); }
private WeChatPayData UnifiedOrder(WeChatPayData inputObj, int timeOut = 6) { string url = WrapConfig.UnifiedorderUrl; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new Exception("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new Exception("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new Exception("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new Exception("缺少统一支付接口必填参数trade_type!"); } //JSAPI关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } //回调url if (!inputObj.IsSet("notify_url")) { inputObj.SetValue("notify_url", WrapConfig.WeChatCallBackUrl); } //支付场景 inputObj.SetValue("appid", BasicConfig.Wechat_PayaAppId); //公众账号ID inputObj.SetValue("mch_id", BasicConfig.WeChat_MchId); //商户号 inputObj.SetValue("spbill_create_ip", "8.8.8.8"); //终端ip inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 string trade_type = inputObj.GetValue("trade_type").ToString(); string keyvalue = BasicConfig.WeChat_AppKey; inputObj.SetValue("sign", inputObj.MakeSign(trade_type, keyvalue)); //签名 string xml = inputObj.ToXml(); Log.Info("支付请求参数xml:" + xml); string response = WHttpUtil.Post(xml, url, false, timeOut); WeChatPayData result = new WeChatPayData(); result.LoadXml(response, trade_type, keyvalue); return(result); }
/// <summary> /// 下载对账单 /// @param WeChatPayData inputObj 提交给下载对账单API的参数 /// @param int timeOut 接口超时时间 /// @throws WeChatPayException /// @return 成功时返回,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData DownloadBill(WeChatPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/downloadbill"; //检测必填参数 if (!inputObj.IsSet("bill_date")) { throw new WeChatPayException("对账单接口中,缺少必填参数bill_date!"); } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); LogHelper.Debug("WeChatPayAPI", "DownloadBill request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API LogHelper.Debug("WeChatPayAPI", "DownloadBill result : " + response); WeChatPayData result = new WeChatPayData(); //若接口调用失败会返回xml格式的结果 if (response.Substring(0, 5) == "<xml>") { result.FromXml(response); } //接口调用成功则返回非xml格式的数据 else { result.SetValue("result", response); } return(result); }
/// <summary> /// 查询退款 /// 提交退款申请后,通过该接口查询退款状态。退款有一定延时, /// 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。 /// out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个 /// @param WeChatPayData inputObj 提交给查询退款API的参数 /// @param int timeOut 接口超时时间 /// @throws WeChatPayException /// @return 成功时返回,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData RefundQuery(WeChatPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/refundquery"; //检测必填参数 if (!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id")) { throw new WeChatPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"); } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); var start = DateTime.Now;//请求开始时间 LogHelper.Debug("WeChatPayAPI", "RefundQuery request : " + xml); string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口以提交数据到API LogHelper.Debug("WeChatPayAPI", "RefundQuery response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 //将xml格式的结果转换为对象以返回 WeChatPayData result = new WeChatPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
public string Post() { //通过code获取access_token string code = Request.Query["code"]; string url = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd001485d83e3628b&secret={WeChatConfig.APP_SECRET}&code={code}&grant_type=authorization_code"; _logger.Info("unifiedorder->请求access_token地址为:" + url); var client = new RestClient(url); var request = new RestRequest(Method.GET); request.Timeout = 10000; IRestResponse response = client.Execute(request); _logger.Info("unifiedorder->请求access_token返回内容为:" + url); string openid = JsonConvert.DeserializeObject <Data>(response.Content)?.openid; _logger.Info("获取到openid为:" + openid); _logger.Info("开始支付:" + url); url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; client = new RestClient(url); request = new RestRequest(Method.POST); request.Timeout = 10000; WeChatPayData payData = new WeChatPayData(); payData.SetValue("appid", WeChatConfig.APP_ID); payData.SetValue("mch_id", WeChatConfig.MCH_ID); payData.SetValue("nonce_str", payData.GenerateNonceStr()); payData.SetValue("body", "test"); payData.SetValue("out_trade_no", DateTime.Now.ToString("yyyyMMddHHmmss")); payData.SetValue("total_fee", 1); payData.SetValue("spbill_create_ip", "127.0.0.1"); payData.SetValue("notify_url", "http://hexiaodong.top/api/pay/notify_url"); payData.SetValue("trade_type", "JSAPI"); payData.SetValue("openid", openid); //"oTSBW5wX09Qwiidfk1sarDeXq-hY" payData.SetValue("sign_type", WeChatPayData.SIGN_TYPE_HMAC_SHA256); //签名类型 payData.SetValue("sign", payData.MakeSign()); string xml = payData.ToXml(); request.AddJsonBody(xml); response = client.Execute(request); payData.FromXml(response.Content); WeChatPayData jsApiParam = new WeChatPayData(); jsApiParam.SetValue("appId", WeChatConfig.APP_ID); jsApiParam.SetValue("timeStamp", jsApiParam.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", jsApiParam.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + payData.GetValue("prepay_id")); jsApiParam.SetValue("signType", WeChatPayData.SIGN_TYPE_HMAC_SHA256); jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); return(jsApiParam.ToJson()); }
private string GetJsApiParameters(string keyvalue, string appid, string prepayid) { WeChatPayData jsApiParam = new WeChatPayData(); jsApiParam.SetValue("appId", appid); jsApiParam.SetValue("timeStamp", GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + prepayid); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign(keyvalue)); string parameters = jsApiParam.ToJson(); return(parameters); }
/// <summary> /// 测速上报接口实现 /// @param WeChatPayData inputObj 提交给测速上报接口的参数 /// @param int timeOut 测速上报接口超时时间 /// @throws WeChatPayException /// @return 成功时返回测速上报接口返回的结果,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData Report(WeChatPayData inputObj, int timeOut = 1) { string url = "https://api.mch.weixin.qq.com/payitil/report"; //检测必填参数 if (!inputObj.IsSet("interface_url")) { throw new WeChatPayException("接口URL,缺少必填参数interface_url!"); } if (!inputObj.IsSet("return_code")) { throw new WeChatPayException("返回状态码,缺少必填参数return_code!"); } if (!inputObj.IsSet("result_code")) { throw new WeChatPayException("业务结果,缺少必填参数result_code!"); } if (!inputObj.IsSet("user_ip")) { throw new WeChatPayException("访问接口IP,缺少必填参数user_ip!"); } if (!inputObj.IsSet("execute_time_")) { throw new WeChatPayException("接口耗时,缺少必填参数execute_time_!"); } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("user_ip", WeChatPayConfig.IP); //终端ip inputObj.SetValue("time", DateTime.Now.ToString("yyyyMMddHHmmss")); //商户上报时间 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign()); //签名 string xml = inputObj.ToXml(); LogHelper.Info("WeChatPayAPI", "Report request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); LogHelper.Info("WeChatPayAPI", "Report response : " + response); WeChatPayData result = new WeChatPayData(); result.FromXml(response); return(result); }
public string GetPrePayUrl(string out_trade_no) { Log.Info("微信端二维码生成开始..."); WeChatPayData data = new WeChatPayData(); data.SetValue("appid", BasicConfig.WeChat_AppId); //公众帐号id data.SetValue("mch_id", BasicConfig.WeChat_MchId); //商户号 data.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 data.SetValue("product_id", out_trade_no); //商品ID data.SetValue("time_stamp", DateTimeUtil.DateTimeToUnixTimestamp(DateTime.Now)); //时间戳 data.SetValue("sign", data.MakeSign(BasicConfig.WeChat_AppKey)); //签名 string str = ToUrlParams(data.GetValues()); //转换为URL串 string url = "weixin://wxpay/bizpayurl?" + str; Log.Info("微信端二维码生成结束,二维码Url : " + url); return(url); }
/// <summary> /// 根据预支付回话标示prepay_id构造JS参数 /// </summary> /// <param name="prepayID"></param> /// <returns></returns> public static string MakeWXPayJsParam(string prepayID) { //返回微信客户端调用JS支付所需的参数 string wxJsApiParam = string.Empty; try { //* 从统一下单成功返回的数据中获取微信浏览器调起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 WeChatPayData jsPayData = new WeChatPayData(); jsPayData.SetValue("appId", Config.APPID); jsPayData.SetValue("timeStamp", WeChatPayData.MakeTimeStamp()); jsPayData.SetValue("nonceStr", WeChatPayData.MakeNonceStr()); jsPayData.SetValue("package", "prepay_id=" + prepayID); jsPayData.SetValue("signType", "MD5"); jsPayData.SetValue("paySign", jsPayData.MakeSign()); wxJsApiParam = jsPayData.ToJson(); Log.Debug("jsApiParam", wxJsApiParam); } catch (Exception ex) { Log.Error("WxPayAPI", ex.Message); throw ex; } return(wxJsApiParam); }
/// <summary> ///测速上报 ///@param string interface_url 接口URL ///@param int timeCost 接口耗时 ///@param WeChatPayData inputObj参数数组 /// </summary> /// <param name="interface_url"></param> /// <param name="timeCost"></param> /// <param name="inputObj"></param> private static void ReportCostTime(string interface_url, int timeCost, WeChatPayData inputObj) { //如果不需要进行上报 if (WeChatPayConfig.REPORT_LEVENL == 0) { return; } //如果仅失败上报 if (WeChatPayConfig.REPORT_LEVENL == 1 && inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" && inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS") { return; } //上报逻辑 WeChatPayData data = new WeChatPayData(); data.SetValue("interface_url", interface_url); data.SetValue("execute_time_", timeCost); //返回状态码 if (inputObj.IsSet("return_code")) { data.SetValue("return_code", inputObj.GetValue("return_code")); } //返回信息 if (inputObj.IsSet("return_msg")) { data.SetValue("return_msg", inputObj.GetValue("return_msg")); } //业务结果 if (inputObj.IsSet("result_code")) { data.SetValue("result_code", inputObj.GetValue("result_code")); } //错误代码 if (inputObj.IsSet("err_code")) { data.SetValue("err_code", inputObj.GetValue("err_code")); } //错误代码描述 if (inputObj.IsSet("err_code_des")) { data.SetValue("err_code_des", inputObj.GetValue("err_code_des")); } //商户订单号 if (inputObj.IsSet("out_trade_no")) { data.SetValue("out_trade_no", inputObj.GetValue("out_trade_no")); } //设备号 if (inputObj.IsSet("device_info")) { data.SetValue("device_info", inputObj.GetValue("device_info")); } try { Report(data); } catch (WeChatPayException ex) { //不做任何处理 } }
/// <summary> /// 微信支付统一下单,接口参考:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 /// 除被扫支付场景以外,商户系统先调用该接口在微信支付服务后台生成预支付交易单,返回正确的预支付交易回话标识prepay_id,有效期2小时,再按扫码、JSAPI、APP等不同场景生成交易串调起支付。 /// </summary> /// <param name="po"></param> /// <param name="jStateCode"></param> /// <returns>正常返回prepay_id,jStateCode为空;如果有错误发生,则prepay_id为空,jStateCode有值</returns> public static string CallUnifiedOrderAPI(ProductOrder po, out WeChatPayData stateCode) { string prepayID = string.Empty; //统一下单API返回的状态码 stateCode = new WeChatPayData(); try { WeChatPayData sendPayData; WeChatPayData recvPayData; //生成统一下单接口报文 string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; sendPayData = new WeChatPayData(); sendPayData.SetValue("appid", Config.APPID); //必填 sendPayData.SetValue("mch_id", Config.MCHID); //必填 sendPayData.SetValue("device_info", "WEB"); sendPayData.SetValue("nonce_str", WeChatPayData.MakeNonceStr()); //必填 sendPayData.SetValue("body", po.ProductNames); //必填 sendPayData.SetValue("detail", po.OrderDetails); sendPayData.SetValue("attach", "订单自定义数据"); sendPayData.SetValue("out_trade_no", po.OrderID); //必填 sendPayData.SetValue("fee_type", "CNY"); sendPayData.SetValue("total_fee", (po.OrderPrice * 100).ToString("F0")); //必填,单位为【分】 sendPayData.SetValue("spbill_create_ip", po.ClientIP); //必填 sendPayData.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); sendPayData.SetValue("time_expire", DateTime.Now.AddMinutes(Config.WeChatOrderExpire).ToString("yyyyMMddHHmmss")); //最短失效时间间隔必须大于5分钟,可为空,默认2小时,也即prepay_id有效期 sendPayData.SetValue("goods_tag", "商品标记,代金券或立减优惠功能的参数"); sendPayData.SetValue("notify_url", Config.PayNotifyUrl); //必填,微信支付成功后异步通知url sendPayData.SetValue("trade_type", "JSAPI"); //必填 sendPayData.SetValue("openid", po.Purchaser.OpenID); //trade_type = JSAPI,此参数必传 //生成报文签名 sendPayData.SetValue("sign", sendPayData.MakeSign()); //必填 //生成接口所需的XML报文格式 string sendMsg = sendPayData.ToXml(); var start = DateTime.Now; Log.Debug("JsApiPay", "UnfiedOrder request" + sendMsg); //调用微信支付统一下单接口,并获取返回报文 string recvMsg = HttpService.Post(sendMsg, url, false, Config.WeChatAPITimeout); Log.Debug("JsApiPay", "UnfiedOrder response" + recvMsg); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); Log.Debug("JsApiPay", "UnfiedOrder time cost" + timeCost.ToString()); //recvMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wxa1ce5cd8d4bc5b3f]]></appid><mch_id><![CDATA[1296478401]]></mch_id><device_info><![CDATA[WEB]]></device_info><nonce_str><![CDATA[vjqH6K12oBv9iEyW]]></nonce_str><sign><![CDATA[42916A4F50AAD1AA1B9A3D222371EBAE]]></sign><result_code><![CDATA[SUCCESS]]></result_code><prepay_id><![CDATA[wx20151223230752785abc1a610183113624]]></prepay_id><trade_type><![CDATA[JSAPI]]></trade_type></xml>"; if (string.IsNullOrEmpty(recvMsg)) { Log.Error("JsApiPay", "UnfiedOrder接口没有返回数据"); throw new Exception("UnfiedOrder接口没有返回数据"); } recvPayData = new WeChatPayData(); //根据XML返回报文生成返回报文对象 recvPayData.FromXml(recvMsg); //校验通信标示 if (recvPayData.IsSet("return_code") && recvPayData.GetValue("return_code").ToString().ToUpper() == "SUCCESS") { //当return_code为SUCCESS,才有sign参数返回,校验对方报文签名 if (!recvPayData.CheckSign()) { stateCode.SetValue("return_code", "FAIL"); stateCode.SetValue("return_msg", "微信支付返回的签名错误,请在安全的网络环境中进行支付!"); Log.Error("CallUnifiedOrderAPI::SIGN签名错误", recvPayData.GetValue("sign").ToString()); } else { //校验业务结果result_code也是SUCCESS,才有prepay_id返回 if (recvPayData.IsSet("result_code") && recvPayData.GetValue("result_code").ToString().ToUpper() == "SUCCESS" && recvPayData.IsSet("prepay_id")) { //获取prepay_id prepayID = recvPayData.GetValue("prepay_id").ToString(); } else //可能是订单已支付、已关闭、订单号重复等错误原因 { stateCode.SetValue("result_code", "FAIL"); stateCode.SetValue("err_code", recvPayData.GetValue("err_code").ToString()); stateCode.SetValue("err_code_des", recvPayData.GetValue("err_code_des").ToString()); Log.Error("CallUnifiedOrderAPI", stateCode.ToJson()); } } } else //可能为我方报文sign签名错误或参数格式错误 { stateCode.SetValue("return_code", "FAIL"); stateCode.SetValue("return_msg", recvPayData.GetValue("return_msg").ToString()); Log.Error("CallUnifiedOrderAPI", stateCode.ToJson()); } } catch (Exception ex) { Log.Error("WxPayAPI", ex.Message); throw ex; } return(prepayID); }
/// <summary> /// 统一下单 /// @param WeChatPayData inputObj 提交给统一下单API的参数 /// @param int timeOut 超时时间 /// @throws WeChatPayException /// @return 成功时返回,其他抛异常 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WeChatPayData UnifiedOrder(WeChatPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WeChatPayException("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WeChatPayException("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WeChatPayException("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WeChatPayException("缺少统一支付接口必填参数trade_type!"); } //关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WeChatPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WeChatPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!inputObj.IsSet("notify_url")) { inputObj.SetValue("notify_url", WeChatPayConfig.NOTIFY_URL);//异步通知url } inputObj.SetValue("appid", WeChatPayConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WeChatPayConfig.MCHID); //商户号 inputObj.SetValue("spbill_create_ip", WeChatPayConfig.IP); //终端ip inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); var start = DateTime.Now; LogHelper.Debug("WeChatPayAPI", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); LogHelper.Debug("WeChatPayAPI", "UnfiedOrder response : " + response); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WeChatPayData result = new WeChatPayData(); result.FromXml(response); ReportCostTime(url, timeCost, result);//测速上报 return(result); }
/// <summary> /// 根据商户订单号查询订单接口,参考:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 /// 该接口提供所有微信支付订单的查询,商户可以通过该接口主动查询订单状态,完成下一步的业务逻辑。 /// </summary> /// <param name="out_trade_no"></param> /// <returns></returns> public static WeChatPayData OrderQueryByOutTradeNo(string out_trade_no) { if (string.IsNullOrEmpty(out_trade_no)) { throw new ArgumentNullException("查询订单,缺少out_trade_no"); } //微信支付消息发送报文 WeChatPayData sendPayData = new WeChatPayData(); //微信支付消息接收报文 WeChatPayData recvPayData = new WeChatPayData(); try { string url = "https://api.mch.weixin.qq.com/pay/orderquery"; sendPayData.SetValue("appid", Config.APPID); //公众账号ID sendPayData.SetValue("mch_id", Config.MCHID); //商户号 sendPayData.SetValue("out_trade_no", out_trade_no); //商户订单号 sendPayData.SetValue("nonce_str", WeChatPayData.MakeNonceStr()); //随机字符串 sendPayData.SetValue("sign", sendPayData.MakeSign()); //签名 string sendMsg = sendPayData.ToXml(); var start = DateTime.Now; Log.Debug("JsApiPay", "OrderQuery request : " + sendMsg); string recvMsg = HttpService.Post(sendMsg, url, false, Config.WeChatAPITimeout);//调用HTTP通信接口提交数据 Log.Debug("JsApiPay", "OrderQuery response : " + recvMsg); var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时 Log.Debug("JsApiPay", "OrderQuery 接口耗时 : " + timeCost); //将xml格式的数据转化为对象以返回 recvPayData.FromXml(recvMsg); //校验微信接口返回值return_code和result_code if (recvPayData.IsSet("return_code") && recvPayData.GetValue("return_code").ToString().ToUpper() == "SUCCESS") { if (!recvPayData.CheckSign()) { throw new Exception("微信“按out_trade_no查询订单”消息sign签名校验错误"); } //return_code和result_code同时为SUCCESS,且sign签名正确,则返回此消息数据 if (recvPayData.IsSet("result_code") && recvPayData.GetValue("result_code").ToString().ToUpper() == "SUCCESS") { return(recvPayData); } else //result_code返回FAIL,可能微信支付中此订单号不存在、微信支付系统异常 { if (recvPayData.IsSet("err_code") && recvPayData.IsSet("err_code_des")) { throw new Exception(recvPayData.GetValue("err_code").ToString() + ":" + recvPayData.GetValue("err_code_des").ToString()); } } } else //return_code返回FAIL,可能签名失败、参数格式校验错误 { if (recvPayData.IsSet("return_msg")) { throw new Exception(recvPayData.GetValue("return_msg").ToString()); } } } catch (Exception ex) { Log.Error("WxPayAPI", ex.Message); throw ex; } return(null); }
/// <summary> /// 微信支付,关闭订单接口 /// 以下情况需要调用关单接口:商户订单支付失败需要生成新单号重新发起支付,要对原订单号调用关单,避免重复支付;系统下单后,用户支付超时,系统退出不再受理,避免用户继续,请调用关单接口。 /// 注意:订单生成后不能马上调用关单接口,最短调用时间间隔为5分钟。 /// API接口参考:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3 /// </summary> /// <param name="orderID"></param> /// <returns></returns> public static WeChatPayData CloseOrder(string orderID) { if (string.IsNullOrEmpty(orderID)) { throw new ArgumentNullException("orderID不能为空"); } //微信支付消息发送报文 WeChatPayData sendPayData = new WeChatPayData(); //微信支付消息接收报文 WeChatPayData recvPayData = new WeChatPayData(); try { string url = "https://api.mch.weixin.qq.com/pay/closeorder"; sendPayData.SetValue("appid", Config.APPID); //公众账号ID sendPayData.SetValue("mch_id", Config.MCHID); //商户号 sendPayData.SetValue("nonce_str", WeChatPayData.MakeNonceStr()); //随机字符串 sendPayData.SetValue("out_trade_no", orderID); //商户订单号 sendPayData.SetValue("sign", sendPayData.MakeSign()); //签名 string sendMsg = sendPayData.ToXml(); string recvMsg = HttpService.Post(sendMsg, url, false, Config.WeChatAPITimeout); recvPayData.FromXml(recvMsg); //校验返回状态码 if (recvPayData.IsSet("return_code") && recvPayData.GetValue("return_code").ToString().ToUpper() == "SUCCESS") { //当return_code为SUCCESS,才有sign参数返回 if (!recvPayData.CheckSign()) { throw new Exception("sign签名校验错误"); } //return_code和result_code同时为SUCCESS,且sign签名正确,则返回此消息数据 if (recvPayData.IsSet("result_code") && recvPayData.GetValue("result_code").ToString().ToUpper() == "SUCCESS") { return(recvPayData); } else //result_code返回FAIL,可能微信支付中此订单号不存在、微信支付系统异常 { if (recvPayData.IsSet("err_code") && recvPayData.IsSet("err_code_des")) { throw new Exception(recvPayData.GetValue("err_code").ToString() + ":" + recvPayData.GetValue("err_code_des").ToString()); } } } else //return_code返回FAIL,可能签名失败、参数格式校验错误 { if (recvPayData.IsSet("return_msg")) { throw new Exception(recvPayData.GetValue("return_msg").ToString()); } } } catch (Exception ex) { Log.Error("WxPayAPI", ex.Message); } return(recvPayData); }
public ResultModel <WeChatPayResultModel> H5Pay(PayParamModel model) { var return_Url = WrapConfig.WeChatCallBackUrl; //统一下单 WeChatPayData data = new WeChatPayData(); if (string.IsNullOrEmpty(model.title)) { model.title = "订单充值"; } var orderamount = ((int)(model.price * 100)).ToString(); data.SetValue("body", model.title); //商品描述 data.SetValue("attach", model.title); //附加数据 data.SetValue("out_trade_no", model.out_trade_osn); //随机字符串 data.SetValue("total_fee", orderamount); //总金额 data.SetValue("trade_type", "MWEB"); //交易类型 data.SetValue("product_id", model.out_trade_osn); //商品ID data.SetValue("notify_url", return_Url); //回调URL data.SetValue("scene_info", model.scene_info); //H5支付格式 data.SetValue("spbill_create_ip", model.spbill_create_ip); //支付发起端IP地址 WeChatPayData result = UnifiedOrder(data); if (result == null) { return(new ResultModel <WeChatPayResultModel>() { code = 0, msg = "微信支付平台接口调用异常", data = null }); } WeChatPayResultModel payResult = new WeChatPayResultModel { prepay_id = result.GetValue("prepay_id") == null ? "" : result.GetValue("prepay_id").ToString(), sign = result.GetValue("sign") == null ? "" : result.GetValue("sign").ToString(), qrcodeurl = result.GetValue("code_url") == null ? "" : result.GetValue("code_url").ToString(), mch_id = result.GetValue("mch_id") == null ? "" : result.GetValue("mch_id").ToString(), mweb_url = result.GetValue("mweb_url") == null ? "" : result.GetValue("mweb_url").ToString() }; if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { if (result.GetValue("return_code") != null && result.GetValue("return_code").ToString() != "") { if (result.GetValue("return_code").ToString() == "FAIL") { var return_msg = result.GetValue("return_msg").ToString(); return(new ResultModel <WeChatPayResultModel>() { code = 0, msg = return_msg, data = payResult }); } } if (result.GetValue("err_code_des") != null && result.GetValue("err_code_des").ToString() != "") { if (result.GetValue("err_code_des").ToString() == "该订单已支付") { return(new ResultModel <WeChatPayResultModel>() { code = 0, msg = "该订单已支付", data = payResult }); } else if (result.GetValue("err_code_des").ToString() == "余额不足") { return(new ResultModel <WeChatPayResultModel>() { code = 0, msg = "余额不足", data = payResult }); } else if (result.GetValue("err_code_des").ToString() == "商户订单号重复") { return(new ResultModel <WeChatPayResultModel>() { code = 0, msg = "商户订单号重复", data = payResult }); } else if (result.GetValue("err_code_des").ToString() == "订单已关闭") { return(new ResultModel <WeChatPayResultModel>() { code = 0, msg = "订单已关闭", data = payResult }); } } return(new ResultModel <WeChatPayResultModel>() { code = 0, msg = "微信支付平台接口调用失败", data = payResult }); } var ApiResult = JsonConvert.DeserializeObject <ShareParaModel>(GetJsApiParameters(BasicConfig.WeChat_AppKey, result.GetValue("appid") == null ? "" : result.GetValue("appid").ToString(), result.GetValue("prepay_id") == null ? "" : result.GetValue("prepay_id").ToString())); payResult.timeStamp = ApiResult.timeStamp; payResult.nonceStr = ApiResult.nonceStr; payResult.package = ApiResult.package; payResult.signType = ApiResult.signType; payResult.paySign = ApiResult.paySign; payResult.appid = data.GetValue("appid").ToString(); return(new ResultModel <WeChatPayResultModel>() { code = 1, msg = "微信支付平台接口调用成功", data = payResult }); }
private WeChatPayData UnifiedOrder(WeChatPayData inputObj, int timeOut = 6) { try { string url = WrapConfig.UnifiedorderUrl; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new Exception("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new Exception("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new Exception("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new Exception("缺少统一支付接口必填参数trade_type!"); } //JSAPI关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } //NATIVE关联参数 if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } //回调url if (!inputObj.IsSet("notify_url")) { throw new Exception("统一支付接口中,缺少必填参数notify_url!trade_type为JSAPI时,notify_url为必填参数!"); } //支付场景 string trade_type = inputObj.GetValue("trade_type").ToString(); string keyvalue = ""; if (trade_type == "JSAPI") { inputObj.SetValue("appid", BasicConfig.Sprogram_AppId); //公众账号ID inputObj.SetValue("mch_id", BasicConfig.WeChat_MchId); //商户号 if (trade_type == "JSAPI") { keyvalue = BasicConfig.WeChat_AppKey; } } inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign", inputObj.MakeSign(trade_type, keyvalue)); //签名 string xml = inputObj.ToXml(); //Log.Info("xml:" + xml); string response = WHttpUtil.Post(xml, url, false, timeOut); WeChatPayData result = new WeChatPayData(); result.LoadXml(response, trade_type, keyvalue); return(result); } catch (Exception ex) { throw new Exception(ex.ToString()); } }