}
?>
"><br/>
            planStartDate: <input type="text" name="planStartDate" value="<?php 
if (isset($_REQUEST['planStartDate'])) {
    echo htmlspecialchars($_REQUEST['planStartDate']);
}
?>
"><br/>
            numPmts: <input type="text" name="numPmts" value="<?php 
if (isset($_REQUEST['numPmts'])) {
    echo htmlspecialchars($_REQUEST['numPmts']);
}
?>
"><br/>
            daysBetweenPmts: <input type="text" name="daysBetweenPmts" value="<?php 
if (isset($_REQUEST['daysBetweenPmts'])) {
    echo htmlspecialchars($_REQUEST['daysBetweenPmts']);
}
?>
"><br/>
    <input type="submit">
<input type="hidden" name="go">
</form>
<?php 
include '../infusionsoft.php';
include 'testUtils.php';
if (isset($_REQUEST['go'])) {
    $out = Infusionsoft_InvoiceService::addPaymentPlan($_REQUEST['invoiceId'], $_REQUEST['autoCharge'], $_REQUEST['creditCardId'], $_REQUEST['merchantAccountId'], $_REQUEST['daysBetweenRetry'], $_REQUEST['maxRetry'], $_REQUEST['initialPmtAmt'], $_REQUEST['initialPmtDate'], $_REQUEST['planStartDate'], $_REQUEST['numPmts'], $_REQUEST['daysBetweenPmts']);
    var_dump($out);
}
 public static function chargeInvoiceArbitraryAmountUsingPayPlan($invoiceId, $cardId, $amount, $merchantAccountId, $paymentNotes = 'API Arbitrary Payment')
 {
     $result = false;
     //Get Invoice info so we can set the temporary PayPlan to match the amount we want to charge
     $invoice = new Infusionsoft_Invoice($invoiceId);
     if ($amount + $invoice->TotalPaid <= $invoice->InvoiceTotal) {
         $temporaryFirstPayment = $amount + $invoice->TotalPaid;
     } else {
         $temporaryFirstPayment = $invoice->InvoiceTotal - $invoice->TotalPaid;
     }
     //Get current PayPlan info so we can set it back after taking the payment
     $payPlan = Infusionsoft_DataService::query(new Infusionsoft_PayPlan(), array('InvoiceId' => $invoiceId));
     if (!empty($payPlan)) {
         $payPlan = reset($payPlan);
         /**
          * @var Infusionsoft_PayPlan $payPlan
          */
         $payPlanItems = Infusionsoft_DataService::queryWithOrderBy(new Infusionsoft_PayPlanItem(), array('PayPlanId' => $payPlan->Id), 'DateDue', true);
         $payPlanStartDate = $payPlan->StartDate;
         $payPlanFirstPaymentAmount = $payPlan->FirstPayAmt;
         $numberOfPayments = count($payPlanItems) - 1;
         $payPlanItemDueDate1 = null;
         $daysBetweenPayments = 1;
         foreach ($payPlanItems as $index => $payPlanItem) {
             if ($index == 0) {
                 continue;
             }
             /**
              * @var Infusionsoft_PayPlanItem $payPlanItem
              */
             if ($payPlanItemDueDate1 == null) {
                 $payPlanItemDueDate1 = $payPlanItem->DateDue;
             } else {
                 $daysBetweenPayments = round((strtotime($payPlanItem->DateDue) - strtotime($payPlanItemDueDate1)) / 60 / 60 / 24);
                 break;
             }
         }
         if ($payPlanItemDueDate1 == null) {
             $payPlanItemDueDate1 = $payPlanStartDate;
         }
     } else {
         //If there is no PayPlan, then just set the order to the default of all due up front
         CakeLog::write('warning', 'PayPlan not found for InvoiceId: ' . $invoiceId . ' PayPlan will be set to the default');
         $payPlanFirstPaymentAmount = $invoice->InvoiceTotal;
         $payPlanStartDate = $invoice->DateCreated;
         $numberOfPayments = 0;
         $payPlanItemDueDate1 = $invoice->DateCreated;
         $daysBetweenPayments = 1;
     }
     try {
         Infusionsoft_InvoiceService::addPaymentPlan($invoiceId, 0, $cardId, $merchantAccountId, 1, 1, $temporaryFirstPayment, Infusionsoft_App::formatDate(date('Y-m-d')), Infusionsoft_App::formatDate(date('Y-m-d', strtotime(' + 1 day'))), 1, 1);
         $result = Infusionsoft_InvoiceService::chargeInvoice($invoiceId, $paymentNotes, $cardId, $merchantAccountId, false);
     } catch (Exception $e) {
         CakeLog::write('error', 'Failed to charge invoice arbitrary amount! InvoiceId: ' . $invoiceId . ' Infusionsoft error: ' . $e->getMessage());
     }
     try {
         Infusionsoft_InvoiceService::addPaymentPlan($invoiceId, 0, $cardId, $merchantAccountId, 1, 3, $payPlanFirstPaymentAmount, $payPlanStartDate, $payPlanItemDueDate1, $numberOfPayments, $daysBetweenPayments);
     } catch (Exception $e) {
         CakeLog::write('error', 'Failed to reset payment plan after chargeInvoiceArbitraryAmount! InvoiceId: ' . $invoiceId . ' PayPlan Details: ' . json_encode(compact('payPlanFirstPaymentAmount', 'payPlanStartDate', 'payPlanItemDueDate1', 'numberOfPayments', 'daysBetweenPayments')));
     }
     return $result;
 }