From 894c2011b08d40ca1f4cc9abbf461cf58845a77b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 11 Jan 2023 15:43:54 +1100 Subject: [PATCH] Improve clean up of stale gateway fees --- app/Jobs/Invoice/CheckGatewayFee.php | 54 ++++++++++ app/Services/ClientPortal/InstantPayment.php | 4 + tests/Feature/CompanyGatewayTest.php | 100 +++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 app/Jobs/Invoice/CheckGatewayFee.php diff --git a/app/Jobs/Invoice/CheckGatewayFee.php b/app/Jobs/Invoice/CheckGatewayFee.php new file mode 100644 index 000000000000..a608eee312f1 --- /dev/null +++ b/app/Jobs/Invoice/CheckGatewayFee.php @@ -0,0 +1,54 @@ +db); + + $i = Invoice::withTrashed()->find($this->invoice_id); + + if(!$i) + return; + + if($i->status_id == Invoice::STATUS_SENT) + { + $i->service()->removeUnpaidGatewayFees(); + } + + } +} diff --git a/app/Services/ClientPortal/InstantPayment.php b/app/Services/ClientPortal/InstantPayment.php index 9455b511009c..b62098c717f9 100644 --- a/app/Services/ClientPortal/InstantPayment.php +++ b/app/Services/ClientPortal/InstantPayment.php @@ -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 diff --git a/tests/Feature/CompanyGatewayTest.php b/tests/Feature/CompanyGatewayTest.php index eb1d2648a656..ff1ce14afa0a 100644 --- a/tests/Feature/CompanyGatewayTest.php +++ b/tests/Feature/CompanyGatewayTest.php @@ -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 = [];