set the basic Item attributes,which are MerchantItemId,Title and UnitPriceAmount in amazon payments - Java AWS

Java examples for AWS:Payment

Description

set the basic Item attributes,which are MerchantItemId,Title and UnitPriceAmount 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"); 
 *  //  ww w .  j a v a 2  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.PurchaseItem;

/**
 * OrderWithBasicAttributes class shows how we can set the basic Item attributes,which are MerchantItemId,Title and UnitPriceAmount.
 * After setting the items,it executes completeOrder() which returns you the amazon Order ID.
 */

class OrderWithBasicAttributes {

    public static void main(String... args) {
        //Set the Purchase Contract Id
        String purchaseContractId = "<<Enter the 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);

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

    }
}

Related Tutorials