Colorado Timberline APIA Practical Guide to Successful Integration

Order Submission

<?php
// -------------------------------
// bootstrap the ct client library
// -------------------------------
$sDir = '../' . dirname(__FILE__);
ini_set('include_path', ini_get('include_path') . ":$sDir");
require('lib/core/autoload.php');

// --------------------------------------------
// note about api client configuration
// --------------------------------------------
// select appropriate environment; this will
// impact the affiliate id and url which is
// used for order submissions in the future,
// there may be additional parameters
// associated w/ an environemnt
// --------------------------------------------
CtApiConfig::setEnv(CtApiConfig::STAGING);

//----------------------------------------------------
// create CtOrder, parameterize the scalar values
//----------------------------------------------------
$oCtOrder = new CtOrder();
$oCtOrder->setPoNumber(13559);
$oCtOrder->setShipToName('Marissa Ann Jones');
//----------------------------------------------------
// ex. calling
//  echo $oCtOrder->getShipToName();
// here would produce 'Marissa Ann Jones'
//----------------------------------------------------
$oCtOrder->setAddressLine1('28 Sunset Drive');
$oCtOrder->setCity('Burlington');
$oCtOrder->setCountry('US');
$oCtOrder->setState('MA');
$oCtOrder->setZip('01803');
$oCtOrder->setShipMethod('DHLSMARTMAIL');

// create CtPackingList, and add it to the order
$oCtPackingList = new CtPackingList();
$oCtPackingList->setOrderDate('10/22/2009');

// ----------------------------------------------------
// set CtPackingList on the CtOrder
// ----------------------------------------------------
// note about settter methods for complex children
// ----------------------------------------------------
// a CtApiDao setter for a complex child, is
// in contrast to an 'append' method.  the setters
// will create a new child if not currently present,
// or overwrite the existing value on a subsequent call
// ----------------------------------------------------
$oCtOrder->setPackingList($oCtPackingList);

// ----------------------------------------------------------
// create CtOrderItem, parameterize the scalar values
// ----------------------------------------------------------
// notice, in building the first CtOrderItem, we create it,
// append it to the CtOrder, then later create a CtImage,
// parameterize that, and append it to the CtOrderItem.
// the flow is a little different for the second CtOrderItem
// ----------------------------------------------------------
$oCtOrderItem = new CtOrderItem();
$oCtOrderItem->setQuantity(1);
$oCtOrderItem->setItemId('132712');
$oCtOrderItem->setPartNumber('97894m');
$oCtOrderItem->setPrintMode('light');

// add CtOrderItems to the CtOrder
// (repeat for additional order items)
$oCtOrder->appendOrderItem($oCtOrderItem);

// ---------------------------------------------------------
// parameterize CtImage and append it to the OrderItem
// the Image objects map a url (you must supply an endpoint)
// to a location on the product.
// the product is part of the part number, set earlier
// ---------------------------------------------------------
$oCtImage = new CtImage();
$oCtImage->setUrl('http://yourdomain.com/images/shirtImage1.png');
$oCtImage->setLocation('front');

$oCtOrderItem->appendImage($oCtImage);

// ----------------------------------------------------------
// here we create an additional OrderItem
// ----------------------------------------------------------
// when building this CtOrderItem, we create & parameterize
// it, then create a CtImage and append it to the CtOrderItem
// lastly, the CtOrderItem is appended to the CtOrder
// ----------------------------------------------------------
$oCtOrderItem2 = new CtOrderItem();
$oCtOrderItem2->setQuantity(2);
$oCtOrderItem2->setItemId('132713');
$oCtOrderItem2->setPartNumber('97894x');
$oCtOrderItem2->setPrintMode('dark');

$oCtImage2 = new CtImage();
$oCtImage2->setUrl('http://yourdomain.com/images/shirtImage2.png');
$oCtImage2->setLocation('back');

// -----------------------------------------------------------
// CtApiDao methods that have an 'append' prefix indicate
// -----------------------------------------------------------
// note about append methods for complex children
// -----------------------------------------------------------
// a number of nodes specified by the type hint in the
// method signature can be appended (as opposed to setters)
// for example, the signature for CtOrderItem::appendImage
//      public function appendImage(CtImage $oCtImage)
// indicates many CtImage objects can be added to an OrderItem
// -----------------------------------------------------------
$oCtOrderItem2->appendImage($oCtImage2);

$oCtOrder->appendOrderItem($oCtOrderItem2);

// -------------------
// Submit the order !!
// -------------------
echo '-----------------' . PHP_EOL;
echo 'SUBMITTING ORDER:' . PHP_EOL;
echo '-----------------' . PHP_EOL;

// output raw xml rendered by the client library
echo $oCtOrder . PHP_EOL;

// submit the order
try
{
    $oOrderRsp = CtApiFactory::createGateway('Order')->connect()->submit($oCtOrder);
}
catch(CtApiException $e)
{
    echo 'There was a problem submitting the order!' . PHP_EOL;
    echo 'Error Code: ' . $e->getCode() . PHP_EOL;
    die('Error Message: ' . $e->getMessage() . PHP_EOL);
}

// evaluate success of order submission
$sErrMsg  = $oOrderRsp->wasError() ? 'yes' : 'no';
echo '---------' . PHP_EOL;
echo 'RESPONSE:' . PHP_EOL;
echo '---------' . PHP_EOL;
echo $oOrderRsp . PHP_EOL;
echo 'Error in submission? ' . $sErrMsg . PHP_EOL;