Refactor company gateway queries to ensure their order is respected

This commit is contained in:
David Bomba 2020-08-05 11:41:32 +10:00
parent 64d7edf513
commit 8c72321c09

View File

@ -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);
}