From a66fa1c6789a10e4a761411e6d4c03dc82863e70 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Sep 2021 11:57:03 +1000 Subject: [PATCH] Fixes for incorrect fee calculation edge case --- app/Services/Invoice/AutoBillInvoice.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index a64b568a988e..480ba0e7fb1c 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -70,7 +70,7 @@ class AutoBillInvoice extends AbstractService /* Determine $amount */ if ($this->invoice->partial > 0) { $is_partial = true; - $invoice_total = $this->invoice->amount; + $invoice_total = $this->invoice->balance; $amount = $this->invoice->partial; } elseif ($this->invoice->balance > 0) { $amount = $this->invoice->balance; @@ -94,10 +94,14 @@ class AutoBillInvoice extends AbstractService /* $gateway fee */ $this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $gateway_token->gateway_type_id, $amount)->save(); + //change from $this->invoice->amount to $this->invoice->balance if($is_partial) - $fee = $this->invoice->amount - $invoice_total; + $fee = $this->invoice->balance - $invoice_total; else - $fee = $this->invoice->amount - $amount; + $fee = $this->invoice->balance - $amount; + + if($fee > $amount) + $fee = 0; /* Build payment hash */ $payment_hash = PaymentHash::create([