Improve gateway fee toggles

This commit is contained in:
David Bomba 2024-08-13 14:30:44 +10:00
parent 2ffe9bb83a
commit f59006aedd
2 changed files with 29 additions and 34 deletions

View File

@ -305,7 +305,7 @@ class BaseDriver extends AbstractPaymentDriver
public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment
{ {
if (in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING])) { if (in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING])) {
$this->confirmGatewayFee(); $this->confirmGatewayFee($data);
} }
/*Never create a payment with a duplicate transaction reference*/ /*Never create a payment with a duplicate transaction reference*/
@ -388,10 +388,9 @@ class BaseDriver extends AbstractPaymentDriver
* *
* @return void Success/Failure * @return void Success/Failure
*/ */
public function confirmGatewayFee(): void public function confirmGatewayFee($data = []): void
{ {
/*Payment invoices*/ nlog("confirming gateway fee");
$payment_invoices = $this->payment_hash->invoices();
/*Fee charged at gateway*/ /*Fee charged at gateway*/
$fee_total = $this->payment_hash->fee_total; $fee_total = $this->payment_hash->fee_total;
@ -399,23 +398,16 @@ class BaseDriver extends AbstractPaymentDriver
if(!$fee_total || $fee_total == 0) if(!$fee_total || $fee_total == 0)
return; return;
$invoices = Invoice::query() $invoice = $this->payment_hash->fee_invoice;
->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();
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() if($invoice && $fee_count == 0){
->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) nlog("apparently no fee, so injecting here!");
return;
$balance = $invoice->balance; $balance = $invoice->balance;
@ -434,6 +426,16 @@ class BaseDriver extends AbstractPaymentDriver
$invoice_items = (array) $invoice->line_items; $invoice_items = (array) $invoice->line_items;
$invoice_items[] = $invoice_item; $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; $invoice->line_items = $invoice_items;
/**Refresh Invoice values*/ /**Refresh Invoice values*/
@ -454,15 +456,10 @@ class BaseDriver extends AbstractPaymentDriver
} }
else { else {
$invoices $invoice->service()->toggleFeesPaid()->save();
->cursor()
->each(function ($i){
$i->service()->toggleFeesPaid()->save();
});
} }
} }
/** /**
@ -474,11 +471,8 @@ class BaseDriver extends AbstractPaymentDriver
*/ */
public function unWindGatewayFees(PaymentHash $payment_hash) public function unWindGatewayFees(PaymentHash $payment_hash)
{ {
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->get(); if($payment_hash->fee_invoice)
$payment_hash->fee_invoice->service()->removeUnpaidGatewayFees();
$invoices->each(function ($invoice) {
$invoice->service()->removeUnpaidGatewayFees();
});
} }
/** /**

View File

@ -17,6 +17,7 @@ use App\Models\Invoice;
use App\Services\AbstractService; use App\Services\AbstractService;
use App\Utils\Ninja; use App\Utils\Ninja;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use App\Models\Product;
class AddGatewayFee extends AbstractService class AddGatewayFee extends AbstractService
{ {
@ -26,16 +27,15 @@ class AddGatewayFee extends AbstractService
public function run() 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); $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) { if (! $gateway_fee || $gateway_fee == 0) {
return $this->invoice; return $this->invoice;
} }
// Removes existing stale gateway fees
$this->cleanPendingGatewayFees();
// If a gateway fee is > 0 insert the line item // If a gateway fee is > 0 insert the line item
if ($gateway_fee > 0) { if ($gateway_fee > 0) {
return $this->processGatewayFee($gateway_fee); 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_name2 = $fees_and_limits->fee_tax_name2;
$invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3; $invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3;
$invoice_item->tax_name3 = $fees_and_limits->fee_tax_name3; $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; $invoice_items = (array) $this->invoice->line_items;