public function query($params) { list($table, $limit, $page, $queryData, $returnFields) = $params; $this->createTableIfNotExists($table); $results = array(); foreach ($this->tables[$table] as $row) { $include = true; foreach ($queryData as $fieldName => $fieldValue) { if ((!isset($row[$fieldName]) || !$this->emulateMySqlLike($fieldValue, $row[$fieldName])) && !(!isset($row[$fieldName]) && $fieldValue == '~null~')) { $include = false; } } if ($include) { foreach ($row as $field => $value) { if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/", $value)) { $row[$field] = Infusionsoft_App::formatDate($value); } } $results[] = $row; } } $resultPages = array(); $pageNumber = 0; foreach ($results as $result) { $resultPages[$pageNumber][] = $result; if (count($resultPages[$pageNumber]) == $limit) { $pageNumber++; } } if (!array_key_exists($page, $resultPages)) { return array(); } return $resultPages[$page]; }
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; }