How to add info to sofort API XML using C# - c#

I am going to use sofort payment API in ASP.Net MVC, but I am having issue to add info in following XML format in C#.
<?xml version="1.0" encoding="UTF-8" ?>
<multipay>
<project_id>53245</project_id>
<interface_version>pn_test_1</interface_version>
<amount>2.20</amount>
<currency_code>EUR</currency_code>
<reasons>
<reason>Testueberweisung</reason>
<reason>-TRANSACTION-</reason>
</reasons>
<user_variables>
<user_variable>test</user_variable>
</user_variables>
<success_url>xyz</success_url>
<success_link_redirect>1</success_link_redirect>
<abort_url>xyz</abort_url>
<notification_urls>
<notification_url>xyz</notification_url>
<notification_url notify_on="received,loss">xyz</notification_url>
</notification_urls>
<su />
</multipay>

var xmlRequest = new XElement("multipay",
new XElement("project_id", "53245"),
new XElement("interface_version", "pn_test_1"),
new XElement("amount", "2.20"),
new XElement("currency_code", "EUR"),
new XElement("reasons", new XElement("reason", "Testueberweisung"), new XElement("reason", "-TRANSACTION-"))
);
string xmlreq = xmlRequest.ToString();

Related

How to add new elements to a SOAP XML

