131 lines
4.3 KiB
PHP
131 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Entity\CustomerVehicle;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Psr7;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
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(CustomerVehicle $cv, $notif_url, $data, $orcr_file)
|
|
{
|
|
$body = [
|
|
'notif_url' => $notif_url,
|
|
'client_info' => [
|
|
'client_type' => $data['client_type'],
|
|
'first_name' => $data['first_name'],
|
|
'middle_name' => $data['middle_name'] ?? null,
|
|
'surname' => $data['surname'],
|
|
'corporate_name' => $data['corporate_name'],
|
|
],
|
|
'client_contact_info' => [
|
|
'address_number' => $data['address_number'],
|
|
'address_street' => $data['address_street'] ?? null,
|
|
'address_building' => $data['address_building'] ?? null,
|
|
'address_barangay' => $data['address_barangay'],
|
|
'address_city' => $data['address_city'],
|
|
'address_province' => $data['address_province'],
|
|
'zipcode' => (int)$data['zipcode'],
|
|
'mobile_number' => $data['mobile_number'],
|
|
'email_address' => $data['email_address'],
|
|
],
|
|
'car_info' => [
|
|
'make' => $data['make'],
|
|
'model' => $data['model'],
|
|
'series' => $data['series'],
|
|
'color' => $data['color'],
|
|
'plate_number' => $cv->getPlateNumber(),
|
|
'mv_file_number' => $data['mv_file_number'],
|
|
'motor_number' => $data['motor_number'],
|
|
'serial_chasis' => $data['serial_chasis'],
|
|
'year_model' => (int)$data['year_model'],
|
|
'mv_type_id' => (int)$data['mv_type_id'],
|
|
'body_type' => $data['body_type'],
|
|
'is_public' => (bool)$data['is_public'],
|
|
'line' => $data['line'],
|
|
'orcr_file' => base64_encode(file_get_contents($orcr_file->getPathname())),
|
|
],
|
|
];
|
|
|
|
return $this->doRequest('/api/v1/ctpl/applications', 'POST', $body);
|
|
}
|
|
|
|
public function tagApplicationPaid($application_id)
|
|
{
|
|
$url = '/api/v1/ctpl/application/' . $application_id . '/paid';
|
|
return $this->doRequest($url, 'POST');
|
|
}
|
|
|
|
public function getVehicleMakers()
|
|
{
|
|
return $this->doRequest('/api/v1/ctpl/vehicle-makers', 'GET');
|
|
}
|
|
|
|
public function getVehicleModels($maker_id)
|
|
{
|
|
return $this->doRequest('/api/v1/ctpl/vehicle-models?maker_id='. $maker_id, 'GET');
|
|
}
|
|
|
|
public function getVehicleTrims($model_id)
|
|
{
|
|
return $this->doRequest('/api/v1/ctpl/vehicle-trims?model_id='. $model_id, 'GET');
|
|
}
|
|
|
|
protected function generateHash()
|
|
{
|
|
return base64_encode($this->username . ":" . $this->password);
|
|
}
|
|
|
|
protected function doRequest($url, $method, $body = [])
|
|
{
|
|
$client = new Client();
|
|
$headers = [
|
|
'Content-Type' => 'application/json',
|
|
'accept' => 'application/json',
|
|
'authorization' => 'Basic '. $this->hash,
|
|
];
|
|
|
|
try {
|
|
$response = $client->request($method, $this->base_url . '/' . $url, [
|
|
'json' => $body,
|
|
'headers' => $headers,
|
|
]);
|
|
} catch (RequestException $e) {
|
|
$error = ['message' => $e->getMessage()];
|
|
|
|
error_log("Insurance API Error: " . $error['message']);
|
|
error_log(Psr7\Message::toString($e->getRequest()));
|
|
|
|
if ($e->hasResponse()) {
|
|
$error['response'] = Psr7\Message::toString($e->getResponse());
|
|
}
|
|
|
|
return [
|
|
'success' => false,
|
|
'error' => $error,
|
|
];
|
|
}
|
|
|
|
//error_log(print_r(json_decode($response->getBody(), true), true));
|
|
|
|
return [
|
|
'success' => true,
|
|
'response' => json_decode($response->getBody(), true)
|
|
];
|
|
}
|
|
}
|