Skip to main content
Published: March 17 2011, 4:39:00 PMUpdated: November 28 2020, 1:54:52 PM

This Java SDK sample demonstrates how to retrieve Sales order using GetOrders API call with order creation time filter described in the KB Order management using Trading API - GetOrders.

/*
© 2011-2013 eBay Inc., All Rights Reserved
Licensed under CDDL 1.0 - http://opensource.org/licenses/cddl1.php
*/

import com.ebay.sdk.*;
import com.ebay.sdk.call.GetOrdersCall;
import com.ebay.soap.eBLBaseComponents.*;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Logger;

/**
* 1. Create an ApiContext Object
* 2. Set the auth token and target api url (Webservice endpoint)
* 3. Create a GetOrders object.
* 4. Set the CreateTimeFrom  and CreateTimeTo filter (just an example)
* 5. Set the DetailLevel flag to ReturnAll
* 6. Call GetOrdersCall.getOrders() to retrieve the orders
*/
public class AppGetOrders {

    final static Logger logger = Logger.getLogger(AppGetOrders.class);

    public static void getOrders() throws ApiException, SdkException, Exception {


        ApiContext apiContext = new ApiContext();
        ApiCredential cred = apiContext.getApiCredential();
        //Set Auth Token and server URL for sandbox
        cred.seteBayToken("YOUR TOKEN");
        apiContext.setApiServerUrl("https://api.sandbox.ebay.com/wsapi");
        ApiLogging apiLogging = new ApiLogging();
        apiContext.setApiLogging(apiLogging);        

        // Set the site based on the seller - for UK sellers, set it to SiteCodeType.UK
        // for US, set to SiteCodeType.US
        apiContext.setSite(SiteCodeType.US); 

        // set detail level to ReturnAll
        DetailLevelCodeType[] detailLevels = new DetailLevelCodeType[]{DetailLevelCodeType.RETURN_ALL};

        // define CreateTimeFrom and CreateTimeTo filters
        // set "To" to the current time 
        Calendar to = new GregorianCalendar(TimeZone.getTimeZone("GMT")).getInstance();         
        Calendar from = (GregorianCalendar) to.clone();

        // In this example we set "From" to 30 mins from current time - you need to set it to the last time you made the call
        from.add(to.MINUTE,-30);        

        GetOrdersCall getOrders = new GetOrdersCall(apiContext);
        getOrders.setDetailLevel(detailLevels);
        getOrders.setCreateTimeFrom(from);
        getOrders.setCreateTimeTo(to);
        
        OrderType[] orders = getOrders.getOrders();
        outputOrderInfo (orders);
    }

