在我的应用程序中,我必须使用PayGate API来处理在线支付。
请求必须采用以下格式:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SinglePaymentRequest xmlns="http://www.paygate.co.za/PayHOST">
<CardPaymentRequest>
<Account>
<PayGateId>10011072130</PayGateId>
<Password>test</Password>
</Account>
<Customer>
<Title>Mr</Title>
<FirstName>Joe</FirstName>
<LastName>Soap</LastName>
<Telephone>0861234567</Telephone>
<Mobile>0735552233</Mobile>
<Email>joe@soap.com</Email>
</Customer>
<CardNumber>4000000000000002</CardNumber>
<CardExpiryDate>122020</CardExpiryDate>
<CVV>999</CVV>
<BudgetPeriod>0</BudgetPeriod>
<Redirect>
<NotifyUrl>https://www.mytestsite.com/notify</NotifyUrl>
<ReturnUrl>https://www.mytestsite.com/return</ReturnUrl>
</Redirect>
<Order>
<MerchantOrderId>INV101</MerchantOrderId>
<Currency>ZAR</Currency>
<Amount>100</Amount>
</Order>
</CardPaymentRequest>
</SinglePaymentRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我试图使用C#中的XElement类创建此结构,但是每个包含单词的标记: 在它们之前的“ SOAP-ENV:”,请不要正确解析。
这是我的功能:
private string ParseCardRequestToXml()
{
XNamespace soapEnv = "SOAP-ENV";
XNamespace spReq = "http://www.paygate.co.za/PayHOST";
//Envelope
XElement xmlEnvlelope = new XElement(soapEnv + "Envelope", new XAttribute(XNamespace.Xmlns + "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"));
//Header
XElement xmlHeader = new XElement(soapEnv + "Header");
//Body element
XElement xmlBody = new XElement("Body");
//SinglePaymentRequest
XElement xmlSinglePaymentRequest = new XElement(spReq + "SinglePaymentRequest");
//CardPaymentRequest
XElement xmlCardPaymentRequest = new XElement("CardPaymentRequest");
//Account
XElement xmlAccount = new XElement("Account");
xmlAccount.Add(new XElement("PayGateId", "10011072130"));
xmlAccount.Add(new XElement("Password", "test"));
//Add account to "CardPaymentRequest" tag
xmlCardPaymentRequest.Add(xmlAccount);
//Customer
XElement xmlCustomer = new XElement("Customer");
xmlCustomer.Add(new XElement("Title", "Mr"));
xmlCustomer.Add(new XElement("FirstName", "Joe"));
xmlCustomer.Add(new XElement("LastName", "Soap"));
xmlCustomer.Add(new XElement("Telephone", "0861234567"));
xmlCustomer.Add(new XElement("Mobile", "0735552233"));
xmlCustomer.Add(new XElement("Email", "joe@soap.com"));
//Add customer to "CardPaymentRequest" tag
xmlCardPaymentRequest.Add(xmlCustomer);
//Add Card info to "CardPaymentRequest" tag
xmlCardPaymentRequest.Add(new XElement("CardNumber", "4000000000000002"));
xmlCardPaymentRequest.Add(new XElement("CardExpiryDate", "122020"));
xmlCardPaymentRequest.Add(new XElement("CVV", 999));
xmlCardPaymentRequest.Add(new XElement("BudgetPeriod", 0));
//Redirect
XElement xmlRedirect = new XElement("Redirect");
xmlRedirect.Add(new XElement("NotifyUrl", "https://www.mytestsite.com/notify"));
xmlRedirect.Add(new XElement("ReturnUrl", "https://www.mytestsite.com/return"));
//Add redirect to "CardPaymentRequest" tag
xmlCardPaymentRequest.Add(xmlRedirect);
//Order
XElement xmlOrder = new XElement("Order");
xmlOrder.Add(new XElement("MerchantOrderId", "INV101"));
xmlOrder.Add(new XElement("Currency", "ZAR"));
xmlOrder.Add(new XElement("Amount", 100));
//Add order to "CardPaymentRequest" tag
xmlCardPaymentRequest.Add(xmlOrder);
xmlSinglePaymentRequest.Add(xmlCardPaymentRequest);
xmlBody.Add(xmlSinglePaymentRequest);
xmlEnvlelope.Add(xmlHeader);
xmlEnvlelope.Add(xmlBody);
return xmlEnvlelope.ToString();
}
这就是我得到的结果:
<Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"SOAP-ENV\">
<Header />
<Body xmlns=\"\">
<SinglePaymentRequest xmlns=\"http://www.paygate.co.za/PayHOST\">
<CardPaymentRequest xmlns=\"\">
<Account>
<PayGateId>10011072130</PayGateId>
<Password>test</Password>
</Account>
<Customer>
<Title></Title>
<FirstName>Joe</FirstName>
<LastName>Soap</LastName>
<Telephone>0861234567</Telephone>
<Mobile>0735552233</Mobile>
<Email>joe@soap.com</Email>
</Customer>
<CardNumber>4000000000000002</CardNumber>
<CardExpiryDate>122020</CardExpiryDate>
<CVV>999</CVV>
<BudgetPeriod>0</BudgetPeriod>
<Redirect>
<NotifyUrl>https://www.mytestsite.com/notify</NotifyUrl>
<ReturnUrl>https://www.mytestsite.com/return</ReturnUrl>
</Redirect>
<Order>
<MerchantOrderId>INV101</MerchantOrderId>
<Currency>ZAR</Currency>
<Amount>100</Amount>
</Order>
</CardPaymentRequest>
</SinglePaymentRequest>
</Body>
</Envelope>
我根据Microsoft的this指南使用了XNamespace类,但是我似乎错过了一些东西。
编辑:在该示例中,make变量XNamespace aw
但由于无法在命名中使用“-”,因此我无法创建此类变量。
编辑2:好吧,我去尝试使用StringBuilder建立请求。
这是功能:
private string GetDemoCardRequestXML()
{
StringBuilder builder = new StringBuilder();
builder.Append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=").Append('"').Append("http://schemas.xmlsoap.org/soap/envelope/").Append('"').Append(">");
builder.Append("<SOAP-ENV:Header/>");
builder.Append("<SOAP-ENV:Body>");
builder.Append("<SinglePaymentRequest xmlns=").Append('"').Append("http://www.paygate.co.za/PayHOST").Append('"').Append(">");
builder.Append("<CardPaymentRequest>");
builder.Append("<Account>");
builder.Append("<PayGateId>").Append("10011072130").Append("</PayGateId>");
builder.Append("<Password>").Append("test").Append("</Password>");
builder.Append("</Account>");
builder.Append("<Customer>");
builder.Append("<Title>").Append("Mr").Append("</Title>");
builder.Append("<FirstName>").Append("Joe").Append("</FirstName>");
builder.Append("<LastName>").Append("Soap").Append("</LastName>");
builder.Append("<Telephone>").Append("0861234567").Append("</Telephone>");
builder.Append("<Mobile>").Append("0735552233").Append("</Mobile>");
builder.Append("<Email>").Append("joe@soap.com").Append("</Email>");
builder.Append("</Customer>");
builder.Append("<CardNumber>").Append("4000000000000002").Append("</CardNumber>");
builder.Append("<CardExpiryDate>").Append("122020").Append("</CardExpiryDate>");
builder.Append("<CVV>").Append("999").Append("</CVV>");
builder.Append("<BudgetPeriod>").Append("0").Append("</BudgetPeriod>");
builder.Append("<Redirect>");
builder.Append("<NotifyUrl>").Append("https://www.mytestsite.com/notify").Append("</NotifyUrl>");
builder.Append("<ReturnUrl>").Append("https://www.mytestsite.com/return").Append("</ReturnUrl>");
builder.Append("</Redirect>");
builder.Append("<Order>");
builder.Append("<MerchantOrderId>").Append("INV101").Append("</MerchantOrderId>");
builder.Append("<Currency>").Append("ZAR").Append("</Currency>");
builder.Append("<Amount>").Append("100").Append("</Amount>");
builder.Append("</Order>");
builder.Append("</CardPaymentRequest>");
builder.Append("</SinglePaymentRequest>");
builder.Append("</SOAP-ENV:Body>");
builder.Append("</SOAP-ENV:Envelope>");
return builder.ToString();
}
结果:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SinglePaymentRequest
xmlns=\"http://www.paygate.co.za/PayHOST\">
<CardPaymentRequest>
<Account>
<PayGateId>10011072130</PayGateId>
<Password>test</Password>
</Account>
<Customer>
<Title>Mr</Title>
<FirstName>Joe</FirstName>
<LastName>Soap</LastName>
<Telephone>0861234567</Telephone>
<Mobile>0735552233</Mobile>
<Email>joe@soap.com</Email>
</Customer>
<CardNumber>4000000000000002</CardNumber>
<CardExpiryDate>122020</CardExpiryDate>
<CVV>999</CVV>
<BudgetPeriod>0</BudgetPeriod>
<Redirect>
<NotifyUrl>https://www.mytestsite.com/notify</NotifyUrl>
<ReturnUrl>https://www.mytestsite.com/return</ReturnUrl>
</Redirect>
<Order>
<MerchantOrderId>INV101</MerchantOrderId>
<Currency>ZAR</Currency>
<Amount>100</Amount>
</Order>
</CardPaymentRequest>
</SinglePaymentRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
现在请求看起来更好了,但是xmlns url区域中的转义字符“ \”阻止了它成为有效的XML。我知道如何删除这些内容吗?
答案 0 :(得分:0)
您必须使用soapEnv.NamespaceName
将前缀附加到XmlElement
答案 1 :(得分:0)
正如我在第一条评论中所写,您需要添加名称空间
soapEnv
至"Body"
和spReq
到从"CardPaymentRequest"
开始的所有元素
<Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns="SOAP-ENV">
<Header />
<Body>
<SinglePaymentRequest xmlns="http://www.paygate.co.za/PayHOST">
<CardPaymentRequest>
...
</CardPaymentRequest>
</SinglePaymentRequest>
</Body>
</Envelope>
不幸的是,我错了,这在形式上不是相同的xml,并且不会通过针对相同模式的检查。
To get excat xml you have to change soapEnv
to http://schemas.xmlsoap.org/soap/envelope/