/// <summary>
        /// This method generates the XML for a Checkout API request. This method
        /// also calls the <b>CheckPreConditions</b> method, which verifies that
        /// if the API request indicates that the merchant will calculate tax and
        /// shipping costs, that the input data for those calculations is included
        /// in the request.
        /// </summary>
        /// <returns>This method returns the XML for a Checkout API request.
        /// </returns>
        public override byte[] GetXml()
        {
            // Verify that if the API request calls for merchant calculations, the
              // input data for those calculations is included in the request.
              //
              CheckPreConditions();

              AutoGen.CheckoutShoppingCart retVal = new AutoGen.CheckoutShoppingCart();
              retVal.shoppingcart = new AutoGen.ShoppingCart();

              // Add the Shopping cart expiration element.
              if (CartExpiration != DateTime.MinValue) {
            retVal.shoppingcart.cartexpiration = new AutoGen.CartExpiration();
            retVal.shoppingcart.cartexpiration.gooduntildate = CartExpiration;
              }

              // Add the items in the shopping cart to the API request.
              retVal.shoppingcart.items = new AutoGen.Item[_items.Count];
              for (int i = 0; i < _items.Count; i++) {

            IShoppingCartItem MyItem = _items[i] as IShoppingCartItem;
            //this can't happen at this time but if we expose a way for people to add items
            //this test needs to be peformed.
            //        if (MyItem == null) {
            //          throw new ApplicationException(
            //            "The Shopping Cart Item did not implement IShoppingCartItem." +
            //            " This is caused by someone changing the implementation of" +
            //            " GCheckout.Checkout.ShoppingCartItem.");
            //        }

            //set to a local variable of current item to lessen the change of
            //a mistake
            retVal.shoppingcart.items[i] = ShoppingCartItem.CreateAutoGenItem(MyItem, _Currency);
              }

              //because we are merging the nodes, create a new ArrayList
              ArrayList copiedMerchantPrivateData = new ArrayList();

              // Add the &lt;merchant-private-data&gt; element to the API request.
              if (MerchantPrivateData != null) {
            copiedMerchantPrivateData.Add(MakeXmlElement(MerchantPrivateData));
              }

              if (_MerchantPrivateDataNodes != null
            && _MerchantPrivateDataNodes.Count > 0) {
            for (int i = 0; i < _MerchantPrivateDataNodes.Count; i++)
              copiedMerchantPrivateData.Add(_MerchantPrivateDataNodes[i]);
              }

              if (copiedMerchantPrivateData.Count > 0) {
            AutoGen.anyMultiple any = new AutoGen.anyMultiple();

            System.Xml.XmlNode[] nodes =
              new System.Xml.XmlNode[copiedMerchantPrivateData.Count];
            copiedMerchantPrivateData.CopyTo(nodes);
            any.Any = nodes;

            retVal.shoppingcart.merchantprivatedata = any;
              }

              // Add the &lt;continue-shopping-url&gt; element to the API request.
              retVal.checkoutflowsupport =
            new AutoGen.CheckoutShoppingCartCheckoutflowsupport();

              retVal.checkoutflowsupport.Item =
            new AutoGen.MerchantCheckoutFlowSupport();

              if (ContinueShoppingUrl != null) {
            retVal.checkoutflowsupport.Item.continueshoppingurl =
              ContinueShoppingUrl;
              }
              object[] buyerMessages = BuyerMessages.ConvertMessages();
              if (buyerMessages != null && buyerMessages.Length > 0) {
            retVal.shoppingcart.buyermessages = new GCheckout.AutoGen.ShoppingCartBuyermessages();
            retVal.shoppingcart.buyermessages.Items = buyerMessages;
              }

              if (AnalyticsData != null) {
            retVal.checkoutflowsupport.Item.analyticsdata = AnalyticsData;
              }

              if (PlatformID != 0) {
            retVal.checkoutflowsupport.Item.platformid = PlatformID;
            retVal.checkoutflowsupport.Item.platformidSpecified = true;
              }

              // Add the &lt;edit-cart-url&gt; element to the API request.
              if (EditCartUrl != null) {
            retVal.checkoutflowsupport.Item.editcarturl = EditCartUrl;
              }

              // Add the &lt;request-buyer-phone-number&gt; element to the API request.
              if (RequestBuyerPhoneNumber) {
            retVal.checkoutflowsupport.Item.requestbuyerphonenumber = true;
            retVal.checkoutflowsupport.Item.requestbuyerphonenumberSpecified =
              true;
              }

              // Add the shipping methods to the API request.
              // Fix potential issue.
              if (_ShippingMethods != null && _ShippingMethods.Items != null
            && _ShippingMethods.Items.Length > 0) {
            retVal.checkoutflowsupport.Item.shippingmethods = _ShippingMethods;
              }

              //jf Tax Tables added 3/1/2007
              if (_alternateTaxTables != null) {
            _alternateTaxTables.AppendToRequest(_TaxTables);
              }

              // Add the tax tables to the API request.
              //Fix for Issue 36
              //Only if we have a tax table do we want to append it.
              if (_TaxTables != null) {
            if (
              (_TaxTables.alternatetaxtables != null
              && _TaxTables.alternatetaxtables.Length > 0)
              ||
              (_TaxTables.defaulttaxtable != null
              && _TaxTables.defaulttaxtable.taxrules != null
              && _TaxTables.defaulttaxtable.taxrules.Length > 0)
              ) {
              retVal.checkoutflowsupport.Item.taxtables = _TaxTables;
            }
              }

              // Add the merchant calculations URL to the API request.
              if (MerchantCalculationsUrl != null) {
            retVal.checkoutflowsupport.Item.merchantcalculations =
              new AutoGen.MerchantCalculations();
            retVal.checkoutflowsupport.Item.merchantcalculations.
              merchantcalculationsurl = MerchantCalculationsUrl;
              }

              // Indicate whether the merchant accepts coupons and gift certificates.
              if (AcceptMerchantCoupons) {
            retVal.checkoutflowsupport.Item.merchantcalculations.
              acceptmerchantcoupons = true;
            retVal.checkoutflowsupport.Item.merchantcalculations.
              acceptmerchantcouponsSpecified = true;
              }
              if (AcceptMerchantGiftCertificates) {
            retVal.checkoutflowsupport.Item.merchantcalculations.
              acceptgiftcertificates = true;
            retVal.checkoutflowsupport.Item.merchantcalculations.
              acceptgiftcertificatesSpecified = true;
              }

              if (_roundingRuleSet) {
            retVal.checkoutflowsupport.Item.roundingpolicy =
              new GCheckout.AutoGen.RoundingPolicy();
            retVal.checkoutflowsupport.Item.roundingpolicy.mode
              = (AutoGen.RoundingMode)Enum.Parse(typeof(AutoGen.RoundingMode),
              _roundingMode.ToString(), true);
            retVal.checkoutflowsupport.Item.roundingpolicy.rule
              = (AutoGen.RoundingRule)Enum.Parse(typeof(AutoGen.RoundingRule),
              _roundingRule.ToString(), true);
              }

              if (RequestInitialAuthDetails) {
            retVal.orderprocessingsupport = new AutoGen.OrderProcessingSupport();
            retVal.orderprocessingsupport.requestinitialauthdetailsSpecified = true;
            retVal.orderprocessingsupport.requestinitialauthdetails = true;
              }

              //See if we have any ParameterizedUrl that need to be added to the message.
              if (_ParameterizedUrls.Urls.Length > 0) {
            GCheckout.AutoGen.ParameterizedUrl[] destUrls
              = new GCheckout.AutoGen.ParameterizedUrl[_ParameterizedUrls.Urls.Length];
            for (int i = 0; i < _ParameterizedUrls.Urls.Length; i++) {
              ParameterizedUrl fromUrl = _ParameterizedUrls.Urls[i];
              GCheckout.AutoGen.ParameterizedUrl toUrl
            = new GCheckout.AutoGen.ParameterizedUrl();
              toUrl.url = fromUrl.URL;

              //ok now we have to see if params exist,
              //and if they do we need to look that array and build it out.
              if (fromUrl.Params.Length > 0) {
            GCheckout.AutoGen.UrlParameter[] destparams
              = new GCheckout.AutoGen.UrlParameter[fromUrl.Params.Length];
            for (int j = 0; j < fromUrl.Params.Length; j++) {
              UrlParamter fromParam = fromUrl.Params[j];
              GCheckout.AutoGen.UrlParameter toParam
              = new GCheckout.AutoGen.UrlParameter();
              toParam.name = fromParam.Name;
              //Use the reflection code to get the string value of the enum.
              toParam.type = fromParam.SerializedValue;
              destparams[j] = toParam;
            }
            toUrl.parameters = destparams;
              }
              destUrls[i] = toUrl; //add the url to the array.
              retVal.checkoutflowsupport.Item.parameterizedurls = destUrls;
            }
              }

              // Call the EncodeHelper.Serialize method to generate the XML for
              // the Checkout API request.
              return EncodeHelper.Serialize(retVal);
        }
        public void TestSubscriptions_All_Data()
        {
            CheckoutShoppingCartRequest request = new CheckoutShoppingCartRequest(MERCHANT_ID, MERCHANT_KEY, EnvironmentType.Sandbox, "USD", 120);
            //Make sure we can add an item to the cart.
            Subscription sub = new Subscription();

            sub.AddSubscriptionPayment(new SubscriptionPayment(2, 12));
            sub.NoChargeAfter = new DateTime(2010, 1, 1);
            sub.Period        = GCheckout.AutoGen.DatePeriod.DAILY;
            sub.RecurrentItem = new ShoppingCartItem("Sub Item", "Sub Item Description", "Item 1", 0, 3);
            sub.StartDate     = new DateTime(2009, 7, 1);
            sub.Type          = SubscriptionType.merchant;
            ShoppingCartItem item = request.AddItem("Item 1", "Cool Candy 1", 1, sub);

            //Console.WriteLine(EncodeHelper.Utf8BytesToString(request.GetXml()));

            //now deserialize it
            AutoGen.CheckoutShoppingCart cart = EncodeHelper.Deserialize(request.GetXml()) as AutoGen.CheckoutShoppingCart;
            //Console.WriteLine(EncodeHelper.Utf8BytesToString(EncodeHelper.Serialize(cart)));

            foreach (AutoGen.Item ai in cart.shoppingcart.items)
            {
                ShoppingCartItem ci = new ShoppingCartItem(ai);
                Assert.AreEqual(ci.Subscription.NoChargeAfter, sub.NoChargeAfter);
                SubscriptionPayment sp = new SubscriptionPayment(ai.subscription.payments[0]);
                SubscriptionPayment dp = sub.Payments[0];

                Assert.AreEqual(sp.MaximumCharge, dp.MaximumCharge);
                Assert.AreEqual(sp.Times, dp.Times);

                Assert.AreEqual(ci.Subscription.Period, sub.Period);
                Assert.AreEqual(ci.Subscription.StartDate, sub.StartDate);
                Assert.AreEqual(ci.Subscription.Type, sub.Type);
            }

            sub.NoChargeAfter = null;
            sub.Period        = GCheckout.AutoGen.DatePeriod.QUARTERLY;
            sub.StartDate     = null;
            sub.Type          = SubscriptionType.google;

            //reset
            cart = EncodeHelper.Deserialize(request.GetXml()) as AutoGen.CheckoutShoppingCart;
            //Console.WriteLine(EncodeHelper.Utf8BytesToString(EncodeHelper.Serialize(cart)));

            foreach (AutoGen.Item ai in cart.shoppingcart.items)
            {
                ShoppingCartItem ci = new ShoppingCartItem(ai);
                //Console.WriteLine(ci.Subscription.NoChargeAfter);
                Assert.IsFalse(ci.Subscription.NoChargeAfter.HasValue, "NoChargeAfter should be null");
                SubscriptionPayment sp = new SubscriptionPayment(ai.subscription.payments[0]);
                SubscriptionPayment dp = sub.Payments[0];

                Assert.AreEqual(sp.MaximumCharge, dp.MaximumCharge);
                Assert.AreEqual(sp.Times, dp.Times);

                Assert.AreEqual(ci.Subscription.Period, DatePeriod.QUARTERLY);
                //Console.WriteLine(ci.Subscription.StartDate);
                //bug in .net
                //Assert.IsFalse(ci.Subscription.StartDate.HasValue, "StartDate should be null");
                Assert.AreEqual(ci.Subscription.Type, SubscriptionType.google);
            }
        }
    /// <summary>
    /// This method generates the XML for a Checkout API request. This method
    /// also calls the <b>CheckPreConditions</b> method, which verifies that
    /// if the API request indicates that the merchant will calculate tax and
    /// shipping costs, that the input data for those calculations is included
    /// in the request.
    /// </summary>
    /// <returns>This method returns the XML for a Checkout API request.
    /// </returns>
    public override byte[] GetXml() {

      // Verify that if the API request calls for merchant calculations, the 
      // input data for those calculations is included in the request.
      //
      CheckPreConditions();

      AutoGen.CheckoutShoppingCart MyCart = new AutoGen.CheckoutShoppingCart();
      MyCart.shoppingcart = new AutoGen.ShoppingCart();

      // Add the Shopping cart expiration element.
      if (_CartExpiration != DateTime.MinValue) {
        MyCart.shoppingcart.cartexpiration = new AutoGen.CartExpiration();
        MyCart.shoppingcart.cartexpiration.gooduntildate = _CartExpiration;
      }

      // Add the items in the shopping cart to the API request.
      MyCart.shoppingcart.items = new AutoGen.Item[_Items.Count];
      for (int i = 0; i < _Items.Count; i++) {
        ShoppingCartItem MyItem = (ShoppingCartItem) _Items[i];
        MyCart.shoppingcart.items[i] = new AutoGen.Item();
        MyCart.shoppingcart.items[i].itemname = MyItem.Name;
        MyCart.shoppingcart.items[i].itemdescription = MyItem.Description;
        MyCart.shoppingcart.items[i].quantity = MyItem.Quantity;
        MyCart.shoppingcart.items[i].unitprice = new AutoGen.Money();
        MyCart.shoppingcart.items[i].unitprice.currency = _Currency;
        MyCart.shoppingcart.items[i].unitprice.Value = MyItem.Price;
        if (MyItem.MerchantPrivateItemData != null) {
          MyCart.shoppingcart.items[i].merchantprivateitemdata = 
            MakeXmlElement(MyItem.MerchantPrivateItemData);
        }
      }

      // Add the &lt;merchant-private-data&gt; element to the API request.
      if (_MerchantPrivateData != null) {
        MyCart.shoppingcart.merchantprivatedata = 
          MakeXmlElement(_MerchantPrivateData);
      }

      // Add the &lt;continue-shopping-url&gt; element to the API request.
      MyCart.checkoutflowsupport = 
        new AutoGen.CheckoutShoppingCartCheckoutflowsupport();
      MyCart.checkoutflowsupport.Item = 
        new AutoGen.MerchantCheckoutFlowSupport();
      if (_ContinueShoppingUrl != null) {
        MyCart.checkoutflowsupport.Item.continueshoppingurl = 
          _ContinueShoppingUrl;
      }

      // Add the &lt;edit-cart-url&gt; element to the API request.
      if (_EditCartUrl != null) {
        MyCart.checkoutflowsupport.Item.editcarturl = _EditCartUrl;
      }

      // Add the &lt;request-buyer-phone-number&gt; element to the API request.
      if (_RequestBuyerPhoneNumber) {
        MyCart.checkoutflowsupport.Item.requestbuyerphonenumber = true;
        MyCart.checkoutflowsupport.Item.requestbuyerphonenumberSpecified = 
          true;
      }

      // Add the shipping methods to the API request.
      MyCart.checkoutflowsupport.Item.shippingmethods = _ShippingMethods;

      // Add the tax tables to the API request.
      if (_TaxTables != null) {
        MyCart.checkoutflowsupport.Item.taxtables = _TaxTables;
      }

      // Add the merchant calculations URL to the API request.
      if (MerchantCalculationsUrl != null) {
        MyCart.checkoutflowsupport.Item.merchantcalculations = 
          new AutoGen.MerchantCalculations();
        MyCart.checkoutflowsupport.Item.merchantcalculations.
          merchantcalculationsurl = MerchantCalculationsUrl;
      }

      // Indicate whether the merchant accepts coupons and gift certificates.
      if (_AcceptMerchantCoupons) {
        MyCart.checkoutflowsupport.Item.merchantcalculations.
          acceptmerchantcoupons = true;
        MyCart.checkoutflowsupport.Item.merchantcalculations.
          acceptmerchantcouponsSpecified = true;
      }
      if (_AcceptMerchantGiftCertificates) {
        MyCart.checkoutflowsupport.Item.merchantcalculations.
          acceptgiftcertificates = true;
        MyCart.checkoutflowsupport.Item.merchantcalculations.
          acceptgiftcertificatesSpecified = true;
      }

      // Call the EncodeHelper.Serialize method to generate the XML for
      // the Checkout API request.
      return EncodeHelper.Serialize(MyCart);
    }