    private static void outputOrderInfo(OrderType[] orders) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
        if (orders ==null ){
              logger.info(" No order found ");
            return;
        }
        for (OrderType order : orders) {
            logger.info("");
            logger.info("Order Information ");

            // get the OrderID
            logger.info("  Order  -> OrderID: " + order.getOrderID());
            CheckoutStatusType checkoutStatus = order.getCheckoutStatus();
            CompleteStatusCodeType completeStatus = checkoutStatus.getStatus();
            logger.info("  Order  -> Checkout Status: " + completeStatus.toString());
           
            // if checkout is complete, then the order is ready for fulfillment
            if (completeStatus.equals(CompleteStatusCodeType.COMPLETE)) {

                // get the amount paid
                logger.info(" Order  -> AmountPaid: " + order.getAmountPaid().getValue() + " " + order.getAmountPaid().getCurrencyID());
                
                // get the payment method 
                BuyerPaymentMethodCodeType paymentMethod = order.getCheckoutStatus().getPaymentMethod();
                logger.info("  Order  -> Payment Method : " + paymentMethod.value());

                // get the checkout message left by the buyer, if any
                String buyerCheckoutMsg = order.getBuyerCheckoutMessage();
                if (buyerCheckoutMsg != null) {
                    logger.info("  Order  -> Buyer CheckoutMsg : " + buyerCheckoutMsg);
                }

                // get the sales tax, if any 
                double salesTax = order.getShippingDetails().getSalesTax().getSalesTaxAmount() == null ? 0.0 : order.getShippingDetails().getSalesTax().getSalesTaxAmount().getValue();
                logger.info("  Order -> SalesTax " + salesTax);

                // get the external transaction information - if payment is made via PayPal, then this is the PayPal transaction info
                ExternalTransactionType[] externalTransactions = order.getExternalTransaction();
                for (ExternalTransactionType et : externalTransactions) {
                    logger.info("  Order -> External TransactionID  : " + et.getExternalTransactionID());
                    logger.info("  Order -> External Transaction Time  : " + dateFormatter.format(et.getExternalTransactionTime().getTime()));
                    logger.info("  Order -> External FeeOrCreditAmount  : " + et.getFeeOrCreditAmount().getValue() + " " + et.getFeeOrCreditAmount().getCurrencyID());
                    logger.info("  Order -> External Transaction PaymentOrRefundAmount   : " + et.getPaymentOrRefundAmount().getValue() + " " + et.getPaymentOrRefundAmount().getCurrencyID());
                }

                // get the shipping service selected by the buyer
                logger.info("  Order -> Shipping Service Selected  : " + order.getShippingServiceSelected().getShippingService());
                logger.info("  Order -> Shipping Service Selected ShippingServiceCost  : " + order.getShippingServiceSelected().getShippingServiceCost().getValue() + " " + order.getShippingServiceSelected().getShippingServiceCost().getCurrencyID());

                // get the buyer's shipping address 
                AddressType shippingAddress = order.getShippingAddress();
                StringBuffer address = new StringBuffer();
                address.append(shippingAddress.getName());
                if (shippingAddress.getStreet() != null) {
                    address.append(",").append(shippingAddress.getStreet());
                }
                if (shippingAddress.getStreet1() != null) {
                    address.append(",").append(shippingAddress.getStreet1());
                }
                if (shippingAddress.getStreet2() != null) {
                    address.append(",").append(shippingAddress.getStreet2());
                }
                if (shippingAddress.getCityName() != null) {
                    address.append(",").append(
                            shippingAddress.getCityName());
                }
                if (shippingAddress.getStateOrProvince() != null) {
                    address.append(",").append(
                            shippingAddress.getStateOrProvince());
                }
                if (shippingAddress.getPostalCode() != null) {
                    address.append(",").append(
                            shippingAddress.getPostalCode());
                }
                if (shippingAddress.getCountryName() != null) {
                    address.append(",").append(
                            shippingAddress.getCountryName());
                }
                if (shippingAddress.getPhone() != null) {
                    address.append(",").append(shippingAddress.getPhone());
                }

                logger.info("  Order -> Shipping Address: " + address);

                TransactionArrayType transAryType = order.getTransactionArray();
                TransactionType[] transArry = transAryType.getTransaction();
                logger.info("  Order -> Transaction Arry  ");

                 // iterate through each transaction for the order
                for (TransactionType tran : transArry) {

                    // get the OrderLineItemID, Quantity, buyer's email and SKU
                    logger.info("   Transaction -> OrderLineItemID  : " + tran.getOrderLineItemID());
                    logger.info("   Transaction -> Quantity Purchased  : " + tran.getQuantityPurchased());
                    logger.info("   Transaction -> Buyer email  : " + tran.getBuyer().getEmail());
                    String sku = tran.getItem().getSKU();
                    if (sku != null) {
                        logger.info("   Transaction -> SKU  : " + sku);
                    }
                    System.out.println(" ");

                    // if the item is listed with variations, get the variation SKU
                    VariationType variation = tran.getVariation();
                    if (variation != null) {
                        logger.info("   Transaction ->  Variation SKU()" + variation.getSKU());
                    }
                }
            }
        }
    }
    
    public static void main(String[] args) {

        try {
            getOrders();
        } catch (ApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SdkException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
        


 Version Info

The code example above was based on the versions specified below:

Java SDK Version javasdk v705 release

How well did this answer your question?
Answers others found helpful