Ejemplo n.º 1
0
        static void TransactionPush(string url, string username, string secret)
        {
            DigitalAPIOperations ops = new DigitalAPIOperations();

            ops.Host        = url;
            ops.AccountName = username;
            ops.Secret      = secret;

            //
            // Retrieve the session token.
            //
            Console.WriteLine("---Retrieving Session Token---");
            try
            {
                // The session token returned should be treated like a password and maintained in a secure fashion.
                ops.RetrieveSessionToken();
                Console.WriteLine("Session Token:" + ops.SessionToken);
                Console.WriteLine();
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to retrieve session token:");
                // Note: The Status property is very helpful to anlayze problems programtically.
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                return;
            }

            //
            // List all the regions
            //
            Console.WriteLine("---Listing Regions---");
            try
            {
                XmlDocument regions = ops.RetrieveRegions();
                Console.WriteLine("Regions:");
                foreach (XmlNode region in regions.DocumentElement)
                {
                    Console.WriteLine(" \'" + region.InnerText + "\'");
                }
                Console.WriteLine();
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to retrieve regions:");
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                Console.WriteLine();
            }

            //
            // Generate a unique permit number for the following create, update, and retrieve calls. The number
            // needs to be unique to the account. It is recommended that for production, the number should
            // increase as more transactions are created.
            //
            // For this exmaple we will use a random number. In your system, you should maintain a counter.
            //
            Random rand         = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
            int    permitNumber = rand.Next(99999);

            XmlDocument permitData = null;

            //
            // Create a permit
            //
            Console.WriteLine("---Creating New Permit---");
            try
            {
                permitData = GenerateNewPermitXml(permitNumber);
                // Note: The call may fail if the permit number generated matches a preexisting permit. When this
                //       happens, the server will return a 412 "Failure - Object already exists" response code.
                ops.InsertNewPermit(permitData);

                Console.WriteLine("New Permit:");
                Console.WriteLine(FormatXml(permitData));
                Console.WriteLine();
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to create permit:");
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                Console.WriteLine();
            }

            //
            // Retrieve the permit that was just created.
            //
            XmlDocument existingPermit = null;

            Console.WriteLine("---Retrieving Permit---");
            try
            {
                existingPermit = ops.RetrievePermit(permitNumber);
                if (existingPermit != null)
                {
                    Console.WriteLine("Permit just created:");
                    Console.WriteLine(FormatXml(existingPermit));
                    Console.WriteLine();
                }
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to retrieve permit:");
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
            }

            //
            // Extend permit
            //
            if (permitData != null)
            {
                Console.WriteLine("---Extending Permit---");

                // Simulate user extending the permit at a later date.
                System.Threading.Thread.Sleep(2000);

                try
                {
                    XmlDocument updatedPermit = GenerateExtendPermitXml(permitData);

                    ops.UpdatePermit(updatedPermit);
                    Console.WriteLine("Permit Extended:");
                    Console.WriteLine(FormatXml(updatedPermit));
                    Console.WriteLine();
                }
                catch (WebException wex)
                {
                    Console.WriteLine("Unable to extend permit:");
                    Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                    Console.WriteLine();
                }
            }

            //
            // Release Token
            //
            // It is important to release the token for security reasons and to release
            // any resources being retained for that token.
            Console.WriteLine("---Release Token---");
            try
            {
                HttpStatusCode result = ops.ReleaseToken();
                Console.WriteLine("Token Released.");
                Console.WriteLine("Result:" + result.ToString());
                Console.WriteLine();
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to release token:");
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                Console.WriteLine();
            }
        }
