diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index c74d6721e7e5..388df11e4795 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -176,7 +176,7 @@ class PaymentController extends Controller // $fee_totals += $fee_tax; // } - $first_invoice->service()->addGatewayFee($gateway, $invoice_totals)->save(); + $first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save(); /** * diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 301e48e43e23..7742cee57d0e 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -232,6 +232,8 @@ class CompanyGateway extends BaseModel return false; } +info(print_r($this->fees_and_limits,1)); + return $this->fees_and_limits->{$gateway_type_id}; // $fees_and_limits = new \stdClass; @@ -250,7 +252,7 @@ class CompanyGateway extends BaseModel * @param Client $client The client object * @return string The fee amount formatted in the client currency */ - public function calcGatewayFeeLabel($amount, Client $client, GatewayType $gateway_type_id = GatewayType::CREDIT_CARD) :string + public function calcGatewayFeeLabel($amount, Client $client, $gateway_type_id = GatewayType::CREDIT_CARD) :string { $label = ''; @@ -268,8 +270,10 @@ class CompanyGateway extends BaseModel return $label; } - public function calcGatewayFee($amount, $include_taxes = false, GatewayType $gateway_type_id = GatewayType::CREDIT_CARD) + public function calcGatewayFee($amount, $include_taxes = false, $gateway_type_id = GatewayType::CREDIT_CARD) { + info($gateway_type_id); + $fees_and_limits = $this->getFeesAndLimits($gateway_type_id); if (! $fees_and_limits) { diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index f7a5005fd7b1..84af200f026e 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -31,18 +31,22 @@ class AddGatewayFee extends AbstractService private $amount; - public function __construct(CompanyGateway $company_gateway, Invoice $invoice, float $amount) + private $gateway_type_id; + + public function __construct(CompanyGateway $company_gateway, int $gateway_type_id, Invoice $invoice, float $amount) { $this->company_gateway = $company_gateway; $this->invoice = $invoice; $this->amount = $amount; + + $this->gateway_type_id = $gateway_type_id; } public function run() { - $gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount), $this->invoice->client->currency()->precision); + $gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount, $this->gateway_type_id), $this->invoice->client->currency()->precision); if((int)$gateway_fee == 0) return $this->invoice; @@ -78,7 +82,7 @@ class AddGatewayFee extends AbstractService $invoice_item->quantity = 1; $invoice_item->cost = $gateway_fee; - if ($fees_and_limits = $this->company_gateway->getFeesAndLimits()) { + if ($fees_and_limits = $this->company_gateway->getFeesAndLimits($this->gateway_type_id)) { $invoice_item->tax_rate1 = $fees_and_limits->fee_tax_rate1; $invoice_item->tax_rate2 = $fees_and_limits->fee_tax_rate2; $invoice_item->tax_rate3 = $fees_and_limits->fee_tax_rate3; diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index d814e382f7e8..325b8328ccea 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -93,9 +93,9 @@ class InvoiceService return $this; } - public function addGatewayFee(CompanyGateway $company_gateway, float $amount) + public function addGatewayFee(CompanyGateway $company_gateway, $gateway_type_id, float $amount) { - $this->invoice = (new AddGatewayFee($company_gateway, $this->invoice, $amount))->run(); + $this->invoice = (new AddGatewayFee($company_gateway, $gateway_type_id, $this->invoice, $amount))->run(); return $this; } diff --git a/app/Transformers/InvoiceTransformer.php b/app/Transformers/InvoiceTransformer.php index 980e1a2acf0b..a35986307900 100644 --- a/app/Transformers/InvoiceTransformer.php +++ b/app/Transformers/InvoiceTransformer.php @@ -95,6 +95,7 @@ class InvoiceTransformer extends EntityTransformer 'vendor_id' => (string) $this->encodePrimaryKey($invoice->vendor_id), 'status_id' => (string) ($invoice->status_id ?: 1), 'design_id' => (string) $this->encodePrimaryKey($invoice->design_id), + 'recurring_id' => (string) $this->encodePrimaryKey($invoice->recurring_id), 'created_at' => (int) $invoice->created_at, 'updated_at' => (int) $invoice->updated_at, 'archived_at' => (int) $invoice->deleted_at, diff --git a/tests/Feature/CompanyGatewayResolutionTest.php b/tests/Feature/CompanyGatewayResolutionTest.php index 51ff50206383..cbecd99f4586 100644 --- a/tests/Feature/CompanyGatewayResolutionTest.php +++ b/tests/Feature/CompanyGatewayResolutionTest.php @@ -18,6 +18,7 @@ use App\Listeners\Credit\CreateCreditInvitation; use App\Models\Client; use App\Models\CompanyGateway; use App\Models\Credit; +use App\Models\GatewayType; use App\Models\Invoice; use App\Models\Payment; use App\Models\Paymentable; @@ -41,6 +42,10 @@ class CompanyGatewayResolutionTest extends TestCase use DatabaseTransactions; use MockAccountData; + public $cg; + + public $cg1; + public function setUp() :void { parent::setUp(); @@ -64,7 +69,7 @@ class CompanyGatewayResolutionTest extends TestCase $data = []; $data[1]['min_limit'] = -1; $data[1]['max_limit'] = -1; - $data[1]['fee_amount'] = 1.00; + $data[1]['fee_amount'] = 0.00; $data[1]['fee_percent'] = 2; $data[1]['fee_tax_name1'] = 'GST'; $data[1]['fee_tax_rate1'] = 10; @@ -74,21 +79,33 @@ class CompanyGatewayResolutionTest extends TestCase $data[1]['fee_tax_rate3'] = 10; $data[1]['fee_cap'] = 0; - $json_config = config('ninja.testvars.stripe'); + $data[2]['min_limit'] = -1; + $data[2]['max_limit'] = -1; + $data[2]['fee_amount'] = 0.00; + $data[2]['fee_percent'] = 1; + $data[2]['fee_tax_name1'] = 'GST'; + $data[2]['fee_tax_rate1'] = 10; + $data[2]['fee_tax_name2'] = 'GST'; + $data[2]['fee_tax_rate2'] = 10; + $data[2]['fee_tax_name3'] = 'GST'; + $data[2]['fee_tax_rate3'] = 10; + $data[2]['fee_cap'] = 0; + + $json_config = json_decode(config('ninja.testvars.stripe')); $json_config->enable_ach = "0"; //disable ach here - $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(json_encode($json_config)); - $cg->fees_and_limits = $data; - $cg->save(); + $this->cg = new CompanyGateway; + $this->cg->company_id = $this->company->id; + $this->cg->user_id = $this->user->id; + $this->cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $this->cg->require_cvv = true; + $this->cg->show_billing_address = true; + $this->cg->show_shipping_address = true; + $this->cg->update_details = true; + $this->cg->config = encrypt(json_encode($json_config)); + $this->cg->fees_and_limits = $data; + $this->cg->save(); $data = []; $data[2]['min_limit'] = -1; @@ -104,23 +121,34 @@ class CompanyGatewayResolutionTest extends TestCase $data[2]['fee_cap'] = 0; //ensable ach here - $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->cg1 = new CompanyGateway; + $this->cg1->company_id = $this->company->id; + $this->cg1->user_id = $this->user->id; + $this->cg1->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $this->cg1->require_cvv = true; + $this->cg1->show_billing_address = true; + $this->cg1->show_shipping_address = true; + $this->cg1->update_details = true; + $this->cg1->config = encrypt(config('ninja.testvars.stripe')); + $this->cg1->fees_and_limits = $data; + $this->cg1->save(); } + /** + * @covers \App\Models\CompanyGateway::calcGatewayFee() + */ public function testGatewayResolution() { - $this->assertTrue(true); - + //i want to test here resolution of bank_transfers inside and outside of fees and limits. + + $fee = $this->cg->calcGatewayFee(10, false, GatewayType::CREDIT_CARD); + + $this->assertEquals(0.2, $fee); + + $fee = $this->cg->calcGatewayFee(10, false, GatewayType::BANK_TRANSFER); + + $this->assertEquals(0.1, $fee); + } } diff --git a/tests/Feature/CompanyGatewayTest.php b/tests/Feature/CompanyGatewayTest.php index 9d1618fa31bb..8dc1499fc17a 100644 --- a/tests/Feature/CompanyGatewayTest.php +++ b/tests/Feature/CompanyGatewayTest.php @@ -142,7 +142,7 @@ class CompanyGatewayTest extends TestCase $balance = $this->invoice->balance; - $this->invoice = $this->invoice->service()->addGatewayFee($cg, $this->invoice->balance)->save(); + $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;