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.
 
 

157 lines
7.1 KiB

namespace com.hitrust.b2b.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(int aMerchantNo, XMLDocument aMessage)
{
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(int aMerchantNo)
{
TrxResponse response = null;
try {
this.iLogWriter = new LogWriter(MerchantConfig.TrxLogFile);
this.iLogWriter.logNewLine("检查交易请求是否合法:");
this.checkRequest();
this.iLogWriter.logNewLine("正确");
XMLDocument requestMessage = this.RequestMessage;
requestMessage = this.composeRequestMessage(aMerchantNo, requestMessage);
this.iLogWriter.logNewLine("签名后的交易请求报文:");
requestMessage = MerchantConfig.signMessage(aMerchantNo, requestMessage);
this.iLogWriter.logNewLine(requestMessage.ToString());
this.iLogWriter.logNewLine("发送交易报文至网上支付平台:");
XMLDocument aMessage = this.sendMessage(requestMessage);
this.iLogWriter.logNewLine("验证网上支付平台响应报文的签名:");
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.closeWriter();
}
return response;
}
public virtual TrxResponse postRequest()
{
return this.extendPostRequest(1);
}
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);
}
string aXMLString = "";
XMLDocument document;
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 + "] ");
HttpWebRequest 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.Headers.Add("Cookie", "JSESSIONID=aIG-RvFXRUU");
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;
BufferedStream 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("等待网上支付平台返回交易结果:");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("UTF-8");
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);
}
return document;
}
protected internal abstract XMLDocument RequestMessage { get; }
}
}