Package com.google.checkout.sample.protocol

Source Code of com.google.checkout.sample.protocol.CheckoutCartBuilder

/*************************************************
* Copyright (C) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*************************************************/
package com.google.checkout.sample.protocol;

import com.google.checkout.sample.util.StringUtil;
import com.google.checkout.schema._2.AlternateTaxRule;
import com.google.checkout.schema._2.AlternateTaxTable;
import com.google.checkout.schema._2.AnyType;
import com.google.checkout.schema._2.CartExpiration;
import com.google.checkout.schema._2.CheckoutShoppingCart;
import com.google.checkout.schema._2.DefaultTaxRule;
import com.google.checkout.schema._2.DefaultTaxTable;
import com.google.checkout.schema._2.FlatRateShipping;
import com.google.checkout.schema._2.Item;
import com.google.checkout.schema._2.MerchantCalculatedShipping;
import com.google.checkout.schema._2.MerchantCalculations;
import com.google.checkout.schema._2.MerchantCheckoutFlowSupport;
import com.google.checkout.schema._2.Money;
import com.google.checkout.schema._2.Pickup;
import com.google.checkout.schema._2.ShippingRestrictions;
import com.google.checkout.schema._2.ShoppingCart;
import com.google.checkout.schema._2.TaxTables;
import com.google.checkout.schema._2.USAreas;
import com.google.checkout.schema._2.USCountryArea;
import com.google.checkout.schema._2.USStateArea;
import com.google.checkout.schema._2.USZipArea;
import com.google.checkout.schema._2.AlternateTaxTable.AlternateTaxRules;
import com.google.checkout.schema._2.CheckoutShoppingCart.CheckoutFlowSupport;
import com.google.checkout.schema._2.DefaultTaxTable.TaxRules;
import com.google.checkout.schema._2.MerchantCheckoutFlowSupport.ShippingMethods;
import com.google.checkout.schema._2.ShippingRestrictions.AllowedAreas;
import com.google.checkout.schema._2.ShippingRestrictions.ExcludedAreas;
import com.google.checkout.schema._2.ShoppingCart.Items;
import com.google.checkout.schema._2.TaxTables.AlternateTaxTables;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
* The <b>CheckoutCartBuilder</b> class contains methods that create the
* JAXB objects needed to construct Checkout API requests.
* @version 1.0 beta
*/
public class CheckoutCartBuilder extends AbstractProtocolBuilder {
  private static DatatypeFactory _dataFactory;
  private static CheckoutCartBuilder _builder;
 
  private CheckoutCartBuilder() throws JAXBException, ProtocolException {
    _dataFactory = createDataFactory();
  }

  private static DatatypeFactory createDataFactory() throws ProtocolException {
    try {
      return DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException configEx) {
      throw new ProtocolException(configEx.getMessage());
    }
  }
 
  public synchronized static CheckoutCartBuilder getInstance()
      throws ProtocolException {
    try {
      if (_builder == null) {
        _builder = new CheckoutCartBuilder();
      }
    } catch (JAXBException jaxbEx) {
      throw new ProtocolException(jaxbEx);
    }
   
    return _builder;
  }
 
  /**
   * The <b>createShoppingItem</b> method creates an Item object, which contains
   * the information that would be contained within a single &lt;item&gt; XML
   * block in a Checkout API request.
   * @param itemName Required. The name of the item.
   * @param itemDesc Required. A description of the item.
   * @param quantity Required. The number of units of the item included in
   * the order.
   * @param unitPrice Required. The numeric cost of the item.
   * @param currency Optional. The three-letter ISO 4217 currency code
   * associated with the numeric cost of the item.  At this time, Google
   * Checkout only supports U.S. dollars (USD).
   * @param taxTableSelector Optional. The name of the alternate tax table
   * that should be used to calculate tax for the item.
   * @param privateItemData Optional. Proprietary information about an item
   * in an order. The value must be well formed XML with a single root tag.
   * @return {@see Item} object
   */
  public Item createShoppingItem(String itemName, String itemDesc,
      int quantity, float unitPrice, String currency,
      String taxTableSelector, Element privateItemData)
      throws ProtocolException {
    Money money = createMoney(unitPrice, currency);
    Item item = _objectFact.createItem();
    item.setItemName(itemName);
    item.setItemDescription(itemDesc);
    item.setQuantity(quantity);
    item.setUnitPrice(money);

    if (taxTableSelector != null) {
      item.setTaxTableSelector(taxTableSelector);
    }
   
    if (privateItemData != null) {
      AnyType anyType = _objectFact.createAnyType();
      anyType.setAny(privateItemData);
      item.setMerchantPrivateItemData(anyType);
    }
   
    return item;
  }
 
