using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; using System.Security.Cryptography; using WebService.SDK; using System.Text; namespace ThirdPartInterfaceService { public class Common { //接口请求地址,固定不变,无需修改 //protected const string req_url = "https://www.weiwoju.com"; protected static string req_url = ConfigurationManager.AppSettings["req_url"] == null ? "https://apisvbak2.ke51.cn" : ConfigurationManager.AppSettings["req_url"].ToString(); protected const string key = "w0j23q2m69po13syyd6pj6a11o64f2r13bj"; protected const string openid = "1z7n0lp0nsi8o0dd5o74lgrhs1575k"; public static string get_reqmsg(string trade_state) { switch (trade_state) { case "0": return "等待付款"; case "1": return "已付款"; case "2": return "已撤单"; case "3": return "申请撤单"; case "4": return "部分退款"; case "5": return "已退款"; case "6": return "交易关闭"; case "7": return "系统保留状态(查询时使用,同时查询退款和撤单记录)"; case "8": return "退款中(平台未返回退款结果)"; case "9": return "退款失败"; case "10": return "待退款"; case "11": return "退款成功"; case "12": return "退款失败"; case "13": return "退款取消"; case "14": return "撤单成功"; default: return ""; } } #region 授权码验签 /// /// 验签 /// /// 秒 /// 和配置文件中匹配 /// public static bool Verify(string timespan, string code, string VerifyCode) { if (!long.TryParse(timespan, out var unixTimestamp) || VerifyCode != code) { return false; } var targetedTime = new DateTime(1970, 1, 1).AddSeconds(unixTimestamp); var oneHour = TimeSpan.FromHours(1); var currentTime = DateTime.Now; if (currentTime - oneHour <= targetedTime) { return false; } return true; } #endregion #region 商家扫用户支付二维码 -反扫 /// /// 交易支付接口 /// 商家扫用户支付二维码 /// /// 用户付款授权码 /// 商户订单号 /// 商品描述 /// 总金额 /// 商户信息(商户号,秘钥) /// 通知回调地址 /// public static string merchantscan(string auth_code, string out_trade_no, string pay_subject, string total_fee, string[] MerchantInfo, string notify_url) { string ResString = ""; if (string.IsNullOrEmpty(MerchantInfo[0])) { return null; } Dictionary _Dictionary = new Dictionary(); _Dictionary.Add("auth_code", auth_code);//支付宝/微信授权码 _Dictionary.Add("bind_code", MerchantInfo[0]);//商户编号 _Dictionary.Add("body", total_fee);//交易金额 _Dictionary.Add("no", out_trade_no);//门店编号 _Dictionary.Add("nonce_str", "0"); //支付渠道 _Dictionary.Add("openid", openid);//客无忧与平台对接的openid jajonaujrocjajheangyunhaohpeng _Dictionary.Add("order_title", pay_subject);//设备编号 _Dictionary.Add("price", total_fee);//支付描述信息 _Dictionary.Add("time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString());//币种。默认CNY _Dictionary.Add("sign", getSign(_Dictionary, key)); LogHelper.WriteReceiveLog("支付反扫-传第三方参数:" + paramsToString(_Dictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(paramsToString(_Dictionary), req_url + "/Payapi/Index/microPay", "application/x-www-form-urlencoded", 5); LogHelper.WriteReceiveLog("支付反扫-返回参数:" + ResString); return ResString; } #endregion #region 用户扫线上二维码-正扫 public static string QRCodePay(string out_trade_no, string pay_subject, string total_fee, string[] MerchantInfo, string notify_url, string channel_type) { string ResString = ""; if (string.IsNullOrEmpty(MerchantInfo[0])) { return null; } Dictionary _Dictionary = new Dictionary(); _Dictionary.Add("openid", openid);//客无忧与平台对接的openid jajonaujrocjajheangyunhaohpeng _Dictionary.Add("bind_code", MerchantInfo[0]);//商户编号 _Dictionary.Add("auth_code", out_trade_no);//支付宝/微信授权码 _Dictionary.Add("channel_type", channel_type); //支付渠道 _Dictionary.Add("fee_type", "CNY");//币种。默认CNY _Dictionary.Add("no", out_trade_no);//交易上行流水号 _Dictionary.Add("order_title", pay_subject);//支付描述信息 _Dictionary.Add("time_expire", "5");//超时时间 _Dictionary.Add("body", total_fee);//交易金额 _Dictionary.Add("price", total_fee);//交易金额 _Dictionary.Add("trade_type", "QRCODE");//交易类型 _Dictionary.Add("time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString());//币种。默认CNY _Dictionary.Add("sign", getSign(_Dictionary, key)); _Dictionary.Add("notify_url", ""); _Dictionary.Add("callback_url", ""); LogHelper.WriteReceiveLog("支付正扫_传第三方参数:" + paramsToString(_Dictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(paramsToString(_Dictionary), req_url + "/Payapi/Index/Webpay", "application/x-www-form-urlencoded", 5); LogHelper.WriteSendLog(" 支付正扫_返回的支付结果:" + ResString); return ResString; } public static string QRCodePay(string out_trade_no, string pay_subject, string total_fee, string MerchantInfo, string notify_url, string channel_type) { string ResString = ""; if (string.IsNullOrEmpty(MerchantInfo)) { return null; } Dictionary _Dictionary = new Dictionary(); _Dictionary.Add("openid", "1z7n0lp0nsi8o0dd5o74lgrhs1575k");//客无忧与平台对接的openid jajonaujrocjajheangyunhaohpeng _Dictionary.Add("bind_code", MerchantInfo);//商户编号 _Dictionary.Add("auth_code", out_trade_no);//支付宝/微信授权码 _Dictionary.Add("channel_type", channel_type); //支付渠道 _Dictionary.Add("fee_type", "CNY");//币种。默认CNY _Dictionary.Add("no", out_trade_no);//交易上行流水号 _Dictionary.Add("order_title", pay_subject);//支付描述信息 _Dictionary.Add("time_expire", "5");//超时时间 _Dictionary.Add("body", total_fee);//交易金额 _Dictionary.Add("price", total_fee);//交易金额 _Dictionary.Add("trade_type", "QRCODE");//交易类型 _Dictionary.Add("time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString());//币种。默认CNY _Dictionary.Add("notify_url", "https://www.weiwoju.com"); _Dictionary.Add("callback_url", "https://www.weiwoju.com"); _Dictionary.Add("sign", getSign(_Dictionary, key)); LogHelper.WriteReceiveLog("支付正扫_传第三方参数:" + paramsToString(_Dictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(paramsToString(_Dictionary), req_url + "/Payapi/Index/Webpay", "application/x-www-form-urlencoded", 5); LogHelper.WriteSendLog("支付正扫_返回的支付结果:" + ResString); return ResString; } #endregion #region 订单查询 //交易支付接口测试 //商家扫用户支付二维码 //传入参数 //@out_trade_no:商户订单号 //@trade_no:交易下行流水号 //@MerchantInfo:商户信息(商户号,秘钥) public static string TradeQuery(string out_trade_no, string trade_no, string MerchantInfo) { string ResString = ""; if (string.IsNullOrEmpty(MerchantInfo)) { return null; } long timestamp = CommonHelper.ConvertDataTimeLong(DateTime.Now); Dictionary _Dictionary = new Dictionary(); //_Dictionary.Add("busi_id", MerchantInfo);//商户编号 _Dictionary.Add("bind_code", MerchantInfo);//商户编号 _Dictionary.Add("openid", openid);// jajonaujrocjajheangyunhaohpeng _Dictionary.Add("time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString());//币种。默认CNY if (!string.IsNullOrEmpty(trade_no)) { _Dictionary.Add("transaction_id", trade_no);//交易上行流水号 } if (!string.IsNullOrEmpty(out_trade_no)) { _Dictionary.Add("out_no", out_trade_no);//交易下行流水号 } _Dictionary.Add("sign", getSign(_Dictionary, key)); LogHelper.WriteReceiveLog("支付查询记录…传第三方参数:" + paramsToString(_Dictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(paramsToString(_Dictionary), req_url + "/Payapi/Index/orderQuery"); LogHelper.WriteSendLog("支付查询记录第三方…返回值:" + ResString); return ResString; } #endregion #region 订单退款-正扫 //交易支付接口测试 //商家扫用户支付二维码 //传入参数 //@operator_id:操作员编号 //@terminal_id:平台订单号 //@refund_fee:退款金额 //@refund_subject:退款描述 //@out_trade_no:交易上行流水号 //@trade_no:交易下行流水号 //@out_refund_no:退款流水号 //@MerchantInfo:商户信息(商户号,秘钥) public static string TradeRefund(string operator_id, string terminal_id, string refund_fee, string refund_subject, string out_trade_no, string trade_no, string out_refund_no, string MerchantInfo) { string ResString = ""; if (string.IsNullOrEmpty(MerchantInfo)) { return null; } long timestamp = CommonHelper.ConvertDataTimeLong(DateTime.Now); Dictionary _Dictionary = new Dictionary(); _Dictionary.Add("bind_code", MerchantInfo);//商户编号 _Dictionary.Add("openid", openid);//客无忧与平台对接的openid jajonaujrocjajheangyunhaohpeng _Dictionary.Add("channel_type", "0"); //支付渠道 _Dictionary.Add("fee_type", "CNY");//币种。默认CNY _Dictionary.Add("operator_id", operator_id);//操作员编号 _Dictionary.Add("transaction_id", trade_no);//退款流水号 _Dictionary.Add("nonce_str", "0"); //支付渠道 _Dictionary.Add("time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString());//币种。默认CNY if (!string.IsNullOrEmpty(out_trade_no)) { _Dictionary.Add("out_trade_no", out_trade_no);//交易上行流水号 } _Dictionary.Add("price", refund_fee);//退款金额 _Dictionary.Add("refund_price", refund_fee);//退款金额 _Dictionary.Add("refund_subject", refund_subject);//退款描述 _Dictionary.Add("terminal_id", terminal_id);//设备编号 if (!string.IsNullOrEmpty(out_refund_no)) { _Dictionary.Add("refund_no", out_refund_no);//交易下行流水号 } _Dictionary.Add("sign", getSign(_Dictionary, key)); LogHelper.WriteReceiveLog("退款-请求参数:" + paramsToString(_Dictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(paramsToString(_Dictionary), req_url + "/Payapi/Index/refundPrice"); LogHelper.WriteSendLog("退款-返回参数:" + ResString); return ResString; } #endregion #region 退款查询 //交易支付接口测试 //商家扫用户支付二维码 //传入参数 //@operator_id:操作员编号 //@terminal_id:设备编号 //@out_refund_no:商户退款单号 //@MerchantInfo:商户信息(商户号,秘钥) public static string RefundQuery(string operator_id, string terminal_id, string out_refund_no, string MerchantInfo) { string ResString = ""; if (string.IsNullOrEmpty(MerchantInfo)) { return null; } Dictionary _ApiDictionary = new Dictionary { { "bind_code", MerchantInfo },//商户编号 { "openid", openid },//客无忧平台OPENID { "nonce_str", "0" }, //随机字符串 { "time_stamp", Convert.ToInt32((DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds).ToString() }//时间戳 }; if (!string.IsNullOrEmpty(out_refund_no)) { _ApiDictionary.Add("out_refund_no", out_refund_no);//商户订单号 } _ApiDictionary.Add("sign", getSign(_ApiDictionary, key)); //拼接交易请求参数 string str_PostData = paramsToString(_ApiDictionary); LogHelper.WriteReceiveLog("退款查询-请求参数:" + str_PostData); //调用服务商接口进行退款查询 ResString = CommonHelper.HttpUrlPost(str_PostData, req_url + "/Payapi/Index/refundOrderQuery"); LogHelper.WriteSendLog("退款查询-返回参数:" + ResString); return ResString; } #endregion #region 获取签名 /// /// 获取签名 /// /// /// /// private static string getSign(Dictionary dic, string key) { var list = dic.OrderBy(s => s.Key); string str = ""; foreach (var ss in list) { str += ss.Key + "=" + ss.Value + "&"; } str += "key=" + key; str = get_md5(str).ToUpper(); return str; } private static string get_md5(string str) { var md5 = MD5.Create(); var result_byte = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); return BitConverter.ToString(result_byte).Replace("-", "").ToLower(); } private static string paramsToString(Dictionary dic) { string str = String.Empty; foreach (var e in dic) { str += e.Key + "=" + e.Value + "&"; } str = str.TrimEnd('&'); return str; } #endregion } }