set Taxes and promotions in amazon payment - Java AWS

Java examples for AWS:Payment

Description

set Taxes and promotions in amazon payment

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 . jav a2 s.  co 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.Promotion;
import com.amazon.payments.model.PromotionList;
import com.amazon.payments.model.PurchaseItem;

/**
 * OrderWithTaxPromotion class shows how we can set Taxes and promotions.
 * If Taxes are set, Shipment charges also should be set and same vice versa.
 * After setting the taxes and promotions,it executes completeOrder() which returns you the amazon Order ID.
 */

class OrderWithTaxPromotion {

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

        try {

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

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

            //Set the tax for the item
            item.setItemTax(2);

            //Set the shipping amount
            item.setItemShippingCharges(3);

            //Create a promotion        
            Promotion promotionObj = new Promotion("Promotion1",
                    "PromotionDesc", 1);

            //Create an array of promotions
            List<Promotion> promotions = new ArrayList<Promotion>();
            promotions.add(promotionObj);

            //Create a promotion list object. Pass the promotions array as parameter. Currently only one promotion is supported per item
            PromotionList promotionListObj = new PromotionList(promotions);

            //Call the library function create Order to associate the Items to Amazon PurchaseContract Id
            item.setItemPromotions(promotionListObj);

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

            items.add(item);

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

            //Call the Library function to associate the Contract charges to Amazon purchase contract id
            boolean areItemsSet = lib.setItems(purchaseContractId, items);

            //Check if the call to setItems was successful
            if (areItemsSet) {
                //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.println("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 the exceptions which will come when properties file is not found
        catch (FileNotFoundException ex) {
            System.out.println("Caught Request Exception: "
                    + ex.getMessage());
        }
    }
}

Related Tutorials