Add support for transient status between making a payment and receiving the webhook #761

This commit is contained in:
Ramon Gutierrez 2023-10-23 01:17:05 +08:00
parent b1ff62d9ec
commit 2432a08c4d

View file

@ -8,7 +8,8 @@ use Catalyst\ApiBundle\Component\Response as ApiResponse;
use App\Entity\CustomerVehicle; use App\Entity\CustomerVehicle;
use App\Entity\VehicleManufacturer; use App\Entity\VehicleManufacturer;
use App\Entity\Vehicle; use App\Entity\Vehicle;
use App\Ramcar\InsuranceApplicationStatus;
use App\Service\PayMongoConnector;
use DateTime; use DateTime;
class VehicleController extends ApiController class VehicleController extends ApiController
@ -107,7 +108,7 @@ class VehicleController extends ApiController
} }
public function getVehicle(Request $req, $id) public function getVehicle(Request $req, $id, PayMongoConnector $paymongo)
{ {
// check requirements // check requirements
$validity = $this->validateRequest($req); $validity = $this->validateRequest($req);
@ -131,7 +132,7 @@ class VehicleController extends ApiController
// response // response
return new ApiResponse(true, '', [ return new ApiResponse(true, '', [
'vehicle' => $this->generateVehicleInfo($cv, true), 'vehicle' => $this->generateVehicleInfo($cv, true, $paymongo),
]); ]);
} }
@ -169,7 +170,7 @@ class VehicleController extends ApiController
]); ]);
} }
public function listVehicles(Request $req) public function listVehicles(Request $req, PayMongoConnector $paymongo)
{ {
// validate params // validate params
$validity = $this->validateRequest($req); $validity = $this->validateRequest($req);
@ -190,7 +191,7 @@ class VehicleController extends ApiController
// only get the customer's vehicles whose flag_active is true // only get the customer's vehicles whose flag_active is true
$cvs = $this->em->getRepository(CustomerVehicle::class)->findBy(['flag_active' => true, 'customer' => $cust]); $cvs = $this->em->getRepository(CustomerVehicle::class)->findBy(['flag_active' => true, 'customer' => $cust]);
foreach ($cvs as $cv) { foreach ($cvs as $cv) {
$cv_list[] = $this->generateVehicleInfo($cv, true); $cv_list[] = $this->generateVehicleInfo($cv, true, $paymongo);
} }
// response // response
@ -283,7 +284,7 @@ class VehicleController extends ApiController
return new ApiResponse(); return new ApiResponse();
} }
protected function generateVehicleInfo(CustomerVehicle $cv, $include_insurance = false) protected function generateVehicleInfo(CustomerVehicle $cv, $include_insurance = false, PayMongoConnector $paymongo)
{ {
$battery_id = null; $battery_id = null;
if ($cv->getCurrentBattery() != null) if ($cv->getCurrentBattery() != null)
@ -325,11 +326,37 @@ class VehicleController extends ApiController
$gt = $iobj->getGatewayTransaction(); $gt = $iobj->getGatewayTransaction();
$date_complete = $iobj->getDateComplete(); $date_complete = $iobj->getDateComplete();
$date_expire = $iobj->getDateExpire(); $date_expire = $iobj->getDateExpire();
$status = $iobj->getStatus();
error_log("\r\nTHIS IS THE CURRENT STATUS: " . $status . "\r\n");
// handle the very transient state between a payment being made and receiving the paymongo webhook
// 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:
// get latest status on this checkout from paymongo
$checkout = $paymongo->getCheckout($gt->getExtTransactionId());
if ($checkout['success']) {
$payment_intent = $checkout['response']['data']['attributes']['payment_intent'] ?? null;
if (!empty($payment_intent)) {
$intent_status = $payment_intent['attributes']['status'] ?? null;
// TODO: define these paymongo payment intent statuses elsewhere
if ($intent_status === 'processing' || $intent_status === 'succeeded') {
$status = InsuranceApplicationStatus::PAID;
}
}
}
break;
default:
break;
}
$insurance = [ $insurance = [
'id' => $iobj->getID(), 'id' => $iobj->getID(),
'ext_transaction_id' => $iobj->getExtTransactionId(), 'ext_transaction_id' => $iobj->getExtTransactionId(),
'status' => $iobj->getStatus(), 'status' => $status,
'coc_url' => $iobj->getCOC(), 'coc_url' => $iobj->getCOC(),
'checkout_url' => $gt->getMetadata()['checkout_url'], 'checkout_url' => $gt->getMetadata()['checkout_url'],
'transaction_status' => $gt->getStatus(), 'transaction_status' => $gt->getStatus(),