using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; namespace WebService.SDK.PayCommon { public class KWYPayHelper { #region 方法 -> 商家扫用户支付二维码 -反扫 /// /// 交易支付接口 /// 商家扫用户支付二维码 /// /// 用户付款授权码 /// 商户订单号 /// 商品描述 /// 总金额 /// 商户信息(商户号,秘钥) /// 通知回调地址 /// public static string MerchantScan(string auth_code, string out_trade_no, string pay_subject, string total_fee, MobilePayConfig.PayMerchant merchantInfo, string notify_url) { string ResString = ""; if (merchantInfo == null || merchantInfo.MerchantCode == null || merchantInfo.MerchantKey == null) { return null; } Dictionary _ApiDictionary = new Dictionary { { "auth_code", auth_code },//支付宝/微信授权码 { "bind_code", merchantInfo.MerchantCode },//平台商户号 { "body", total_fee },//交易金额 { "no", out_trade_no },//商户订单号 { "nonce_str", "0" }, //支付渠道 { "openid", MobilePayConfig.KWYPay.ApiOpenID },//客无忧与平台对接的openid { "order_title", pay_subject },//商品信息 { "price", total_fee },//支付金额 { "time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() }//时间戳 }; _ApiDictionary.Add("sign", GetSign(_ApiDictionary)); LogHelper.WriteReceiveLog("支付反扫-传第三方参数:" + ApiParamToString(_ApiDictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(ApiParamToString(_ApiDictionary), MobilePayConfig.KWYPay.BASE_URL + "/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, MobilePayConfig.PayMerchant merchantInfo, string notify_url, string channel_type) { string ResString = ""; if (merchantInfo == null || merchantInfo.MerchantCode == null || merchantInfo.MerchantKey == null) { return null; } Dictionary _ApiDictionary = new Dictionary { { "openid", MobilePayConfig.KWYPay.ApiOpenID },//客无忧与平台对接的openid { "bind_code", merchantInfo.MerchantCode },//商户编号 { "auth_code", out_trade_no },//支付宝/微信授权码 { "channel_type", channel_type }, //支付渠道 { "fee_type", "CNY" },//币种。默认CNY { "no", out_trade_no },//交易上行流水号 { "order_title", pay_subject },//支付描述信息 { "time_expire", "5" },//超时时间 { "body", total_fee },//交易金额 { "price", total_fee },//交易金额 { "trade_type", "QRCODE" },//交易类型 { "time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() },//币种。默认CNY { "notify_url", "https://www.weiwoju.com" }, { "callback_url", "https://www.weiwoju.com" } }; _ApiDictionary.Add("sign", GetSign(_ApiDictionary)); LogHelper.WriteReceiveLog("支付正扫_传第三方参数:" + ApiParamToString(_ApiDictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(ApiParamToString(_ApiDictionary), MobilePayConfig.KWYPay.BASE_URL + "/Webpay", "application/x-www-form-urlencoded", 5); LogHelper.WriteSendLog("支付正扫_返回的支付结果:" + ResString); return ResString; } #endregion #region 方法 -> 订单查询 /// /// 客无忧订单查询 /// /// 平台订单号 /// 商户订单号 /// 平台商户号 /// public static string TradeQuery(string out_trade_no, string trade_no, MobilePayConfig.PayMerchant merchantInfo) { string ResString = ""; if (merchantInfo == null || merchantInfo.MerchantCode == null || merchantInfo.MerchantKey == null) { return null; } Dictionary _ApiDictionary = new Dictionary { { "bind_code", merchantInfo.MerchantCode },//商户编号 { "openid", MobilePayConfig.KWYPay.ApiOpenID },// 客无忧平台openid { "time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() }//币种。默认CNY }; if (!string.IsNullOrEmpty(trade_no)) { _ApiDictionary.Add("transaction_id", trade_no);//交易上行流水号 } if (!string.IsNullOrEmpty(out_trade_no)) { _ApiDictionary.Add("out_no", out_trade_no);//交易下行流水号 } _ApiDictionary.Add("sign", GetSign(_ApiDictionary)); LogHelper.WriteReceiveLog("支付查询记录…传第三方参数:" + ApiParamToString(_ApiDictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(ApiParamToString(_ApiDictionary), MobilePayConfig.KWYPay.BASE_URL + "/orderQuery"); LogHelper.WriteSendLog("支付查询记录第三方…返回值:" + ResString); return ResString; } #endregion #region 方法 -> 订单退款-正扫 /// /// 客无忧订单退款 /// /// 操作员编号 /// 平台订单号 /// 退款金额 /// 退款描述 /// 交易上行流水号 /// 交易下行流水号 /// 退款流水号 /// 商户信息(商户号,秘钥) /// 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, MobilePayConfig.PayMerchant merchantInfo) { string ResString = ""; if (merchantInfo == null || merchantInfo.MerchantCode == null || merchantInfo.MerchantKey == null) { return null; } long timestamp = CommonHelper.ConvertDataTimeLong(DateTime.Now); Dictionary _ApiDictionary = new Dictionary { { "bind_code", merchantInfo.MerchantCode },//商户编号 { "openid", MobilePayConfig.KWYPay.ApiOpenID },//客无忧与平台对接的openid { "channel_type", "0" }, //支付渠道 { "fee_type", "CNY" },//币种。默认CNY { "operator_id", operator_id },//操作员编号 { "transaction_id", trade_no },//退款流水号 { "nonce_str", "0" }, //支付渠道 { "price", refund_fee },//退款金额 { "refund_price", refund_fee },//退款金额 { "refund_subject", refund_subject },//退款描述 { "terminal_id", terminal_id },//设备编号 { "time_stamp", Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString() }//币种。默认CNY }; if (!string.IsNullOrEmpty(out_trade_no)) { _ApiDictionary.Add("out_trade_no", out_trade_no);//交易上行流水号 } if (!string.IsNullOrEmpty(out_refund_no)) { _ApiDictionary.Add("refund_no", out_refund_no);//交易下行流水号 } _ApiDictionary.Add("sign", GetSign(_ApiDictionary)); LogHelper.WriteReceiveLog("退款-请求参数:" + ApiParamToString(_ApiDictionary)); //以上参数进行签名 ResString = CommonHelper.HttpUrlPost(ApiParamToString(_ApiDictionary), MobilePayConfig.KWYPay.BASE_URL + "/refundPrice"); LogHelper.WriteSendLog("退款-返回参数:" + ResString); return ResString; } #endregion #region 方法 -> 客无忧参数MD5校验 /// /// 客无忧MD5签名 /// /// /// private static string GetSign(Dictionary ApiParam) { string _strSign = ""; foreach (var keyValuePair in ApiParam.OrderBy(p => p.Key)) { _strSign += (string.IsNullOrWhiteSpace(_strSign) ? "" : "&") + keyValuePair.Key + "=" + keyValuePair.Value; } return BitConverter.ToString(MD5.Create().ComputeHash( Encoding.UTF8.GetBytes(_strSign + "&key=" + MobilePayConfig.KWYPay.ApiKey))).Replace("-", "").ToLower(); } /// /// 客无忧参数 /// /// /// private static string ApiParamToString(Dictionary ApiParam) { string _strSign = ""; foreach (var keyValuePair in ApiParam) { _strSign += (string.IsNullOrWhiteSpace(_strSign) ? "" : "&") + keyValuePair.Key + "=" + keyValuePair.Value; } return _strSign; } #endregion } }