From 2ffca37e74988fc3621304ad941ba3d93971ce49 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 9 Sep 2019 14:54:39 +1000 Subject: [PATCH] Working on gateways and gateway fees --- app/Models/Client.php | 7 +++--- app/Models/CompanyGateway.php | 45 ++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/Models/Client.php b/app/Models/Client.php index 1cbe8b5d0346..0a49f99fe264 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -181,19 +181,20 @@ class Client extends BaseModel //** Reduce gateways so that only one TYPE is present in the list ie. cannot have multiple credit card options $payment_methods_collections = collect($payment_methods); + $payment_methods_intersect = $payment_methods_collections->intersectByKeys( $payment_methods_collections->flatten(1)->unique() ); $payment_list = $payment_methods_intersect->map(function ($value, $key) { $gateway = $gateways->where('id', $key)->first(); - $fee_label = $gateway->calcGatewayFee($amount, $this); + $fee_label = $gateway->calcGatewayFeeLabel($amount, $this); return [ 'company_gateway_id' => $key, 'payment_method_id' => $value, - 'url' => $label - 'label' => ctrans('texts.'$gateway->type->alias) . $fee_label, + 'url' => $label, + 'label' => ctrans('texts.' . $gateway->type->alias) . $fee_label, ]; }); diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 891fac8a7f62..6c953940dae1 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -14,6 +14,7 @@ namespace App\Models; use App\Models\Company; use App\Models\Gateway; use App\Models\GatewayType; +use App\Utils\Number; use Illuminate\Database\Eloquent\Model; class CompanyGateway extends BaseModel @@ -128,6 +129,11 @@ class CompanyGateway extends BaseModel return ! empty($this->config('enablePayPal')); } + public function feesEnabled() + { + return floatval($this->fee_amount) || floatval($this->fee_percent); + } + /** * Returns the formatted fee amount for the gateway * @@ -135,11 +141,44 @@ class CompanyGateway extends BaseModel * @param Client $client The client object * @return string The fee amount formatted in the client currency */ - public function calcGatewayFee($amount, Client $client) :string + public function calcGatewayFeeLabel($amount, Client $client) :string { + $label = ''; - $fee = ''; + if(!$this->feesEnabled()) + return $label; + $fee = $this->calcGatewayFee($amount); + + if($fee > 0 ){ + $fee = Number::formatMoney(round($fee, 2), $client->currency(), $client->country(), $client->getMergedSettings()); + $label = ' - ' . $fee . ' ' . ctrans('texts.fee'); + } + + return $label; } - + + public function calcGatewayFee($amount) + { + $fee = 0; + + if ($this->fee_amount) + $fee += $this->fee_amount; + + if ($this->fee_percent) + $fee += $amount * $this->fee_percent / 100; + + $pre_tax_fee = $fee; + + if ($this->fee_tax_rate1) + $fee += $pre_tax_fee * $this->fee_tax_rate1 / 100; + + + if ($this->fee_tax_rate2) + $fee += $pre_tax_fee * $this->fee_tax_rate2 / 100; + + + return $fee; + } + }