diff --git a/app/Models/Client.php b/app/Models/Client.php index fe041aff0333..f4af6cee2fab 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -474,54 +474,30 @@ class Client extends BaseModel implements HasLocalePreference $gateways = $this->company ->company_gateways ->whereIn('id', $transformed_ids) - ->sortby(function ($model) use ($transformed_ids) { - return array_search($model->id, $transformed_ids); + ->sortby(function ($model) use ($transformed_ids) { //company gateways are sorted in order of priority + return array_search($model->id, $transformed_ids);// this closure sorts for us }); } else { $gateways = $this->company->company_gateways->where('is_deleted', false); } - - - // $valid_gateways = $gateways->filter(function ($method) use ($amount) { - // if (isset($method->fees_and_limits)) { - // //sometimes the key value of the fees and limits object are not static, - // //we have to harvest the key value as follows - // //Update!!! apparently we use the gateway_type_id - // $properties = array_keys(get_object_vars($method->fees_and_limits)); - // $fees_and_limits = $method->fees_and_limits->{$properties[0]}; //need to iterate over the $properties array as there may be many fees_and_limits - // } else { - // return true; - // } - - // if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $fees_and_limits->min_limit != -1 && $amount < $fees_and_limits->min_limit) { - // return false; - // } - - // if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $fees_and_limits->max_limit != -1 && $amount > $fees_and_limits->max_limit) { - // return false; - // } - - // return true; - // })->all(); - - - $payment_methods = []; foreach ($gateways as $gateway) { foreach ($gateway->driver($this)->gatewayTypes() as $type) { - // info(print_r($gateway,1)); - // info($type); - if(property_exists($gateway->fees_and_limits, $type)) + + if(isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) { - if($this->validGatewayForAmount($gateway->fees_and_limits->{$type}, $amount)){ + + if($this->validGatewayForAmount($gateway->fees_and_limits->{$type}, $amount)) $payment_methods[] = [$gateway->id => $type]; - } + } else { + $payment_methods[] = [$gateway->id => $type]; + } } @@ -544,7 +520,7 @@ class Client extends BaseModel implements HasLocalePreference 'label' => ctrans('texts.'.$gateway->getTypeAlias($gateway_type_id)).$fee_label, 'company_gateway_id' => $gateway_id, 'gateway_type_id' => $gateway_type_id, - ]; + ]; } } diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 0bdcea04cbeb..b05d20a94b05 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -263,7 +263,6 @@ class CompanyGateway extends BaseModel 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); diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 80c3e414c63e..42bc0498e094 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -77,7 +77,7 @@ class AutoBillInvoice extends AbstractService return $this->invoice; /* $gateway fee */ - $fee = $gateway_token->gateway->calcGatewayFee($amount); + $fee = $gateway_token->gateway->calcGatewayFee($amount, $this->invoice->uses_inclusive_taxes); //todo determine exact fee as per PaymentController diff --git a/tests/Feature/CompanyGatewayResolutionTest.php b/tests/Feature/CompanyGatewayResolutionTest.php index 8a54062985e3..fd2cc0db0f1d 100644 --- a/tests/Feature/CompanyGatewayResolutionTest.php +++ b/tests/Feature/CompanyGatewayResolutionTest.php @@ -92,10 +92,10 @@ class CompanyGatewayResolutionTest extends TestCase $data[2]['fee_tax_rate3'] = 10; $data[2]['fee_cap'] = 0; + //disable ach here $json_config = json_decode(config('ninja.testvars.stripe')); $json_config->enable_ach = "0"; - //disable ach here $this->cg = new CompanyGateway; $this->cg->company_id = $this->company->id; $this->cg->user_id = $this->user->id; @@ -108,31 +108,7 @@ class CompanyGatewayResolutionTest extends TestCase $this->cg->fees_and_limits = $data; $this->cg->save(); - // $data = []; - // $data[2]['min_limit'] = -1; - // $data[2]['max_limit'] = -1; - // $data[2]['fee_amount'] = 1.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; - // //ensable ach here - // $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(); } /** @@ -142,11 +118,8 @@ class CompanyGatewayResolutionTest extends TestCase { $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); } @@ -175,7 +148,6 @@ class CompanyGatewayResolutionTest extends TestCase if(property_exists($this->cg->fees_and_limits, $type)) { if($this->client->validGatewayForAmount($this->cg->fees_and_limits->{$type}, $amount)){ - info("valid gateways"); $payment_methods[] = [$this->cg->id => $type]; } }