diff --git a/config/services.yaml b/config/services.yaml index a5d7d05f..b2460c3f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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)%" diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php new file mode 100644 index 00000000..7599321a --- /dev/null +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -0,0 +1,75 @@ +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; + } + +} diff --git a/src/Service/InsuranceConnector.php b/src/Service/InsuranceConnector.php new file mode 100644 index 00000000..b29d6225 --- /dev/null +++ b/src/Service/InsuranceConnector.php @@ -0,0 +1,76 @@ +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; + } +}