1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Improve clean up of stale gateway fees

This commit is contained in:
David Bomba 2023-01-11 15:43:54 +11:00
parent 9337814bab
commit 894c2011b0
3 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Invoice;
use App\Libraries\MultiDB;
use App\Models\Invoice;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class CheckGatewayFee implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
/**
* @param $invoice_id
* @param string $db
*/
public function __construct(public int $invoice_id, public string $db){}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->db);
$i = Invoice::withTrashed()->find($this->invoice_id);
if(!$i)
return;
if($i->status_id == Invoice::STATUS_SENT)
{
$i->service()->removeUnpaidGatewayFees();
}
}
}

View File

@ -16,6 +16,7 @@ use App\Exceptions\PaymentFailed;
use App\Factory\PaymentFactory;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Invoice\CheckGatewayFee;
use App\Jobs\Invoice\InjectSignature;
use App\Jobs\Util\SystemLogger;
use App\Models\CompanyGateway;
@ -198,6 +199,9 @@ class InstantPayment
$first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save();
}
/* Schedule a job to check the gateway fees for this invoice*/
CheckGatewayFee::dispatch($first_invoice, $client->company->db)->delay(600);
/**
* Gateway fee is calculated
* by adding it as a line item, and then subtract

View File

@ -11,8 +11,10 @@
namespace Tests\Feature;
use App\Jobs\Invoice\CheckGatewayFee;
use App\Models\CompanyGateway;
use App\Models\GatewayType;
use App\Models\Invoice;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\MockAccountData;
use Tests\TestCase;
@ -154,6 +156,104 @@ class CompanyGatewayTest extends TestCase
$this->assertEquals(($balance + 1), $this->invoice->balance);
}
public function testGatewayFeesAreClearedAppropriately()
{
$data = [];
$data[1]['min_limit'] = -1;
$data[1]['max_limit'] = -1;
$data[1]['fee_amount'] = 1.00;
$data[1]['fee_percent'] = 0.000;
$data[1]['fee_tax_name1'] = '';
$data[1]['fee_tax_rate1'] = 0;
$data[1]['fee_tax_name2'] = '';
$data[1]['fee_tax_rate2'] = 0;
$data[1]['fee_tax_name3'] = '';
$data[1]['fee_tax_rate3'] = 0;
$data[1]['adjust_fee_percent'] = false;
$data[1]['fee_cap'] = 0;
$data[1]['is_enabled'] = true;
$cg = new CompanyGateway;
$cg->company_id = $this->company->id;
$cg->user_id = $this->user->id;
$cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23';
$cg->require_cvv = true;
$cg->require_billing_address = true;
$cg->require_shipping_address = true;
$cg->update_details = true;
$cg->config = encrypt(config('ninja.testvars.stripe'));
$cg->fees_and_limits = $data;
$cg->save();
$balance = $this->invoice->balance;
$wiped_balance = $balance;
$this->invoice = $this->invoice->service()->addGatewayFee($cg, GatewayType::CREDIT_CARD, $this->invoice->balance)->save();
$this->invoice = $this->invoice->calc()->getInvoice();
$items = $this->invoice->line_items;
$this->assertEquals(($balance + 1), $this->invoice->balance);
(new CheckGatewayFee($this->invoice->id, $this->company->db))->handle();
$i = Invoice::withTrashed()->find($this->invoice->id);
$this->assertEquals($wiped_balance, $i->balance);
}
public function testMarkPaidAdjustsGatewayFeeAppropriately()
{
$data = [];
$data[1]['min_limit'] = -1;
$data[1]['max_limit'] = -1;
$data[1]['fee_amount'] = 1.00;
$data[1]['fee_percent'] = 0.000;
$data[1]['fee_tax_name1'] = '';
$data[1]['fee_tax_rate1'] = 0;
$data[1]['fee_tax_name2'] = '';
$data[1]['fee_tax_rate2'] = 0;
$data[1]['fee_tax_name3'] = '';
$data[1]['fee_tax_rate3'] = 0;
$data[1]['adjust_fee_percent'] = false;
$data[1]['fee_cap'] = 0;
$data[1]['is_enabled'] = true;
$cg = new CompanyGateway;
$cg->company_id = $this->company->id;
$cg->user_id = $this->user->id;
$cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23';
$cg->require_cvv = true;
$cg->require_billing_address = true;
$cg->require_shipping_address = true;
$cg->update_details = true;
$cg->config = encrypt(config('ninja.testvars.stripe'));
$cg->fees_and_limits = $data;
$cg->save();
$balance = $this->invoice->balance;
$wiped_balance = $balance;
$this->invoice = $this->invoice->service()->addGatewayFee($cg, GatewayType::CREDIT_CARD, $this->invoice->balance)->save();
$this->invoice = $this->invoice->calc()->getInvoice();
$items = $this->invoice->line_items;
$this->assertEquals(($balance + 1), $this->invoice->balance);
$this->invoice->service()->markPaid()->save();
$i = Invoice::withTrashed()->find($this->invoice->id);
$this->assertEquals($wiped_balance, $i->amount);
}
public function testProRataGatewayFees()
{
$data = [];