From f59006aedd2bdbb4a7b875b676e46ad403a1d608 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 13 Aug 2024 14:30:44 +1000 Subject: [PATCH] Improve gateway fee toggles --- app/PaymentDrivers/BaseDriver.php | 54 ++++++++++++-------------- app/Services/Invoice/AddGatewayFee.php | 9 +++-- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 26489ff7df13..dd84001f872e 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -305,7 +305,7 @@ class BaseDriver extends AbstractPaymentDriver public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment { if (in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING])) { - $this->confirmGatewayFee(); + $this->confirmGatewayFee($data); } /*Never create a payment with a duplicate transaction reference*/ @@ -388,10 +388,9 @@ class BaseDriver extends AbstractPaymentDriver * * @return void Success/Failure */ - public function confirmGatewayFee(): void + public function confirmGatewayFee($data = []): void { - /*Payment invoices*/ - $payment_invoices = $this->payment_hash->invoices(); + nlog("confirming gateway fee"); /*Fee charged at gateway*/ $fee_total = $this->payment_hash->fee_total; @@ -399,23 +398,16 @@ class BaseDriver extends AbstractPaymentDriver if(!$fee_total || $fee_total == 0) return; - $invoices = Invoice::query() - ->where('company_id', $this->company_gateway->company_id) - ->whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id'))) - ->whereJsonContains('line_items', ['type_id' => '3']) - ->withTrashed(); + $invoice = $this->payment_hash->fee_invoice; - if($invoices->count() == 0){ + $fee_count = collect($invoice->line_items) + ->whereIn('type_id', ['3','4']) + ->where('gross_line_total', $fee_total) + ->count(); - $invoice = Invoice::query() - ->where('company_id', $this->company_gateway->company_id) - ->whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id'))) - ->orderBy('id','desc') - ->withTrashed() - ->first(); + if($invoice && $fee_count == 0){ - if(!$invoice) - return; + nlog("apparently no fee, so injecting here!"); $balance = $invoice->balance; @@ -434,6 +426,16 @@ class BaseDriver extends AbstractPaymentDriver $invoice_items = (array) $invoice->line_items; $invoice_items[] = $invoice_item; + if (isset($data['gateway_type_id']) && $fees_and_limits = $this->company_gateway->getFeesAndLimits($data['gateway_type_id'])) { + $invoice_item->tax_rate1 = $fees_and_limits->fee_tax_rate1; + $invoice_item->tax_name1 = $fees_and_limits->fee_tax_name1; + $invoice_item->tax_rate2 = $fees_and_limits->fee_tax_rate2; + $invoice_item->tax_name2 = $fees_and_limits->fee_tax_name2; + $invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3; + $invoice_item->tax_name3 = $fees_and_limits->fee_tax_name3; + $invoice_item->tax_id = \App\Models\Product::PRODUCT_TYPE_OVERRIDE_TAX; + } + $invoice->line_items = $invoice_items; /**Refresh Invoice values*/ @@ -454,15 +456,10 @@ class BaseDriver extends AbstractPaymentDriver } else { - $invoices - ->cursor() - ->each(function ($i){ - $i->service()->toggleFeesPaid()->save(); - }); - + $invoice->service()->toggleFeesPaid()->save(); + } - } /** @@ -474,11 +471,8 @@ class BaseDriver extends AbstractPaymentDriver */ public function unWindGatewayFees(PaymentHash $payment_hash) { - $invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->get(); - - $invoices->each(function ($invoice) { - $invoice->service()->removeUnpaidGatewayFees(); - }); + if($payment_hash->fee_invoice) + $payment_hash->fee_invoice->service()->removeUnpaidGatewayFees(); } /** diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 9601d671f6b0..73e7064fe265 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -17,6 +17,7 @@ use App\Models\Invoice; use App\Services\AbstractService; use App\Utils\Ninja; use Illuminate\Support\Facades\App; +use App\Models\Product; class AddGatewayFee extends AbstractService { @@ -26,16 +27,15 @@ class AddGatewayFee extends AbstractService public function run() { - - // Removes existing stale gateway fees - $this->cleanPendingGatewayFees(); - $gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount, $this->gateway_type_id, $this->invoice->uses_inclusive_taxes), $this->invoice->client->currency()->precision); if (! $gateway_fee || $gateway_fee == 0) { return $this->invoice; } + // Removes existing stale gateway fees + $this->cleanPendingGatewayFees(); + // If a gateway fee is > 0 insert the line item if ($gateway_fee > 0) { return $this->processGatewayFee($gateway_fee); @@ -81,6 +81,7 @@ class AddGatewayFee extends AbstractService $invoice_item->tax_name2 = $fees_and_limits->fee_tax_name2; $invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3; $invoice_item->tax_name3 = $fees_and_limits->fee_tax_name3; + $invoice_item->tax_id = Product::PRODUCT_TYPE_OVERRIDE_TAX; } $invoice_items = (array) $this->invoice->line_items;