217 lines
11 KiB
C#
217 lines
11 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 扫码支付接口
|
|
/// </summary>
|
|
/// <param name="payConfig">接口配置</param>
|
|
/// <param name="merchantInfo">平台商户信息</param>
|
|
/// <param name="authCode">用户付款授权码</param>
|
|
/// <param name="merchantOrderCode">商户交易订单号</param>
|
|
/// <param name="payAmount">交易金额(元)</param>
|
|
/// <returns></returns>
|
|
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<string, string> _ApiDictionary = new Dictionary<string, string>
|
|
{
|
|
{ "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<Model.QueryResultModel>(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));
|
|
}
|
|
/// <summary>
|
|
/// 二维码支付接口
|
|
/// </summary>
|
|
/// <param name="payConfig">接口配置</param>
|
|
/// <param name="merchantInfo">平台商户信息</param>
|
|
/// <param name="authCode">用户付款授权码</param>
|
|
/// <param name="merchantOrderCode">商户交易订单号</param>
|
|
/// <param name="payAmount">交易金额(元)</param>
|
|
/// <param name="channelType">支付渠道</param>
|
|
/// <returns></returns>
|
|
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<string, string> _ApiDictionary = new Dictionary<string, string>
|
|
{
|
|
{ "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<Model.WebPayResultModel>(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));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 支付查询接口
|
|
/// </summary>
|
|
/// <param name="payConfig">接口配置</param>
|
|
/// <param name="merchantInfo">平台商户信息</param>
|
|
/// <param name="merchantOrderCode">商户订单号
|
|
/// 注:与平台订单号必填一项</param>
|
|
/// <param name="platformOrderCode">平台订单号
|
|
/// 注:与商户订单号必填一项</param>
|
|
/// <param name="payAmount">交易金额(元)</param>
|
|
/// <returns></returns>
|
|
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<string, string> _ApiDictionary = new Dictionary<string, string>
|
|
{
|
|
{ "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<Model.QueryResultModel>(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));
|
|
}
|
|
/// <summary>
|
|
/// 退款接口
|
|
/// </summary>
|
|
/// <param name="payConfig">接口配置</param>
|
|
/// <param name="merchantInfo">平台商户信息</param>
|
|
/// <param name="platformOrderCode">平台订单号(原流水号)
|
|
/// 注:与商户订单号必填一项</param>
|
|
/// <param name="merchantOrderCode">商户订单号(原流水号)
|
|
/// 注:与平台订单号必填一项</param>
|
|
/// <param name="refundOrderCode">退款订单号</param>
|
|
/// <param name="payAmount">退款金额(元)</param>
|
|
/// <returns></returns>
|
|
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<string, string> _ApiDictionary = new Dictionary<string, string>
|
|
{
|
|
{ "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<Model.QueryResultModel>(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));
|
|
}
|
|
/// <summary>
|
|
/// 客无忧支付平台参数签名
|
|
/// </summary>
|
|
/// <param name="signParm">待签名参数集</param>
|
|
/// <param name="MD5Key">签名密钥</param>
|
|
/// <returns></returns>
|
|
public static string ApiParamSign(Dictionary<string, string> 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();
|
|
}
|
|
}
|
|
}
|