/// <summary> /// 获取微信退款异步通知结果 /// 接受退款通知不需要验证签名 /// </summary> /// <param name="request"></param> /// <returns></returns> public Message GetPayRefundNotifyResult(string ApiKey, Dictionary <string, string> Parameters) { var result = new Message(); if (Parameters["return_code"] == "SUCCESS") { var strA = Parameters["req_info"]; //得到加密字符串 string key = TenPayHelp.MD5(ApiKey).ToLower(); var strB = TenPayHelp.AESDecrypt(strA, key); //AES解密 Parameters = Parameters.Union(TenPayHelp.GetDictionaryFromCDATAXml(strB)).ToDictionary(pi => pi.Key, pi => pi.Value); if (Parameters["refund_status"] == "SUCCESS") {//支付成功 result.state = true; result.error = "退款成功"; } else { if (Parameters["refund_status"] == "CHANGE") { result.error = "退款异常"; } if (Parameters["refund_status"] == "REFUNDCLOSE") { result.error = "退款关闭"; } } result.data = string.Format("{0},{1}", Parameters["out_refund_no"], Parameters["refund_id"]); result.paramsData = strB; } else { result.error = Parameters["return_msg"]; } return(result); }
/// <summary> /// 获取随机字符串 /// </summary> /// <returns></returns> public static string nonce_str() { // 生成随机数算法 //微信支付API接口协议中包含字段nonce_str,主要保证签名不可预测。 //我们推荐生成随机数算法如下:调用随机数函数生成,将得到的值转换为字符串。 Random Ran = new Random(); string nonce_str = Ran.Next(11111111, 99999999).ToString() + "CNMB"; return(TenPayHelp.MD5(nonce_str)); }
/// <summary> /// 查询订单退款状态 /// </summary> /// <param name="config">公共参数</param> /// <param name="out_refund_no">商户退款单号</param> /// <returns></returns> public static Message RefundQuery(TenPayConfig config, string out_refund_no) { var RequsetParam = new Dictionary<string, string>(); RequsetParam.Add("appid", config.appid); RequsetParam.Add("mch_id", config.mch_id); RequsetParam.Add("nonce_str", TenPayConfig.nonce_str()); RequsetParam.Add("out_refund_no", out_refund_no); RequsetParam.Add("sign_type", "MD5"); string sign = TenPaySign.CreateSign(RequsetParam, config.key);//创建签名 RequsetParam.Add("sign", sign); string RequestData = TenPayHelp.InstallXml(RequsetParam);//组装数据 //微信支付统一查询API地址 string sUrl = "https://api.mch.weixin.qq.com/pay/refundquery"; string sResult = TenPayHelp.HttpPost(sUrl, RequestData);//调用接口 var ResponeData = TenPayHelp.GetDictionaryFromCDATAXml(sResult); Message Msg = new Message(); if (TenPaySign.CheckSign(ResponeData, config.key)) {//验证签名 if (ResponeData["return_code"] == "SUCCESS") { if (ResponeData["result_code"] == "SUCCESS") { if (ResponeData["refund_status_0"] == "SUCCESS") { Msg.state = true; Msg.error = "退款成功"; } else { if (ResponeData["refund_status_0"] == "PROCESSING") Msg.error = "退款处理中"; if (ResponeData["refund_status_0"] == "REFUNDCLOSE") Msg.error = "退款关闭"; if (ResponeData["refund_status_0"] == "CHANGE") Msg.error = "退款异常"; } } else { Msg.error = ResponeData["err_code_des"];//错误信息描述 } } else { Msg.error = ResponeData["return_msg"]; } } else { Msg.error = sResult; } return Msg; }
/// <summary> /// 支付下单 /// </summary> /// <param name="config">公共参数</param> /// <param name="sClientIp">客户端的原始Ip(不能是代理IP必须是真实的IP)</param> /// <param name="sOrderNo">订单号</param> /// <param name="sDescription">订单描述,展示在支付页面上的文字描述</param> /// <param name="total_fee">支付金额(单位是分)</param> /// <param name="notify_url">异步通知地址</param> /// <param name="return_url">同步通知地址</param> /// <param name="iPayType">支付方式 1-支付宝 ;2-微信</param> /// <returns></returns> public Message bookOrder(TenPayConfig config, string sClientIp, string sOrderNo, string sDescription, decimal total_fee, string notify_url, string return_url, int iPayType, int time_expire = 2) { if (iPayType == 1) {//支付宝支付 var res = AlipayHelp.Alipay(new AlipayConfig() { app_id = config.appid, alipay_public_key = config.key, merchant_private_key = config.merchant_private_key }, sDescription, sOrderNo, total_fee.ToString(), notify_url, return_url, time_expire); return(res); } else { if (string.IsNullOrEmpty(sClientIp)) { return(new Message() { state = false, error = "用户真实IP必传" }); } string wxPayType = System.Configuration.ConfigurationManager.AppSettings["wxPayMtd"]; total_fee = total_fee * 100; wxPayType = string.IsNullOrEmpty(wxPayType) ? "weixin" : wxPayType; Message res = null; if (wxPayType.Equals("weixin")) { res = TenPayMode.UniteOrder_First(config, sDescription, sOrderNo, Convert.ToInt32(total_fee).ToString(), sClientIp, notify_url, time_expire); } else if (wxPayType.Equals("pingan")) { res = PingAnPay.UniteOrder_First(config, sDescription, sOrderNo, Convert.ToInt32(total_fee).ToString(), sClientIp, notify_url, time_expire); } else { return(res = new Message() { state = false, error = "微信支付类型错误." }); } if (res.state) {//对url进行urlencode处理 string redirect_url = return_url; if (redirect_url != null) { res.data = string.Format("{0}&redirect_url={1}", res.data, TenPayHelp.UrlEncode(redirect_url)); } } return(res); } }
/// <summary> /// 微信申请退款 /// </summary> /// <param name="config">公共参数</param> /// <param name="out_trade_no"></param> /// <param name="out_refund_no"></param> /// <param name="total_fee"></param> /// <param name="refund_fee"></param> /// <param name="refund_desc"></param> /// <returns></returns> public static Message PayRefund(TenPayConfig config,string out_trade_no, string out_refund_no, string total_fee, string refund_fee,string refund_desc) { var RequsetParam = new Dictionary<string, string>(); RequsetParam.Add("appid", config.appid); RequsetParam.Add("mch_id", config.mch_id); RequsetParam.Add("nonce_str", TenPayConfig.nonce_str()); RequsetParam.Add("sign_type", "MD5"); RequsetParam.Add("out_trade_no", out_trade_no); RequsetParam.Add("out_refund_no", out_refund_no); RequsetParam.Add("total_fee", total_fee); RequsetParam.Add("refund_fee", refund_fee); if (!string.IsNullOrEmpty(refund_desc)) {//退款原因 RequsetParam.Add("refund_desc", refund_desc); } string sign = TenPaySign.CreateSign(RequsetParam, config.key);//创建签名 RequsetParam.Add("sign", sign); string RequestData = TenPayHelp.InstallXml(RequsetParam);//组装数据 //微信退款申请API string sUrl = "https://api.mch.weixin.qq.com/secapi/pay/refund"; string sResult = TenPayHelp.HttpPost(sUrl, RequestData, true, config);//调用接口 var ResponeData = TenPayHelp.GetDictionaryFromCDATAXml(sResult); Message result = new Message(); if (TenPaySign.CheckSign(ResponeData, config.key)) {//验证签名 if (ResponeData["return_code"] == "SUCCESS") { if (ResponeData["result_code"] == "SUCCESS") { result.state = true; result.error = "退款申请成功"; } else { result.error = ResponeData["err_code_des"];//错误信息描述 } } else { result.error = ResponeData["return_msg"]; } } else { result.error = sResult; } result.data = sResult; return result; }
/*******************微信支付的请求和接收数据均需要校验签名,详细方法请参考安全规范-签名算法********************/ /* * 签名算法 * 签名生成的通用步骤如下: * 第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序), * 使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA。 * 特别注意以下重要规则: * ◆ 参数名ASCII码从小到大排序(字典序); * ◆ 如果参数的值为空不参与签名; * ◆ 参数名区分大小写; * ◆ 验证调用返回或微信主动通知签名时,传送的sign参数不参与签名,将生成的签名与该sign值作校验。 * ◆ 微信接口可能增加字段,验证签名时必须支持增加的扩展字段 * 第二步,在stringA最后拼接上key得到stringSignTemp字符串,并对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。 * key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 */ /// <summary> /// 创建签名 /// </summary> /// <param name="Parameters"></param> /// <returns></returns> public static string CreateSign(Dictionary <string, string> Parameters, string ApiKey) { StringBuilder Sign = new StringBuilder(); var Keys = new ArrayList(Parameters.Keys); Keys.Sort();//字典排序 foreach (string key in Keys) { if (!string.IsNullOrEmpty(Parameters[key])) {//拼接成键值对字符串 Sign.Append(key + "=" + Parameters[key] + "&"); } } Sign.Append("key=" + ApiKey); string sign = TenPayHelp.MD5(Sign.ToString()).ToUpper(); return(sign); }
/// <summary> /// 统一下单支付接口 /// </summary> /// <param name="config">公共参数</param> /// <param name="body">描述</param> /// <param name="out_trade_no">商户系统内部的订单编号</param> /// <param name="total_fee">订单总金额,单位为分</param> /// <param name="spbill_create_ip">APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP.</param> /// <param name="notify_url">接收微信支付异步通知回调地址,不能携带参数</param> /// <returns></returns> public static Message UniteOrder_First(TenPayConfig config, string body, string out_trade_no, string total_fee, string spbill_create_ip, string notify_url, int time_expire=2) { /************************* 调用微信统一支付API所必需的参数******************************************/ //公众账号ID appid 微信分配的公众账号ID(企业号corpid即为此appId) //商户号 mch_id 微信支付分配的商户号 //随机字符串 nonce_str 随机字符串,不长于32位。推荐随机数生成算法 //签名 sign 签名,详见签名生成算法 //商品描述 body 商品或支付单简要描述 //商户订单号 out_trade_no 商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号 //总金额 total_fee 订单总金额,单位为分,详见支付金额 //终端IP spbill_create_ip APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。 //通知地址 notify_url 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。 //交易类型 trade_type 取值如下:JSAPI,NATIVE,APP,详细说明见参数规定 //商品ID product_id trade_type=NATIVE,此参数必传。此id为二维码中包含的商户定义的商品id或者订单号,商户自行定义。 /************************* 调用微信统一支付API所必需的参数******************************************/ var Dic = new Dictionary<string, string>(); Dic.Add("appid", config.appid); Dic.Add("mch_id", config.mch_id); Dic.Add("nonce_str", TenPayConfig.nonce_str()); Dic.Add("body", body); Dic.Add("out_trade_no", out_trade_no); Dic.Add("total_fee", total_fee); Dic.Add("spbill_create_ip", spbill_create_ip); Dic.Add("notify_url", notify_url); Dic.Add("trade_type", "MWEB");//新h5模式 Dic.Add("time_expire", DateTime.Now.AddHours(time_expire).ToString("yyyyMMddHHmmss"));//订单的失效时间 string sign = TenPaySign.CreateSign(Dic, config.key);//创建签名 Dic.Add("sign", sign); string RequestData = TenPayHelp.InstallXml(Dic); //请求统一下单支付API string sUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder"; string sResult = TenPayHelp.HttpPost(sUrl, RequestData);//调用接口 var Parameters = TenPayHelp.GetDictionaryFromCDATAXml(sResult); Message Msg = new Message(); if (TenPaySign.CheckSign(Parameters,config.key)) {//验证签名 if (Parameters["return_code"] == "SUCCESS") { if (Parameters["result_code"] == "SUCCESS") {//统一下单成功 Msg.state = true; Msg.data = Parameters["mweb_url"]; } else {//统一下单失败 Msg.error = Parameters["err_code_des"];//错误信息描述 } } else { Msg.error = Parameters["return_msg"]; } } else { Msg.error = sResult; } return Msg; }