resq/src/Service/InsuranceConnector.php

158 lines
5.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, $request_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' => $request_body,
'headers' => $headers,
]);
} catch (RequestException $e) {
$error = ['message' => $e->getMessage()];
error_log("Insurance API Error: " . $error['message']);
error_log(Psr7\Message::toString($e->getRequest()));
error_log($e->getResponse()->getBody()->getContents());
error_log("Insurance Creds: " . $this->username . ", " . $this->password);
error_log("Insurance Hash: " . $this->generateHash());
// log this error
$this->log($url, Psr7\Message::toString($e->getRequest()), Psr7\Message::toString($e->getResponse()), 'error');
if ($e->hasResponse()) {
$error['response'] = Psr7\Message::toString($e->getResponse());
}
return [
'success' => false,
'error' => $error,
];
}
$result_body = $response->getBody();
// log response
$this->log($url, json_encode($request_body), $result_body);
return [
'success' => true,
'response' => json_decode($result_body, true),
];
}
// TODO: make this more elegant
public function log($title, $request_body = "[]", $result_body = "[]", $type = 'api')
{
$filename = '/../../var/log/insurance_' . $type . '.log';
$date = date("Y-m-d H:i:s");
// build log entry
$entry = implode("\r\n", [
$date,
$title,
"REQUEST:\r\n" . $request_body,
"RESPONSE:\r\n" . $result_body,
"\r\n----------------------------------------\r\n\r\n",
]);
@file_put_contents(__DIR__ . $filename, $entry, FILE_APPEND);
}
}