/** * Copy an existing RecurringOrder to a new date and mark the old one as Inactive. * @param int $orderId The ID of the RecurringOrder to replicate * @param string $newStartDate String date of when to start the new RecurringOrder * @param string $nextBillDate Optional, string date of when to set the NextBillingDate on the new RecurringOrder * @return bool|Infusionsoft_RecurringOrder The newly created RecurringOrder object, or false if there was a problem. */ public static function rescheduleRecurringOrder($orderId, $newStartDate, $nextBillDate = null) { // Attempt to load the order $order = Infusionsoft_DataService::query(new Infusionsoft_RecurringOrder(), array('Id' => $orderId)); $order = array_shift($order); if (count($order) == 0) { return false; } // Create a new recurring order based on existing data $newOrder = new Infusionsoft_RecurringOrder(); foreach ($order->getFields() as $field) { $newOrder->{$field} = $order->{$field}; } $startDate = date('Ymd\\TH:i:s', strtotime($newStartDate)); $nextBillDate = date('Ymd\\TH:i:s', strtotime($nextBillDate == null ? $newStartDate : $nextBillDate)); // Use an optional next bill date, otherwise use the start date $newOrder->Id = null; $newOrder->StartDate = $startDate; $newOrder->Status = 'Active'; $newOrder->ReasonStopped = 'This order will stop billing on the billing end date.'; $newOrder->LastBillDate = ''; $newOrder->PaidThruDate = ''; //NextBillDate is set farther down // We will look for properly named custom fields to automatically update references if (in_array('_OriginalSubscriptionId', $order->getFields())) { $newOrder->_OriginalSubscriptionId = strval($order->Id); } $newOrder->save(); // Wanted to limit to one save operation. Id is required for next custom field. if (in_array('_NewSubscriptionId', $order->getFields())) { $order->_NewSubscriptionId = strval($newOrder->Id); } // The new subscription has been saved. Make API call to change next bill date try { Infusionsoft_InvoiceService::updateJobRecurringNextBillDate($newOrder->Id, $nextBillDate); } catch (Exception $e) { CakeLog::write('error', "Problem updating next billing date on subscription. Id: {$newOrder->Id}, Date: {$startDate}, Error: " . $e->getMessage()); } // Attempt to create invoices for the subscription try { Infusionsoft_InvoiceService::createInvoiceForRecurring($newOrder->Id); } catch (Exception $e) { CakeLog::write('error', "Problem creating invoices for new subscription. Id: {$newOrder->Id}, Error: " . $e->getMessage()); } // Mark original subscription as inactive $order->Status = 'Inactive'; $order->ReasonStopped = "Subscription updated on " . date('m/d/Y') . " to new subscription. ID: {$newOrder->Id}"; $order->save(); return new Infusionsoft_RecurringOrder($newOrder->Id); }
<?php include '../infusionsoft.php'; include 'object_editor_all_tables.php'; include '../tests/testUtils.php'; if (isset($_POST['contactid'])) { $recurring_order_id = Infusionsoft_InvoiceService::addRecurringOrder($_POST['contactid'], true, $_POST['subscriptionplanid'], 0, 0.01, false, 0, 0, 0, 0); $recurringOrder = new Infusionsoft_RecurringOrder($recurring_order_id); $recurringOrder->StartDate = date('Y-m-d H:i:s', strtotime("-1 month")); $recurringOrder->PaidThruDate = date('Y-m-d H:i:s', strtotime("-1 month")); $recurringOrder->save(); echo "<h1>Subscription Created: {$recurring_order_id}</h1>"; } ?> <h1>Create Subscription</h1> <form method="post"> Contact Id: <input type="text" name="contactid" value="1459" placeholder="Contact Id"/><br/> SubscriptionPlanId: <input type="text" name="subscriptionplanid" value="33" placeholder="Subscription Plan / CProgram Id"/><br/> <input type="submit"> </form>
public function addRecurringOrder($args) { array_shift($args); list($contactId, $allowDuplicate, $cProgramId, $qty, $price, $allowTax, $merchantAccountId, $creditCardId, $affiliateId, $daysTillCharge) = $args; $recurringOrder = new Infusionsoft_RecurringOrder(); $recurringOrder->ContactId = $contactId; $recurringOrder->ProgramId = $cProgramId; $recurringOrder->Qty = $qty; $recurringOrder->BillingAmt = $price; $recurringOrder->MerchantAccountId = $merchantAccountId; $recurringOrder->CC1 = $creditCardId; $recurringOrder->NextBillDate = date('Y-m-d H:i:s', strtotime("-{$daysTillCharge} days")); $recurringOrder->AffiliateId = $affiliateId; $recurringOrder->save(); }