135 lines
3.8 KiB
PHP
135 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Ramcar\InsuranceApplicationStatus;
|
|
use App\Service\FCMSender;
|
|
use App\Entity\InsuranceApplication;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
use DateTime;
|
|
|
|
class InsuranceController extends Controller
|
|
{
|
|
protected $em;
|
|
protected $fcmclient;
|
|
|
|
public function __construct(EntityManagerInterface $em, FCMSender $fcmclient)
|
|
{
|
|
$this->em = $em;
|
|
$this->fcmclient = $fcmclient;
|
|
}
|
|
|
|
public function listen(Request $req)
|
|
{
|
|
$payload = $req->request->all();
|
|
|
|
// DEBUG
|
|
@file_put_contents(__DIR__ . '/../../var/log/insurance.log', print_r($payload, true) . "\r\n----------------------------------------\r\n\r\n", FILE_APPEND);
|
|
error_log(print_r($payload, true));
|
|
|
|
/*
|
|
return $this->json([
|
|
'success' => true,
|
|
]);
|
|
*/
|
|
|
|
// END DEBUG
|
|
|
|
// if no transaction code given, silently fail
|
|
if (empty($payload['transaction_code'])) {
|
|
error_log("Invalid insurance callback received: " . print_r($payload, true));
|
|
|
|
return $this->json([
|
|
'success' => true,
|
|
]);
|
|
}
|
|
|
|
// get event type and process accordingly
|
|
$event_name = $payload['transaction_code'];
|
|
|
|
switch ($event_name) {
|
|
case 'GR002':
|
|
return $this->handleAuthenticated($payload);
|
|
break;
|
|
case 'GR003':
|
|
return $this->handleUpdateMade($payload);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return $this->json([
|
|
'success' => true,
|
|
'payload' => $payload,
|
|
]);
|
|
}
|
|
|
|
protected function handleAuthenticated($payload)
|
|
{
|
|
$obj = $this->getApplication($payload['id']);
|
|
$now = new DateTime();
|
|
$expiry = DateTime::createFromFormat("Y-m-d", $payload['expiry_date']);
|
|
|
|
if (!empty($obj)) {
|
|
// mark as completed
|
|
$obj->setStatus(InsuranceApplicationStatus::COMPLETED);
|
|
$obj->setDateComplete($now);
|
|
$obj->setDateExpire($expiry);
|
|
$obj->setCOC($payload['coc_url']);
|
|
$this->em->flush();
|
|
|
|
// send notification
|
|
$this->fcmclient->sendEvent($obj->getCustomer(), "insurance_fcm_title_completed", "insurance_fcm_body_completed", [
|
|
'cv_id' => $obj->getCustomerVehicle()->getID(),
|
|
]);
|
|
}
|
|
|
|
return $this->json([
|
|
'success' => true,
|
|
]);
|
|
}
|
|
|
|
protected function handleUpdateMade($payload)
|
|
{
|
|
$obj = $this->getApplication($payload['id']);
|
|
|
|
if (!empty($obj)) {
|
|
$metadata = $obj->getMetadata();
|
|
|
|
// initialize change list if not present
|
|
if (empty($metadata['changes'])) {
|
|
$metadata['changes'] = [];
|
|
}
|
|
|
|
$now = new DateTime;
|
|
$metadata['changes'][$now->format('Y-m-d H:i:s')] = $payload['data'];
|
|
|
|
// update metadata to record change
|
|
$obj->setMetadata($metadata);
|
|
$this->em->flush();
|
|
|
|
// send notification
|
|
$this->fcmclient->sendEvent($obj->getCustomer(), "insurance_fcm_title_updated", "insurance_fcm_body_updated", [
|
|
'cv_id' => $obj->getCustomerVehicle()->getID(),
|
|
]);
|
|
}
|
|
|
|
return $this->json([
|
|
'success' => true,
|
|
]);
|
|
}
|
|
|
|
protected function getApplication($transaction_id)
|
|
{
|
|
$result = $this->em->getRepository(InsuranceApplication::class)->findBy([
|
|
'ext_transaction_id' => $transaction_id,
|
|
], [], 1);
|
|
|
|
return !empty($result) ? $result[0] : false;
|
|
}
|
|
}
|