Ejemplo n.º 2
0
        static void CouponPush(string url, string username, string secret)
        {
            DigitalAPIOperations ops = new DigitalAPIOperations();

            ops.Host        = url;
            ops.AccountName = username;
            ops.Secret      = secret;

            //
            // Retrieve the session token.
            //
            Console.WriteLine("---Retrieving Session Token---");
            try
            {
                // The session token returned should be treated like a password and maintained in a secure fashion.
                ops.RetrieveSessionToken();
                Console.WriteLine("Session Token:" + ops.SessionToken);
                Console.WriteLine();
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to retrieve session token:");
                // Note: The Status property is very helpful to anlayze problems programtically.
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                return;
            }

            //
            // List all the regions
            //
            Console.WriteLine("---Listing Regions---");
            try
            {
                XmlDocument regions = ops.RetrieveRegions();
                Console.WriteLine("Regions:");
                foreach (XmlNode region in regions.DocumentElement)
                {
                    Console.WriteLine(" \'" + region.InnerText + "\'");
                }
                Console.WriteLine();
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to retrieve regions:");
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                Console.WriteLine();
            }

            Random         rand         = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
            string         couponCode   = "A" + rand.Next(99999).ToString().PadLeft(5, '0');
            HttpStatusCode result       = HttpStatusCode.OK;
            XmlDocument    requestData  = null;
            XmlDocument    responseData = null;

            //
            // Create Coupon
            //
            Console.WriteLine("---Adding a new coupon---");
            requestData = GetNewCouponXml(couponCode);
            result      = ops.InsertCoupon(couponCode, requestData);
            Console.WriteLine("Response:" + result);
            Console.WriteLine();

            //
            // Update Coupon
            //
            Console.WriteLine("---Updating the coupon---");
            requestData = GetUpdateCouponXml(couponCode);
            result      = ops.UpdateCoupon(couponCode, requestData);
            Console.WriteLine("Response:" + result);
            Console.WriteLine();

            //
            // Retrieve Coupon
            //
            Console.WriteLine("---Retreiving the updated coupon---");
            result = ops.RetrieveCoupon(couponCode, out responseData);
            Console.WriteLine("Response:" + result);
            if (result == HttpStatusCode.OK)
            {
                Console.WriteLine(FormatXml(responseData));
            }
            Console.WriteLine();

            //
            // Delete Permit
            //
            Console.WriteLine("---Deleting the coupon---");
            result = ops.DeleteCoupon(couponCode, out responseData);
            Console.WriteLine("Response:" + result);
            if (result == HttpStatusCode.OK)
            {
                Console.WriteLine(FormatXml(responseData));
            }
            Console.WriteLine();
            Console.WriteLine("---Deleting the coupon---");
            result = ops.DeleteCoupon("SARAN123", out responseData);
            Console.WriteLine("Response:" + result);
            if (result == HttpStatusCode.OK)
            {
                Console.WriteLine(FormatXml(responseData));
            }
            Console.WriteLine();
            Console.WriteLine("---Deleting the coupon---");
            result = ops.DeleteCoupon("SARAN691", out responseData);
            Console.WriteLine("Response:" + result);
            if (result == HttpStatusCode.OK)
            {
                Console.WriteLine(FormatXml(responseData));
            }
            Console.WriteLine();

            string[] couponCodes = new string[]
            {
                "A" + rand.Next(99999).ToString().PadLeft(5, '0'),
                "A" + rand.Next(99999).ToString().PadLeft(5, '0'),
                "A" + rand.Next(99999).ToString().PadLeft(5, '0'),
                "A" + rand.Next(99999).ToString().PadLeft(5, '0')
            };

            //
            // Create Multiple Coupons
            //
            Console.WriteLine("---Adding multiple coupons---");
            requestData = GetNewCouponsXml(couponCodes);
            result      = ops.InsertCoupons(requestData, out responseData);
            Console.WriteLine("Response:" + result);
            if (result == HttpStatusCode.OK)
            {
                Console.WriteLine(FormatXml(responseData));
            }
            Console.WriteLine();

            //
            // Retrieve Multiple Coupons
            //
            Console.WriteLine("---Retreiving the created coupons---");
            string coupons = GetCouponList(couponCodes);

            result = ops.RetrieveCoupons(coupons, out responseData);
            Console.WriteLine("Response:" + result);
            if (result == HttpStatusCode.OK)
            {
                Console.WriteLine(FormatXml(responseData));
            }
            Console.WriteLine();

            //
            // Delete Multiple Coupons
            //
            Console.WriteLine("---Deleting the created coupon---");
            requestData = GetDeleteCouponsXml(couponCodes);
            result      = ops.DeleteCoupons(requestData, out responseData);
            Console.WriteLine("Response:" + result);
            if (result == HttpStatusCode.OK)
            {
                Console.WriteLine(FormatXml(responseData));
            }
            Console.WriteLine();

            //
            // Release Token
            //
            // It is important to release the token for security reasons and to release
            // any resources being retained for that token.
            Console.WriteLine("---Release Token---");
            try
            {
                result = ops.ReleaseToken();
                Console.WriteLine("Token Released.");
                Console.WriteLine("Result:" + result.ToString());
                Console.WriteLine();
            }
            catch (WebException wex)
            {
                Console.WriteLine("Unable to release token:");
                Console.WriteLine(wex.Message + " (" + wex.Status.ToString() + ")");
                Console.WriteLine();
            }
        }