Пример #4
0
        /// <summary>
        /// This method generates the XML for a Checkout API request. This method
        /// also calls the <b>CheckPreConditions</b> method, which verifies that
        /// if the API request indicates that the merchant will calculate tax and
        /// shipping costs, that the input data for those calculations is included
        /// in the request.
        /// </summary>
        /// <returns>This method returns the XML for a Checkout API request.
        /// </returns>
        public override byte[] GetXml()
        {
            // Verify that if the API request calls for merchant calculations, the
            // input data for those calculations is included in the request.
            //
            CheckPreConditions();

            AutoGen.CheckoutShoppingCart MyCart = new AutoGen.CheckoutShoppingCart();
            MyCart.shoppingcart = new AutoGen.ShoppingCart();

            // Add the Shopping cart expiration element.
            if (_CartExpiration != DateTime.MinValue)
            {
                MyCart.shoppingcart.cartexpiration = new AutoGen.CartExpiration();
                MyCart.shoppingcart.cartexpiration.gooduntildate = _CartExpiration;
            }

            // Add the items in the shopping cart to the API request.
            MyCart.shoppingcart.items = new AutoGen.Item[_Items.Count];
            for (int i = 0; i < _Items.Count; i++)
            {
                ShoppingCartItem MyItem = (ShoppingCartItem)_Items[i];
                MyCart.shoppingcart.items[i]                    = new AutoGen.Item();
                MyCart.shoppingcart.items[i].itemname           = MyItem.Name;
                MyCart.shoppingcart.items[i].itemdescription    = MyItem.Description;
                MyCart.shoppingcart.items[i].quantity           = MyItem.Quantity;
                MyCart.shoppingcart.items[i].unitprice          = new AutoGen.Money();
                MyCart.shoppingcart.items[i].unitprice.currency = _Currency;
                MyCart.shoppingcart.items[i].unitprice.Value    = MyItem.Price;
                if (MyItem.MerchantPrivateItemData != null)
                {
                    MyCart.shoppingcart.items[i].merchantprivateitemdata =
                        MakeXmlElement(MyItem.MerchantPrivateItemData);
                }
            }

            // Add the &lt;merchant-private-data&gt; element to the API request.
            if (_MerchantPrivateData != null)
            {
                MyCart.shoppingcart.merchantprivatedata =
                    MakeXmlElement(_MerchantPrivateData);
            }

            // Add the &lt;continue-shopping-url&gt; element to the API request.
            MyCart.checkoutflowsupport =
                new AutoGen.CheckoutShoppingCartCheckoutflowsupport();
            MyCart.checkoutflowsupport.Item =
                new AutoGen.MerchantCheckoutFlowSupport();
            if (_ContinueShoppingUrl != null)
            {
                MyCart.checkoutflowsupport.Item.continueshoppingurl =
                    _ContinueShoppingUrl;
            }

            // Add the &lt;edit-cart-url&gt; element to the API request.
            if (_EditCartUrl != null)
            {
                MyCart.checkoutflowsupport.Item.editcarturl = _EditCartUrl;
            }

            // Add the &lt;request-buyer-phone-number&gt; element to the API request.
            if (_RequestBuyerPhoneNumber)
            {
                MyCart.checkoutflowsupport.Item.requestbuyerphonenumber          = true;
                MyCart.checkoutflowsupport.Item.requestbuyerphonenumberSpecified =
                    true;
            }

            // Add the shipping methods to the API request.
            MyCart.checkoutflowsupport.Item.shippingmethods = _ShippingMethods;

            // Add the tax tables to the API request.
            if (_TaxTables != null)
            {
                MyCart.checkoutflowsupport.Item.taxtables = _TaxTables;
            }

            // Add the merchant calculations URL to the API request.
            if (MerchantCalculationsUrl != null)
            {
                MyCart.checkoutflowsupport.Item.merchantcalculations =
                    new AutoGen.MerchantCalculations();
                MyCart.checkoutflowsupport.Item.merchantcalculations.
                merchantcalculationsurl = MerchantCalculationsUrl;
            }

            // Indicate whether the merchant accepts coupons and gift certificates.
            if (_AcceptMerchantCoupons)
            {
                MyCart.checkoutflowsupport.Item.merchantcalculations.
                acceptmerchantcoupons = true;
                MyCart.checkoutflowsupport.Item.merchantcalculations.
                acceptmerchantcouponsSpecified = true;
            }
            if (_AcceptMerchantGiftCertificates)
            {
                MyCart.checkoutflowsupport.Item.merchantcalculations.
                acceptgiftcertificates = true;
                MyCart.checkoutflowsupport.Item.merchantcalculations.
                acceptgiftcertificatesSpecified = true;
            }

            // Call the EncodeHelper.Serialize method to generate the XML for
            // the Checkout API request.
            return(EncodeHelper.Serialize(MyCart));
        }