Skip to content

Instantly share code, notes, and snippets.

@KevinGraham-com
Last active December 5, 2024 04:39
Show Gist options
  • Select an option

  • Save KevinGraham-com/6f2a48c55b13d6a37b68b9fc1ee87fab to your computer and use it in GitHub Desktop.

Select an option

Save KevinGraham-com/6f2a48c55b13d6a37b68b9fc1ee87fab to your computer and use it in GitHub Desktop.
<?php
require_once '/home/whmcs/public_html/init.php';
// update above to your WHMCS folder
$capturetotal=0;
$invoicescaptured=0;
$paymethodstried=0;
$command=array(
'status' => 'Overdue',
'orderby' => 'duedate',
'order' => 'desc',
'limitnum' => 200,
);
$invoices=localAPI('GetInvoices', $command);
foreach ($invoices['invoices']['invoice'] as $invoice) {
if (date('Y-m-d', strtotime($invoice['duedate'])) < date('Y-m-d', strtotime('-65 days'))) {
$command=array(
'invoiceid' => $invoice['id'],
'status' => 'Cancelled',
);
$invcanxresp=localAPI('UpdateInvoice', $command);
echo 'Cancelled overdue invoice '.$invoice['id'].PHP_EOL;
} else {
// you pay now. i work too hard. i want my money.
$command=array(
'clientid' => $invoice['userid'],
);
$paymethods=localAPI('GetPayMethods', $command);
if (count($paymethods['paymethods']) > 0) {
echo 'Trying '.count($paymethods['paymethods']).' for invoice '.$invoice['id'].PHP_EOL;
foreach ($paymethods['paymethods'] as $token) {
$command= array(
'clientid' => $invoice['userid'],
'paymethodid' => $token['id'],
'set_as_default' => true,
);
$paydefaultresp = localAPI('UpdatePayMethod', $command);
$setdefaultlogdata=array('description' => 'Set '.$token['description'].' as default.', 'clientid' => $invoice['userid']);
$setdefaultlog=localAPI("LogActivity", $setdefaultlogdata);
$paymethodstried++;
$command= array(
'invoiceid' => $invoice['id'],
);
$invoicepay=localAPI('CapturePayment', $command);
if ($invoicepay['result']=="success") {
echo 'Successful capture of $'.$invoice['subtotal'].PHP_EOL;
$capturelogdata=array('description' => 'Successful capture of $'.$invoice['subtotal'].' from payment method '.$token['description'], 'clientid' => $invoice['userid']);
$capturelog=localAPI("LogActivity", $capturelogdata);
$capturetotal=$capturetotal+$invoice['subtotal'];
$invoicescaptured++;
break;
} else { echo 'Failed capture'.PHP_EOL;
$capturelogdata=array('description' => 'Failed capture of $'.$invoice['subtotal'].' from payment method '.$token['description'], 'clientid' => $invoice['userid']);
$capturelog=localAPI("LogActivity", $capturelogdata);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment