You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

179 lines
8.1 KiB

namespace com.hitrust.trustpay.client
{
using Security;
using System;
using System.IO;
using System.Net;
using System.Text;
public abstract class TrxRequest
{
public const string EC_MERCHANT_TYPE_B2B = "B2B";
public const string EC_MERCHANT_TYPE_B2C = "B2C";
private readonly string iECMerchantType = "";
private LogWriter iLogWriter = null;
public TrxRequest(string aECMerchantType)
{
this.iECMerchantType = aECMerchantType;
}
protected internal abstract void checkRequest();
private XMLDocument composeRequestMessage(string aMerchantNo, XMLDocument aMessage)
{
this.iLogWriter.logNewLine("composeRequestMessage中==" + aMerchantNo);
return new XMLDocument("<Merchant><ECMerchantType>" + this.iECMerchantType + "</ECMerchantType><MerchantID>" + MerchantConfig.MerchantID(aMerchantNo) + "</MerchantID></Merchant>" + aMessage.ToString());
}
protected internal abstract TrxResponse constructResponse(XMLDocument aResponseMessage);
public virtual TrxResponse extendPostRequest(string aMerchantNo)
{
TrxResponse response = null;
try {
this.iLogWriter = new LogWriter();
this.iLogWriter.logNewLine("TrustPayClient ASP V2.0.1 交易开始==========================");
if (!MerchantConfig.hasMerchant(aMerchantNo)) {
throw new TrxException("1008", "指定的商户配置编号不合法", string.Concat(new object[] {"配置文件中商户数为", MerchantConfig.MerchantNum, ", 但是请求指定的商户配置编号为", aMerchantNo, "!"}));
}
this.iLogWriter.logNewLine("extendPostRequest中==" + aMerchantNo);
this.iLogWriter.logNewLine("检查交易请求是否合法:");
this.checkRequest();
this.iLogWriter.logNewLine("正确");
this.iLogWriter.logNewLine("交易报文:");
XMLDocument requestMessage = this.RequestMessage;
this.iLogWriter.logNewLine("完整交易报文:");
requestMessage = this.composeRequestMessage(aMerchantNo, requestMessage);
this.iLogWriter.logNewLine("签名后的报文:");
requestMessage = MerchantConfig.signMessage(aMerchantNo, requestMessage);
this.iLogWriter.logNewLine("发送交易报文至网上支付平台:");
XMLDocument aMessage = this.sendMessage(requestMessage);
this.iLogWriter.logNewLine("验证网上支付平台响应报文的签名:");
aMessage = MerchantConfig.verifySign(aMessage);
this.iLogWriter.log("正确");
this.iLogWriter.logNewLine("生成交易响应对象:");
response = this.constructResponse(aMessage);
this.iLogWriter.logNewLine("交易结果:[" + response.ReturnCode + "]");
this.iLogWriter.logNewLine("错误信息:[" + response.ErrorMessage + "]");
} catch (TrxException exception) {
response = new TrxResponse(exception.Code, exception.Message + " - " + exception.DetailMessage);
if (this.iLogWriter != null) {
this.iLogWriter.logNewLine("错误代码:[" + response.ReturnCode + "] 错误信息:[" + response.ErrorMessage + "]");
}
} catch (Exception exception2) {
response = new TrxResponse("1999", "系统发生无法预期的错误 - " + exception2.Message);
if (this.iLogWriter != null) {
this.iLogWriter.logNewLine("错误代码:[" + response.ReturnCode + "] 错误信息:[" + response.ErrorMessage + "]");
}
}
if (this.iLogWriter != null) {
this.iLogWriter.logNewLine("交易结束==================================================");
this.iLogWriter.closeWriter(MerchantConfig.getTrxLogFile());
}
return response;
}
public virtual TrxResponse postRequest()
{
string aMerchantNo = MerchantConfig.FirstMerchantID();
return this.extendPostRequest(aMerchantNo);
}
private XMLDocument sendMessage(XMLDocument aMessage)
{
string s = "<MSG>" + aMessage.ToString() + "</MSG>";
this.iLogWriter.logNewLine("提交网上支付平台的报文:\n" + s);
try {
int length = Encoding.UTF8.GetBytes(s).Length;
} catch (Exception exception) {
SupportClass.WriteStackTrace(exception, Console.Out);
throw new TrxException("1999", "系统发生无法预期的错误", exception.Message);
}
HttpWebRequest request = null;
BufferedStream stream = null;
HttpWebResponse response = null;
string aXMLString = "";
XMLDocument document = null;
ServicePointManager.ServerCertificateValidationCallback = delegate {
return true;
};
string str3 = MerchantConfig.TrustPayConnectMethod + "://" + MerchantConfig.TrustPayServerName;
if ((MerchantConfig.TrustPayConnectMethod.Equals("https") && (MerchantConfig.TrustPayServerPort != 0x1bb)) || (MerchantConfig.TrustPayConnectMethod.Equals("http") && (MerchantConfig.TrustPayServerPort != 80))) {
str3 = str3 + ":" + MerchantConfig.TrustPayServerPort;
}
try {
this.iLogWriter.logNewLine("连线网上支付平台:[" + str3 + "] ");
request = (HttpWebRequest)WebRequest.Create(str3 + MerchantConfig.TrustPayTrxURL);
if (MerchantConfig.getParameterByName("ProxyIP", false) != null) {
request.Proxy = new WebProxy(MerchantConfig.getParameterByName("ProxyIP", false), int.Parse(MerchantConfig.getParameterByName("ProxyPort", false)));
}
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version10;
request.ContentType = "application/x-www-form-urlencoded";
this.iLogWriter.log("成功");
this.iLogWriter.logNewLine("提交交易报文:");
byte[] bytes = Encoding.UTF8.GetBytes(s);
request.ContentLength = bytes.Length;
stream = new BufferedStream(request.GetRequestStream());
if (!stream.CanWrite) {
throw new TrxException("1201", "无法连线网上支付平台", "无法连线到[" + str3 + "]");
}
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
stream.Close();
this.iLogWriter.log("成功");
this.iLogWriter.logNewLine("等待网上支付平台返回交易结果:");
response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("gb2312");
StreamReader reader = new StreamReader(responseStream, encoding);
for (string str4 = null; (str4 = reader.ReadLine()) != null; str4 = reader.ReadLine()) {
aXMLString = aXMLString + str4;
if (str4.IndexOf("</MSG>") != -1) {
break;
}
}
response.Close();
this.iLogWriter.log("成功");
this.iLogWriter.logNewLine("返回报文:");
this.iLogWriter.log("\n" + aXMLString.ToString());
if (response.StatusCode != HttpStatusCode.OK) {
throw new TrxException("1206", "网上支付平台服务暂时停止");
}
document = new XMLDocument(aXMLString).getValue("MSG");
if (document == null) {
throw new TrxException("1205", "无法辨识网上支付平台的响应报文", "无[MSG]段!");
}
} catch (WebException exception2) {
this.iLogWriter.logNewLine(exception2.ToString());
throw new TrxException("1201", "无法连线网上支付平台", "无法连线到[" + str3 + "], " + exception2.Message);
} catch (IOException exception3) {
this.iLogWriter.logNewLine(exception3.ToString());
throw new TrxException("1202", "提交交易时发生网络错误", "连线中断!");
} catch (SecurityException exception4) {
this.iLogWriter.logNewLine(exception4.ToString());
throw new TrxException("1201", "无法连线网上支付平台", "进程权限太低!");
} catch (TrxException exception5) {
throw exception5;
} catch (Exception exception6) {
this.iLogWriter.logNewLine(exception6.StackTrace);
throw new TrxException("1201", "无法连线网上支付平台", exception6.StackTrace);
} finally {
if (stream != null) {
try {
stream.Close();
} catch (Exception) {}
}
if (response != null) {
try {
response.Close();
} catch (Exception) {}
}
}
return document;
}
protected internal abstract XMLDocument RequestMessage { get; }
}
}