1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/tests/Feature/CompanyGatewayTest.php

294 lines
9.6 KiB
PHP
Raw Normal View History

2020-07-07 16:50:51 +02:00
<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
2020-09-14 13:11:46 +02:00
*/
2020-07-07 16:50:51 +02:00
namespace Tests\Feature;
2023-01-11 05:43:54 +01:00
use App\Jobs\Invoice\CheckGatewayFee;
2020-07-07 16:50:51 +02:00
use App\Models\CompanyGateway;
2020-10-12 06:58:07 +02:00
use App\Models\GatewayType;
2023-01-11 05:43:54 +01:00
use App\Models\Invoice;
2020-07-07 16:50:51 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2023-02-20 21:50:00 +01:00
use Illuminate\Foundation\Testing\RefreshDatabase;
2020-07-07 16:50:51 +02:00
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Models\CompanyGateway
*/
class CompanyGatewayTest extends TestCase
{
use MockAccountData;
2023-02-20 22:11:39 +01:00
use DatabaseTransactions;
// use RefreshDatabase;
2020-07-07 16:50:51 +02:00
protected function setUp() :void
2020-07-07 16:50:51 +02:00
{
parent::setUp();
2020-07-07 16:50:51 +02:00
$this->makeTestData();
if (! config('ninja.testvars.stripe')) {
2020-07-08 02:18:13 +02:00
$this->markTestSkipped('Skip test no company gateways installed');
}
2020-07-07 16:50:51 +02:00
}
public function testGatewayExists()
{
$company_gateway = CompanyGateway::first();
$this->assertNotNull($company_gateway);
}
public function testFeesAndLimitsExists()
{
$data = [];
$data[1]['min_limit'] = 234;
$data[1]['max_limit'] = 65317;
$data[1]['fee_amount'] = 0.00;
$data[1]['fee_percent'] = 0.000;
$data[1]['fee_tax_name1'] = '';
$data[1]['fee_tax_rate1'] = '';
$data[1]['fee_tax_name2'] = '';
$data[1]['fee_tax_rate2'] = '';
$data[1]['fee_tax_name3'] = '';
$data[1]['fee_tax_rate3'] = 0;
$data[1]['adjust_fee_percent'] = true;
$data[1]['fee_cap'] = 0;
$data[1]['is_enabled'] = true;
2020-07-07 16:50:51 +02:00
$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;
2020-11-03 23:19:31 +01:00
$cg->require_shipping_address = true;
2020-07-07 16:50:51 +02:00
$cg->update_details = true;
$cg->config = encrypt(config('ninja.testvars.stripe'));
$cg->fees_and_limits = $data;
$cg->save();
$this->assertNotNull($cg->fees_and_limits);
2020-08-05 13:48:53 +02:00
$properties = array_keys(get_object_vars($cg->fees_and_limits));
$fees_and_limits = $cg->fees_and_limits->{$properties[0]};
$this->assertNotNull($fees_and_limits);
2020-07-07 16:50:51 +02:00
//confirm amount filtering works
$amount = 100;
2020-07-07 17:11:54 +02:00
$this->assertFalse($this->checkSieve($cg, $amount));
2020-07-07 17:11:54 +02:00
$amount = 235;
2020-07-07 17:11:54 +02:00
$this->assertTrue($this->checkSieve($cg, $amount));
2020-07-07 17:11:54 +02:00
$amount = 70000;
2020-07-07 17:11:54 +02:00
$this->assertFalse($this->checkSieve($cg, $amount));
2020-07-08 02:18:13 +02:00
}
2020-07-07 17:11:54 +02:00
2020-07-08 02:18:13 +02:00
public function checkSieve($cg, $amount)
{
if (isset($cg->fees_and_limits)) {
2020-08-05 13:48:53 +02:00
$properties = array_keys(get_object_vars($cg->fees_and_limits));
$fees_and_limits = $cg->fees_and_limits->{$properties[0]};
} else {
2020-07-08 02:18:13 +02:00
$passes = true;
}
if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $amount < $fees_and_limits->min_limit) {
2020-12-29 22:10:03 +01:00
nlog("amount {$amount} less than ".$fees_and_limits->min_limit);
2020-07-08 02:18:13 +02:00
$passes = false;
} elseif ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $amount > $fees_and_limits->max_limit) {
2020-12-29 22:10:03 +01:00
nlog("amount {$amount} greater than ".$fees_and_limits->max_limit);
$passes = false;
} else {
2020-07-08 02:18:13 +02:00
$passes = true;
}
2020-07-08 02:18:13 +02:00
return $passes;
2020-07-07 16:50:51 +02:00
}
2020-08-30 00:00:57 +02:00
public function testFeesAreAppendedToInvoice() //after refactor this may be redundant
{
$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;
2022-03-17 02:19:43 +01:00
$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;
2020-11-03 23:19:31 +01:00
$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;
2020-10-12 11:38:55 +02:00
$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);
}
2020-08-29 14:16:23 +02:00
2023-01-11 05:43:54 +01:00
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);
}
2020-08-29 14:16:23 +02:00
public function testProRataGatewayFees()
{
$data = [];
$data[1]['min_limit'] = -1;
$data[1]['max_limit'] = -1;
$data[1]['fee_amount'] = 1.00;
$data[1]['fee_percent'] = 2;
$data[1]['fee_tax_name1'] = 'GST';
$data[1]['fee_tax_rate1'] = 10;
$data[1]['fee_tax_name2'] = 'GST';
$data[1]['fee_tax_rate2'] = 10;
$data[1]['fee_tax_name3'] = 'GST';
$data[1]['fee_tax_rate3'] = 10;
2022-03-17 02:19:43 +01:00
$data[1]['adjust_fee_percent'] = false;
2020-08-29 14:16:23 +02:00
$data[1]['fee_cap'] = 0;
$data[1]['is_enabled'] = true;
2020-08-29 14:16:23 +02:00
$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;
2020-11-03 23:19:31 +01:00
$cg->require_shipping_address = true;
2020-08-29 14:16:23 +02:00
$cg->update_details = true;
$cg->config = encrypt(config('ninja.testvars.stripe'));
$cg->fees_and_limits = $data;
$cg->save();
$total = 10.93;
$total_invoice_count = 5;
2021-01-06 13:06:42 +01:00
$total_gateway_fee = round($cg->calcGatewayFee($total, GatewayType::CREDIT_CARD, true), 2);
2020-08-29 14:16:23 +02:00
$this->assertEquals(1.58, $total_gateway_fee);
/*simple pro rata*/
2020-10-12 06:58:07 +02:00
$fees_and_limits = $cg->getFeesAndLimits(GatewayType::CREDIT_CARD);
2020-08-29 14:16:23 +02:00
}
}