377 lines
18 KiB
C#
377 lines
18 KiB
C#
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using WebService.SDK.PayCommon;
|
||
using WebService.SDK.UnionPay.Utils;
|
||
|
||
namespace WebService.SDK.UnionPay
|
||
{
|
||
public class UnionPayAPI
|
||
{
|
||
/// <summary>
|
||
/// 扫码支付接口
|
||
/// </summary>
|
||
/// <param name="payConfig">接口配置</param>
|
||
/// <param name="merchantInfo">平台商户信息</param>
|
||
/// <param name="authCode">用户付款授权码</param>
|
||
/// <param name="mch_create_ip">设备IP</param>
|
||
/// <param name="merchantOrderCode">商户交易订单号</param>
|
||
/// <param name="body">商品描述</param>
|
||
/// <param name="payAmount">交易金额(元)</param>
|
||
/// <param name="timeStart">订单生成时间</param>
|
||
/// <param name="timeOut">订单超时时间</param>
|
||
/// <param name="attach">附加信息</param>
|
||
/// <param name="notify_url">通知地址</param>
|
||
/// <returns></returns>
|
||
public static Model.ScanResultModel MerchantScanPay(UnionPayConfig payConfig,
|
||
MobilePayConfig.PayMerchant merchantInfo, string authCode, string mch_create_ip,
|
||
string merchantOrderCode, string body, string payAmount,
|
||
string timeStart, string timeOut, string attach = "", string notify_url = "")
|
||
{
|
||
if (payConfig == null)
|
||
{
|
||
return default(Model.ScanResultModel);
|
||
}
|
||
if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode))
|
||
{
|
||
return default(Model.ScanResultModel);
|
||
}
|
||
if (!decimal.TryParse(payAmount, out decimal _PayAmount) || _PayAmount <= 0)
|
||
{
|
||
return default(Model.ScanResultModel);
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
_PayHttpClient.SetTimeOut(payConfig.PlatformTimeout);
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(payConfig.UnionPayAPIURL);
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_RequestHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_RequestHandler.setKey(payConfig.PlatformKey);
|
||
_RequestHandler.setParameter("sign_agentno", payConfig.Organization);//平台机构号,统一签名密钥
|
||
}
|
||
_RequestHandler.setParameter("auth_code", authCode);//付款授权码
|
||
_RequestHandler.setParameter("out_trade_no", merchantOrderCode);//商户订单号
|
||
_RequestHandler.setParameter("body", body);//商品描述
|
||
_RequestHandler.setParameter("attach", attach);//附加信息
|
||
_RequestHandler.setParameter("total_fee", (_PayAmount * 100).ToString("F0"));//总金额
|
||
_RequestHandler.setParameter("mch_create_ip", mch_create_ip);//终端IP
|
||
_RequestHandler.setParameter("time_start", timeStart); //订单生成时间
|
||
_RequestHandler.setParameter("time_expire", timeOut);//订单超时时间
|
||
_RequestHandler.setParameter("service", "unified.trade.micropay");//接口类型:
|
||
_RequestHandler.setParameter("mch_id", merchantInfo.MerchantCode);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", payConfig.Version);//接口版本号
|
||
//通知地址,必填项,接收平台通知的URL,需给绝对路径,255字符内;此URL要保证外网能访问
|
||
_RequestHandler.setParameter("notify_url", notify_url);
|
||
_RequestHandler.setParameter("nonce_str", Utils.Utils.Random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = Utils.Utils.ToXml(_RequestHandler.getAllParameters());//生成XML报文
|
||
Dictionary<string, string> reqContent = new Dictionary<string, string>();
|
||
reqContent.Add("url", _RequestHandler.getGateUrl());
|
||
reqContent.Add("data", data);
|
||
_PayHttpClient.SetReqContent(reqContent);
|
||
|
||
if (_PayHttpClient.Call())
|
||
{
|
||
_ClientResponseHandler.setContent(_PayHttpClient.GetResContent());
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_ClientResponseHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_ClientResponseHandler.setKey(payConfig.PlatformKey);
|
||
}
|
||
_Hashtable = _ClientResponseHandler.GetAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
|
||
return JsonConvert.DeserializeObject<Model.ScanResultModel>(JsonConvert.SerializeObject(_Hashtable));
|
||
}
|
||
|
||
/// <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 QueryPay(UnionPayConfig 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);
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
_PayHttpClient.SetTimeOut(payConfig.PlatformTimeout);
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(payConfig.UnionPayAPIURL);
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_RequestHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_RequestHandler.setKey(payConfig.PlatformKey);
|
||
_RequestHandler.setParameter("sign_agentno", payConfig.Organization);//平台机构号,统一签名密钥
|
||
}
|
||
_RequestHandler.setParameter("out_trade_no", merchantOrderCode);//商户订单号
|
||
if (!string.IsNullOrWhiteSpace(platformOrderCode) && platformOrderCode != merchantOrderCode)
|
||
{
|
||
_RequestHandler.setParameter("transaction_id", platformOrderCode);//平台订单号
|
||
}
|
||
_RequestHandler.setParameter("service", "unified.trade.query");//接口 unified.trade.query
|
||
_RequestHandler.setParameter("mch_id", merchantInfo.MerchantCode);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", payConfig.Version);//接口版本号
|
||
|
||
_RequestHandler.setParameter("nonce_str", Utils.Utils.Random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = Utils.Utils.ToXml(_RequestHandler.getAllParameters());//生成XML报文
|
||
Dictionary<string, string> reqContent = new Dictionary<string, string>();
|
||
reqContent.Add("url", _RequestHandler.getGateUrl());
|
||
reqContent.Add("data", data);
|
||
_PayHttpClient.SetReqContent(reqContent);
|
||
|
||
if (_PayHttpClient.Call())
|
||
{
|
||
_ClientResponseHandler.setContent(_PayHttpClient.GetResContent());
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_ClientResponseHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_ClientResponseHandler.setKey(payConfig.PlatformKey);
|
||
}
|
||
_Hashtable = _ClientResponseHandler.GetAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
|
||
return JsonConvert.DeserializeObject<Model.QueryResultModel>(JsonConvert.SerializeObject(_Hashtable));
|
||
|
||
}
|
||
/// <summary>
|
||
/// 交易退款接口
|
||
/// </summary>
|
||
/// <param name="payConfig">接口配置</param>
|
||
/// <param name="merchantInfo">平台商户信息</param>
|
||
/// <param name="merchantOrderCode">商户订单号
|
||
/// 注:与平台订单号必填一项</param>
|
||
/// <param name="platformOrderCode">平台订单号
|
||
/// 注:与商户订单号必填一项</param>
|
||
/// <param name="refundOrderCode">商户退款单号</param>
|
||
/// <param name="payAmount">退款金额(元)</param>
|
||
/// <param name="refundChannel">退款渠道</param>
|
||
/// <returns></returns>
|
||
public static Model.RefundResultModel RefundPay(UnionPayConfig payConfig,
|
||
MobilePayConfig.PayMerchant merchantInfo,
|
||
string platformOrderCode, string merchantOrderCode,
|
||
string refundOrderCode, string payAmount, string refundChannel = "ORIGINAL")
|
||
{
|
||
if (payConfig == null)
|
||
{
|
||
return default(Model.RefundResultModel);
|
||
}
|
||
if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode))
|
||
{
|
||
return default(Model.RefundResultModel);
|
||
}
|
||
if (!decimal.TryParse(payAmount, out decimal _PayAmount) || _PayAmount <= 0)
|
||
{
|
||
return default(Model.RefundResultModel);
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
_PayHttpClient.SetTimeOut(payConfig.PlatformTimeout);
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(payConfig.UnionPayAPIURL);
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_RequestHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_RequestHandler.setKey(payConfig.PlatformKey);
|
||
_RequestHandler.setParameter("sign_agentno", payConfig.Organization);//平台机构号,统一签名密钥
|
||
}
|
||
_RequestHandler.setParameter("out_trade_no", merchantOrderCode);//商户订单号
|
||
_RequestHandler.setParameter("transaction_id", platformOrderCode);//平台订单号
|
||
_RequestHandler.setParameter("out_refund_no", refundOrderCode);//商户退款单号
|
||
_RequestHandler.setParameter("total_fee", (_PayAmount * 100).ToString("F0"));//总金额
|
||
_RequestHandler.setParameter("refund_fee", (_PayAmount * 100).ToString("F0"));//退款金额
|
||
_RequestHandler.setParameter("refund_channel", refundChannel);//退款渠道
|
||
|
||
_RequestHandler.setParameter("service", "unified.trade.refund");//接口 unified.trade.refund
|
||
_RequestHandler.setParameter("mch_id", merchantInfo.MerchantCode);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", payConfig.Version);//接口版本号
|
||
_RequestHandler.setParameter("op_user_id", merchantInfo.MerchantCode);//必填项,操作员帐号,默认为商户号
|
||
_RequestHandler.setParameter("nonce_str", Utils.Utils.Random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = Utils.Utils.ToXml(_RequestHandler.getAllParameters());//生成XML报文
|
||
Dictionary<string, string> reqContent = new Dictionary<string, string>();
|
||
reqContent.Add("url", _RequestHandler.getGateUrl());
|
||
reqContent.Add("data", data);
|
||
_PayHttpClient.SetReqContent(reqContent);
|
||
|
||
if (_PayHttpClient.Call())
|
||
{
|
||
_ClientResponseHandler.setContent(_PayHttpClient.GetResContent());
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_ClientResponseHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_ClientResponseHandler.setKey(payConfig.PlatformKey);
|
||
}
|
||
_Hashtable = _ClientResponseHandler.GetAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
|
||
return JsonConvert.DeserializeObject<Model.RefundResultModel>(JsonConvert.SerializeObject(_Hashtable));
|
||
}
|
||
/// <summary>
|
||
/// 交易退款查询接口
|
||
/// </summary>
|
||
/// <param name="payConfig">接口配置</param>
|
||
/// <param name="merchantInfo">平台商户信息</param>
|
||
/// <param name="platformOrderCode">平台订单号</param>
|
||
/// <param name="merchantOrderCode">商户订单号</param>
|
||
/// <param name="platformRefundCode">平台退款单号</param>
|
||
/// <param name="refundOrderCode">商户退款单号</param>
|
||
/// <returns></returns>
|
||
public static Model.RefundQueryResultModel RefundQuery(UnionPayConfig payConfig,
|
||
MobilePayConfig.PayMerchant merchantInfo, string platformOrderCode,
|
||
string merchantOrderCode, string platformRefundCode, string refundOrderCode)
|
||
{
|
||
if (payConfig == null)
|
||
{
|
||
return default(Model.RefundQueryResultModel);
|
||
}
|
||
if (merchantInfo == null || string.IsNullOrWhiteSpace(merchantInfo.MerchantCode))
|
||
{
|
||
return default(Model.RefundQueryResultModel);
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
_PayHttpClient.SetTimeOut(payConfig.PlatformTimeout);
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(payConfig.UnionPayAPIURL);
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_RequestHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_RequestHandler.setKey(payConfig.PlatformKey);
|
||
_RequestHandler.setParameter("sign_agentno", payConfig.Organization);//平台机构号,统一签名密钥
|
||
}
|
||
_RequestHandler.setParameter("out_trade_no", merchantOrderCode);//商户订单号
|
||
_RequestHandler.setParameter("transaction_id", platformOrderCode);//平台订单号
|
||
_RequestHandler.setParameter("out_refund_no", refundOrderCode);//商户退款单号
|
||
_RequestHandler.setParameter("refund_id", platformRefundCode);//平台退款单号
|
||
|
||
_RequestHandler.setParameter("service", "unified.trade.refundquery");//接口 unified.trade.refundquery
|
||
_RequestHandler.setParameter("mch_id", merchantInfo.MerchantCode);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", payConfig.Version);//接口版本号
|
||
|
||
_RequestHandler.setParameter("nonce_str", Utils.Utils.Random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = Utils.Utils.ToXml(_RequestHandler.getAllParameters());//生成XML报文
|
||
Dictionary<string, string> reqContent = new Dictionary<string, string>();
|
||
reqContent.Add("url", _RequestHandler.getGateUrl());
|
||
reqContent.Add("data", data);
|
||
_PayHttpClient.SetReqContent(reqContent);
|
||
|
||
if (_PayHttpClient.Call())
|
||
{
|
||
_ClientResponseHandler.setContent(_PayHttpClient.GetResContent());
|
||
if (!string.IsNullOrWhiteSpace(merchantInfo.MerchantKey))
|
||
{
|
||
_ClientResponseHandler.setKey(merchantInfo.MerchantKey);
|
||
}
|
||
else
|
||
{
|
||
_ClientResponseHandler.setKey(payConfig.PlatformKey);
|
||
}
|
||
_Hashtable = _ClientResponseHandler.GetAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
return JsonConvert.DeserializeObject<Model.RefundQueryResultModel>(JsonConvert.SerializeObject(_Hashtable));
|
||
}
|
||
}
|
||
}
|