Initial implementation of insurance connector #730

This commit is contained in:
Ramon Gutierrez 2023-05-06 03:24:45 +08:00
parent 3861a9bf93
commit a52267ca76
2 changed files with 107 additions and 0 deletions

View file

@ -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:

View file

@ -0,0 +1,100 @@
<?php
namespace App\Service;
class InsuranceConnector
{
protected $base_url;
protected $username;
protected $password;
protected $hash;
public function __construct($base_url, $username, $password)
{
$this->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;
}
}