From 3d468e5f40ca18d20d628abc45ba2fd0a205a4c0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 22 Jan 2021 07:45:00 +1100 Subject: [PATCH 1/2] Company Gateway cleanup --- .../StoreCompanyGatewayRequest.php | 18 ++++++++++++------ .../CompanyGatewayFeesAndLimitsSaver.php | 18 ------------------ 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php index ecd9e410c768..9e3ebe6f3763 100644 --- a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php +++ b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php @@ -25,6 +25,7 @@ class StoreCompanyGatewayRequest extends Request * * @return bool */ + public function authorize() : bool { return auth()->user()->isAdmin(); @@ -43,27 +44,32 @@ class StoreCompanyGatewayRequest extends Request protected function prepareForValidation() { $input = $this->all(); + $gateway = Gateway::where('key', $input['gateway_key'])->first(); $default_gateway_fields = json_decode($gateway->fields); /*Force gateway properties */ if (isset($input['config']) && is_object(json_decode($input['config']))) { + foreach (json_decode($input['config']) as $key => $value) { + $default_gateway_fields->{$key} = $value; + } $input['config'] = json_encode($default_gateway_fields); + } - if (isset($input['config'])) { + if (isset($input['config'])) $input['config'] = encrypt($input['config']); - } - - if (isset($input['fees_and_limits'])) { + + if (isset($input['fees_and_limits'])) $input['fees_and_limits'] = $this->cleanFeesAndLimits($input['fees_and_limits']); - } - + $this->replace($input); } + + } diff --git a/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php b/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php index e8f31ac1b7fc..4863a0f5ab54 100644 --- a/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php +++ b/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php @@ -78,24 +78,6 @@ trait CompanyGatewayFeesAndLimitsSaver } } - // public function cleanFeesAndLimits($fees_and_limits) - // { - // $new_arr = []; - - // foreach ($fees_and_limits as $key => $value) { - // $fal = new FeesAndLimits; - - // foreach ($value as $k => $v) { - // $fal->{$k} = $v; - // $fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v); - // } - - // $new_arr[$key] = (array)$fal; - // } - - // return $new_arr; - // } - // public function cleanFeesAndLimits($fees_and_limits) { $new_arr = []; From 4b87df07b703a84af467a1e7b58fdd1ba70fefb1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 22 Jan 2021 08:36:25 +1100 Subject: [PATCH 2/2] Check that a company gateway always has at least one fee and limits object --- app/Factory/CompanyGatewayFactory.php | 4 +++- app/Http/Controllers/CompanyGatewayController.php | 14 ++++++++++++++ app/Models/CompanyGateway.php | 1 - 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/Factory/CompanyGatewayFactory.php b/app/Factory/CompanyGatewayFactory.php index cc7ec46297c0..f19391ce3b78 100644 --- a/app/Factory/CompanyGatewayFactory.php +++ b/app/Factory/CompanyGatewayFactory.php @@ -11,6 +11,7 @@ namespace App\Factory; +use App\DataMapper\FeesAndLimits; use App\Models\CompanyGateway; class CompanyGatewayFactory @@ -20,7 +21,8 @@ class CompanyGatewayFactory $company_gateway = new CompanyGateway; $company_gateway->company_id = $company_id; $company_gateway->user_id = $user_id; - + // $company_gateway->fees_and_limits = new FeesAndLimits; + return $company_gateway; } } diff --git a/app/Http/Controllers/CompanyGatewayController.php b/app/Http/Controllers/CompanyGatewayController.php index 3674f1aa4ed2..6e0bba277b30 100644 --- a/app/Http/Controllers/CompanyGatewayController.php +++ b/app/Http/Controllers/CompanyGatewayController.php @@ -11,6 +11,7 @@ namespace App\Http\Controllers; +use App\DataMapper\FeesAndLimits; use App\Factory\CompanyGatewayFactory; use App\Http\Requests\CompanyGateway\CreateCompanyGatewayRequest; use App\Http\Requests\CompanyGateway\DestroyCompanyGatewayRequest; @@ -18,6 +19,7 @@ use App\Http\Requests\CompanyGateway\EditCompanyGatewayRequest; use App\Http\Requests\CompanyGateway\ShowCompanyGatewayRequest; use App\Http\Requests\CompanyGateway\StoreCompanyGatewayRequest; use App\Http\Requests\CompanyGateway\UpdateCompanyGatewayRequest; +use App\Models\Client; use App\Models\CompanyGateway; use App\Repositories\CompanyRepository; use App\Transformers\CompanyGatewayTransformer; @@ -191,6 +193,18 @@ class CompanyGatewayController extends BaseController $company_gateway->fill($request->all()); $company_gateway->save(); + /*Always ensure at least one fees and limits object is set per gateway*/ + if(!isset($company_gateway->fees_and_limits)) { + + $gateway_types = $company_gateway->driver(new Client)->gatewayTypes(); + + $fees_and_limits = new \stdClass; + $fees_and_limits->{$gateway_types[0]} = new FeesAndLimits; + + $company_gateway->fees_and_limits = $fees_and_limits; + $company_gateway->save(); + } + return $this->itemResponse($company_gateway); } diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 51fc3eb722da..9823753544c4 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -108,7 +108,6 @@ class CompanyGateway extends BaseModel private function driver_class() { $class = 'App\\PaymentDrivers\\'.$this->gateway->provider.'PaymentDriver'; - //$class = str_replace('\\', '', $class); $class = str_replace('_', '', $class); if (class_exists($class)) {