Add support for handling multiple paymongo accounts #799

This commit is contained in:
Ramon Gutierrez 2024-08-12 05:09:58 +08:00
parent e3649c3d2d
commit debb399e96
7 changed files with 56 additions and 22 deletions

View file

@ -17,7 +17,12 @@ parameters:
ios_app_version: "%env(IOS_APP_VERSION)%"
insurance_premiums_banner_url: "%env(INSURANCE_PREMIUMS_BANNER_URL)%"
enabled_hub_filters: "%env(ENABLED_HUB_FILTERS)%"
insurance_paymongo_public_key: "%env(INSURANCE_PAYMONGO_PUBLIC_KEY)%"
insurance_paymongo_secret_key: "%env(INSURANCE_PAYMONGO_SECRET_KEY)%"
insurance_paymongo_webhook_id: "%env(INSURANCE_PAYMONGO_WEBHOOK_ID)%"
subscription_paymongo_public_key: "%env(SUBSCRIPTION_PAYMONGO_PUBLIC_KEY)%"
subscription_paymongo_secret_key: "%env(SUBSCRIPTION_PAYMONGO_SECRET_KEY)%"
subscription_paymongo_webhook_id: "%env(SUBSCRIPTION_PAYMONGO_WEBHOOK_ID)%"
services:
# default configuration for services in *this* file
@ -115,7 +120,6 @@ services:
arguments:
$em: "@doctrine.orm.entity_manager"
$paymongo: "@App\\Service\\PayMongoConnector"
$webhook_id: "%env(PAYMONGO_WEBHOOK_ID)%"
# rider tracker service
App\Service\RiderTracker:
@ -239,8 +243,6 @@ services:
App\Service\PayMongoConnector:
arguments:
$base_url: "%env(PAYMONGO_BASE_URL)%"
$public_key: "%env(PAYMONGO_PUBLIC_KEY)%"
$secret_key: "%env(PAYMONGO_SECRET_KEY)%"
# entity listener for customer vehicle warranty code history
App\EntityListener\CustomerVehicleSerialListener:

View file

