2020-07-07 16:50:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use App\Models\CompanyGateway;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Models\CompanyGateway
|
|
|
|
*/
|
|
|
|
class CompanyGatewayTest extends TestCase
|
|
|
|
{
|
|
|
|
use MockAccountData;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-07-07 16:50:51 +02:00
|
|
|
$this->makeTestData();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
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;
|
2020-08-26 00:10:49 +02:00
|
|
|
$data[1]['fee_cap'] = 0;
|
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->show_billing_address = true;
|
|
|
|
$cg->show_shipping_address = true;
|
|
|
|
$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
|
2020-09-06 11:38:10 +02:00
|
|
|
$amount = 100;
|
2020-07-07 17:11:54 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->assertFalse($this->checkSieve($cg, $amount));
|
2020-07-07 17:11:54 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$amount = 235;
|
2020-07-07 17:11:54 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->assertTrue($this->checkSieve($cg, $amount));
|
2020-07-07 17:11:54 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$amount = 70000;
|
2020-07-07 17:11:54 +02:00
|
|
|
|
2020-09-06 11:38:10 +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)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
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]};
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-07-08 02:18:13 +02:00
|
|
|
$passes = true;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $amount < $fees_and_limits->min_limit) {
|
|
|
|
info("amount {$amount} less than ".$fees_and_limits->min_limit);
|
2020-07-08 02:18:13 +02:00
|
|
|
$passes = false;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $amount > $fees_and_limits->max_limit) {
|
|
|
|
info("amount {$amount} greater than ".$fees_and_limits->max_limit);
|
|
|
|
$passes = false;
|
|
|
|
} else {
|
2020-07-08 02:18:13 +02:00
|
|
|
$passes = true;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-07-08 02:18:13 +02:00
|
|
|
|
|
|
|
return $passes;
|
2020-07-07 16:50:51 +02:00
|
|
|
}
|
2020-08-26 00:10:49 +02:00
|
|
|
|
2020-08-30 00:00:57 +02:00
|
|
|
public function testFeesAreAppendedToInvoice() //after refactor this may be redundant
|
2020-08-26 00:10:49 +02:00
|
|
|
{
|
|
|
|
$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]['fee_cap'] = 0;
|
|
|
|
|
|
|
|
$cg = new CompanyGateway;
|
|
|
|
$cg->company_id = $this->company->id;
|
|
|
|
$cg->user_id = $this->user->id;
|
|
|
|
$cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23';
|
|
|
|
$cg->require_cvv = true;
|
|
|
|
$cg->show_billing_address = true;
|
|
|
|
$cg->show_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;
|
|
|
|
|
|
|
|
$this->invoice = $this->invoice->service()->addGatewayFee($cg, $this->invoice->balance)->save();
|
|
|
|
$this->invoice = $this->invoice->calc()->getInvoice();
|
|
|
|
|
|
|
|
$items = $this->invoice->line_items;
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->assertEquals(($balance + 1), $this->invoice->balance);
|
2020-08-26 00:10:49 +02:00
|
|
|
}
|
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;
|
|
|
|
$data[1]['fee_cap'] = 0;
|
|
|
|
|
|
|
|
$cg = new CompanyGateway;
|
|
|
|
$cg->company_id = $this->company->id;
|
|
|
|
$cg->user_id = $this->user->id;
|
|
|
|
$cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23';
|
|
|
|
$cg->require_cvv = true;
|
|
|
|
$cg->show_billing_address = true;
|
|
|
|
$cg->show_shipping_address = true;
|
|
|
|
$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;
|
2020-09-06 11:38:10 +02:00
|
|
|
$total_gateway_fee = round($cg->calcGatewayFee($total, true), 2);
|
2020-08-29 14:16:23 +02:00
|
|
|
|
|
|
|
$this->assertEquals(1.58, $total_gateway_fee);
|
|
|
|
|
|
|
|
/*simple pro rata*/
|
|
|
|
$fees_and_limits = $cg->getFeesAndLimits();
|
|
|
|
|
|
|
|
/*Calculate all subcomponents of the fee*/
|
|
|
|
|
|
|
|
// $fee_component_amount = $fees_and_limits->fee_amount ?: 0;
|
|
|
|
// $fee_component_percent = $fees_and_limits->fee_percent ? ($total * $fees_and_limits->fee_percent / 100) : 0;
|
|
|
|
|
|
|
|
// $combined_fee_component = $fee_component_amount + $fee_component_percent;
|
|
|
|
|
|
|
|
// $fee_component_tax_name1 = $fees_and_limits->fee_tax_name1 ?: '';
|
|
|
|
// $fee_component_tax_rate1 = $fees_and_limits->fee_tax_rate1 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate1 / 100) : 0;
|
|
|
|
|
|
|
|
// $fee_component_tax_name2 = $fees_and_limits->fee_tax_name2 ?: '';
|
|
|
|
// $fee_component_tax_rate2 = $fees_and_limits->fee_tax_rate2 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate2 / 100) : 0;
|
|
|
|
|
|
|
|
// $fee_component_tax_name3 = $fees_and_limits->fee_tax_name3 ?: '';
|
|
|
|
// $fee_component_tax_rate3 = $fees_and_limits->fee_tax_rate3 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate3 / 100) : 0;
|
|
|
|
|
|
|
|
// $pro_rata_fee = round($total_gateway_fee / $total_invoice_count,2);
|
|
|
|
|
|
|
|
// while($pro_rata_fee * $total_invoice_count != $total_gateway_fee) {
|
|
|
|
|
|
|
|
// //nudge one pro rata fee until we get the desired amount
|
|
|
|
// $sub_total_fees = ($pro_rata_fee*($total_invoice_count--));
|
|
|
|
|
|
|
|
// //work out if we have to nudge up or down
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-29 14:16:23 +02:00
|
|
|
// if($pro_rata_fee*$total_invoice_count > $total_gateway_fee) {
|
|
|
|
// //nudge DOWN
|
|
|
|
// $pro_rata_fee - 0.01; //this will break if the currency doesn't have decimals
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// //nudge UP
|
|
|
|
// }
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-29 14:16:23 +02:00
|
|
|
// }
|
|
|
|
|
|
|
|
// $this->assertEquals(1.56, $pro_rata_fee*$total_invoice_count);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|