From 763fa6ef0864c7a1d85f02b03d451b53d87e8b16 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 11 Dec 2020 08:09:04 +1100 Subject: [PATCH 1/2] Fix for custom gateway query --- app/Models/Client.php | 48 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/app/Models/Client.php b/app/Models/Client.php index 922beefd2098..43194b30760c 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -13,6 +13,8 @@ namespace App\Models; use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; +use App\Models\CompanyGateway; +use App\Models\Gateway; use App\Models\Presenters\ClientPresenter; use App\Services\Client\ClientService; use App\Utils\Traits\GeneratesCounter; @@ -452,11 +454,15 @@ class Client extends BaseModel implements HasLocalePreference $gateways = $this->company ->company_gateways ->whereIn('id', $transformed_ids) + ->where('gateway_key', '!=', '54faab2ab6e3223dbe848b1686490baa') ->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); + $gateways = $this->company + ->company_gateways + ->where('gateway_key', '!=', '54faab2ab6e3223dbe848b1686490baa') + ->where('is_deleted', false); } $payment_methods = []; @@ -478,11 +484,49 @@ class Client extends BaseModel implements HasLocalePreference //** Plucks the remaining keys into its own collection $payment_methods_intersect = $payment_methods_collections->intersectByKeys($payment_methods_collections->flatten(1)->unique()); + //handle custom gateways which are not unique'd() + + //we need to check for "0" here as we disable a payment gateway for a client with the number "0" + if ($company_gateways || $company_gateways == '0') { + $transformed_ids = $this->transformKeys(explode(',', $company_gateways)); + $gateways = $this->company + ->company_gateways + ->whereIn('id', $transformed_ids) + ->where('gateway_key', '=', '54faab2ab6e3223dbe848b1686490baa') + ->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('gateway_key', '=', '54faab2ab6e3223dbe848b1686490baa') + ->where('is_deleted', false); + } + + //note we have to use GatewayType::CREDIT_CARD as alias for CUSTOM + foreach ($gateways as $gateway) { + foreach ($gateway->driver($this)->gatewayTypes() as $type) { + if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) { + if ($this->validGatewayForAmount($gateway->fees_and_limits->{GatewayType::CREDIT_CARD}, $amount)) { + $payment_methods_intersect->push([$gateway->id => $type]); + } + } else { + $payment_methods_intersect->push([$gateway->id => $type]); + } + } + } + + //handle custom gateways which are not unique'd() + + + + + $payment_urls = []; foreach ($payment_methods_intersect as $key => $child_array) { foreach ($child_array as $gateway_id => $gateway_type_id) { - $gateway = $gateways->where('id', $gateway_id)->first(); + $gateway = CompanyGateway::find($gateway_id); $fee_label = $gateway->calcGatewayFeeLabel($amount, $this); From f49faf8f161073d539e8dd5c4899e450612b6724 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 11 Dec 2020 08:10:50 +1100 Subject: [PATCH 2/2] Fix for custom gateway query --- app/Models/Client.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/Models/Client.php b/app/Models/Client.php index 43194b30760c..35ba155cef61 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -484,9 +484,10 @@ class Client extends BaseModel implements HasLocalePreference //** Plucks the remaining keys into its own collection $payment_methods_intersect = $payment_methods_collections->intersectByKeys($payment_methods_collections->flatten(1)->unique()); - //handle custom gateways which are not unique'd() - - //we need to check for "0" here as we disable a payment gateway for a client with the number "0" + // handle custom gateways as they are not unique'd()--------------------------------------------------------- + // we need to split the query here as we allow multiple custom gateways, so we must show all of them, they query logic + // above only pulls in unique gateway types.. ie.. we only allow 1 credit card gateway, but many custom gateways. + if ($company_gateways || $company_gateways == '0') { $transformed_ids = $this->transformKeys(explode(',', $company_gateways)); $gateways = $this->company @@ -516,10 +517,7 @@ class Client extends BaseModel implements HasLocalePreference } } - //handle custom gateways which are not unique'd() - - - + //handle custom gateways as they are not unique'd()--------------------------------------------------------- $payment_urls = [];