@ -6,6 +6,7 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Doctrine\ORM\EntityManagerInterface;
@ -19,14 +20,18 @@ class ProcessLatePaymongoTransactionsCommand extends Command
{
protected $em;
protected $paymongo;
protected $webhook_id;
public function __construct(EntityManagerInterface $em, PayMongoConnector $paymongo, $webhook_id)
public function __construct(EntityManagerInterface $em, PayMongoConnector $paymongo, ParameterBagInterface $params)
{
$this->em = $em;
$this->webhook_id = $params->get('insurance_paymongo_webhook_id');
$this->paymongo = $paymongo;
$this->webhook_id = $webhook_id;
$this->paymongo->initialize(
$params->get('insurance_paymongo_public_key'),
$params->get('insurance_paymongo_secret_key')
);
parent::__construct();
}

View file

@ -46,10 +46,7 @@ class CustomerController extends ApiController
public function updateInfo(Request $req)
{
// validate params
$validity = $this->validateRequest($req, [
'first_name',
'last_name',
]);
$validity = $this->validateRequest($req);
if (!$validity['is_valid']) {
return new ApiResponse(false, $validity['error']);
@ -129,10 +126,21 @@ class CustomerController extends ApiController
$this->session->setCustomer($cust);
}
$cust->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email', ''))
->setConfirmed($this->session->isConfirmed());
if (!is_null($req->request->get('first_name'))) {
$cust->setFirstName($req->request->get('first_name'));
}
if (!is_null($req->request->get('last_name'))) {
$cust->setLastName($req->request->get('last_name'));
}
if (!is_null($req->request->get('email'))) {
$cust->setEmail($req->request->get('email'));
}
if (!is_null($this->session->isConfirmed())) {
$cust->setConfirmed($this->session->isConfirmed());
}
// if customer user isn't set, set it now
if ($cust->getCustomerUser() == null) {

View file

@ -3,6 +3,7 @@
namespace App\Controller\CustomerAppAPI;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Catalyst\ApiBundle\Component\Response as ApiResponse;
@ -33,7 +34,7 @@ class InsuranceController extends ApiController
$this->client = $client;
}
public function createApplication(Request $req, PayMongoConnector $paymongo, UrlGeneratorInterface $router)
public function createApplication(Request $req, PayMongoConnector $paymongo, UrlGeneratorInterface $router, ParameterBagInterface $params)
{
// validate params
$validity = $this->validateRequest($req, [
@ -162,6 +163,12 @@ class InsuranceController extends ApiController
$this->em->persist($gt);
$this->em->flush();
// initialize paymongo connector
$paymongo->initialize(
$params->get('insurance_paymongo_public_key'),
$params->get('insurance_paymongo_secret_key')
);
// create paymongo checkout resource
$checkout = $paymongo->createCheckout(
$cust,

View file

@ -28,7 +28,8 @@ class SubscriptionController extends ApiController
$batts = $vehicle->getActiveBatteries();
if (!empty($batts)) {
$fee = $batts[0]->getSize()->getSubRecurringFee() ?? 0;
$size_fee_raw = $batts[0]->getSize()->getSubRecurringFee();
$fee = $size_fee_raw ? bcmul($size_fee_raw, 100) : 0;
}
// response

View file

@ -3,6 +3,7 @@
namespace App\Controller\CustomerAppAPI;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Catalyst\ApiBundle\Component\Response as ApiResponse;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
@ -115,7 +116,7 @@ class VehicleController extends ApiController
}
public function getVehicle(Request $req, $id, PayMongoConnector $paymongo)
public function getVehicle(Request $req, $id, PayMongoConnector $paymongo, ParameterBagInterface $params)
{
// check requirements
$validity = $this->validateRequest($req);
@ -139,7 +140,7 @@ class VehicleController extends ApiController
// response
return new ApiResponse(true, '', [
'vehicle' => $this->generateVehicleInfo($cv, true, true, $paymongo),
'vehicle' => $this->generateVehicleInfo($cv, true, true, $paymongo, $params),
]);
}
@ -210,7 +211,7 @@ class VehicleController extends ApiController
]);
}
public function listVehicles(Request $req, PayMongoConnector $paymongo)
public function listVehicles(Request $req, PayMongoConnector $paymongo, ParameterBagInterface $params)
{
// validate params
$validity = $this->validateRequest($req);
@ -231,7 +232,7 @@ class VehicleController extends ApiController
// only get the customer's vehicles whose flag_active is true
$cvs = $this->em->getRepository(CustomerVehicle::class)->findBy(['flag_active' => true, 'customer' => $cust]);
foreach ($cvs as $cv) {
$cv_list[] = $this->generateVehicleInfo($cv, true, true, $paymongo);
$cv_list[] = $this->generateVehicleInfo($cv, true, true, $paymongo, $params);
}
// response
@ -417,7 +418,7 @@ class VehicleController extends ApiController
];
}
protected function generateVehicleInfo(CustomerVehicle $cv, $include_insurance = false, $include_active_sub = false, PayMongoConnector $paymongo)
protected function generateVehicleInfo(CustomerVehicle $cv, $include_insurance = false, $include_active_sub = false, PayMongoConnector $paymongo, ParameterBagInterface $params)
{
$battery_id = null;
if ($cv->getCurrentBattery() != null)
@ -469,6 +470,12 @@ class VehicleController extends ApiController
// TODO: maybe handle this more elegantly. issue is not sure it is a good idea to update the db for this very transient status as the webhook listener also updates this status right away
switch ($status) {
case InsuranceApplicationStatus::CREATED:
// initialize paymongo connector
$paymongo->initialize(
$params->get('insurance_paymongo_public_key'),
$params->get('insurance_paymongo_secret_key')
);
// get latest status on this checkout from paymongo
$checkout = $paymongo->getCheckout($gt->getExtTransactionId());

View file

@ -15,9 +15,13 @@ class PayMongoConnector
protected $secret_key;
protected $hash;
public function __construct($base_url, $public_key, $secret_key)
public function __construct($base_url)
{
$this->base_url = $base_url;
}
public function initialize($public_key, $secret_key)
{
$this->public_key = $public_key;
$this->secret_key = $secret_key;
$this->hash = $this->generateHash();