  /**
   * The <b>createShoppingCart</b> method constructs a ShoppingCart object,
   * which is comprised of a list of {@see Item} objects as well as a
   * {@see CartExpiration} date. This method must be called after you have
   * called the
   * {@see #createShoppingItem(String, String, int, float, String, String, Element)}
   * method for each item in the order.
   * @param itemList Required. A list of all of the items in the order.
   * @param cartExpire Optional. The date that the shopping cart expires. If
   * you specify a value for this parameter, Google Checkout will reject the
   * shopping cart if the user submits the cart after the specified date. If
   * you do not specify a value for this parameter, the cart will not expire.
   * @param merchantPrivateData Optional. Proprietary information about an
   * order. The parameter value must be well formed XML with a single root tag.
   * @return ShoppingCart object
   * @throws ProtocolException if itemList is null
   */
  public ShoppingCart createShoppingCart(List<Item> itemList, Date cartExpire,
    Element merchantPrivateData)
      throws ProtocolException {
    if (itemList == null) {
      throw new ProtocolException("itemList cannot be null");
    }
   
    // Create the <items> tag and add each item in the list to the cart
    ShoppingCart cart = _objectFact.createShoppingCart();
    Items items = _objectFact.createShoppingCartItems();
    List<Item> item_list = items.getItem();
    for (Item item : itemList) {
      item_list.add(item);
    }
   
    cart.setItems(items);
   
    // Add the expiration date if one is provided
    if (cartExpire != null) {
      GregorianCalendar gCal = new GregorianCalendar();
      gCal.setTime(cartExpire);
      XMLGregorianCalendar xmlCalendar
          = _dataFactory.newXMLGregorianCalendar(gCal);
      CartExpiration cartExpiry = _objectFact.createCartExpiration();
      cartExpiry.setGoodUntilDate(xmlCalendar);
      cart.setCartExpiration(cartExpiry);
    }

    // Add merchantPrivateData if it is provided
    if (merchantPrivateData != null) {
      AnyType anyType = _objectFact.createAnyType();
      anyType.setAny(merchantPrivateData);
      cart.setMerchantPrivateData(anyType);
    }
   
    return cart;
  }

  /**
   * The <b>createCheckoutShoppingCart</b> method constructs and returns a DOM
   * object containing all of the items and checkout-related information for an
   * order.
   * @param cart Required. A ShoppingCart object. Also see:
   * {@see #createShoppingCart(List, Date)}
   * @param checkoutFlowSupport - Required. Information used in the checkout
   * process, including tax tables and available shipping methods. Also see
   * the <code>createMerchantSupport</code> method
   * @throws ProtocolException if cart is null or checkoutFlowSupport is null
   */
  public Document createCheckoutCart(ShoppingCart cart,
      MerchantCheckoutFlowSupport checkoutFlowSupport)
      throws ProtocolException {

    if (cart == null) {
      throw new ProtocolException("both cart and flow support cannot be null");
    }
   
    CheckoutFlowSupport flowSupport
        = _objectFact.createCheckoutShoppingCartCheckoutFlowSupport();
    flowSupport.setMerchantCheckoutFlowSupport(checkoutFlowSupport);
    CheckoutShoppingCart chkoutCart = _objectFact.createCheckoutShoppingCart();
    chkoutCart.setShoppingCart(cart);
    chkoutCart.setCheckoutFlowSupport(flowSupport);
    JAXBElement<CheckoutShoppingCart> elem
        = _objectFact.createCheckoutShoppingCart(chkoutCart);
    return convertToDOM(elem);
  }
 
  /**
   * The <b>createMerchantSupport</b> method constructs a
   * MerchantCheckoutFlowSupport object, which contains the information that
   * would be contained within the &lt;merchant-checkout-flow-support&gt;
   * XML block in a Checkout API request.
   * @param methods Optional. A list of options for shipping the order. These
   * options can be either flat-rate shipping options, merchant-calculated
   * shipping options or in-store pickup. Also see the
   * <code>createFlatShippingMethod</code>,
   * <code>createMerchantCalculatedMethod</code> and
   * <code>createPickupMethod</code> methods.
   *<br>
   * @param defaultTaxTable Required. This parameter contains a DefaultTaxTable
   * object, which specifies the default tax rules to apply to the order.
   * Also see the <code>createDefaultTaxTable</code> methods in this class
   * for creating tax tables for country, state and zip code areas.
   *<br>
   * @param alterTaxTableList Optional. This parameter contains a list of
   * AlternateTaxTable objects, which specify alternate tax rules to apply to
   * certain types of items. Also see the <code>createAlternateTaxTable</code>
   * methods in this class creating alternate tax tables for country, state and
   * zip code areas.
   *<br>
   * @param couponInfo Optional. This parameter contains a MerchantCalculations
   * object, which contains the information that would be contained within the
   * &lt;merchant-calculations&gt; XML block of a Checkout API request. This
   * information includes an indication of whether the merchant accepts coupons
   * and/or gift certificates and, if so, the URL to which Google Checkout
   * should send the &lt;merchant-calculation-callback&gt; request. Also see
   * {@see #createMerchantCalculations(boolean, boolean, String)}
   *<br>
   * @param editCartUrl Optional. A URL that the customer could click to
   * edit her shopping cart.
   *<br>
   * @param continueShoppingUrl Optional. A URL that the customer could
   * click to postpone the checkout process and continue shopping.
   */
  public MerchantCheckoutFlowSupport createMerchantSupport(
      ShippingMethods methods, DefaultTaxTable defaultTaxTable,
      List<AlternateTaxTable> alterTaxTableList,
      MerchantCalculations couponInfo, String editCartUrl,
      String continueShoppingUrl) throws ProtocolException {

    AlternateTaxTables aTables
        = _objectFact.createTaxTablesAlternateTaxTables();
    List<AlternateTaxTable> aTaxList = aTables.getAlternateTaxTable();
    if (alterTaxTableList != null) {
      for (AlternateTaxTable aTaxtable : alterTaxTableList) {
        aTaxList.add(aTaxtable);
      }
    }
   
    TaxTables taxTables = _objectFact.createTaxTables();
    taxTables.setDefaultTaxTable(defaultTaxTable);
    taxTables.setAlternateTaxTables(aTables);
   
    MerchantCheckoutFlowSupport mChkoutSupport
        = _objectFact.createMerchantCheckoutFlowSupport();
    mChkoutSupport.setContinueShoppingUrl(continueShoppingUrl);
    mChkoutSupport.setEditCartUrl(editCartUrl);
    mChkoutSupport.setMerchantCalculations(couponInfo);
    mChkoutSupport.setShippingMethods(methods);
    mChkoutSupport.setTaxTables(taxTables);
    return mChkoutSupport;
  }
 
