1

我正在尝试实现支付网关(INTUIT 支付网关)。我想将 xml 序列化为模型类并保存到我的数据库中。我正在将桌面模型用于 intuit 支付网关,因为托管模型很难使用特别是 ssl 证书,所以我不想尝试。

我必须提到我能够使用下面提到的代码获得响应,我现在卡住的地方是序列化 xml 并将响应保存到数据库中。此 xml 是从位于我的项目中的 xmlfiles 文件夹中的文件夹中提取的。

有关示例 xml 格式,请查看此格式。https://ipp.developer.intuit.com/0085_QuickBooks_Windows_SDK/qbms/0060_Documentation/Sending_Requests

这是我试图发布到 url ( https://merchantaccount.ptc.quickbooks.com/j/AppGateway )的 xml 文件

<?xml version="1.0"?>
<?qbmsxml version="4.5"?>
<QBMSXML>
    <SignonMsgsRq>
        <SignonDesktopRq>
            <ClientDateTime>2012-07-25T17:13:45</ClientDateTime>
            <ApplicationLogin>abc.abc.us</ApplicationLogin>
            <ConnectionTicket>TGT-1-g42FGaMfOTQ82GcWFBpsuQ</ConnectionTicket>
        </SignonDesktopRq>
    </SignonMsgsRq>
    <QBMSXMLMsgsRq>
        <CustomerCreditCardChargeRq>
            <TransRequestID>4540453787200</TransRequestID>
            <CreditCardNumber>4111111111111111</CreditCardNumber>
            <ExpirationMonth>12</ExpirationMonth>
            <ExpirationYear>2016</ExpirationYear>
            <IsCardPresent>false</IsCardPresent>
            <Amount>10.00</Amount>
        </CustomerCreditCardChargeRq>
    </QBMSXMLMsgsRq>
</QBMSXML>

我用来发布到 url 的控制器

public ActionResult Index()
{
    WebRequest req = null;
    WebResponse rsp = null;
    string fileName = Server.MapPath("~/XMLData/XMLFile1.xml");
    string uri = "https://merchantaccount.ptc.quickbooks.com/j/AppGateway";
    req = WebRequest.Create(uri);
    //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
    req.Method = "POST";        // Post method
    req.ContentType = "application/x-qbmsxml";     // content type

    // Wrap the request stream with a text-based writer
    StreamWriter writer = new StreamWriter(req.GetRequestStream());
    // Write the XML text into the stream
    writer.WriteLine(this.GetTextFromXMLFile(fileName));
    writer.Close();
    // Send the data to the webserver
    rsp = req.GetResponse();
   // var resp = (Object)rsp.GetResponseStream();
    if (rsp != null)
    {
        StreamReader inStream = new StreamReader(rsp.GetResponseStream());
        var data = inStream.ReadToEnd();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(data);
        string path = Server.MapPath("~/XMLData/SampleXmlE2E.xml");
        xmlDoc.Save(path);//regenerates the xml file in different system.
    }

    return View();
    //XElement root = new XElement("root");
    //root.Add(new XElement("element1"));
    //root.Add(new XElement("element2"));
    //root.Add(new XAttribute("attribute1", "a value"));
    //return new XmlResult(root);
}

private string GetTextFromXMLFile(string file)
{
    StreamReader reader = new StreamReader(file);
    string ret = reader.ReadToEnd();
    reader.Close();
    return ret;
}

private string SendRequest(Uri UriObj, string data)
{
    string _result;

    var request = (HttpWebRequest)WebRequest.Create(UriObj);
    request.Method = "POST";
    request.ContentType = "text/xml";
    var writer = new StreamWriter(request.GetRequestStream());
    writer.Write(data);
    writer.Close();

    var response = (HttpWebResponse)request.GetResponse();

    var streamResponse = response.GetResponseStream();
    var streamRead = new StreamReader(streamResponse);

    _result = streamRead.ReadToEnd().Trim();
    streamRead.Close();
    streamResponse.Close();
    response.Close();
    return _result;
}

任何帮助,将不胜感激。谢谢

4

1 回答 1

1

瓦伦,

[编辑-更新]

基于此 XML:

<?qbmsxml version="4.5"?>
<QBMSXML>
  <SignonMsgsRq>
    <SignonDesktopRq>
      <ClientDateTime>2012-07-25T17:13:45</ClientDateTime>
      <ApplicationLogin>app.app.login.url</ApplicationLogin>
      <ConnectionTicket>TGT-1-g42FGaMfOTQ82GcWFBpsuQ</ConnectionTicket>
    </SignonDesktopRq>
  </SignonMsgsRq>
  <QBMSXMLMsgsRq>
    <CustomerCreditCardChargeRq>
      <TransRequestID>4540453787200</TransRequestID>
      <CreditCardNumber>4111111111111111</CreditCardNumber>
      <ExpirationMonth>12</ExpirationMonth>
      <ExpirationYear>2016</ExpirationYear>
      <IsCardPresent>false</IsCardPresent>
      <Amount>10.00</Amount>
    </CustomerCreditCardChargeRq>
  </QBMSXMLMsgsRq>
</QBMSXML>

这是您需要根据上面的示例反序列化的类结构:

[XmlRoot("QBMSXML")]
public class QbmsXml
{
    [XmlElement("SignonMsgsRq")]
    public SignonMsgsRq SignonMsgsRq { get; set; }

    [XmlElement("QBMSXMLMsgsRq")]
    public QbmsXmlMsgsRq QbmsXmlMsgsRq { get; set; }
}

public class SignonMsgsRq
{
    [XmlElement("SignonDesktopRq")]
    public SignonDesktopRq SignonDesktopRq { get; set; }
}

public class SignonDesktopRq
{
    [XmlElement("ClientDateTime")]
    public DateTime ClientDateTime { get; set; }

    [XmlElement("ApplicationLogin")]
    public string ApplicationLogin { get; set; }

    [XmlElement("ConnectionTicket")]
    public string ConnectionTicket { get; set; }
}

public class QbmsXmlMsgsRq
{
    [XmlElement("CustomerCreditCardChargeRq")]
    public CustomerCreditCardChargeRq CustomerCreditCardChargeRq { get; set; }
}

public class CustomerCreditCardChargeRq
{
    [XmlElement("TransRequestID")]
    public Int64 TransRequestID { get; set; }

    [XmlElement("CreditCardNumber")]
    public string CreditCardNumber { get; set; }

    [XmlElement("ExpirationMonth")]
    public int ExpirationMonth { get; set; }

    [XmlElement("ExpirationYear")]
    public int ExpirationYear { get; set; }

    [XmlElement("IsCardPresent")]
    public bool IsCardPresent { get; set; }

    [XmlElement("Amount")]
    public double Amount { get; set; }
}

只需按照以下方式使用 xmlSerialiser:

class Program
{
    private static T DeSerialize<T>(string fromXmlFile) where T: class
    {
        if (!File.Exists(fromXmlFile))
        {
            return default(T);
        }
        T deserializedClass;
        var serializer = new XmlSerializer(typeof(T));

        // ToDo: add error catching etc
        using (var reader = new StreamReader(fromXmlFile))
        {
            deserializedClass = (T)serializer.Deserialize(reader);
        }
        return deserializedClass;
    }

    static void Main(string[] args)
    {
        var yourXmlFilePath = @"d:\temp\xmltest.xml";
        var deserializedClass = DeSerialize<QbmsXml>(yourXmlFilePath);

        Console.WriteLine(deserializedClass
                            .QbmsXmlMsgsRq
                            .CustomerCreditCardChargeRq.Amount);
        Console.Read();
    }
}

希望这可以帮助。

于 2012-08-01T08:55:42.573 回答