PHP SDK Sample: Replacement Order

Sample Code
<?php

require_once __DIR__ . '/vendor/autoload.php';

// TODO: Replace YOUR KEY HERE with an Application API Key that has read & write permission to the order resource.

// Configure API key authorization: ultraCartSimpleApiKey
ultracart\v2\Configuration::getDefaultConfiguration()->setApiKey('x-ultracart-simple-key', 'YOUR KEY HERE');

// Disable SSL verification if we're having issues with this PHP install not having the proper CA installed.  Fix your CA for a production environment!
// Set debug to true if you need more information on request/response
$client = new GuzzleHttp\Client(['verify' => false, 'debug' => false]);

$api_instance = new ultracart\v2\api\OrderApi(
    $client,
    ultracart\v2\Configuration::getDefaultConfiguration(),
    new ultracart\v2\HeaderSelector("2016-10-01")
);

// TODO: Replace this with the order id you want to replace
$replacingOrderId = "DEMO-2019057896";

// Setup an array to contain the items that will be on the order
$items = array();

// TODO: Create each item and push it into the array.
$item = new \ultracart\v2\models\OrderReplacementItem();
$item->setMerchantItemId("SAMPLE");
$item->setQuantity(1);

array_push($items, $item);

// Setup the overall order replacement object.
$orderReplacement = new ultracart\v2\models\OrderReplacement();
// TODO: Decide if the replacement is free or if there is a cost that needs to be charged to the customer
$orderReplacement->setFree(false);
$orderReplacement->setImmediateCharge(false);
$orderReplacement->setItems($items);
// You can store values in the custom fields such as the original order id for example
$orderReplacement->setCustomField7($replacingOrderId);

// Perform the replacement
$result = $api_instance->replacement($replacingOrderId, $orderReplacement);

// Dump the result for debug
var_dump($result);

// If the order was created, then let's grab the full order object
if ($result->getSuccessful()) {
    $order = $api_instance->getOrder($result->getOrderId(), "all");

    // Dump the result for debug
    var_dump($order);

    // Generate a text formatted version of the order for display.
    $formatOptions = new \ultracart\v2\models\OrderFormat();
    $formatOptions->setFormat("text");

    $formattedOrder = $api_instance->format($result->getOrderId(), $formatOptions);

    echo $formattedOrder->getFormattedResult();
}

?>