329 lines
15 KiB
C#
329 lines
15 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using swiftpass.utils;
|
||
|
||
namespace SwiftPayService
|
||
{
|
||
public class Common
|
||
{
|
||
//商户秘钥
|
||
protected const string key = "9d101c97133837e13dde2d32a5054abb";
|
||
//接口请求地址,固定不变,无需修改
|
||
protected const string req_url = "https://pay.swiftpass.cn/pay/gateway";
|
||
//版本号,固定不变,无需修改
|
||
protected const string version = "2.0";
|
||
//服务区、门店编码
|
||
protected static string[] ShopCode = ConfigurationManager.AppSettings["ShopCode"].ToString().Split(',');
|
||
//商户号
|
||
protected static string[] Business_Code = ConfigurationManager.AppSettings["Business_Code"].ToString().Split(',');
|
||
//商户号
|
||
protected static string[] term_Code = ConfigurationManager.AppSettings["term_Code"].ToString().Split(',');
|
||
|
||
#region 商家扫用户支付二维码
|
||
//交易支付接口测试
|
||
//商家扫用户支付二维码
|
||
//传入参数
|
||
//@auth_code:用户付款授权码
|
||
//@orderNo:商户订单号
|
||
//@body:商品描述
|
||
//@attach:附加信息
|
||
//@total_fee:总金额
|
||
//@mch_create_ip:终端IP
|
||
//@time_start:订单生成时间(yyyyMMddHHmmss)
|
||
//@timeout:订单超时时间(yyyyMMddHHmmss)
|
||
//@MerchantInfo:商户信息(商户号,秘钥)
|
||
//@notify_url:通知回调地址
|
||
public static Hashtable merchantscan(string auth_code, string out_trade_no, string body, string attach, string total_fee,
|
||
string mch_create_ip, string time_start, string timeout, string[] MerchantInfo, string notify_url)
|
||
{
|
||
if (string.IsNullOrEmpty(MerchantInfo[0]) || string.IsNullOrEmpty(MerchantInfo[1]))
|
||
{
|
||
return null;
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(req_url);
|
||
_RequestHandler.setKey(MerchantInfo[1]);
|
||
_RequestHandler.setParameter("auth_code", auth_code);//付款授权码
|
||
_RequestHandler.setParameter("out_trade_no", out_trade_no);//商户订单号
|
||
_RequestHandler.setParameter("body", body);//商品描述
|
||
_RequestHandler.setParameter("attach", attach);//附加信息
|
||
_RequestHandler.setParameter("total_fee", total_fee);//总金额
|
||
_RequestHandler.setParameter("mch_create_ip", mch_create_ip);//终端IP
|
||
_RequestHandler.setParameter("time_start", time_start); //订单生成时间
|
||
_RequestHandler.setParameter("time_expire", timeout);//订单超时时间
|
||
_RequestHandler.setParameter("service", "unified.trade.micropay");//接口类型:
|
||
_RequestHandler.setParameter("mch_id", MerchantInfo[0]);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", version);//接口版本号
|
||
//通知地址,必填项,接收平台通知的URL,需给绝对路径,255字符内;此URL要保证外网能访问
|
||
_RequestHandler.setParameter("notify_url", notify_url);
|
||
_RequestHandler.setParameter("nonce_str", Utils.random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = 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());
|
||
_ClientResponseHandler.setKey(MerchantInfo[1]);
|
||
_Hashtable = _ClientResponseHandler.getAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
|
||
return _Hashtable;
|
||
}
|
||
#endregion
|
||
|
||
#region 订单查询
|
||
//交易支付接口测试
|
||
//商家扫用户支付二维码
|
||
//传入参数
|
||
//@out_trade_no:商户订单号
|
||
//@transaction_id:平台订单号
|
||
//@MerchantInfo:商户信息(商户号,秘钥)
|
||
public static Hashtable TradeQuery(string out_trade_no, string transaction_id, string[] MerchantInfo)
|
||
{
|
||
if (string.IsNullOrEmpty(MerchantInfo[0]) || string.IsNullOrEmpty(MerchantInfo[1]))
|
||
{
|
||
return null;
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(req_url);
|
||
_RequestHandler.setKey(MerchantInfo[1]);
|
||
_RequestHandler.setParameter("out_trade_no", out_trade_no);//商户订单号
|
||
_RequestHandler.setParameter("transaction_id", transaction_id);//平台订单号
|
||
_RequestHandler.setParameter("service", "unified.trade.query");//接口 unified.trade.query
|
||
_RequestHandler.setParameter("mch_id", MerchantInfo[0]);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", version);//接口版本号
|
||
|
||
_RequestHandler.setParameter("nonce_str", Utils.random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = 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());
|
||
_ClientResponseHandler.setKey(MerchantInfo[1]);
|
||
_Hashtable = _ClientResponseHandler.getAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
|
||
return _Hashtable;
|
||
}
|
||
#endregion
|
||
|
||
#region 订单退款
|
||
//交易支付接口测试
|
||
//商家扫用户支付二维码
|
||
//传入参数
|
||
//@out_trade_no:商户订单号
|
||
//@transaction_id:平台订单号
|
||
//@out_refund_no:商户退款单号
|
||
//@total_fee:总金额
|
||
//@refund_fee:退款金额
|
||
//@refund_channel:ORIGINAL-原路退款,默认
|
||
//@MerchantInfo:商户信息(商户号,秘钥)
|
||
public static Hashtable TradeRefund(string out_trade_no, string transaction_id, string out_refund_no,
|
||
string total_fee, string refund_fee, string refund_channel, string[] MerchantInfo)
|
||
{
|
||
if (string.IsNullOrEmpty(MerchantInfo[0]) || string.IsNullOrEmpty(MerchantInfo[1]))
|
||
{
|
||
return null;
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(req_url);
|
||
_RequestHandler.setKey(MerchantInfo[1]);
|
||
_RequestHandler.setParameter("out_trade_no", out_trade_no);//商户订单号
|
||
_RequestHandler.setParameter("transaction_id", transaction_id);//平台订单号
|
||
_RequestHandler.setParameter("out_refund_no", out_refund_no);//商户退款单号
|
||
_RequestHandler.setParameter("total_fee", total_fee);//总金额
|
||
_RequestHandler.setParameter("refund_fee", refund_fee);//退款金额
|
||
_RequestHandler.setParameter("refund_channel", refund_channel);//退款渠道
|
||
|
||
_RequestHandler.setParameter("service", "unified.trade.refund");//接口 unified.trade.refund
|
||
_RequestHandler.setParameter("mch_id", MerchantInfo[0]);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", version);//接口版本号
|
||
_RequestHandler.setParameter("op_user_id", MerchantInfo[0]);//必填项,操作员帐号,默认为商户号
|
||
_RequestHandler.setParameter("nonce_str", Utils.random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = 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());
|
||
_ClientResponseHandler.setKey(MerchantInfo[1]);
|
||
_Hashtable = _ClientResponseHandler.getAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
|
||
return _Hashtable;
|
||
}
|
||
#endregion
|
||
|
||
#region 退款查询
|
||
//交易支付接口测试
|
||
//商家扫用户支付二维码
|
||
//传入参数
|
||
//@out_trade_no:商户订单号
|
||
//@transaction_id:平台订单号
|
||
//@out_refund_no:商户退款单号
|
||
//@refund_id:平台退款单号
|
||
//@MerchantInfo:商户信息(商户号,秘钥)
|
||
public static Hashtable RefundQuery(string out_trade_no, string transaction_id, string out_refund_no,
|
||
string refund_id, string[] MerchantInfo)
|
||
{
|
||
if (string.IsNullOrEmpty(MerchantInfo[0]) || string.IsNullOrEmpty(MerchantInfo[1]))
|
||
{
|
||
return null;
|
||
}
|
||
ClientResponseHandler _ClientResponseHandler = new ClientResponseHandler();
|
||
PayHttpClient _PayHttpClient = new PayHttpClient();
|
||
RequestHandler _RequestHandler = new RequestHandler(null);
|
||
//加载配置数据
|
||
//Dictionary<string, string> cfg = new Dictionary<string, string>(1);
|
||
Hashtable _Hashtable = new Hashtable();
|
||
//cfg = Utils.loadCfg();
|
||
//初始化数据
|
||
//初始化数据
|
||
_RequestHandler.setGateUrl(req_url);
|
||
_RequestHandler.setKey(MerchantInfo[1]);
|
||
_RequestHandler.setParameter("out_trade_no", out_trade_no);//商户订单号
|
||
_RequestHandler.setParameter("transaction_id", transaction_id);//平台订单号
|
||
_RequestHandler.setParameter("out_refund_no", out_refund_no);//商户退款单号
|
||
_RequestHandler.setParameter("refund_id", refund_id);//平台退款单号
|
||
|
||
_RequestHandler.setParameter("service", "unified.trade.refundquery");//接口 unified.trade.refundquery
|
||
_RequestHandler.setParameter("mch_id", MerchantInfo[0]);//必填项,商户号,由平台分配
|
||
_RequestHandler.setParameter("version", version);//接口版本号
|
||
|
||
_RequestHandler.setParameter("nonce_str", Utils.random());//随机字符串,必填项,不长于 32 位
|
||
_RequestHandler.createSign();//创建签名
|
||
//以上参数进行签名
|
||
string data = 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());
|
||
_ClientResponseHandler.setKey(MerchantInfo[1]);
|
||
_Hashtable = _ClientResponseHandler.getAllParameters();
|
||
if (!_ClientResponseHandler.isTenpaySign())
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_Hashtable = null;
|
||
}
|
||
|
||
return _Hashtable;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取商户编号
|
||
public static string[] GetMerchantInfo(string TICKETCODE)
|
||
{
|
||
string[] MerchantInfo = new string[3];
|
||
string _SERVERPARTCODE = TICKETCODE.Substring(0, 6);
|
||
string _SHOPCODE = TICKETCODE.Length == 28 ? TICKETCODE.Substring(6, 4) : TICKETCODE.Substring(6, 6);
|
||
|
||
for (int i = 0; i < ShopCode.Length; i++)
|
||
{
|
||
if (_SERVERPARTCODE == ShopCode[i] || _SHOPCODE.Substring(2, _SHOPCODE.Length - 2) == ShopCode[i] ||
|
||
_SHOPCODE == ShopCode[i])
|
||
{
|
||
MerchantInfo[0] = Business_Code[i];
|
||
MerchantInfo[1] = term_Code[i];
|
||
break;
|
||
}
|
||
}
|
||
|
||
return MerchantInfo;
|
||
}
|
||
#endregion
|
||
|
||
#region 根据门店获取商户编号
|
||
public static string[] GetMerchantInfo(string serverPartCode, string shopCode)
|
||
{
|
||
string[] MerchantInfo = new string[3];
|
||
|
||
for (int i = 0; i < ShopCode.Length; i++)
|
||
{
|
||
if (serverPartCode == ShopCode[i] || shopCode == ShopCode[i]
|
||
|| shopCode.Substring(2, shopCode.Length - 2) == ShopCode[i])
|
||
{
|
||
MerchantInfo[0] = Business_Code[i];
|
||
MerchantInfo[1] = term_Code[i];
|
||
break;
|
||
}
|
||
}
|
||
return MerchantInfo;
|
||
}
|
||
#endregion
|
||
}
|
||
} |