  /**
   * The <b>createAllowedAreasByStates</b> method is a wrapper method that calls
   * the <code>createAllowedAreaByList</code> private method to set the list of
   * states where a shipping method is available. This list will be incorporated
   * into the shipping restrictions for a shipping method. Also see the
   * <code>createShippingRestrictions</code> method.
   * @param states Required. This parameter contains a list of two-letter U.S.
   * state abbreviations. Also see {@see #createStateArea(String)}
   * @throws ProtocolException if state list given is null
   */
  public AllowedAreas createAllowedAreasByStates(List<USStateArea> states)
      throws ProtocolException {
    if (states == null) {
      throw new ProtocolException("states list cannot be null");
    }
    return createAllowedAreaByList(states);
  }
 
  /**
   * The <b>createAllowedAreasByZips</b> method is a wrapper method that
   * calls the <code>createAllowedAreaByList)</code> private method to set the
   * list of zip codes where a shipping method is available. This list will be
   * incorporated into the shipping restrictions for a shipping method. Also
   * see the <code>createShippingRestrictions</code> method.
   * @param zips Required. This parameter contains a list of U.S. zip codes
   * or zip code patterns. Also see {@see #createZipArea(String)}
   * @throws ProtocolException if zips list is null
   */
  public AllowedAreas createAllowedAreasByZips(List<USZipArea> zips)
      throws ProtocolException {
    if (zips == null) {
      throw new ProtocolException("zip list cannot be null");
    }
    return createAllowedAreaByList(zips);
  }
 
  /**
   * The <b>createAllowedAreasByList</b> method sets the list of states or zip
   * codes where a shipping method is available. This list will be incorporated
   * into the shipping restrictions for a shipping method. This method is
   * called by either the createAllowedAreasByStates() or the
   * createAllowedAreasByZips() method.
   * @param List Required. This parameter contains a list of U.S. two-letter
   * state abbreviations, U.S. zip codes or zip code patterns.
   * @return AllowedAreas object
   */
  private AllowedAreas createAllowedAreaByList(List areas)
      throws ProtocolException {
    AllowedAreas allowedAreas
        = _objectFact.createShippingRestrictionsAllowedAreas();
    List allowedList = allowedAreas.getUsStateAreaOrUsZipAreaOrUsCountryArea();
    for (Object area : areas) {
      allowedList.add(area);
    }
    return allowedAreas;   
  }

  /**
   * The <b>createAllowedAreasByCountryArea</b> method adds a U.S. country area
   * to an AllowedAreas object, which identifies country regions, states and zip
   * codes where a shipping method is available.
   * @param biggerArea Required. This parameter identifies the U.S. country
   * area where a shipping option is available. Valid values for this parameter
   * are CONTINENTAL_48, FULL_50_STATES and ALL. Also see {@see USAreas}
   * @throws ProtocolException if biggerArea is null
   */
  public AllowedAreas createAllowedAreasByCountryArea(USAreas biggerArea)
      throws ProtocolException {
    if (biggerArea == null) {
      throw new ProtocolException("biggerArea cannot be null");
    }
   
    AllowedAreas allowedAreas
        = _objectFact.createShippingRestrictionsAllowedAreas();
    List allowedList = allowedAreas.getUsStateAreaOrUsZipAreaOrUsCountryArea();
    USCountryArea countryArea = _objectFact.createUSCountryArea();
    countryArea.setCountryArea(biggerArea);
    allowedList.add(countryArea);
    return allowedAreas;
  }

  /**
   * The <b>createExcludedAreasByStates</b> method is a wrapper method that calls
   * the <code>createExcludedAreaByList</code> private method to set the list
   * of states where a shipping method is not available. This list will be
   * incorporated into the shipping restrictions for a shipping method. Also
   * see the <code>createShippingRestrictions</code> method.
   * @param states Required. A list of two-letter U.S. state abbreviations.
   * Also see {@see #createStateArea(String)}
   * @throws ProtocolException if state list given is null
   */
  public ExcludedAreas createExcludedAreasByStates(List<USStateArea> states)
      throws ProtocolException {
    if (states == null) {
      throw new ProtocolException("states cannot be null");
    }
    return createExcludedAreaByList(states);
  }

