using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using WebService.SDK.PayCommon; namespace WebService.SDK.KwyPay { public class KwyPayAPI { /// /// 扫码支付接口 /// /// 接口配置 /// 平台商户信息 /// 用户付款授权码 /// 商户交易订单号 /// 交易金额(元) /// public static Model.QueryResultModel MicroPay(KwyPayConfig payConfig, MobilePayConfig.PayMerchant merchantInfo, string authCode, string merchantOrderCode, string payAmount) { if (payConfig == null) { return default(Model.QueryResultModel); } if (!decimal.TryParse(payAmount, out decimal _PayAmount) || _PayAmount <= 0) { return default(Model.QueryResultModel); } Dictionary _ApiDictionary = new Dictionary { { "auth_code", authCode },//支付宝/微信授权码 { "bind_code", merchantInfo.MerchantCode },//商户编号 { "body", payAmount },//备注信息 { "no", merchantOrderCode },//商户交易订单号 { "nonce_str", "0" }, //随机字符串 { "openid", payConfig.PlatformAPPID },//客无忧平台对接的OPENID { "order_title", "服务区商品" },//订单标题 { "price", payAmount },//订单金额 { "time_stamp",Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() }//时间戳 }; _ApiDictionary.Add("sign", ApiParamSign(_ApiDictionary, payConfig.PlatformKey)); LogHelper.WriteReceiveLog("支付反扫-传第三方参数:" + _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b)); return JsonConvert.DeserializeObject(CommonHelper.HttpUrlPost( _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b), payConfig.MicroPayURL, "application/x-www-form-urlencoded", payConfig.PlatformTimeout)); } /// /// 二维码支付接口 /// /// 接口配置 /// 平台商户信息 /// 用户付款授权码 /// 商户交易订单号 /// 交易金额(元) /// 支付渠道 /// public static Model.WebPayResultModel WebPay(KwyPayConfig payConfig, MobilePayConfig.PayMerchant merchantInfo, string merchantOrderCode, string payAmount ) { if (payConfig == null) { return default(Model.WebPayResultModel); } if (!decimal.TryParse(payAmount, out decimal _PayAmount) || _PayAmount <= 0) { return default(Model.WebPayResultModel); } Dictionary _ApiDictionary = new Dictionary { { "openid", payConfig.PlatformAPPID },//客无忧平台对接的openid { "bind_code", merchantInfo.MerchantCode },//商户编号 { "no", merchantOrderCode },//交易上行流水号 { "order_title", "服务区商品" },//支付描述信息 { "nonce_str", "0" }, //随机字符串 { "body", payAmount },//备注 { "price", payAmount },//交易金额 { "time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() },//时间戳 { "notify_url", "https://www.weiwoju.com" }, { "callback_url", "https://www.weiwoju.com" } }; _ApiDictionary.Add("sign", ApiParamSign(_ApiDictionary, payConfig.PlatformKey)); LogHelper.WriteReceiveLog("支付正扫-传第三方参数:" + _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b)); return JsonConvert.DeserializeObject(CommonHelper.HttpUrlPost( _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b), payConfig.WebPayURL, "application/x-www-form-urlencoded", payConfig.PlatformTimeout)); } /// /// 支付查询接口 /// /// 接口配置 /// 平台商户信息 /// 商户订单号 /// 注:与平台订单号必填一项 /// 平台订单号 /// 注:与商户订单号必填一项 /// 交易金额(元) /// public static Model.QueryResultModel OrderQuery(KwyPayConfig payConfig, MobilePayConfig.PayMerchant merchantInfo, string merchantOrderCode, string platformOrderCode, string payAmount) { if (payConfig == null) { return default(Model.QueryResultModel); } if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode)) { return default(Model.QueryResultModel); } Dictionary _ApiDictionary = new Dictionary { { "bind_code", merchantInfo.MerchantCode },//商户编号 { "openid", payConfig.PlatformAPPID },//客无忧平台OPENID { "nonce_str", "0" }, //随机字符串 { "time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() }//时间戳 }; if (!string.IsNullOrEmpty(platformOrderCode)) { _ApiDictionary.Add("transaction_id", platformOrderCode);//平台订单号 } if (!string.IsNullOrEmpty(merchantOrderCode)) { _ApiDictionary.Add("out_no", merchantOrderCode);//商户订单号 } _ApiDictionary.Add("sign", ApiParamSign(_ApiDictionary, payConfig.PlatformKey)); LogHelper.WriteReceiveLog("支付查询-传第三方参数:" + _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b)); return JsonConvert.DeserializeObject(CommonHelper.HttpUrlPost( _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b), payConfig.OrderQueryURL, "application/x-www-form-urlencoded", payConfig.PlatformTimeout)); } /// /// 退款接口 /// /// 接口配置 /// 平台商户信息 /// 平台订单号(原流水号) /// 注:与商户订单号必填一项 /// 商户订单号(原流水号) /// 注:与平台订单号必填一项 /// 退款订单号 /// 退款金额(元) /// public static Model.QueryResultModel RefundPrice(KwyPayConfig payConfig, MobilePayConfig.PayMerchant merchantInfo, string platformOrderCode, string merchantOrderCode, string refundOrderCode, string payAmount) { if (payConfig == null) { return default(Model.QueryResultModel); } if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode)) { return default(Model.QueryResultModel); } Dictionary _ApiDictionary = new Dictionary { { "bind_code", merchantInfo.MerchantCode },//商户编号 { "openid", payConfig.PlatformAPPID },//客无忧与平台对接的openid { "nonce_str", "0" }, //随机字符串 { "time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() },//时间戳 { "price", payAmount },//原订单金额 { "refund_no", refundOrderCode },//退款订单号 { "refund_price", payAmount }//退款金额 }; if (!string.IsNullOrWhiteSpace(platformOrderCode)) { _ApiDictionary.Add("transaction_id", platformOrderCode);//平台订单号 } if (!string.IsNullOrWhiteSpace(merchantOrderCode)) { _ApiDictionary.Add("out_no", merchantOrderCode);//商户订单号 } _ApiDictionary.Add("sign", ApiParamSign(_ApiDictionary, payConfig.PlatformKey)); LogHelper.WriteReceiveLog("支付退款-传第三方参数:" + _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b)); return JsonConvert.DeserializeObject(CommonHelper.HttpUrlPost( _ApiDictionary.Select(pair => pair.Key + "=" + pair.Value.ToString()). DefaultIfEmpty("").Aggregate((a, b) => a + "&" + b), payConfig.RefundPrice, "application/x-www-form-urlencoded", payConfig.PlatformTimeout)); } /// /// 客无忧支付平台参数签名 /// /// 待签名参数集 /// 签名密钥 /// public static string ApiParamSign(Dictionary signParm, string MD5Key) { string _strSign = ""; foreach (var keyValuePair in signParm.OrderBy(p => p.Key)) { _strSign += (string.IsNullOrWhiteSpace(_strSign) ? "" : "&") + keyValuePair.Key + "=" + keyValuePair.Value; } return BitConverter.ToString(MD5.Create().ComputeHash( Encoding.UTF8.GetBytes(_strSign + "&key=" + MD5Key))).Replace("-", "").ToUpper(); } } }