Skip to content

Commit c3b66f5

Browse files
authored
Merge pull request #17 from myadmin-plugins/gtest
Retry CC feature
2 parents 786d15a + a597ae9 commit c3b66f5

1 file changed

Lines changed: 78 additions & 57 deletions

File tree

src/cc.inc.php

Lines changed: 78 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Punic\Currency;
1111
use Brick\Money\Money;
1212
use Brick\Math\RoundingMode;
13+
use MyAdmin\App;
1314

1415
/**
1516
* @param $cc
@@ -243,61 +244,6 @@ function format_cc_exp()
243244
return $value;
244245
}
245246

246-
/**
247-
* generates a cc decline email body
248-
*
249-
* @param int $custid the customer id #
250-
* @param int $invoice_id the invoice id #
251-
* @return array array w/ the information needed to send an email or display the cc decline
252-
* @throws \Exception
253-
* @throws \SmartyException
254-
*/
255-
function make_cc_decline($custid, $invoice_id)
256-
{
257-
$admin_dir = INSTALL_ROOT;
258-
$data = $GLOBALS['tf']->accounts->read($custid);
259-
$domain = $GLOBALS['tf']->accounts->cross_reference($custid);
260-
$groupinfo = get_groupinfo($domain);
261-
if ($groupinfo['email'] != '') {
262-
$emailfrom = $groupinfo['email'];
263-
} else {
264-
$emailfrom = EMAIL_FROM;
265-
}
266-
$smarty = new TFSmarty();
267-
$smarty->assign('invoice_id', $invoice_id);
268-
$smarty->assign('customer_domain', $domain);
269-
$smarty->assign('customer_id', $custid);
270-
$smarty->assign('customer_name', $data['name']);
271-
$smarty->assign('company_name', $groupinfo['account_lid']);
272-
$invoice_data = get_invoice($invoice_id);
273-
$smarty->assign('customer_balance', $invoice_data['invoices_amount']);
274-
if (DOMAIN == 'interserver.net' || trim(DOMAIN) == '') {
275-
$smarty->assign('url', 'my.interserver.net');
276-
} else {
277-
$smarty->assign('url', DOMAIN.URLDIR);
278-
}
279-
$ret_invoice['invoice'] = $smarty->fetch('email/client/ccdecline.tpl');
280-
$ret_invoice['toname'] = $data['name'];
281-
$ret_invoice['toemail'] = get_invoices_email($data);
282-
$ret_invoice['subject'] = 'Problem With Account '.$domain;
283-
$ret_invoice['fromname'] = $groupinfo['account_lid'].' Billing Department';
284-
$ret_invoice['fromemail'] = $emailfrom;
285-
return $ret_invoice;
286-
}
287-
288-
/**
289-
* sends a cc decline email
290-
*
291-
* @param int $custid
292-
* @param mixed $invoice_id
293-
* @return void
294-
*/
295-
function email_cc_decline($custid, $invoice_id)
296-
{
297-
$email = make_cc_decline($custid, $invoice_id);
298-
myadmin_log('billing', 'debug', ' Emailing CC Decline Message To '.$email['toname'], __LINE__, __FILE__);
299-
(new \MyAdmin\Mail())->multiMail($email['subject'], '<PRE>'.$email['invoice'].'</PRE>', $email['toemail'], 'client/ccdecline.tpl');
300-
}
301247

