Add service to connect to insurance API. Add test command to test service. #727

This commit is contained in:
Korina Cordero 2023-01-09 07:26:36 +00:00
parent 08ecd2ebea
commit a9e2d95ab4
3 changed files with 159 additions and 0 deletions

View file

@ -328,3 +328,11 @@ services:
App\Service\WarrantySerialLoadLogger:
arguments:
$em: "@doctrine.orm.entity_manager"
# insurance connector
App\Service\InsuranceConnector:
arguments:
$base_url: "%env(INSURANCE_BASE_URL)%"
$username: "%env(INSURANCE_USERNAME)%"
$password: "%env(INSURANCE_PASSWORD)%"
$token: "%env(INSURANCE_TOKEN)%"

View file

@ -0,0 +1,75 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Service\InsuranceConnector;
class TestInsuranceConnectorCommand extends Command
{
protected $insurance;
protected function configure()
{
$this->setName('test:create-insurance-application')
->setDescription('Test create insurance application service.')
->setHelp('Test the create insuranace application service.');
}
public function __construct(InsuranceConnector $insurance)
{
$this->insurance = $insurance;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client_info = [
'client_type' => 'I',
'first_name' => '',
'middle_name' => 'Resq',
'surname' => 'Customer',
'corporate_name' => 'Customer, Test Resq'
];
$client_contact_info = [
'address_number' => '112',
'address_street' => 'Test Street',
'address_building' => 'Test Building',
'address_barangay' => 'Legaspi Village',
'address_city' => 'Makati City',
'address_province' => 'Metro Manila',
'zipcode' => '1200',
'mobile_number' => '09171234567',
'email_address' => 'test.resq@gmail.com'
];
$car_info = [
'make' => 'HYUNDAI',
'model' => 'ACCENT',
'series' => '1.4 A/T',
'color' => 'PHANTOM BLACK',
'plate_number' => 'ABC1234',
'mv_file_number' => '123456789012345',
'motor_number' => 'E31TE-0075268',
'serial_chasis' => 'PA0SEF210K0075701',
'year_model' => '2020',
'mv_type_id' => 1,
'body_type' => 'SEDAN',
'is_public' => false,
'line' => 'pcoc'
];
$result = $this->insurance->createApplication($client_info, $client_contact_info, $car_info);
error_log(json_encode($result));
return 0;
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace App\Service;
class InsuranceConnector
{
protected $base_url;
protected $username;
protected $password;
protected $token;
public function __construct($base_url, $username, $password, $token)
{
$this->base_url = $base_url;
$this->username = $username;
$this->password = $password;
$this->token = $token;
}
public function createApplication($client_info = [], $client_contact_info = [], $car_info = [])
{
$body = [
'client_info' => $client_info,
'client_contact_info' => $client_contact_info,
'car_info' => $car_info,
];
$body_text = json_encode($body);
// generate token for basic authorization
$token = $this->generateAuthToken($this->username, $this->password);
$res = $this->curlPost('api/v1/ctpl/applications', $body_text, $token);
$app_res = json_decode($res, true);
// check result
if ($app_res == null)
return [];
return $app_res;
}
protected function generateAuthToken($username, $password)
{
$token = base64_encode($username . ':' . $password);
// error_log('token ' . $token);
return $token;
}
protected function curlPost($url, $body, $token)
{
$curl = curl_init();
$options = [
CURLOPT_URL => $this->base_url . '/' . $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Basic ' . $token,
],
];
curl_setopt_array($curl, $options);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}