|
|
using System.IO.Compression;
|
|
|
using com.hitrust.util.GZip;
|
|
|
|
|
|
namespace com.hitrust.trustpay.client
|
|
|
{
|
|
|
using Security;
|
|
|
//using util.GZip;
|
|
|
using System;
|
|
|
using System.IO;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
|
|
|
public abstract class TrxRequest
|
|
|
{
|
|
|
protected string iBusinessID = "";
|
|
|
protected string iChannelType = "";
|
|
|
protected string iCustomer = "";
|
|
|
protected string iFunctionID = "";
|
|
|
private LogWriter iLogWriter = null;
|
|
|
protected string iMerchantID = "";
|
|
|
protected string iMerchantRemarks = "";
|
|
|
protected string iRequestID = "";
|
|
|
|
|
|
protected TrxRequest() {}
|
|
|
|
|
|
protected virtual void checkRequest()
|
|
|
{
|
|
|
if ((this.iMerchantID == null) || (this.iMerchantID.Length > 15)) {
|
|
|
throw new TrxException("1101", "商户提交的交易资料不完整", "商户Id[" + this.iMerchantID + "]不正确");
|
|
|
}
|
|
|
if ((this.iFunctionID == null) || (this.iFunctionID.Trim().Length == 0))
|
|
|
{
|
|
|
throw new TrxException("1101", "商户提交的交易资料不完整", "交易码FunctionID[" + this.iFunctionID + "]不能为空");
|
|
|
}
|
|
|
if ((this.iMerchantRemarks != null) && (this.iMerchantRemarks.Length > 200)) {
|
|
|
throw new TrxException("1101", "商户提交的交易资料不完整", "商户备注[" + this.iMerchantRemarks + "]超过长度限制");
|
|
|
}
|
|
|
if ((this.iRequestID == null) || (this.iRequestID.Trim().Length == 0)) {
|
|
|
throw new TrxException("1101", "商户提交的交易资料不完整", "缺少商户请求流水号!");
|
|
|
}
|
|
|
if (this.iRequestID.Length > 12) {
|
|
|
throw new TrxException("1101", "商户提交的交易资料不完整", "商户请求流水号[" + this.iRequestID + "]的长度超过12!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
protected internal abstract TrxResponse constructResponse(XMLDocument aResponseMessage);
|
|
|
|
|
|
public virtual TrxResponse extendPostRequest(int aMerchantNo)
|
|
|
{
|
|
|
TrxResponse tTrxResponse = null;
|
|
|
try {
|
|
|
this.iLogWriter = new LogWriter();
|
|
|
this.iLogWriter.logNewLine("TrustPayClient Market ASP V1.0.1 交易开始==========================");
|
|
|
if ((aMerchantNo < 0) || (aMerchantNo > MerchantConfig.MerchantNum)) {
|
|
|
throw new TrxException("1008", "指定的商户配置编号不合法", string.Concat(new object[] {"配置文件中商户数为", MerchantConfig.MerchantNum, ", 但是请求指定的商户配置编号为", aMerchantNo, "!"}));
|
|
|
}
|
|
|
this.iLogWriter.logNewLine("检查交易请求是否合法:");
|
|
|
this.checkRequest();
|
|
|
this.iLogWriter.log("正确");
|
|
|
XMLDocument tRequestMessage = this.RequestMessage;
|
|
|
tRequestMessage = MerchantConfig.signMessage(aMerchantNo, tRequestMessage);
|
|
|
XMLDocument tResponseMessage = this.sendMessage(tRequestMessage);
|
|
|
this.iLogWriter.logNewLine("验证网上支付平台响应报文的签名:");
|
|
|
tResponseMessage = MerchantConfig.verifySign(tResponseMessage);
|
|
|
this.iLogWriter.log("正确");
|
|
|
this.iLogWriter.logNewLine("生成交易响应对象:");
|
|
|
tTrxResponse = this.constructResponse(tResponseMessage);
|
|
|
this.iLogWriter.logNewLine("交易结果:[" + tTrxResponse.ReturnCode + "]");
|
|
|
this.iLogWriter.logNewLine("错误信息:[" + tTrxResponse.ErrorMessage + "]");
|
|
|
} catch (TrxException e) {
|
|
|
tTrxResponse = new TrxResponse(e.Code, e.Message + " - " + e.DetailMessage);
|
|
|
if (this.iLogWriter != null) {
|
|
|
this.iLogWriter.logNewLine("错误代码:[" + tTrxResponse.ReturnCode + "] 错误信息:[" + tTrxResponse.ErrorMessage + "]");
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
tTrxResponse = new TrxResponse("1999", "系统发生无法预期的错误 - " + e.Message);
|
|
|
if (this.iLogWriter != null) {
|
|
|
this.iLogWriter.logNewLine("错误代码:[" + tTrxResponse.ReturnCode + "] 错误信息:[" + tTrxResponse.ErrorMessage + "]");
|
|
|
}
|
|
|
}
|
|
|
if (this.iLogWriter != null) {
|
|
|
this.iLogWriter.logNewLine("交易结束==================================================\n\n\n\n");
|
|
|
this.iLogWriter.closeWriter(MerchantConfig.getTrxLogFile());
|
|
|
}
|
|
|
return tTrxResponse;
|
|
|
}
|
|
|
|
|
|
public virtual TrxResponse postRequest()
|
|
|
{
|
|
|
return this.extendPostRequest(1);
|
|
|
}
|
|
|
|
|
|
private XMLDocument sendMessage(XMLDocument aMessage)
|
|
|
{
|
|
|
Exception ee;
|
|
|
string tMessage = "<MSG>" + aMessage.ToString() + "</MSG>";
|
|
|
this.iLogWriter.logNewLine("提交网上支付平台的报文:\n" + tMessage);
|
|
|
int tContentLength = 0;
|
|
|
try {
|
|
|
tContentLength = Encoding.UTF8.GetBytes(tMessage).Length;
|
|
|
} catch (Exception exception1) {
|
|
|
ee = exception1;
|
|
|
SupportClass.WriteStackTrace(ee, Console.Out);
|
|
|
throw new TrxException("1999", "系统发生无法预期的错误", ee.Message);
|
|
|
}
|
|
|
HttpWebRequest tWebRequest = null;
|
|
|
BufferedStream tRequestStream = null;
|
|
|
HttpWebResponse tWebResponse = null;
|
|
|
string tResponseMessage = "";
|
|
|
XMLDocument tTrxResponse = null;
|
|
|
ServicePointManager.ServerCertificateValidationCallback = delegate {
|
|
|
return true;
|
|
|
};
|
|
|
string tURL = MerchantConfig.TrustPayConnectMethod + "://" + MerchantConfig.TrustPayServerName;
|
|
|
if ((MerchantConfig.TrustPayConnectMethod.Equals("https") && (MerchantConfig.TrustPayServerPort != 0x1bb)) || (MerchantConfig.TrustPayConnectMethod.Equals("http") && (MerchantConfig.TrustPayServerPort != 80))) {
|
|
|
tURL = tURL + ":" + MerchantConfig.TrustPayServerPort;
|
|
|
}
|
|
|
try {
|
|
|
this.iLogWriter.logNewLine("连线网上支付平台:[" + tURL + "] ");
|
|
|
tWebRequest = (HttpWebRequest)WebRequest.Create(tURL + MerchantConfig.TrustPayTrxURL);
|
|
|
//不需要支持代理 yashen
|
|
|
//if (MerchantConfig.getParameterByName("ProxyIP", false) != null) {
|
|
|
// tWebRequest.Proxy = new WebProxy(MerchantConfig.getParameterByName("ProxyIP", false), int.Parse(MerchantConfig.getParameterByName("ProxyPort", false)));
|
|
|
//}
|
|
|
tWebRequest.Method = "POST";
|
|
|
tWebRequest.ProtocolVersion = HttpVersion.Version10;
|
|
|
this.iLogWriter.log("成功");
|
|
|
tMessage = MerchantConfig.Compress(tMessage);
|
|
|
this.iLogWriter.logNewLine("提交交易报文:");
|
|
|
this.iLogWriter.logNewLine(tMessage);
|
|
|
byte[] tReqBytes = MerchantConfig.DefaultEncoding.GetBytes(tMessage);
|
|
|
tWebRequest.ContentLength = tReqBytes.Length;
|
|
|
tRequestStream = new BufferedStream(tWebRequest.GetRequestStream());
|
|
|
if (!tRequestStream.CanWrite) {
|
|
|
throw new TrxException("1201", "无法连线网上支付平台", "无法连线到[" + tURL + "]");
|
|
|
}
|
|
|
tRequestStream.Write(tReqBytes, 0, tReqBytes.Length);
|
|
|
tRequestStream.Flush();
|
|
|
tRequestStream.Close();
|
|
|
this.iLogWriter.logNewLine("等待网上支付平台返回交易结果:");
|
|
|
tWebResponse = (HttpWebResponse)tWebRequest.GetResponse();
|
|
|
Stream tReceiveStream = tWebResponse.GetResponseStream();
|
|
|
Encoding tEncode = MerchantConfig.DefaultEncoding;
|
|
|
StreamReader tStreamReader = new StreamReader(tReceiveStream, tEncode);
|
|
|
for (string tLine = null; (tLine = tStreamReader.ReadLine()) != null; tLine = tStreamReader.ReadLine()) {
|
|
|
tResponseMessage = tResponseMessage + tLine;
|
|
|
if (tLine.IndexOf("</MSG>") != -1) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
tWebResponse.Close();
|
|
|
this.iLogWriter.log("成功");
|
|
|
this.iLogWriter.logNewLine("返回报文:");
|
|
|
this.iLogWriter.log("\n" + tResponseMessage.ToString());
|
|
|
if (tWebResponse.StatusCode != HttpStatusCode.OK) {
|
|
|
throw new TrxException("1206", "网上支付平台服务暂时停止");
|
|
|
}
|
|
|
tResponseMessage = MerchantConfig.DeCompress(tResponseMessage);
|
|
|
this.iLogWriter.logNewLine("接受到的响应报文:");
|
|
|
this.iLogWriter.log("\n" + tResponseMessage.ToString());
|
|
|
tTrxResponse = new XMLDocument(tResponseMessage).getValue("MSG");
|
|
|
if (tTrxResponse == null) {
|
|
|
throw new TrxException("1205", "无法辨识网上支付平台的响应报文", "无[MSG]段!");
|
|
|
}
|
|
|
} catch (WebException e) {
|
|
|
this.iLogWriter.logNewLine(e.ToString());
|
|
|
throw new TrxException("1201", "无法连线网上支付平台", "无法连线到[" + tURL + "], " + e.Message);
|
|
|
} catch (IOException e) {
|
|
|
this.iLogWriter.logNewLine(e.ToString());
|
|
|
throw new TrxException("1202", "提交交易时发生网络错误", "连线中断!");
|
|
|
} catch (SecurityException e) {
|
|
|
this.iLogWriter.logNewLine(e.ToString());
|
|
|
throw new TrxException("1201", "无法连线网上支付平台", "进程权限太低!");
|
|
|
} catch (TrxException e) {
|
|
|
throw e;
|
|
|
} catch (Exception exception6) {
|
|
|
ee = exception6;
|
|
|
this.iLogWriter.logNewLine(ee.StackTrace);
|
|
|
throw new TrxException("1201", "无法连线网上支付平台", ee.StackTrace);
|
|
|
} finally {
|
|
|
if (tRequestStream != null) {
|
|
|
try {
|
|
|
tRequestStream.Close();
|
|
|
} catch (Exception) {}
|
|
|
}
|
|
|
if (tWebResponse != null) {
|
|
|
try {
|
|
|
tWebResponse.Close();
|
|
|
} catch (Exception) {}
|
|
|
}
|
|
|
}
|
|
|
return tTrxResponse;
|
|
|
}
|
|
|
|
|
|
public virtual string BusinessID
|
|
|
{
|
|
|
get { return this.iBusinessID; }
|
|
|
set { this.iBusinessID = value.Trim(); }
|
|
|
}
|
|
|
|
|
|
public virtual string ChannelType
|
|
|
{
|
|
|
get { return this.iChannelType; }
|
|
|
set { this.iChannelType = value.Trim(); }
|
|
|
}
|
|
|
|
|
|
public virtual string Customer
|
|
|
{
|
|
|
get { return this.iCustomer; }
|
|
|
set { this.iCustomer = value.Trim(); }
|
|
|
}
|
|
|
|
|
|
public virtual string FunctionID
|
|
|
{
|
|
|
get { return this.iFunctionID; }
|
|
|
set { this.iFunctionID = value.Trim(); }
|
|
|
}
|
|
|
|
|
|
public virtual string MerchantID
|
|
|
{
|
|
|
get { return this.iMerchantID; }
|
|
|
set { this.iMerchantID = value.Trim(); }
|
|
|
}
|
|
|
|
|
|
public virtual string RequestID
|
|
|
{
|
|
|
get { return this.iRequestID; }
|
|
|
set { this.iRequestID = value.Trim(); }
|
|
|
}
|
|
|
|
|
|
protected internal abstract XMLDocument RequestMessage { get; }
|
|
|
}
|
|
|
}
|