set the contract charges for the whole Order in amazon payments - Java AWS

Java examples for AWS:Payment

Description

set the contract charges for the whole Order in amazon payments

Demo Code

package com.amazon.samples;

/******************************************************************************* 
 *  Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  //from www  .j ava 2s  .  c  o m
 *  You may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 *  This file 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.
 * ***************************************************************************** 
 */

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import com.amazon.payments.CheckoutByAmazonServiceException;
import com.amazon.payments.CBAPurchaseContract;
import com.amazon.payments.CheckoutByAmazonServiceRequestException;
import com.amazon.payments.model.ContractCharges;
import com.amazon.payments.model.Promotion;
import com.amazon.payments.model.PromotionList;
import com.amazon.payments.model.PurchaseItem;

/**
 * 
 * OrderWithContractCharges class shows how you can set the contract charges for the whole Order. 
 * The Contract charges are Taxes, Shipment charges and Promotions for the whole Order.
 * If Taxes are set,Shipment charges also should be set and same vice versa.
 *
 */
class OrderWithContractCharges {

    public static void main(String... args) {
        //Set the Purchase Contract Id
        String purchaseContractId = "<<Enter Purchase Contract ID here>>";

        try {
            //Create an Object of create Order which contains the functions for setting Items, Contract Charges and Completing Order
            CBAPurchaseContract lib = new CBAPurchaseContract();

            //Create an Item object 
            PurchaseItem item = new PurchaseItem();

            /*Create an Item
             *Params are MerchantItemId, Title, UnitPriceAmount 
             */
            item.createItem("Item1", "Title1", 10);

            //create an Items list to hold all the items in the current Order
            List<PurchaseItem> items = new ArrayList<PurchaseItem>();

            //Add the item to Items list
            items.add(item);

            //Call the library function create Order to associate the Items to Amazon PurchaseContract Id
            boolean areItemsSet = lib.setItems(purchaseContractId, items);

            //Setting Contract Charges
            //Create a Charges object to set charges at contract level.
            ContractCharges chargesObj = new ContractCharges();

            //Set tax at order level
            chargesObj.setContractTax(1.5);

            //Set shipment charges at Order level
            chargesObj.setContractShippingCharges(2.5);

            //Create a promotion        
            List<Promotion> contractPromotions = new ArrayList<Promotion>();
            Promotion contractPromotionObj = new Promotion(
                    "ContractPromotion1", "ContractPromotionDesc", 2);
            contractPromotions.add(contractPromotionObj);
            PromotionList contractPromotionListObj = new PromotionList(
                    contractPromotions);

            //set Promotion at Order level
            chargesObj.setContractPromotions(contractPromotionListObj);

            //Call the Library function to associate the Contract charges to Amazon purchase contract id
            boolean areContractChargesSet = lib.setContractCharges(
                    purchaseContractId, chargesObj);

            //Check if the call to setItems and  setContract charges where successful
            if (areItemsSet && areContractChargesSet) {
                //Call completeOrder to complete the Order. This returns the list of Amazon Order Id's
                java.util.List<String> orderIdList = lib.completeOrder(
                        purchaseContractId, "A136CGJE3412",
                        "Test Cart Integrator");
                for (String orderId : orderIdList) {
                    System.out.print("OrderId : " + orderId);
                }
            }
        }
        //Catch the internal exceptions
        catch (CheckoutByAmazonServiceException ex) {
            System.out.println("Caught Service Exception: "
                    + ex.getMessage());
            System.out.println("Response Status Code: "
                    + ex.getStatusCode());
            System.out.println("Error Code: " + ex.getErrorCode());
            System.out.println("Error Type: " + ex.getErrorType());
            System.out.println("Request ID: " + ex.getRequestId());
            System.out.print("XML: " + ex.getXML());

        }
        //Catch the request exceptions which will come when there is some error in the
        //inputs passed while calling the API
        catch (CheckoutByAmazonServiceRequestException ex) {
            System.out.println("Caught Request Exception: "
                    + ex.getMessage());
            System.out.println("Response Status Code: "
                    + ex.getStatusCode());
            System.out.println("Error Code: " + ex.getErrorCode());
            System.out.println("Error Type: " + ex.getErrorType());
            System.out.println("Request ID: " + ex.getRequestId());
            System.out.print("XML: " + ex.getXML());
        } catch (FileNotFoundException ex) {
            System.out.println("Caught Request Exception: "
                    + ex.getMessage());
        }
    }
}

Related Tutorials