diff --git a/app/Jobs/Subscription/CleanStaleInvoiceOrder.php b/app/Jobs/Subscription/CleanStaleInvoiceOrder.php index 9795d08cd2c7..fccc11b7ee40 100644 --- a/app/Jobs/Subscription/CleanStaleInvoiceOrder.php +++ b/app/Jobs/Subscription/CleanStaleInvoiceOrder.php @@ -59,7 +59,7 @@ class CleanStaleInvoiceOrder implements ShouldQueue Invoice::query() ->withTrashed() - ->where('status_id', Invoice::STATUS_SENT) + ->where('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->where('updated_at', '<', now()->subHour()) ->where('balance', '>', 0) ->whereJsonContains('line_items', ['type_id' => '3']) diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 44df80d61069..5ef8dd23ea68 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -404,8 +404,8 @@ class BaseDriver extends AbstractPaymentDriver $item->gross_line_total = round($item->gross_line_total, 2); return $item; }) - ->whereIn('type_id', ['3','4']) - ->where('gross_line_total', round($fee_total,2)) + ->whereIn('type_id', ['3']) + ->where('gross_line_total', '<=', round($fee_total,2)) ->count(); if($invoice && $fee_count == 0){ diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 3c7fc0bb9c75..178e37c42899 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -98,7 +98,9 @@ class AutoBillInvoice extends AbstractService return $this->invoice; } - info("Auto Bill - balance remains to be paid!! - {$amount}"); + nlog("Auto Bill - balance remains to be paid!! - {$amount}"); + nlog($this->invoice->amount); + nlog($this->invoice->balance); /* Retrieve the Client Gateway Token */ /** @var \App\Models\ClientGatewayToken $gateway_token */ @@ -112,11 +114,15 @@ class AutoBillInvoice extends AbstractService // return $this->invoice; } - nlog('Gateway present - adding gateway fee'); + nlog("Gateway present - adding gateway fee on {$amount}"); /* $gateway fee */ $this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $gateway_token->gateway_type_id, $amount)->save(); + + nlog($this->invoice->amount); + nlog($this->invoice->balance); + //change from $this->invoice->amount to $this->invoice->balance if ($is_partial) { $fee = $this->invoice->balance - $invoice_total; @@ -124,6 +130,8 @@ class AutoBillInvoice extends AbstractService $fee = $this->invoice->balance - $amount; } + nlog("fee is {$fee}"); + if ($fee > $amount) { $fee = 0; } @@ -148,8 +156,7 @@ class AutoBillInvoice extends AbstractService ]); nlog("Payment hash created => {$payment_hash->id}"); - $this->invoice->saveQuietly(); - + $payment = false; try { $payment = $gateway_token->gateway @@ -171,8 +178,6 @@ class AutoBillInvoice extends AbstractService $this->invoice->auto_bill_tries = 0; //reset the counter here in case auto billing is turned on again in the future. } - $this->invoice->save(); - if ($payment) { info('Auto Bill payment captured for '.$this->invoice->number); event(new InvoiceAutoBillSuccess($this->invoice, $this->invoice->company, Ninja::eventVars()));