  /**
   * The <b>createExcludedAreasByZips</b> method is a wrapper method that calls
   * the <code>createExcludedAreaByList</code> private method to set the list
   * of zip codes where a shipping method is not available. This list will
   * be incorporated into the shipping restrictions for a shipping method.
   * Also see the <code>createShippingRestriction</code> method.
   * @param zips Required. This parameter contains a list of U.S. zip codes
   * or zip code patterns. Also see {@see #createZipArea(String)}
   * @throws ProtocolException if zips list is null
   */
  public ExcludedAreas createExcludedAreasByZips(List<USZipArea> zips)
      throws ProtocolException {
    if (zips == null) {
      throw new ProtocolException("zips cannot be null");
    }
    return createExcludedAreaByList(zips);
  }
 
  /**
   * The <b>createExcludedAreasByList</b> method sets the list of states or zip
   * codes where a shipping method is not available. This list will be
   * incorporated into the shipping restrictions for a shipping method. This
   * method is called by either the createExcludedAreasByStates() or the
   * createExcludedAreasByZips() method.
   * @param List Required. This parameter contains a list of U.S. two-letter
   * state abbreviations, U.S. zip codes or zip code patterns.
   * @return ExcludedAreas object
   */
  private ExcludedAreas createExcludedAreaByList(List areas)
      throws ProtocolException {
    ExcludedAreas excludedAreas
        = _objectFact.createShippingRestrictionsExcludedAreas();
    List excludedList
        = excludedAreas.getUsStateAreaOrUsZipAreaOrUsCountryArea();
    for (Object area : areas) {
      excludedList.add(area);
    }
    return excludedAreas;
  }
 
  /**
   * The <b>createExcludedAreasByCountryArea</b> method adds a U.S. country area
   * to an ExcludedAreas object, which identifies country regions, states and
   * zip codes where a shipping method is not available.
   * @param biggerArea Required. This parameter identifies the U.S. country
   * area where a shipping option is not available. Valid values for this
   * parameter are CONTINENTAL_48, FULL_50_STATES and ALL.
   * Also see {@see USAreas}
   * @throws ProtocolException if biggerArea is null
   */
  public ExcludedAreas createExcludedAreasByCountryArea(USAreas biggerArea)
      throws ProtocolException {
    if (biggerArea == null) {
      throw new ProtocolException("biggerArea cannot be null");
    }
   
    ExcludedAreas excludedAreas
        = _objectFact.createShippingRestrictionsExcludedAreas();
    List allowedList = excludedAreas.getUsStateAreaOrUsZipAreaOrUsCountryArea();
    USCountryArea countryArea = _objectFact.createUSCountryArea();
    countryArea.setCountryArea(biggerArea);
    allowedList.add(countryArea);
    return excludedAreas;
  }
 
  /**
   * The <b>createShippingRestrictions</b> method creates a
   * ShippingRestrictions object, which is comprised of an AllowedAreas
   * object and an ExcludedAreas object. The ShippingRestrictions object
   * is then used to create either a FlatRateShipping object or a
   * MerchantCalculatedShipping object.
   *
   * @param allowedAreas Optional. An AllowedAreas object.
   * @param excludedAreas Optional. An ExcludedAreas object.
   */
  public ShippingRestrictions createShippingRestrictions(
      AllowedAreas allowedAreas, ExcludedAreas excludedAreas)
      throws ProtocolException {
    ShippingRestrictions restrict = _objectFact.createShippingRestrictions();
    restrict.setAllowedAreas(allowedAreas);
    restrict.setExcludedAreas(excludedAreas);
    return restrict;
  }

  /**
   * The <b>addShippingMethod</b> method adds a FlatRateShipping,
   * MerchantCalculatedShipping or Pickup object to the list of
   * shipping methods available for an order and then returns that list.
   * @param shippingMethod Required. An object that represents the
   * shipping method to be added.
   * @return list of {@see FlatRateShipping}, {@see MerchantCalculatedShipping},
   * {@see Pickup}
   */
  private ShippingMethods addShippingMethod(Object shippingMethod) {
    ShippingMethods methods
        = _objectFact.createMerchantCheckoutFlowSupportShippingMethods();
    List methodList
        = methods.getFlatRateShippingOrMerchantCalculatedShippingOrPickup();
    methodList.add(shippingMethod);
    return methods;
  }

  /**
   * The <b>createFlatRateShipping</b> method creates a {@see FlatRateShipping}
   * object, which contains information about a shipping method that is
   * available for the order, and adds that object to the list of the order's
   * shipping options.
   * @param shippingName Required. A string that can be used to identify a
   * particular shipping option, such as &quot;UPS Ground&quot;
   * @param shippingCost Required. The numeric cost of the shipping option.
   * @param shipRestrictions Optional. A {@see ShippingRestrictions} object,
   * which identifies the areas where a shipping option is either available
   * or unavailable.
   * @return ShippingMethods object
   */
  public ShippingMethods createFlatRateShipping(String shippingName,
      float shippingCost, ShippingRestrictions shipRestrictions)
      throws ProtocolException {
    return createFlatRateShipping(shippingName, shippingCost, null,
        shipRestrictions);
  }
 
