Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
breakoutModefull-width
languagephp
<?php

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

// 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\Apiapi\AutoOrderApi(
    $client,
    ultracart\v2\Configuration::getDefaultConfiguration(),
    new ultracart\v2\HeaderSelector("2016-10-01")
);

$_limit = 200; // int | The maximum number of records to return on this one API call. (Maximum 200)
$_offset = 0; // int | Pagination of the record set.  Offset is a zero based index.
$_sort = "order_id"; // string | The sort order of the orders.  See Sorting documentation for examples of using multiple values and sorting by ascending and descending.
$_expand = "items,items.simple_schedule,original_order"; // string | The object expansion to perform on the result.

$auto_order_query = new \ultracart\v2\models\AutoOrderQuery();

for ($_offset = 0; ; $_offset += $_limit) {
    echo 'Pulling offset ' . $_offset . PHP_EOL;
    try {
        $result = $api_instance->getAutoOrdersByQuery($auto_order_query, $_limit, $_offset, $_sort, $_expand);

        if ($result->getSuccess()) {
            echo 'Retrieved ' . count($result->getAutoOrders()) . PHP_EOL;

            for ($i = 0; $i < count($result->getAutoOrders()); $i++) {

                $auto_order = $result->getAutoOrders()[$i];


                // Put your code here to process each of the orders that came back
                for ($j = 0; $j < count($auto_order->getItems()); $j++) {
                    $auto_order_item = $auto_order->getItems()[$j];
                    if ($auto_order_item->getSimpleSchedule() != null && $auto_order_item->getSimpleSchedule()->getRepeatCount() != null) {

                        $total_payments_required = $auto_order_item->getSimpleSchedule()->getRepeatCount() + 1; // Plus one for the original order
                        $total_payments_completed = $auto_order_item->getNumberOfRebills() + 1; // Plus one for the original order
                        $total_revenue_collected = $auto_order_item->getLifeTimeValue();
                        $frequency = $auto_order_item->getSimpleSchedule()->getFrequency();
                        $start_dts = $auto_order->getOriginalOrder()->getCreationDts();
                        $end_dts = null;
                        $active = $auto_order->getEnabled();
                        $next_charge = null;
                        $original_order_refunded = $auto_order->getOriginalOrder()->getRefundDts() != null;
                        $failed_card_attempts = $auto_order->getCreditCardAttempt();
                        if (!$auto_order->getEnabled()) {
                            if ($auto_order->getCanceledDts() != null) {
                                $end_dts = $auto_order->getCanceledDts();
                            }
                            if ($auto_order->getCanceledDts() != null) {
                                $end_dts = $auto_order->getDisabledDts();
                            }
                        } else {
                            $next_charge = $auto_order_item->getNextShipmentDts();
                        }

                        echo $auto_order->getOriginalOrderId() . " - " . $auto_order->getAutoOrderCode() . PHP_EOL;
                        echo "\t Total Payments Required: " . $total_payments_required . PHP_EOL;
                        echo "\tTotal Payments Completed: " . $total_payments_completed . PHP_EOL;
                        echo "\t Total Revenue Collected: " . $total_revenue_collected . PHP_EOL;
                        echo "\t               Frequency: " . $frequency . PHP_EOL;
                        echo "\t               Start Dts: " . $start_dts . PHP_EOL;
                        echo "\t             Next Charge: " . $next_charge . PHP_EOL;
                        echo "\t                End Date: " . $end_dts . PHP_EOL;
                        echo "\t                  Active: " . ($active ? "Yes" : "No") . PHP_EOL;
                        echo "\t                Refunded: " . ($original_order_refunded ? "Yes" : "No") . PHP_EOL;
                        echo "\t    Failed Card Attempts: " . $failed_card_attempts . PHP_EOL;
                        echo PHP_EOL;

                        //var_dump($auto_order_item);
                    }
                }
            }

            // Are we done?
            if (count($result->getAutoOrders()) < $_limit) {
                echo 'Finished' . PHP_EOL;
                break;
            }
        } else {
            echo 'Failed to retrieve orders' . PHP_EOL;
            var_dump($result);
            break;
        }
    } catch (Exception $e) {
        echo 'Exception when calling AutoOrderApi->getAutoOrders: ', $e->getMessage(), PHP_EOL;
    }
}

?>