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.
 

232 lines
7.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BWP.ABCClient.Businesses;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Security;
namespace BWP.ABCClient.B2C.OnlineRemits
{
public abstract class OnlineRemitBaseRequest
{
protected abstract void checkRequest();
protected abstract XmlDocument createMessage();
XmlDocument verifySign(XmlDocument aMessage)
{
XmlDocument document = aMessage.GetValue("Message");
if (document == null)
{
throw new OnlineRemitException("1301", "网上支付平台的响应报文不完整", "无[Message]段!");
}
if (aMessage.GetValueNoNull("Signature-Algorithm") == null)
{
throw new OnlineRemitException("1301", "网上支付平台的响应报文不完整", "无[Signature-Algorithm]段!");
}
string data = aMessage.GetValueNoNull("Signature");
if (data == null)
{
throw new OnlineRemitException("1301", "网上支付平台的响应报文不完整", "无[Signature]段!");
}
byte[] rgbSignature = Convert.FromBase64String(data);
try
{
SHA1Managed managed = new SHA1Managed();
byte[] rgbHash = managed.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(document.ToString()));
RSACryptoServiceProvider provider = new RSACryptoServiceProvider(new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore });
//RSAParameters parameters = TrustpayCertificate.GetPublicKey().ExportParameters();
RSAParameters parameters = new RSAParameters() { Exponent = OnlineRemitConfig.GetCertificate().GetPublicKey() };
provider.ImportParameters(parameters);
bool flag = provider.VerifyHash(rgbHash, CryptoConfig.MapNameToOID("SHA1"), rgbSignature);
managed.Clear();
provider.Clear();
if (!flag)
{
throw new OnlineRemitException("1302", "网上支付平台的响应报文签名验证失败");
}
}
catch (OnlineRemitException exception)
{
throw exception;
}
catch (Exception exception2)
{
throw new OnlineRemitException("1302", "网上支付平台的响应报文签名验证失败 - " + exception2.Message);
}
return document;
}
string ECMerchantType = "B2C";
/// <summary>
/// 商户编号
/// </summary>
public string MerchantID { get; set; }
XmlDocument sendMessage(XmlDocument aMessage)
{
Exception exception;
string s = "<MSG>" + aMessage.ToString() + "</MSG>";
int length = 0;
try
{
length = Encoding.UTF8.GetBytes(s).Length;
}
catch (Exception exception1)
{
exception = exception1;
throw new OnlineRemitException("1999", "系统发生无法预期的错误", exception.Message);
}
HttpWebRequest request = null;
BufferedStream stream = null;
HttpWebResponse response = null;
string aXMLString = "";
XmlDocument document = null;
string str3 = OnlineRemitConfig.TrustPayConnectMethod + "://" + OnlineRemitConfig.TrustPayServerName;
if ((OnlineRemitConfig.TrustPayConnectMethod.Equals("https") && (OnlineRemitConfig.TrustPayServerPort != 0x1bb)) || (OnlineRemitConfig.TrustPayConnectMethod.Equals("http") && (OnlineRemitConfig.TrustPayServerPort != 80)))
{
str3 = str3 + ":" + OnlineRemitConfig.TrustPayServerPort;
}
try
{
request = (HttpWebRequest)WebRequest.Create(str3 + OnlineRemitConfig.TrustPayTrxURL);
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 OnlineRemitException("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);
string str4 = null;
while ((str4 = reader.ReadLine()) != null)
{
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 OnlineRemitException("1206", "网上支付平台服务暂时停止");
}
document = new XmlDocument(aXMLString).GetValue("MSG");
if (document == null)
{
throw new OnlineRemitException("1205", "无法辨识网上支付平台的响应报文", "无[MSG]段!");
}
}
catch (WebException exception2)
{
//this.iLogWriter.logNewLine(exception2.ToString());
throw new OnlineRemitException("1201", "无法连线网上支付平台", "无法连线到[" + str3 + "], " + exception2.Message);
}
catch (IOException exception3)
{
//this.iLogWriter.logNewLine(exception3.ToString());
throw new OnlineRemitException("1202", "提交交易时发生网络错误", "连线中断!");
}
catch (SecurityException exception4)
{
//this.iLogWriter.logNewLine(exception4.ToString());
throw new OnlineRemitException("1201", "无法连线网上支付平台", "进程权限太低!");
}
catch (OnlineRemitException exception5)
{
throw exception5;
}
catch (Exception exception10)
{
exception = exception10;
//this.iLogWriter.logNewLine(exception.StackTrace);
throw new OnlineRemitException("1201", "无法连线网上支付平台", exception.StackTrace);
}
finally
{
if (stream != null)
{
try
{
stream.Close();
}
catch (Exception)
{
}
}
if (response != null)
{
try
{
response.Close();
}
catch (Exception)
{
}
}
}
return document;
}
/// <summary>
/// 在消息头部加上商户信息
/// </summary>
/// <param name="aMessage"></param>
/// <returns></returns>
XmlDocument composeRequestMessage(XmlDocument aMessage)
{
return new XmlDocument("<Merchant><ECMerchantType>" + ECMerchantType + "</ECMerchantType><MerchantID>" + MerchantID + "</MerchantID></Merchant>" + aMessage.ToString());
}
/// <summary>
/// 加入签名信息
/// </summary>
/// <param name="aMessage"></param>
/// <returns></returns>
XmlDocument fileSignMessage(XmlDocument aMessage)
{
RSACryptoServiceProvider provider = OnlineRemitConfig.GetMerchantKey(MerchantID);
byte[] rgbHash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(aMessage.ToString()));
byte[] data = provider.SignHash(rgbHash, CryptoConfig.MapNameToOID("SHA1"));
string str = Convert.ToBase64String(data);
return new XmlDocument("<Message>" + aMessage.ToString() + "</Message><Signature-Algorithm>SHA1withRSA</Signature-Algorithm><Signature>" + str + "</Signature>");
}
protected XmlDocument Send()
{
checkRequest();
var message = createMessage();
message = composeRequestMessage(message);
message = fileSignMessage(message);
message = sendMessage(message);
message = verifySign(message);
return message;
}
}
}