From 605ddd7d3eae11d81b3f4d2d556715326abd3b6d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 25 Jun 2024 11:09:33 +1000 Subject: [PATCH] Ensure gateway fees are not cleared inappropriately --- app/PaymentDrivers/BaseDriver.php | 122 +++++++++++++++++------- app/Services/Invoice/InvoiceService.php | 1 - 2 files changed, 90 insertions(+), 33 deletions(-) diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 78fbc57d79f2..4e453c612799 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -11,35 +11,36 @@ namespace App\PaymentDrivers; -use App\Events\Invoice\InvoiceWasPaid; -use App\Events\Payment\PaymentWasCreated; -use App\Exceptions\PaymentFailed; -use App\Factory\PaymentFactory; -use App\Jobs\Mail\NinjaMailer; -use App\Jobs\Mail\NinjaMailerJob; -use App\Jobs\Mail\NinjaMailerObject; -use App\Jobs\Mail\PaymentFailedMailer; -use App\Jobs\Util\SystemLogger; -use App\Mail\Admin\ClientPaymentFailureObject; -use App\Models\Client; -use App\Models\ClientContact; -use App\Models\ClientGatewayToken; -use App\Models\CompanyGateway; -use App\Models\GatewayType; -use App\Models\Invoice; -use App\Models\Payment; -use App\Models\PaymentHash; -use App\Models\SystemLog; -use App\Services\Subscription\SubscriptionService; -use App\Utils\Helpers; use App\Utils\Ninja; use App\Utils\Number; -use App\Utils\Traits\MakesHash; -use App\Utils\Traits\SystemLogTrait; -use Illuminate\Http\Request; -use Illuminate\Support\Carbon; -use Illuminate\Support\Facades\App; +use App\Models\Client; +use App\Utils\Helpers; +use App\Models\Invoice; +use App\Models\Payment; +use App\Models\SystemLog; +use App\Models\GatewayType; +use App\Models\PaymentHash; use Illuminate\Support\Str; +use Illuminate\Http\Request; +use App\Models\ClientContact; +use App\Jobs\Mail\NinjaMailer; +use App\Models\CompanyGateway; +use Illuminate\Support\Carbon; +use App\DataMapper\InvoiceItem; +use App\Factory\PaymentFactory; +use App\Jobs\Util\SystemLogger; +use App\Utils\Traits\MakesHash; +use App\Exceptions\PaymentFailed; +use App\Jobs\Mail\NinjaMailerJob; +use App\Models\ClientGatewayToken; +use Illuminate\Support\Facades\App; +use App\Jobs\Mail\NinjaMailerObject; +use App\Utils\Traits\SystemLogTrait; +use App\Events\Invoice\InvoiceWasPaid; +use App\Jobs\Mail\PaymentFailedMailer; +use App\Events\Payment\PaymentWasCreated; +use App\Mail\Admin\ClientPaymentFailureObject; +use App\Services\Subscription\SubscriptionService; /** * Class BaseDriver. @@ -394,14 +395,71 @@ class BaseDriver extends AbstractPaymentDriver /*Fee charged at gateway*/ $fee_total = $this->payment_hash->fee_total; - /*Hydrate invoices*/ - $invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->withTrashed()->get(); + if(!$fee_total || $fee_total == 0) + return; - $invoices->each(function ($invoice) { - if (collect($invoice->line_items)->contains('type_id', '3')) { - $invoice->service()->toggleFeesPaid()->save(); + $invoices = Invoice::query() + ->whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id'))) + ->whereJsonContains('line_items', ['type_id' => '3']) + ->withTrashed(); + + if($invoices->count() == 0){ + + $invoice = Invoice::query() + ->whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id'))) + ->orderBy('id','desc') + ->withTrashed() + ->first(); + + if(!$invoice) + return; + + $balance = $invoice->balance; + + App::forgetInstance('translator'); + $t = app('translator'); + $t->replace(Ninja::transformTranslations($invoice->company->settings)); + App::setLocale($invoice->client->locale()); + + $invoice_item = new InvoiceItem(); + $invoice_item->type_id = '4'; + $invoice_item->product_key = ctrans('texts.surcharge'); + $invoice_item->notes = ctrans('texts.online_payment_surcharge'); + $invoice_item->quantity = 1; + $invoice_item->cost = $fee_total; + + $invoice_items = (array) $invoice->line_items; + $invoice_items[] = $invoice_item; + + $invoice->line_items = $invoice_items; + + /**Refresh Invoice values*/ + $invoice = $invoice->calc()->getInvoice(); + + $new_balance = $invoice->balance; + + if (floatval($new_balance) - floatval($balance) != 0) { + $adjustment = $new_balance - $balance; + + $invoice + ->ledger() + ->updateInvoiceBalance($adjustment, 'Adjustment for adding gateway fee'); + + $invoice->client->service()->calculateBalance(); } - }); + + } + else { + + $invoices + ->cursor() + ->each(function ($i){ + $i->service()->toggleFeesPaid()->save(); + }); + + } + + } /** diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 00c619d6236c..8d3ac788fe75 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -383,7 +383,6 @@ class InvoiceService return $item; })->toArray(); - // $this->deletePdf(); $this->deleteEInvoice(); return $this;