function AddTimeCredit($merchantAutobillId, $daysRemaining)
{
    $autoBill = new AutoBill();
    $autoBill->setMerchantAutoBillId($merchantAutobillId);
    $timeInterval = new TimeInterval();
    $timeInterval->setDays($daysRemaining);
    $timeInterval->setDescription("Early Pay on NonRecurring Billing");
    $timeInterval->setReason("Credit for July");
    $cr = new Credit();
    $cr->setTimeIntervals(array($timeInterval));
    $response = $autoBill->grantCredit($cr, '');
    logCall($response, 'AutoBill::GrantCredit', $daysRemaining);
}
function fetchMerchantAutobillId($merchantAccountId)
{
    $merchantAutoBillId = null;
    $autobillAPI = new AutoBill();
    $account = new Account();
    $account->merchantAccountId = $merchantAccountId;
    logCall('Autobill->fetchByAccount ' . $merchantAccountId);
    $response = $autobillAPI->fetchByAccount($account, true);
    if (isCallSuccessful($response)) {
        print "Found account with id: " . $merchantAccountId . PHP_EOL;
        $fetchedAutoBills = $response['data']->autobills;
        foreach ($fetchedAutoBills as $autobill) {
            // process each autobill found here
            print "Found autobill with id: " . $autobill->getMerchantAutoBillId() . PHP_EOL;
            // If this is the autobill you want, set it here.  Check if $autobill is still active.
            if ($autobill->status == 'Active') {
                $merchantAutoBillId = $autobill->getMerchantAutoBillId();
            }
        }
    }
    return $merchantAutoBillId;
}
function prepayNonRecurringWithNonRecurringPrice($merchantAutobillId)
{
    // Fetch the current active non-recurring autobill by fetching all autobills by merchantAccountId.
    // Pick the current active non-recurring autobill for which they are pre-paying.
    $currentActiveNonRecurringAutobill = get_current_active_nonrecurring_autobill($merchantAutobillId);
    // Pre-pay capturing a transaction for price of product on current active non-recurring autobill.
    $merchantTransactionId = prepay($currentActiveNonRecurringAutobill);
    $startTimestamp = $currentActiveNonRecurringAutobill->endTimestamp;
    $merchantAutoBillId = $currentActiveNonRecurringAutobill->merchantAutoBillId . '+';
    $merchantAccountId = $currentActiveNonRecurringAutobill->account->merchantAccountId;
    // Only use sparse object, so all data members are not over-written.
    $sparseAccount = new Account();
    $sparseAccount->merchantAccountId = $currentActiveNonRecurringAutobill->account->merchantAccountId;
    // Assume same billing plan as on current autobill.
    $billingPlan = $currentActiveNonRecurringAutobill->billingPlan;
    // Assume same product as on current autobill.
    $product = $currentActiveNonRecurringAutobill->items[0]->product;
    $itemNonRecurring = new AutoBillItem();
    $itemNonRecurring->setIndex(1);
    // Override price of product to 0 USD, since payment is taken up front as one-time transaction.
    $itemNonRecurring->setAmount(0);
    $itemNonRecurring->setProduct($product);
    $autobill = new AutoBill();
    $autobill->setMerchantAutoBillId($merchantAutoBillId);
    $autobill->setAccount($sparseAccount);
    $autobill->setItems(array($itemNonRecurring));
    $autobill->setBillingPlan($billingPlan);
    // Future dated autobill will start when the current active autobill ends.
    // Price is overrided as 0 USD, so no transaction will occur and the user will be entitled for the next non-recurring cycle.
    $autobill->setStartTimestamp($startTimestamp);
    $autobill->setWarnOnExpiration(true);
    $autobill->setCurrency('USD');
    // Add a name value pair to signify a linkage to the previous Autobill.
    // Add a name value pair to signify a linkage to the Transaction used to collect payment.
    // This will help out with revenue recognition.
    $nameVals[0] = new NameValuePair();
    $nameVals[0]->setName("PreviousNonRecurringAutobillId");
    $nameVals[0]->setValue($currentActiveNonRecurringAutobill->merchantAutoBillId);
    $nameVals[1] = new NameValuePair();
    $nameVals[1]->setName("PaidForByMerchantTransactionId");
    $nameVals[1]->setValue($merchantTransactionId);
    $autobill->setNameValues($nameVals);
    $immediateAuthFailurePolicy = 'doNotSaveAutoBill';
    $validateForFuturePayment = false;
    $minChargebackProbability = 100;
    $ignoreAvsPolicy = true;
    $ignoreCvnPolicy = true;
    $campaignCode = null;
    $dryrun = false;
    $response = $autobill->update($immediateAuthFailurePolicy, $validateForFuturePayment, $minChargebackProbability, $ignoreAvsPolicy, $ignoreCvnPolicy, $campaignCode, $dryrun);
    logCall($response, 'Autobill::Update', $merchantAccountId);
}
    }
}
$transactionAPI = new Transaction();
logCall('transaction->fetchByAccount ' . $merchantAccountId);
$response = $transactionAPI->fetchByAccount($account, false);
if (isCallSuccessful($response)) {
    $fetchedTxns = $response['data']->transactions;
    if ($fetchedTxns != null) {
        foreach ($fetchedTxns as $fetchedTx) {
            print "Transaction VID " . $fetchedTx->getVID() . PHP_EOL;
            print "Transaction amount " . $fetchedTx->getAmount() . PHP_EOL;
            print "Transaction status " . PHP_EOL;
            print $fetchedTx->statusLog[0]->status . PHP_EOL;
        }
    } else {
        print "No transactions found \n";
    }
}
$refundAPI = new Refund();
logCall('refundAPI->fetchByAccount ' . $merchantAccountId);
$response = $refundAPI->fetchByAccount($account, false);
if (isCallSuccessful($response)) {
    $refunds = $response['data']->refunds;
    if ($refunds != null) {
        foreach ($refunds as $refund) {
            print 'Refund of ' . $refund->amount . ' with Refund Id ' . $refund->merchantRefundId . ' had been issued against payment (transaction Id ' . $refund->transaction->merchantTransactionId . ') of ' . $refund->transaction->amount . ' made on ' . $refund->transaction->timestamp . PHP_EOL;
        }
    } else {
        print "No refunds found \n";
    }
}