  /**
   * The <b>createFlatRateShipping</b> method creates a {@see FlatRateShipping}
   * object, which contains information about a shipping method that is
   * available for the order, and adds that object to the list of the order's
   * shipping options.
   * @param shippingName Required. A string that can be used to identify a
   * particular shipping option, such as &quot;UPS Ground&quot;
   * @param shippingCost Required. The numeric cost of the shipping option.
   * @param currency A three-letter ISO 4217 currency code.
   * @param shipRestrictions Optional. A ShippingRestrictions object, which
   * identifies the areas where a shipping option is either available or
   * unavailable.
   * @return ShippingMethods object
   */
  public ShippingMethods createFlatRateShipping(String shippingName,
      float shippingCost, String currency, ShippingRestrictions shipRestrictions)
      throws ProtocolException {
    FlatRateShipping flatRate = _objectFact.createFlatRateShipping();
    flatRate.setName(shippingName);
    flatRate.setPrice(createMoney(shippingCost, currency));
    flatRate.setShippingRestrictions(shipRestrictions);
    return addShippingMethod(flatRate);
  }

  /**
   * The <b>createMerchantCalculatedShipping</b> method creates a
   * {@see MerchantCalculatedShipping} object, which contains information about
   * a shipping method that is available for the order, and adds that object
   * to the list of the order's shipping options.
   * @param shippingName Required. A string that can be used to identify a
   * particular shipping option, such as &quot;UPS Ground&quot;
   * @param shippingCost Required. The numeric cost of the shipping option.
   * This value will only be used if the &lt;merchant-calculation-callback&gt;
   * request fails.
   * @param shipRestrictions Optional. A ShippingRestrictions object, which
   * identifies the areas where a shipping option is either available or
   * unavailable.
   * @return ShippingMethods object
   */
  public ShippingMethods createMerchantShipping(String shippingName,
      float shippingCost, ShippingRestrictions shipRestrictions)
      throws ProtocolException {
    return createMerchantShipping(shippingName, shippingCost, null,
        shipRestrictions);
  }

