2025-03-28 09:49:56 +08:00

230 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace WebService.SDK.PayCommon
{
public class KWYPayHelper
{
#region -> -
/// <summary>
/// 交易支付接口
/// 商家扫用户支付二维码
/// </summary>
/// <param name="auth_code">用户付款授权码</param>
/// <param name="out_trade_no">商户订单号</param>
/// <param name="pay_subject">商品描述</param>
/// <param name="total_fee">总金额</param>
/// <param name="merchantInfo">商户信息(商户号,秘钥)</param>
/// <param name="notify_url">通知回调地址</param>
/// <returns></returns>
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<string, string> _ApiDictionary = new Dictionary<string, string>
{
{ "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 -> 线-
/// <summary>
/// 用户扫线上二维码-正扫
/// </summary>
/// <param name="out_trade_no"></param>
/// <param name="pay_subject"></param>
/// <param name="total_fee"></param>
/// <param name="merchantInfo"></param>
/// <param name="notify_url"></param>
/// <param name="channel_type"></param>
/// <returns></returns>
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<string, string> _ApiDictionary = new Dictionary<string, string>
{
{ "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 ->
/// <summary>
/// 客无忧订单查询
/// </summary>
/// <param name="out_trade_no">平台订单号</param>
/// <param name="trade_no">商户订单号</param>
/// <param name="merchantInfo">平台商户号</param>
/// <returns></returns>
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<string, string> _ApiDictionary = new Dictionary<string, string>
{
{ "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 -> 退-
/// <summary>
/// 客无忧订单退款
/// </summary>
/// <param name="operator_id">操作员编号</param>
/// <param name="terminal_id">平台订单号</param>
/// <param name="refund_fee">退款金额</param>
/// <param name="refund_subject">退款描述</param>
/// <param name="out_trade_no">交易上行流水号</param>
/// <param name="trade_no">交易下行流水号</param>
/// <param name="out_refund_no">退款流水号</param>
/// <param name="merchantInfo">商户信息(商户号,秘钥)</param>
/// <returns></returns>
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<string, string> _ApiDictionary = new Dictionary<string, string>
{
{ "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校验
/// <summary>
/// 客无忧MD5签名
/// </summary>
/// <param name="ApiParam"></param>
/// <returns></returns>
private static string GetSign(Dictionary<string, string> 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();
}
/// <summary>
/// 客无忧参数
/// </summary>
/// <param name="ApiParam"></param>
/// <returns></returns>
private static string ApiParamToString(Dictionary<string, string> ApiParam)
{
string _strSign = "";
foreach (var keyValuePair in ApiParam)
{
_strSign += (string.IsNullOrWhiteSpace(_strSign) ? "" : "&") + keyValuePair.Key + "=" + keyValuePair.Value;
}
return _strSign;
}
#endregion
}
}