From 8c72321c09c070f11605500aa83b678de0de4803 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 5 Aug 2020 11:41:32 +1000 Subject: [PATCH] Refactor company gateway queries to ensure their order is respected --- app/Models/Client.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/Models/Client.php b/app/Models/Client.php index 3f8f9db09f7c..7ea45d0e4d29 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -364,10 +364,17 @@ class Client extends BaseModel implements HasLocalePreference { $company_gateways = $this->getSetting('company_gateway_ids'); + /* It is very important to respect the order of the company_gateway_ids as they are ordered by priority*/ if (strlen($company_gateways)>=1) { - $gateways = $this->company->company_gateways->whereIn('id', $company_gateways); + $transformed_ids = $this->transformKeys(explode(",", $company_gateways)); + $gateways = $this->company + ->company_gateways + ->whereIn('id', $transformed_ids) + ->sortby(function($model) use ($transformed_ids){ + return array_search($model->id, $transformed_ids); + }); } else { - $gateways = $this->company->company_gateways; + $gateways = $this->company->company_gateways;//todo perhaps we can remove this or keep as a catch all. } foreach ($gateways as $gateway) { @@ -384,7 +391,13 @@ class Client extends BaseModel implements HasLocalePreference $company_gateways = $this->getSetting('company_gateway_ids'); if (strlen($company_gateways)>=1) { - $gateways = $this->company->company_gateways->whereIn('id', $company_gateways); + $transformed_ids = $this->transformKeys(explode(",", $company_gateways)); + $gateways = $this->company + ->company_gateways + ->whereIn('id', $transformed_ids) + ->sortby(function($model) use ($transformed_ids){ + return array_search($model->id, $transformed_ids); + }); } else { $gateways = $this->company->company_gateways; } @@ -448,7 +461,14 @@ class Client extends BaseModel implements HasLocalePreference $company_gateways = $this->getSetting('company_gateway_ids'); if ($company_gateways) { - $gateways = $this->company->company_gateways->whereIn('id', $payment_gateways); //this will never hit + $transformed_ids = $this->transformKeys(explode(",", $company_gateways)); + $gateways = $this->company + ->company_gateways + ->whereIn('id', $transformed_ids) + ->sortby(function($model) use ($transformed_ids){ + return array_search($model->id, $transformed_ids); + }); + } else { $gateways = $this->company->company_gateways->where('is_deleted', false); }