Handle insurance callbacks, add support for non-JO related FCM notifications #761
This commit is contained in:
parent
dd9c9dd0ec
commit
9b616cf398
5 changed files with 168 additions and 28 deletions
|
|
@ -158,7 +158,6 @@ class InsuranceController extends ApiController
|
||||||
$gt->setStatus(TransactionStatus::PENDING);
|
$gt->setStatus(TransactionStatus::PENDING);
|
||||||
$gt->setGateway('paymongo'); // TODO: define values elsewhere
|
$gt->setGateway('paymongo'); // TODO: define values elsewhere
|
||||||
$gt->setType('insurance_premium'); // TODO: define values elsewhere
|
$gt->setType('insurance_premium'); // TODO: define values elsewhere
|
||||||
$gt->setExtTransactionId($result['response']['id']);
|
|
||||||
$this->em->persist($gt);
|
$this->em->persist($gt);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
|
|
@ -179,9 +178,9 @@ class InsuranceController extends ApiController
|
||||||
$checkout_url = $checkout['response']['data']['attributes']['checkout_url'];
|
$checkout_url = $checkout['response']['data']['attributes']['checkout_url'];
|
||||||
|
|
||||||
// add checkout url and id to transaction metadata
|
// add checkout url and id to transaction metadata
|
||||||
|
$gt->setExtTransactionId($checkout['response']['data']['id']);
|
||||||
$gt->setMetadata([
|
$gt->setMetadata([
|
||||||
'checkout_url' => $checkout_url,
|
'checkout_url' => $checkout_url,
|
||||||
'checkout_id' => $checkout['response']['data']['id'],
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// store application in db
|
// store application in db
|
||||||
|
|
@ -191,8 +190,11 @@ class InsuranceController extends ApiController
|
||||||
$app->setCustomerVehicle($cv);
|
$app->setCustomerVehicle($cv);
|
||||||
$app->setGatewayTransaction($gt);
|
$app->setGatewayTransaction($gt);
|
||||||
$app->setStatus(InsuranceApplicationStatus::CREATED);
|
$app->setStatus(InsuranceApplicationStatus::CREATED);
|
||||||
|
$app->setExtTransactionId($result['response']['id']);
|
||||||
$app->setMetadata($input);
|
$app->setMetadata($input);
|
||||||
$this->em->persist($app);
|
$this->em->persist($app);
|
||||||
|
|
||||||
|
// save everything
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
|
|
||||||
// return
|
// return
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,116 @@
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Ramcar\InsuranceApplicationStatus;
|
||||||
|
use App\Service\FCMSender;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
class InsuranceController extends Controller
|
class InsuranceController extends Controller
|
||||||
{
|
{
|
||||||
public function listen(Request $req, EntityManagerInterface $em)
|
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();
|
$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));
|
error_log(print_r($payload, true));
|
||||||
|
|
||||||
|
/*
|
||||||
|
return $this->json([
|
||||||
|
'success' => true,
|
||||||
|
]);
|
||||||
|
*/
|
||||||
|
|
||||||
|
// END DEBUG
|
||||||
|
|
||||||
|
// 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([
|
return $this->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'payload' => $payload,
|
'payload' => $payload,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function handleAuthenticated($payload)
|
||||||
|
{
|
||||||
|
$obj = $this->getApplication($payload['id']);
|
||||||
|
|
||||||
|
if (!empty($obj)) {
|
||||||
|
// mark as completed
|
||||||
|
$obj->setStatus(InsuranceApplicationStatus::COMPLETED);
|
||||||
|
$obj->setCOC($payload['coc_url']);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
// send notification
|
||||||
|
$this->fcmclient->sendEvent($obj->getCustomer(), "insurance_fcm_title_completed", "insurance_fcm_body_completed");
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,12 @@ class InsuranceApplication
|
||||||
*/
|
*/
|
||||||
protected $date_complete;
|
protected $date_complete;
|
||||||
|
|
||||||
|
// external transaction id
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
|
*/
|
||||||
|
protected $ext_transaction_id;
|
||||||
|
|
||||||
// form data when submitting the application
|
// form data when submitting the application
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="json")
|
* @ORM\Column(type="json")
|
||||||
|
|
@ -179,6 +185,17 @@ class InsuranceApplication
|
||||||
return $this->date_complete;
|
return $this->date_complete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setExtTransactionId($transaction_id)
|
||||||
|
{
|
||||||
|
$this->ext_transaction_id = $transaction_id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getExtTransactionId()
|
||||||
|
{
|
||||||
|
return $this->ext_transaction_id;
|
||||||
|
}
|
||||||
|
|
||||||
public function setMetadata($metadata)
|
public function setMetadata($metadata)
|
||||||
{
|
{
|
||||||
return $this->metadata = $metadata;
|
return $this->metadata = $metadata;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Entity\Customer;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
use Fcm\FcmClient;
|
use Fcm\FcmClient;
|
||||||
use Fcm\Push\Notification;
|
use Fcm\Push\Notification;
|
||||||
|
|
@ -53,42 +54,62 @@ class FCMSender
|
||||||
|
|
||||||
public function sendJoEvent(JobOrder $job_order, $title, $body, $data = [])
|
public function sendJoEvent(JobOrder $job_order, $title, $body, $data = [])
|
||||||
{
|
{
|
||||||
// get all v2 sessions
|
// get customer object
|
||||||
|
$cust = $job_order->getCustomer();
|
||||||
|
|
||||||
|
// attach jo info
|
||||||
|
$data['jo_id'] = $job_order->getID();
|
||||||
|
$data['jo_status'] = $job_order->getStatus();
|
||||||
|
|
||||||
|
// send the event
|
||||||
|
return $this->sendEvent($cust, $title, $body, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sendEvent(Customer $cust, $title, $body, $data = [])
|
||||||
|
{
|
||||||
|
// get all v2 devices
|
||||||
|
$devices = $this->getDevices($cust);
|
||||||
|
|
||||||
|
if (empty($devices)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// send fcm notification
|
||||||
|
$result = $this->send(array_keys($devices), $this->translator->trans($title), $this->translator->trans($body), $data);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getDevices(Customer $cust)
|
||||||
|
{
|
||||||
$sessions = [];
|
$sessions = [];
|
||||||
$cust_user = $job_order->getCustomer()->getCustomerUser();
|
$device_ids = [];
|
||||||
|
|
||||||
|
$cust_user = $cust->getCustomerUser();
|
||||||
if (!empty($cust_user)) {
|
if (!empty($cust_user)) {
|
||||||
$sessions = $cust_user->getMobileSessions();
|
$sessions = $cust_user->getMobileSessions();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($sessions)) {
|
if (empty($sessions)) {
|
||||||
error_log("no sessions to send fcm notification to");
|
error_log("no sessions to send fcm notification to");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$device_ids = [];
|
|
||||||
|
|
||||||
// send to every customer session
|
// send to every customer session
|
||||||
foreach ($sessions as $sess) {
|
foreach ($sessions as $sess) {
|
||||||
$device_id = $sess->getDevicePushID();
|
$device_id = $sess->getDevicePushID();
|
||||||
|
|
||||||
if (!empty($device_id) && !isset($device_ids[$device_id])) {
|
if (!empty($device_id) && !isset($device_ids[$device_id])) {
|
||||||
// send fcm notification
|
// send to this device
|
||||||
$device_ids[$device_id] = true;
|
$device_ids[$device_id] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($device_ids)) {
|
if (empty($device_ids)) {
|
||||||
error_log("no devices to send fcm notification to");
|
error_log("no devices to send fcm notification to");
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attach jo info
|
return $device_ids;
|
||||||
$data['jo_id'] = $job_order->getID();
|
|
||||||
$data['jo_status'] = $job_order->getStatus();
|
|
||||||
|
|
||||||
// send fcm notification
|
|
||||||
$result = $this->send(array_keys($device_ids), $this->translator->trans($title), $this->translator->trans($body), $data);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -160,13 +160,19 @@ menu.database.ownershiptypes: 'Ownership Types'
|
||||||
menu.database.serviceofferings: 'Service Offerings'
|
menu.database.serviceofferings: 'Service Offerings'
|
||||||
|
|
||||||
# fcm jo status updates
|
# fcm jo status updates
|
||||||
jo_fcm_title_outlet_assign: Looking for riders
|
jo_fcm_title_outlet_assign: 'Looking for riders'
|
||||||
jo_fcm_title_driver_assigned: Rider found
|
jo_fcm_title_driver_assigned: 'Rider found'
|
||||||
jo_fcm_title_driver_arrived: Rider nearby
|
jo_fcm_title_driver_arrived: 'Rider nearby'
|
||||||
jo_fcm_title_cancelled: Order cancelled
|
jo_fcm_title_cancelled: 'Order cancelled'
|
||||||
jo_fcm_title_fulfilled: Thank you!
|
jo_fcm_title_fulfilled: 'Thank you!'
|
||||||
jo_fcm_body_outlet_assign: We're assigning a rider for your order, please wait.
|
jo_fcm_body_outlet_assign: 'We`re assigning a rider for your order, please wait.'
|
||||||
jo_fcm_body_driver_assigned: A rider is on their way.
|
jo_fcm_body_driver_assigned: 'A rider is on their way.'
|
||||||
jo_fcm_body_driver_arrived: Your order is almost there!
|
jo_fcm_body_driver_arrived: 'Your order is almost there!'
|
||||||
jo_fcm_body_cancelled: Your order has been cancelled.
|
jo_fcm_body_cancelled: 'Your order has been cancelled.'
|
||||||
jo_fcm_body_fulfilled: Order complete! Your receipt is ready.
|
jo_fcm_body_fulfilled: 'Order complete! Your receipt is ready.'
|
||||||
|
|
||||||
|
# fcm insurance
|
||||||
|
insurance_fcm_title_updated: 'Application updated'
|
||||||
|
insurance_fcm_title_completed: 'Application completed'
|
||||||
|
insurance_fcm_body_updated: 'Some details on your insurance application have been updated.'
|
||||||
|
insurance_fcm_body_completed: 'Your insurance application has been processed!'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue