Replace sub fee endpoint with complete plan details #799
This commit is contained in:
parent
b3548fcc50
commit
7af20f3d69
3 changed files with 39 additions and 24 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue