From 7af20f3d69c3eea9d6609b3a25cd59788c818b70 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Mon, 12 Aug 2024 16:30:49 +0800 Subject: [PATCH] Replace sub fee endpoint with complete plan details #799 --- config/routes/apiv2.yaml | 6 +-- .../CustomerAppAPI/SubscriptionController.php | 16 ++++++-- src/Service/PayMongoConnector.php | 41 +++++++++++-------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/config/routes/apiv2.yaml b/config/routes/apiv2.yaml index ae9e6404..6702a5a2 100644 --- a/config/routes/apiv2.yaml +++ b/config/routes/apiv2.yaml @@ -321,9 +321,9 @@ apiv2_static_content: methods: [GET] # subscription -apiv2_subscription_fee: - path: /apiv2/subscription/fee/{vid} - controller: App\Controller\CustomerAppAPI\SubscriptionController::getRecurringFee +apiv2_subscription_plan_details: + path: /apiv2/subscription/vehicle/{vid}/plan + controller: App\Controller\CustomerAppAPI\SubscriptionController::getPlanDetails methods: [GET] apiv2_subscription_paymongo_public_key: diff --git a/src/Controller/CustomerAppAPI/SubscriptionController.php b/src/Controller/CustomerAppAPI/SubscriptionController.php index 45df060c..cda8f0e0 100644 --- a/src/Controller/CustomerAppAPI/SubscriptionController.php +++ b/src/Controller/CustomerAppAPI/SubscriptionController.php @@ -4,12 +4,13 @@ namespace App\Controller\CustomerAppAPI; use Symfony\Component\HttpFoundation\Request; use Catalyst\ApiBundle\Component\Response as ApiResponse; +use App\Service\PayMongoConnector; use App\Entity\Vehicle; class SubscriptionController extends ApiController { - public function getRecurringFee(Request $req, $vid) + public function getPlanDetails(Request $req, $vid, PayMongoConnector $pm) { // check requirements $validity = $this->validateRequest($req); @@ -24,17 +25,24 @@ class SubscriptionController extends ApiController return new ApiResponse(false, 'Invalid vehicle.'); } + $plan = null; + // get compatible batteries $batts = $vehicle->getActiveBatteries(); if (!empty($batts)) { - $size_fee_raw = $batts[0]->getSize()->getSubRecurringFee(); - $fee = $size_fee_raw ? bcmul($size_fee_raw, 100) : 0; + // initialize paymongo + $pm->initialize( + $this->getParameter('subscription_paymongo_public_key'), + $this->getParameter('subscription_paymongo_secret_key'), + ); + + $plan = $pm->getPlanByBatterySize($batts[0]->getSize()); } // response return new ApiResponse(true, '', [ - 'amount' => $fee, + 'plan' => $plan, ]); } diff --git a/src/Service/PayMongoConnector.php b/src/Service/PayMongoConnector.php index 92eda90d..68ff17ba 100644 --- a/src/Service/PayMongoConnector.php +++ b/src/Service/PayMongoConnector.php @@ -94,6 +94,28 @@ class PayMongoConnector 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) { $body = [ @@ -128,27 +150,12 @@ class PayMongoConnector public function createOrUpdateSubPlan(BatterySize $bsize) { - // get all plans - $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; - } - } - } + $found_plan = $this->getPlanByBatterySize($bsize); if (!empty($found_plan)) { // update existing plan $result = $this->updatePlan($found_plan['id'], [ - 'amount' => bcmul($bsize->getSubRecurringFee(), 100), + 'amount' => (int)bcmul($bsize->getSubRecurringFee(), 100), ]); } else { // create new plan