PHP SDK Example: Query Auto Order for Customer Profile and Update Credit Card Information
This content is archived.
Make sure to read all the comments and TODO: statements within this code sample. For this to work you'll have to use hosted fields to collect the new card number and provide the token.
<?php
require_once __DIR__ . '/vendor/autoload.php';
// Configure API key authorization: ultraCartSimpleApiKey
ultracart\v2\Configuration::getDefaultConfiguration()->setApiKey('x-ultracart-simple-key', 'YOUR SIMPLE KEY HERE'); // TODO: Put your simple 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]);
$auto_order_api_instance = new ultracart\v2\api\AutoOrderApi(
$client,
ultracart\v2\Configuration::getDefaultConfiguration(),
new ultracart\v2\HeaderSelector("2016-10-01")
);
$order_api_instance = new ultracart\v2\Api\OrderApi(
$client,
ultracart\v2\Configuration::getDefaultConfiguration(),
new ultracart\v2\HeaderSelector("2016-10-01")
);
$_limit = 1; // Limit it to the one order
$_offset = 0; // int | Pagination of the record set. Offset is a zero based index.
$_sort = "-creation_dts"; // Get the newest order possible
$_expand = "original_order.payment"; // Expand the original order payment object as well as the base auto order record
$auto_order_query = new \ultracart\v2\models\AutoOrderQuery();
$auto_order_query->setCustomerProfileOid(123456); // TODO: Set this to the customer profile OID
echo 'Pulling offset ' . $_offset . PHP_EOL;
try {
$result = $auto_order_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];
echo $auto_order->getOriginalOrderId() . " - " . $auto_order->getAutoOrderCode() . PHP_EOL;
// Update the payment information on the original order which is what auto orders rebill off of.
$auto_order->getOriginalOrder()->getPayment()->getCreditCard()->setCardExpirationMonth(12); // TODO: Set this to the new expiration month
$auto_order->getOriginalOrder()->getPayment()->getCreditCard()->setCardExpirationMonth(2020); // TODO: Set this to the new expiration year
$auto_order->getOriginalOrder()->getPayment()->getCreditCard()->setCardNumberToken("TOKEN_FOR_HOSTED_FIELDS_HERE"); // TODO: Set this to the token from the hosted field card
$order_api_instance->updateOrder($auto_order->getOriginalOrder(), $auto_order->getOriginalOrder()->getOrderId());
// Re-enable the auto order
$auto_order->setEnabled(true);
$auto_order->setCanceledDts(null);
$auto_order_api_instance->updateAutoOrder($auto_order, $auto_order->getAutoOrderOid());
echo 'Done updating the order and the auto order.';
// Since we just want to update the most recent auto order we'll stop looping
break;
}
} else {
echo 'Failed to retrieve orders' . PHP_EOL;
var_dump($result);
}
} catch (Exception $e) {
echo 'Exception when calling AutoOrderApi->getAutoOrders: ', $e->getMessage(), PHP_EOL;
}
?>
, multiple selections available,