302248
/**
303249
* given the account data array, it parses out and returns an array of ccs
@@ -569,13 +515,13 @@ function charge_card($custid, $amount = false, $invoice = false, $module = 'defa
569515
return $retval;
570516
}
571517
//$data['status'] = 'pending-fixcc';
572-
$GLOBALS['tf']->accounts->update($custid, ['payment_method' => 'paypal']);
573518
$subject = $settings['TITLE'].' Credit Card Payment Declined';
574519
$smarty = new TFSmarty();
575520
$smarty->assign('amount', $amount);
576521
$smarty->assign('service_name', $settings['TBLNAME']);
577522
$smarty->assign('company', $settings['TITLE']);
578523
$smarty->assign('name', $data['name']);
524+
$smarty->assign('cc_num', mask_cc($cc, true));
579525
if (!defined(DOMAIN) || in_array(DOMAIN, ['interserver.net', 'misha.interserver.net', 'mymisha.interserver.net']) || trim(DOMAIN) == '') {
580526
$smarty->assign('domain', 'my.interserver.net');
581527
} else {
@@ -620,7 +566,32 @@ function charge_card($custid, $amount = false, $invoice = false, $module = 'defa
620566
$smarty->assign('invoices', $rows);
621567
$email = $smarty->fetch('email/client/payment_failed.tpl');
622568
(new \MyAdmin\Mail())->multiMail($subject, $email, get_invoice_email($data), 'client/payment_failed.tpl');
623-
//email_cc_decline($custid, $invoice);
569+
570+
//"ot_cc" is added because it came from pay_balance where they try specific card so no retry for that.
571+
if (
572+
count($ccs) > 1 && //more than 1 cc present then proceed
573+
RETRY_CC == 1 && //When CC Retry is enabled from config
574+
(!isset(App::variables()->request['ot_cc']) || isset(App::variables()->request['retry_cc']))
575+
) {
576+
$cc_encrypted = $GLOBALS['tf']->encrypt(trim(str_replace([' ', '_', '-'], ['', '', ''], $cc)));
577+
$dec_ccs = [];
578+
$db->query("SELECT * FROM user_log WHERE history_owner = {$custid} AND history_type = 'carddecline'", __LINE__, __FILE__);
579+
if ($db->num_rows() > 0) {
580+
while ($db->next_record(MYSQL_ASSOC)) {
581+
$dec_ccs[] = $GLOBALS['tf']->decrypt($db->Record['history_new_value']);
582+
}
583+
}
584+
if (!in_array($cc, $dec_ccs)) {
585+
$GLOBALS['tf']->history->add('users', 'carddecline', $cc_encrypted, $cc_exp, $custid);
586+
}
587+
$retval = retry_charge_card($custid, $amount, $invoice, $module, $returnURL, $useHandlePayment, $queue);
588+
} else {
589+
$new_data = [
590+
'payment_method' => 'paypal',
591+
'cc_auto' => '0'
592+
];
593+
App::accounts()->update($custid, $new_data);
594+
}
624595
//$GLOBALS['tf']->history->add('users', 'carddecline', $data['cc'], $data['cc_exp'], $custid);
625596
break;
626597
}
@@ -752,6 +723,56 @@ function auth_charge_card($custid, $cc, $cc_exp, $amount, $module = 'default', $
752723
return $retval;
753724
}
754725

726+
/**
727+
* Gets next available verified CC number to charge
728+
*
729+
*/
730+
function get_next_cc($custid)
731+
{
732+
function_requirements('parse_ccs');
733+
$data = App::accounts()->read($custid);
734+
$dec_ccs = [];
735+
$db = get_module_db('default');
736+
$db->query("SELECT * FROM user_log WHERE history_owner = {$custid} AND history_type = 'carddecline'", __LINE__, __FILE__);
737+
if ($db->num_rows() > 0) {
738+
while ($db->next_record(MYSQL_ASSOC)) {
739+
$dec_ccs[] = $GLOBALS['tf']->decrypt($db->Record['history_new_value']);
740+
}
741+
}
742+
$ccs = parse_ccs($data);
743+
foreach ($ccs as $cc_id => $cc_det) {
744+
$cc_num = $GLOBALS['tf']->decrypt($cc_det['cc']);
745+
if (!in_array($cc_num, $dec_ccs) && can_use_cc($data, $cc_det, false)) {
746+
myadmin_log('billing', 'info', "Backup CC - found for customer $custid ".mask_cc($cc_num), __LINE__, __FILE__);
747+
return $cc_id;
748+
}
749+
}
750+
myadmin_log('billing', 'info', "Backup CC - not found for customer $custid", __LINE__, __FILE__);
751+
//Here Primary & Backup both CCs failed so updating payment method paypal.
752+
$new_data = [
753+
'payment_method' => 'paypal',
754+
'cc_auto' => '0'
755+
];
756+
App::accounts()->update($custid, $new_data);
757+
return false;
758+
}
759+
760+
function retry_charge_card($custid, $amount = false, $invoice = false, $module = 'default', $returnURL = false, $useHandlePayment = true, $queue = false)
761+
{
762+
myadmin_log('billing', 'info', "Retrying BackupCC - Custid: $custid, Amount: $amount", __LINE__, __FILE__);
763+
$next_cc = get_next_cc($custid);
764+
if ($next_cc !== false) {
765+
App::variables()->request['ot_cc'] = $next_cc;
766+
App::variables()->request['retry_cc'] = 1;
767+
$success = charge_card($custid, $amount, $invoice, $module, $returnURL, $useHandlePayment, $queue);
768+
if ($success) {
769+
myadmin_log('billing', 'info', "Retrying BackupCC - Success for $custid, Amount: $amount, CC ID - $next_cc", __LINE__, __FILE__);
770+
return true;
771+
}
772+
}
773+
return false;
774+
}
775+
755776
/**
756777
* gets the cc bank number / bin for the given encrypted cc
757778
*

0 commit comments

Comments
 (0)