OneShopNotificationListener.cs

From Developer's API

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text.RegularExpressions;

public partial class SampleClients_OneShopNotificationListener : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            // TODO: Add your Merchant ID as the First Parameter, and your Merchant Key as Second Parameter.
            OneShopApi oneShopApiWrapper = new OneShopApi("", "", "https://www.mcssl.com");

            System.Text.UTF8Encoding encodeResponse = new System.Text.UTF8Encoding();

            string requestData = encodeResponse.GetString(Request.BinaryRead(Request.TotalBytes));
           
            XmlDocument requestBodyXML = new XmlDocument();

            // This Line remove the Byte-Order-Mark from the request as the LoadXML does not like data before the XML
            requestBodyXML.LoadXml(Regex.Replace(requestData, "^[^<]|(\\r\\n *)", ""));

            string notificationType = requestBodyXML.DocumentElement.Name;

            string Token = requestBodyXML.SelectSingleNode("/" + notificationType + "/Token").InnerText;

            XmlDocument apiResultXML= new XmlDocument();

            switch (notificationType)
            {
                case "NewOrder":
                   
                    apiResultXML.Load(oneShopApiWrapper.GetOrderById(Token));

                    break;
                default:
                    break;
            }

            // Check that the API returned success
            string apiSuccess = apiResultXML.SelectSingleNode("/Response").Attributes["success"].Value;

            if (apiSuccess == "true")
            {
                // TODO: Do something useful with the XML
            }
            else
            {
                // TODO: Do something with the error returned by the API
            }
        }
        catch (Exception ex)
        {
            // TODO: Log or alert that an exception has occured
        }        
    }
}