From a52267ca76f0ae67cea43881d467cd5953c2db40 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Sat, 6 May 2023 03:24:45 +0800 Subject: [PATCH] Initial implementation of insurance connector #730 --- config/services.yaml | 7 ++ src/Service/InsuranceConnector.php | 100 +++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/Service/InsuranceConnector.php diff --git a/config/services.yaml b/config/services.yaml index d7cd73b2..cdd04db8 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -199,6 +199,13 @@ services: $sub_key: "%env(MOTIV_KEY)%" $token: "%env(MOTIV_TOKEN)%" + # insurance connector + App\Service\InsuranceConnector: + arguments: + $base_url: "%env(INSURANCE_BASE_URL)%" + $username: "%env(INSURANCE_USERNAME)%" + $password: "%env(INSURANCE_PASSWORD)%" + # entity listener for customer vehicle warranty code history App\EntityListener\CustomerVehicleSerialListener: arguments: diff --git a/src/Service/InsuranceConnector.php b/src/Service/InsuranceConnector.php new file mode 100644 index 00000000..e10066a2 --- /dev/null +++ b/src/Service/InsuranceConnector.php @@ -0,0 +1,100 @@ +base_url = $base_url; + $this->username = $username; + $this->password = $password; + $this->hash = $this->generateHash(); + } + + public function createApplication($notif_url, $client_info, $client_contact_info, $car_info) + { + $body = [ + 'notif_url' => $notif_url, + 'client_info' => $client_info, + 'client_contact_info' => $client_contact_info, + 'car_info' => $car_info, + ]; + + return $this->doRequest('/api/v1/ctpl/applications', true, $body); + } + + public function tagApplicationPaid($application_id) + { + $url = '/api/v1/ctpl/application/' . $application_id . '/paid'; + return $this->doRequest($url, true); + } + + public function getVehicleMakers() + { + return $this->doRequest('/api/v1/ctpl/vehicle-makers'); + } + + public function getVehicleModels() + { + return $this->doRequest('/api/v1/ctpl/vehicle-models'); + } + + public function getVehicleTrims() + { + return $this->doRequest('/api/v1/ctpl/vehicle-trims'); + } + + protected function generateHash() + { + return base64_encode($this->username . ":" . $this->password); + } + + protected function doRequest($url, $is_post = false, $body = []) + { + $curl = curl_init(); + + $options = [ + CURLOPT_URL => $this->base_url . '/' . $url, + CURLOPT_POST => $is_post, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + 'Authorization: Basic ' . $this->hash, + ], + ]; + + // add post body if present + if (!empty($body)) { + $options[CURLOPT_POSTFIELDS] = json_encode($body); + } + + curl_setopt_array($curl, $options); + $res = curl_exec($curl); + + curl_close($curl); + + error_log('Insurance API connector'); + error_log(print_r($options, true)); + error_log($res); + + // response + return $this->handleResponse($res); + } + + protected function handleResponse($res) + { + $inv_res = json_decode($res, true); + + // make sure result is always an array + if ($inv_res == null) + return []; + + return $inv_res; + } +}