I have a SOAP XML file name "LogEvents.xml" that I want to add/remove elements from it.
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
<SOAP-ENV:Body xmlns:lle="http://www.examplee.org/logevents" >
<Event>
<eventid>ID-1</eventid>
<title>title1</title>
<description>desc1</description>
<location>
<lat>1.357207</lat>
<long>103.944880</long>
<address>address1</address>
</location>
<datetimestamp>datetime1</datetimestamp>
</Event>
<Event>
<eventid>ID-2</eventid>
<title>title2</title>
<description>desc2</description>
<location>
<lat>1.304121</lat>
<long>103.831950</long>
<address>addres2</address>
</location>
<datetimestamp>datetime2</datetimestamp>
</Event>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Below shows the code that I have tried but failed using Visual Studio.
XDocument xDoc1 = XDocument.Load(#"C:\LogEvents.xml");
xDoc1.Element("SOAP-ENV").Add(new XElement("Event", new XElement("eventid", "strEventID"),
new XElement("title", "title2"),
new XElement("description", "desc3"),
new XElement("location", new XElement("lat", "lat3"),
new XElement("long", "long3"),
new XElement("address", "addresses3")),
new XElement("datetimestamp", "DateTime3")));
I have tried to remove the SOAP envelope in the XML file and replace it with "Events" and edit the C# code accordingly and it works as shown below.
<?xml version="1.0" encoding="utf-8"?>
<!--Changed to Events-->
<Events>
<Event>
<eventid>ID-1</eventid>
<title>title1</title>
<description>desc1</description>
<location>
<lat>1.357207</lat>
<long>103.944880</long>
<address>address1Edge</address>
</location>
<datetimestamp>datetime1</datetimestamp>
</Event>
<Event>
<eventid>ID-2</eventid>
<title>title2</title>
<description>desc2</description>
<location>
<lat>1.304121</lat>
<long>103.831950</long>
<address>addres2</address>
</location>
<datetimestamp>datetime2</datetimestamp>
<!--Changed to Events-->
</Event>
<Events>
XDocument xDoc1 = XDocument.Load(#"C:\LogEvents.xml");
// Changed SOAP-ENV to Events
xDoc1.Element("Events").Add(new XElement("Event", new XElement("eventid", "ID-3"),
new XElement("title", "title3"),
new XElement("description", "desc3"),
new XElement("location", new XElement("lat", "lat3"),
new XElement("long", "long3"),
new XElement("address", "addresses3")),
new XElement("datetimestamp", "DateTime3")));
Is there any method to ignore the SOAP envelope, or a way to work around it?
Use Root. You can always get the default namespace from the root when needed.
XDocument xDoc1 = XDocument.Load(FILENAME);
XElement soap = xDoc1.Root;
XNamespace ns = soap.GetDefaultNamespace();
soap.Add(new XElement("Event", new XElement("eventid", "strEventID"),
new XElement("title", "title2"),
new XElement("description", "desc3"),
new XElement("location", new XElement("lat", "lat3"),
new XElement("long", "long3"),
new XElement("address", "addresses3")),
new XElement("datetimestamp", "DateTime3")));

Create SAML response from assertion in C# 4.5 (WIF)

I need a way to POST a <samlp:response> message to a certain URL, its fairly simple and .NET helps me with Saml2Assertion class but I can't seem to find a way to wrap that assertion in a response and have it serialized (or even send without manual post)?
Saml2Assertion assert = new Saml2Assertion(new Saml2NameIdentifier("SAMLIssuer"));
assert.Subject = new Saml2Subject(new Saml2NameIdentifier("10001", new Uri("urn:oasis:names:tc:SAML:2.0:nameid-format:persistent")));
Saml2AuthenticationContext context = new Saml2AuthenticationContext(new Uri("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"));
assert.Statements.Add(new Saml2AuthenticationStatement(context, DateTime.Now));
string assertion;
using (var sw = new StringWriter())
{
var xws = new XmlWriterSettings();
using (var xw = XmlWriter.Create(sw, xws))
{
var handler = new Saml2SecurityTokenHandler();
handler.WriteToken(xw, new Saml2SecurityToken(assert));
}
assertion = sw.ToString();
}
And the XML I get for assert seems fine:
<?xml version="1.0" encoding="utf-16"?>
<Assertion ID="_fc348927-c0bf-4955-b98f-483043d8dedd" IssueInstant="2017-04-19T11:29:38.464Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
<Issuer>SAMLIssuer</Issuer>
<Subject>
<NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:persistent">10001</NameID>
</Subject>
<AuthnStatement AuthnInstant="2017-04-19T11:29:39.040Z">
<AuthnContext>
<AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</AuthnContextClassRef>
</AuthnContext>
</AuthnStatement>
</Assertion>
So, what now? How do I get from my code to getting:
<samlp:Response
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
ID="new id"
InResponseTo="old id"
Version="2.0"
IssueInstant="2017-04-19T11:29:39.040Z"
Destination="some url">
<saml:Issuer>SAMLIssuer</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion ....
without using external libraries or making my own wrappers/string concatenations? I can't seem to find anything in .NET 4.5 implementation of WIF that can help me.
AFAIK .NET has no built-in support for the SAMLp protocol. It only supports WsFederation. Also have a look here : How should I implement SAMLP 2.0 in an ASP.NET MVC 4 service provider?

XML serialized from classes created with xsd is not correct

I have an XML file (it's a SOAP request to an SAP ME Service).
The file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:me="http://sap.com/xi/ME">
<soapenv:Header />
<soapenv:Body>
<me:ShopOrderByBasicDataQuery_sync>
<me:ShopOrderByBasicDataQuery>
<me:ShopOrder>BOXBUILD_LASER_TEST</me:ShopOrder>
<me:SiteRef>
<me:Site>TEST1</me:Site>
</me:SiteRef>
</me:ShopOrderByBasicDataQuery>
</me:ShopOrderByBasicDataQuery_sync>
</soapenv:Body>
</soapenv:Envelope>
Using Paste Special I used Paste XML as Classes and got a number of classes.
I the created a new Envelope object
Envelope shoporder = new Envelope();
EnvelopeBody shoporderBody = new EnvelopeBody();
ShopOrderByBasicDataQuery_sync shoporderSync = new ShopOrderByBasicDataQuery_sync();
ShopOrderByBasicDataQuery_syncShopOrderByBasicDataQuery shoporderDataQuery = new ShopOrderByBasicDataQuery_syncShopOrderByBasicDataQuery();
shoporderSync.ShopOrderByBasicDataQuery = shoporderDataQuery;
shoporderDataQuery.ShopOrder = "BOXBUILD_LASER_TEST";
shoporderDataQuery.SiteRef = new ShopOrderByBasicDataQuery_syncShopOrderByBasicDataQuerySiteRef();
shoporderDataQuery.SiteRef.Site = "TEST1";
shoporderSync.ShopOrderByBasicDataQuery = shoporderDataQuery;
shoporderBody.ShopOrderByBasicDataQuery_sync = shoporderSync;
shoporder.Body = shoporderBody;
string tmp = (string)SoapHelper.SerializeToXmlString(shoporder);
The string tmp contains this (after a bit of formatting)
<?xml version="1.0" encoding="utf-16"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<ShopOrderByBasicDataQuery_sync xmlns="http://sap.com/xi/ME">
<ShopOrderByBasicDataQuery>
<ShopOrder>BOXBUILD_LASER_TEST</ShopOrder>
<SiteRef>
<Site>TEST1</Site>
</SiteRef>
</ShopOrderByBasicDataQuery>
</ShopOrderByBasicDataQuery_sync>
</Body>
</Envelope>
Which is not a correct file! What have I done wrong?

Build USPS web service request

I want to build a web service request which accesses USPS's address web service.
I am facing a problem with building the request URL string in the format they want.
What i want to do is give the zip code in a variable so that it can be dynamic.
But USPS web service isn't accepting the URL string I am sending, guess I am making a mistake with the format.
The format which USPS expects is:
<CityStateLookupRequest USERID=”xxxxxxxx”>
<ZipCode ID="0">
<Zip5>90210</Zip5>
</ZipCode>
<ZipCode ID="1">
<Zip5>20770</Zip5>
</ZipCode>
</CityStateLookupRequest>
https://servername/ShippingAPI.dll?API=CityStateLookup&XML=<CityStateLookupRe
quest USERID="username">.......</CityStateLookupRequest>
This is how I am trying to build the URL:
WebRequest USPSReq = String.Format("http://production.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML=CityStateLookupRequest&USERID=xxxxxxxx&ZipCode ID=0&Zip5=" + oZip);
How can I build this request URL?
Simply build that XML using your favorite XML API. For instance:
XDocument requestXml = new XDocument(
new XElement("CityStateLookupRequest",
new XAttribute("USERID", userID),
new XElement("ZipCode",
new XAttribute("ID", "0"),
new XElement("ZIP5", zip5)));
var requestUrl = new UriBuilder("http://production.shippingapis.com/ShippingAPITest.dll");
requestUrl.Query = "API=CityStateLookup&XML=" + requestXml.ToString();
var request = WebRequest.Create(requestUrl.Uri);

Update Merchant Order ID in Amazon Marketplace

I need to update MerchantOrderID in Amazon Marketplace via Amazon MWS API using C#.
So, first I created XML file like:
<?xml version="1.0"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>M_STORE_3918753</MerchantIdentifier>
</Header>
<MessageType>OrderAcknowledgement</MessageType>
<Message>
<MessageID>1</MessageID>
<OrderAcknowledgement>
<AmazonOrderID>104-9209939-0469021</AmazonOrderID>
<MerchantOrderID>100828163</MerchantOrderID>
<StatusCode>Success</StatusCode>
<Item>
<AmazonOrderItemCode>11111111111111</AmazonOrderItemCode>
<MerchantOrderItemID>111111</MerchantOrderItemID>
<AmazonOrderItemCode>22222222222222</AmazonOrderItemCode>
<MerchantOrderItemID>222222222</MerchantOrderItemID>
</Item>
</OrderAcknowledgement>
</Message>
</AmazonEnvelope>
then I try to call Amazon MWS SubmitFeed:
SubmitFeedRequest request = new SubmitFeedRequest();
request.Merchant = merchantId;
request.MarketplaceIdList = new IdList();
request.MarketplaceIdList.Id = new List<string>(new string[] { marketplaceId });
request.FeedContent = File.Open(path, FileMode.Open, FileAccess.Read);
request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent);
request.FeedContent.Position = 0;
request.FeedType = "_POST_ORDER_ACKNOWLEDGEMENT_DATA_";
MarketplaceWebService.MarketplaceWebService serviceFeed = new MarketplaceWebServiceClient(
accessKeyId,
secretAccessKey,
applicationName,
applicationVersion,
configFeed);
SubmitFeedResponse response = serviceFeed.SubmitFeed(request);
it succesfully submits but when I open my order in Amazon (sellercentral.amazon.com) it says "none saved":
Your Merchant Order ID: # none saved
Who can help me, what is wrong or what should I do?
Thanks!
For those who want to update merchant order id in multiple orders at once, here is example (also works in amazon mws scratchpad):
<?xml version="1.0"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>ALVH0CYWPBFNA</MerchantIdentifier>
</Header>
<MessageType>OrderAcknowledgement</MessageType>
<Message>
<MessageID>1</MessageID>
<OrderAcknowledgement>
<AmazonOrderID>000-0000000-0000000</AmazonOrderID>
<MerchantOrderID>YOUR_MERCHANT_ORDER_ID</MerchantOrderID>
<StatusCode>Success</StatusCode>
</OrderAcknowledgement>
</Message>
<Message>
<MessageID>2</MessageID>
<OrderAcknowledgement>
<AmazonOrderID>000-0000000-0000000</AmazonOrderID>
<MerchantOrderID>YOUR_MERCHANT_ORDER_ID</MerchantOrderID>
<StatusCode>Success</StatusCode>
</OrderAcknowledgement>
</Message>
</AmazonEnvelope>
It's actually right code. Somehow amazon doesn't mark some special orders or marks them later.

Categories

Resources