  /**
   * The <b>createMerchantCalculatedShipping</b> method creates a
   * {@see MerchantCalculatedShipping} object, which contains information about
   * a shipping method that is available for the order, and adds that object
   * to the list of the order's shipping options.
   * @param shippingName Required. A string that can be used to identify a
   * particular shipping option, such as &quot;UPS Ground&quot;
   * @param shippingCost Required. The numeric cost of the shipping option.
   * This value will only be used if the &lt;merchant-calculation-callback&gt;
   * request fails.
   * @param currency A three-letter ISO 4217 currency code.
   * @param shipRestrictions Optional. A ShippingRestrictions object, which
   * identifies the areas where a shipping option is either available or
   * unavailable.
   * @return ShippingMethods object
   */
  public ShippingMethods createMerchantShipping(String shippingName,
      float shippingCost, String currency, ShippingRestrictions shipRestrictions)
      throws ProtocolException {
    MerchantCalculatedShipping merchantCal
        = _objectFact.createMerchantCalculatedShipping();
    merchantCal.setName(shippingName);
    merchantCal.setPrice(createMoney(shippingCost, currency));
    merchantCal.setShippingRestrictions(shipRestrictions);
    return addShippingMethod(merchantCal);
 

  /**
   * The <b>createPickup</b> method creates a {@see Pickup} object, which
   * contains information about a shipping method that is available for the
   * order, and adds that object to the list of the order's shipping options.
   * @param shippingName Required. A string that can be used to identify the
   * shipping option, such as &quot;Instore Pickup&quot;
   * @param shippingCost Required. The numeric cost of the shipping/handling
   * option.
   * @return ShippingMethods object
   */
  public ShippingMethods createPickup(String shippingName, float shippingCost)
      throws ProtocolException {
    return createPickup(shippingName, shippingCost, null);
  }
 
  /**
   * The <b>createPickup</b> method creates a {@see Pickup} object, which
   * contains information about a shipping method that is available for the
   * order, and adds that object to the list of the order's shipping options.
   * @param shippingName Required. A string that can be used to identify the
   * shipping option, such as &quot;Instore Pickup&quot;
   * @param shippingCost Required. The numeric cost of the shipping/handling
   * option.
   * @param currency A three-letter ISO 4217 currency code.
   * @return ShippingMethods object
   */
  public ShippingMethods createPickup(String shippingName, float shippingCost,
      String currency) throws ProtocolException {
    Pickup pickup = _objectFact.createPickup();
    pickup.setName(shippingName);
    pickup.setPrice(createMoney(shippingCost, currency));
    return addShippingMethod(pickup);
  }
 
  /**
   * The <b>createStateArea</b> method creates a {@see USStateArea} object
   * @param state Required. A two-letter U.S. state abbreviation
   * @throws ProtocolException if no state is provided
   * @return {@see USStateArea} object
   */
  public USStateArea createStateArea(String state)
      throws ProtocolException {
    if (StringUtil.isEmpty(state)) {
      throw new ProtocolException("invalid state: " + state);
    }
   
    USStateArea stateArea = _objectFact.createUSStateArea();
    stateArea.setState(state.toUpperCase());
    return stateArea;
  }

  /**
   * The <b>createZipArea</b> method creates a {@see USZipArea} object
   * @param zip Required. A five-digit U.S. zip code or a zip code pattern
   * @throws ProtocolException if no zip code or zip code pattern is provided
   * @return {@see USZipArea} object
   */
  public USZipArea createZipArea(String zip)
      throws ProtocolException {
    if (StringUtil.isEmpty(zip)) {
      throw new ProtocolException("invalid zip: " + zip);
    }
       
    USZipArea zipArea = _objectFact.createUSZipArea();
    zipArea.setZipPattern(zip);
    return zipArea;
  }
 
  /**
   * The <b>createCountryArea</b> method creates a {@see USCountryArea} object
   * @param usArea Required. A U.S. country area. Valid values are
   * CONTINENTAL_48, FULL_50_STATES, and ALL. Also see {@see USAreas}
   * @return {@see USCountryArea} object
   * @throws ProtocolException if no USArea is provided
   */
  public USCountryArea createCountryArea(USAreas usArea)
      throws ProtocolException {
    if (usArea == null) {
      throw new ProtocolException("invalid USArea: " + usArea);
    }
   
    USCountryArea countryArea = _objectFact.createUSCountryArea();
    countryArea.setCountryArea(usArea);
    return countryArea;
  }

  /**
   * The <b>createDefaultTaxRule</b> method constructs a DefaultTaxRule object,
   * which contains information about a default rule that will be used to
   * calculate taxes for an order. This method creates a default tax rule
   * for a U.S. state. After creating all of the default tax rules for an
   * order, call the createDefaultTaxTable method.
   * @param taxState Required. This parameter identifies the state where
   * a tax rule should be applied. Also see {@see #createStateArea(String)}
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @param isShippingTaxable Required. This parameter contains a Boolean
   * value indicating whether tax is applied to the shipping cost
   * @return {@see DefaultTaxRule} object
   */
  public DefaultTaxRule createDefaultTaxRule(USStateArea taxState,
      float taxRate, boolean isShippingTaxable) throws ProtocolException {
    DefaultTaxRule.TaxArea taxArea = _objectFact.createDefaultTaxRuleTaxArea();
    taxArea.setUsStateArea(taxState);
    return createDefaultTaxRule(taxArea, taxRate, isShippingTaxable);
  }
 
  /**
   * The <b>createDefaultTaxRule</b> method constructs a DefaultTaxRule object,
   * which contains information about a default rule that will be used to
   * calculate taxes for an order. This method creates a default tax rule
   * for a U.S. zip code or zip code pattern. After creating all of the
   * default tax rules for an order, call the createDefaultTaxTable method.
   * @param taxZip Required. This parameter identifies the zip code or set of
   * zip codes (using a zip code pattern) where a tax rule should be applied.
   * Also see {@see #createZipArea(String)}
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @param isShippingTaxable Required. This parameter contains a Boolean
   * value indicating whether tax is applied to the shipping cost
   * @return {@see DefaultTaxRule} object
   */
  public DefaultTaxRule createDefaultTaxRule(USZipArea taxZip, float taxRate,
      boolean isShippingTaxable) throws ProtocolException {
    DefaultTaxRule.TaxArea taxArea = _objectFact.createDefaultTaxRuleTaxArea();
    taxArea.setUsZipArea(taxZip);
    return createDefaultTaxRule(taxArea, taxRate, isShippingTaxable);
  }
 
  /**
   * The <b>createDefaultTaxRule</b> method constructs a DefaultTaxRule object,
   * which contains information about a default rule that will be used to
   * calculate taxes for an order. This method creates a default tax rule
   * for a U.S. country area. After creating all of the default tax rules
   * for an order, call the createDefaultTaxTable method.
   * @param taxCountryArea Required. This parameter identifies the country
   * area where a tax rule should be applied. Also see
   * {@see #createCountryArea(String)}
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @param isShippingTaxable Required. This parameter contains a Boolean
   * value indicating whether tax is applied to the shipping cost
   * @return {@see DefaultTaxRule} object
   */
  public DefaultTaxRule createDefaultTaxRule(USCountryArea taxCountryArea,
      float taxRate, boolean isShippingTaxable) throws ProtocolException {
    DefaultTaxRule.TaxArea taxArea = _objectFact.createDefaultTaxRuleTaxArea();
    taxArea.setUsCountryArea(taxCountryArea);
    return createDefaultTaxRule(taxArea, taxRate, isShippingTaxable);
  }
 
  /**
   * This method creates and returns a DefaultTaxRule object to one of the
   * other three createDefaultTaxRule() methods. This method handles all
   * tax areas (country area, state, zip).
   * @param DefaultTaxRule.TaxArea Required. This parameter identifies the
   * country area, state or zip code where a tax rule should be applied.
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @param isShippingTaxable Required. This parameter contains a Boolean
   * value indicating whether tax is applied to the shipping cost
   * @return {@see DefaultTaxRule} object
   */
  private DefaultTaxRule createDefaultTaxRule(DefaultTaxRule.TaxArea taxArea,
      float taxRate, boolean isShippingTaxable) {
    DefaultTaxRule taxRule = _objectFact.createDefaultTaxRule();
    taxRule.setRate(taxRate);
    taxRule.setShippingTaxed(isShippingTaxable);
    taxRule.setTaxArea(taxArea);
    return taxRule;
  }

  /**
   * The <b>addDefaultTaxRule</b> method adds a {@see DefaultTaxRule} to an
   * existing {@see DefaultTaxTable}.
   * @param taxTable This parameter contains the DefaultTaxTable to which
   * the DefaultTaxRule should be added.
   * @param rule This parameter contains the DefaultTaxRule that should be
   * added to the DefaultTaxTable
   * @return DefaultTaxTable object
   */
  public DefaultTaxTable addDefaultTaxRule(DefaultTaxTable taxTable,
      DefaultTaxRule rule) throws ProtocolException {
    DefaultTaxTable returnTaxTable = taxTable;
    if (returnTaxTable == null) {
      returnTaxTable = _objectFact.createDefaultTaxTable();
    }
   
    TaxRules taxRules = _objectFact.createDefaultTaxTableTaxRules();
    List<DefaultTaxRule> taxRuleList = taxRules.getDefaultTaxRule();
    if (!taxRuleList.contains(rule) && rule != null) {
      taxRuleList.add(rule);
    }
   
    returnTaxTable.setTaxRules(taxRules);
    return returnTaxTable;
  }

  /**
   * The <b>createDefaultTaxTable</b> method constructs an object that contains
   * a list of DefaultTaxRule objects.
   * @param ruleList Required. The list of {@see DefaultTaxRule} objects that
   * apply to the order.
   * @return DefaultTaxTable object
   * @throws ProtocolException if the ruleList parameter is null.
   */
  public DefaultTaxTable createDefaultTaxTable(List<DefaultTaxRule> ruleList)
      throws ProtocolException {
    if (ruleList == null) {
      throw new ProtocolException("ruleList cannot be null");
    }
   
    TaxRules taxRules = _objectFact.createDefaultTaxTableTaxRules();
    List<DefaultTaxRule> taxRuleList = taxRules.getDefaultTaxRule();
    for (DefaultTaxRule rule : ruleList) {
      taxRuleList.add(rule);
    }
   
    DefaultTaxTable taxTable = _objectFact.createDefaultTaxTable();
    taxTable.setTaxRules(taxRules);
    return taxTable;
  }
 
  /**
   * The <b>createMerchantCalculations</b> method constructs a MerchantCalculations
   * object, object, which contains the information that would be contained
   * within the &lt;merchant-calculations&gt; XML block of a Checkout API
   * request. This information includes an indication of whether the merchant
   * accepts coupons and/or gift certificates and, if so, the URL to which
   * Google Checkout should send the &lt;merchant-calculation-callback&gt;.
   * @param acceptCoupon Required. This value should be set to true if
   * Google Checkout should allow the customer to enter merchant coupon codes
   * @param acceptGiftCerts Required. This value should be set to true if
   * Google Checkout should allow the customer to enter gift certificate codes
   * @param merchantCalculateUrl Required. The URL to which Google Checkout
   * should send &lt;merchant-calculation-callback&gt; requests.
   */
  public MerchantCalculations createMerchantCalculations(boolean acceptCoupon,
      boolean acceptGiftCerts, String merchantCalculateUrl)
      throws ProtocolException {
    MerchantCalculations mCal = _objectFact.createMerchantCalculations();
    mCal.setAcceptGiftCertificates(acceptGiftCerts);
    mCal.setAcceptMerchantCoupons(acceptCoupon);
    mCal.setMerchantCalculationsUrl(merchantCalculateUrl);
    return mCal;
  }
 
  /**
   * The <b>createAlternateTaxRule</b> method constructs an AlternateTaxRule
   * object, which contains information about a rule that will be used to
   * calculate taxes for particular types of items in an order. This method
   * creates an alternate tax rule for a U.S. state.
   * @param taxState Required. This parameter identifies the state where
   * the tax rule could be applied. Also see {@see #createStateArea(String)}
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @return {@see AlternateTaxRule} object
   */
  public AlternateTaxRule createAlternateTaxRule(USStateArea state,
      float taxRate)
      throws ProtocolException {
    AlternateTaxRule.TaxArea taxArea
        = _objectFact.createAlternateTaxRuleTaxArea();
    taxArea.setUsStateArea(state);
    return createAlternateTaxRule(taxArea, taxRate);
  }

  /**
   * The <b>createAlternateTaxRule</b> method constructs an AlternateTaxRule
   * object, which contains information about an alternate rule that will be
   * used to calculate taxes for particular types of items in an order. This
   * method creates an alternate tax rule for a U.S. zip code or zip code
   * pattern.
   * @param taxZip Required. This parameter identifies the zip code or set of
   * zip codes (using a zip code pattern) where a tax rule could be applied.
   * Also see {@see #createZipArea(String)}
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @return {@see AlternateTaxRule} object
   */
  public AlternateTaxRule createAlternateTaxRule(USZipArea zip,
      float taxRate)
      throws ProtocolException {
    AlternateTaxRule.TaxArea taxArea
        = _objectFact.createAlternateTaxRuleTaxArea();
    taxArea.setUsZipArea(zip);
    return createAlternateTaxRule(taxArea, taxRate);
  }

  /**
   * The <b>createAlternateTaxRule</b> method constructs an AlternateTaxRule
   * object, which contains information about an alternate rule that will be
   * used to calculate taxes for an order. This method creates an alternate
   * tax rule for a U.S. country area.
   * @param taxCountryArea Required. This parameter identifies the country
   * area where a tax rule could be applied. Also see
   * {@see #createCountryArea(String)}
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @return {@see AlternateTaxRule} object
   */
  public AlternateTaxRule createAlternateTaxRule(USCountryArea area,
      float taxRate)
      throws ProtocolException {
    AlternateTaxRule.TaxArea taxArea
        = _objectFact.createAlternateTaxRuleTaxArea();
    taxArea.setUsCountryArea(area);
    return createAlternateTaxRule(taxArea, taxRate);
  }
 
  /**
   * This method creates and returns an AlternateTaxRule object to one of the
   * other three createAlternateTaxRule() methods. This method handles all
   * tax areas (country area, state, zip).
   * @param AlternateTaxRule.TaxArea Required. This parameter identifies the
   * country area, state or zip code where a tax rule could be applied.
   * @param taxRate Required. This parameter identifies the tax rate that
   * should be assessed. The value should be a non-negative decimal value
   * between 0 and 1.
   * @return {@see DefaultTaxRule} object
   */
  private AlternateTaxRule createAlternateTaxRule(
      AlternateTaxRule.TaxArea taxArea, float taxRate) {
    AlternateTaxRule aTaxRule = _objectFact.createAlternateTaxRule();
    aTaxRule.setRate(taxRate);
    aTaxRule.setTaxArea(taxArea);
    return aTaxRule;
  }
 
  /**
   * The <b>addAlternateTaxRule</b> method adds an {@see AlternateTaxRule} to an
   * existing {@see AlternateTaxTable}.
   * @param taxTable This parameter contains the AlternateTaxTable to which
   * the AlternateTaxRule should be added.
   * @param rule This parameter contains the AlternateTaxRule that should be
   * added to the AlternateTaxTable
   * @return AlternateTaxTable object
   */
  public AlternateTaxTable addAlternateTaxRule(AlternateTaxTable taxTable,
      AlternateTaxRule rule) throws ProtocolException {
    AlternateTaxTable returnTaxTable = taxTable;
    if (returnTaxTable == null) {
      returnTaxTable = _objectFact.createAlternateTaxTable();
    }
   
    AlternateTaxRules aRules
        = _objectFact.createAlternateTaxTableAlternateTaxRules();
    List<AlternateTaxRule> taxRuleList = aRules.getAlternateTaxRule();
    if (!taxRuleList.contains(rule) && rule != null) {
      taxRuleList.add(rule);
    }

    returnTaxTable.setAlternateTaxRules(aRules);
    return returnTaxTable;
  }
 
  /**
   * The <b>createAlternateTaxTable</b> method constructs an object that
   * contains a list of AlternateTaxRule objects. The
   * {@see AlternateTaxTable} is identified by a unique name, such as
   * &quot;food&quot; or &quot;services&quot;
   * @param rules Required. The list of {@see AlternateTaxRule} objects that
   * apply to the order.
   * @param taxTableName Required. A name that identifies the set of tax rules.
   * @param isTaxRulesStandAlone Required. A Boolean value that indicates how
   * taxes should be calculated if there is no matching {@see AlternateTaxRule}
   * for the country area, state or zip code where the customer is located.
   * @return AlternateTaxTable object
   * @throws ProtocolException if the rules parameter is null.
   */
  public AlternateTaxTable createAlternateTaxTable(List<AlternateTaxRule> rules,
      String taxTableName, boolean isTaxRulesStandAlone)
      throws ProtocolException {
    if (rules == null) {
      throw new ProtocolException("rules cannot be null");
    }
   
    AlternateTaxRules taxRules
        = _objectFact.createAlternateTaxTableAlternateTaxRules();
    List<AlternateTaxRule> ruleList = taxRules.getAlternateTaxRule();
    for (AlternateTaxRule rule : rules) {
      ruleList.add(rule);
    }
   
    AlternateTaxTable aTaxTable = _objectFact.createAlternateTaxTable();
    aTaxTable.setAlternateTaxRules(taxRules);
    aTaxTable.setName(taxTableName);
    aTaxTable.setStandalone(isTaxRulesStandAlone);
    return aTaxTable;
  }
}
TOP

Related Classes of com.google.checkout.sample.protocol.CheckoutCartBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.