Replace sub fee endpoint with complete plan details #799

This commit is contained in:
Ramon Gutierrez 2024-08-12 16:30:49 +08:00
parent b3548fcc50
commit 7af20f3d69
3 changed files with 39 additions and 24 deletions

View file

@ -321,9 +321,9 @@ apiv2_static_content:
methods: [GET] methods: [GET]
# subscription # subscription
apiv2_subscription_fee: apiv2_subscription_plan_details:
path: /apiv2/subscription/fee/{vid} path: /apiv2/subscription/vehicle/{vid}/plan
controller: App\Controller\CustomerAppAPI\SubscriptionController::getRecurringFee controller: App\Controller\CustomerAppAPI\SubscriptionController::getPlanDetails
methods: [GET] methods: [GET]
apiv2_subscription_paymongo_public_key: apiv2_subscription_paymongo_public_key:

View file

@ -4,12 +4,13 @@ namespace App\Controller\CustomerAppAPI;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Catalyst\ApiBundle\Component\Response as ApiResponse; use Catalyst\ApiBundle\Component\Response as ApiResponse;
use App\Service\PayMongoConnector;
use App\Entity\Vehicle; use App\Entity\Vehicle;
class SubscriptionController extends ApiController class SubscriptionController extends ApiController
{ {
public function getRecurringFee(Request $req, $vid) public function getPlanDetails(Request $req, $vid, PayMongoConnector $pm)
{ {
// check requirements // check requirements
$validity = $this->validateRequest($req); $validity = $this->validateRequest($req);
@ -24,17 +25,24 @@ class SubscriptionController extends ApiController
return new ApiResponse(false, 'Invalid vehicle.'); return new ApiResponse(false, 'Invalid vehicle.');
} }
$plan = null;
// get compatible batteries // get compatible batteries
$batts = $vehicle->getActiveBatteries(); $batts = $vehicle->getActiveBatteries();
if (!empty($batts)) { if (!empty($batts)) {
$size_fee_raw = $batts[0]->getSize()->getSubRecurringFee(); // initialize paymongo
$fee = $size_fee_raw ? bcmul($size_fee_raw, 100) : 0; $pm->initialize(
$this->getParameter('subscription_paymongo_public_key'),
$this->getParameter('subscription_paymongo_secret_key'),
);
$plan = $pm->getPlanByBatterySize($batts[0]->getSize());
} }
// response // response
return new ApiResponse(true, '', [ return new ApiResponse(true, '', [
'amount' => $fee, 'plan' => $plan,
]); ]);
} }

View file

@ -94,6 +94,28 @@ class PayMongoConnector
return $this->doRequest('/v1/subscriptions/plans', 'GET'); return $this->doRequest('/v1/subscriptions/plans', 'GET');
} }
public function getPlanByBatterySize(BatterySize $bsize)
{
// get all plans
$plans = $this->getPlans();
// find the plan with the matching metadata for plan ID
$found_plan = null;
if ($plans['success'] && !empty($plans['response']['data'])) {
foreach ($plans['response']['data'] as $plan) {
$plan_bsize_id = $plan['attributes']['metadata']['battery_size_id'] ?? null;
if ($plan_bsize_id == $bsize->getID()) {
$found_plan = $plan;
break;
}
}
}
return $found_plan;
}
public function createPlan($plan_data) public function createPlan($plan_data)
{ {
$body = [ $body = [
@ -128,27 +150,12 @@ class PayMongoConnector
public function createOrUpdateSubPlan(BatterySize $bsize) public function createOrUpdateSubPlan(BatterySize $bsize)
{ {
// get all plans $found_plan = $this->getPlanByBatterySize($bsize);
$plans = $this->getPlans();
// find the plan with the matching substring (battery size name)
$found_plan = null;
if ($plans['success'] && !empty($plans['response']['data'])) {
foreach ($plans['response']['data'] as $plan) {
$plan_bsize_id = $plan['attributes']['metadata']['battery_size_id'] ?? null;
if (!empty($plan_bsize_id)) {
$found_plan = $plan;
break;
}
}
}
if (!empty($found_plan)) { if (!empty($found_plan)) {
// update existing plan // update existing plan
$result = $this->updatePlan($found_plan['id'], [ $result = $this->updatePlan($found_plan['id'], [
'amount' => bcmul($bsize->getSubRecurringFee(), 100), 'amount' => (int)bcmul($bsize->getSubRecurringFee(), 100),
]); ]);
} else { } else {
// create new plan // create new plan