Minor refactor for auto billing

This commit is contained in:
= 2022-05-17 14:46:03 +10:00
parent d7bbc36251
commit ede64ef03d
2 changed files with 3 additions and 9 deletions

View File

@ -90,8 +90,7 @@ class Invoice extends BaseModel
'subscription_id', 'subscription_id',
'auto_bill_enabled', 'auto_bill_enabled',
'uses_inclusive_taxes', 'uses_inclusive_taxes',
'vendor_id', 'vendor_id'
'auto_bill_tries'
]; ];
protected $casts = [ protected $casts = [

View File

@ -117,31 +117,26 @@ class AutoBillInvoice extends AbstractService
$payment = false; $payment = false;
$number_of_retries = $this->invoice->auto_bill_tries;
try { try {
$payment = $gateway_token->gateway $payment = $gateway_token->gateway
->driver($this->client) ->driver($this->client)
->setPaymentHash($payment_hash) ->setPaymentHash($payment_hash)
->tokenBilling($gateway_token, $payment_hash); ->tokenBilling($gateway_token, $payment_hash);
} catch (\Exception $e) { } catch (\Exception $e) {
//increase the counter
$this->invoice->auto_bill_tries = $number_of_retries + 1; $this->invoice->auto_bill_tries = $number_of_retries + 1;
$this->invoice->save();
//disable auto bill if limit of 3 is reached
if ($this->invoice->auto_bill_tries == 3) { if ($this->invoice->auto_bill_tries == 3) {
$this->invoice->auto_bill_enabled = false; $this->invoice->auto_bill_enabled = false;
$this->invoice->auto_bill_tries = 0; //reset the counter here in case auto billing is turned on again in the future.
$this->invoice->save(); $this->invoice->save();
} }
nlog("payment NOT captured for " . $this->invoice->number . " with error " . $e->getMessage()); nlog("payment NOT captured for " . $this->invoice->number . " with error " . $e->getMessage());
// $this->invoice->service()->removeUnpaidGatewayFees();
} }
if ($payment) { if ($payment) {
info("Auto Bill payment captured for " . $this->invoice->number); info("Auto Bill payment captured for " . $this->invoice->number);
} }
// return $this->invoice->fresh();
} }
/** /**