set all the Item attributes like MerchantItemId, Title , UnitPriceAmount etc in amazon payment - Java AWS

Java examples for AWS:Payment

Description

set all the Item attributes like MerchantItemId, Title , UnitPriceAmount etc 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   ww w  .j a v a2  s . c  om
 *  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.FulfillmentNetwork;
import com.amazon.payments.model.ProductType;
import com.amazon.payments.model.Promotion;
import com.amazon.payments.model.PromotionList;
import com.amazon.payments.model.PurchaseItem;
import com.amazon.payments.model.ShippingServiceLevel;

/**
 * OrderWithItemCharges class shows how we can set all the Item attributes like  MerchantItemId, Title , UnitPriceAmount etc.
 * You can set the attributes based on your requirements.
 * After setting the items,it executes completeOrder() which returns you the amazon Order ID.
 */

class OrderWithItemCharges {

    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", 100);

            //Set the required optional parameters

            //Setting the quantity 
            item.setQuantity(2);

            //Setting Description
            item.setDescription("ItemDesc1");

            //Setting SKU
            item.setSKU("ItemSku1");

            //Setting URL
            item.setURL("http://www.amazon.com");

            //Setting Category
            item.setCategory("Category1");

            //Setting Fulfillment network. It can be either AMAZON_NA or MERCHANT
            item.setFulfillmentNetwork(FulfillmentNetwork.AMAZON_NA);

            //Setting item custom data
            item.setItemCustomData("Custom Data1");

            //Setting Product type.Currenly it should be PHYSICAL
            item.setProductType(ProductType.PHYSICAL);

            //Set condition .  Possible values are Any, Club, Collectible, New, Refurbished
            item.setCondition("New");

            //Set Shipping label
            item.setShippingLabel("Shipping Label1");

            //Set Destionation name
            item.setDestinationName("#default");

            //Set shipping custom data
            item.setShippingCustomData("Shipping Custom Data1");

            //Set weight
            item.setWeight(5);

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

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

            //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);

            //Add the promotion list to the Item
            item.setItemPromotions(promotionListObj);

            //Create a Physical Item
            PurchaseItem item1 = new PurchaseItem();

            //Call create item. Parameters are MerchantItemId, Title, UnitPriceAmount, Shipping ServiceLevel 
            item1.createPhysicalItem("Item2", "Title2", 20,
                    ShippingServiceLevel.EXPEDITED);

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

            //Add all the items to the ItemsList
            items.add(item);
            items.add(item1);

            //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 create Order to associate the Items to Amazon PurchaseContract Id
            boolean areItemsSet = lib.setItems(purchaseContractId, items);

            //Check if the call to setItems and  setContract charges where 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, null, null);
                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