Add endpoint for activating a subscription #799
This commit is contained in:
parent
aa85198b7a
commit
40c629eee3
1 changed files with 66 additions and 1 deletions
|
|
@ -7,6 +7,11 @@ use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
|||
use App\Service\PayMongoConnector;
|
||||
|
||||
use App\Entity\Vehicle;
|
||||
use App\Entity\Subscription;
|
||||
use App\Entity\CustomerVehicle;
|
||||
use App\Ramcar\SubscriptionStatus;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class SubscriptionController extends ApiController
|
||||
{
|
||||
|
|
@ -66,6 +71,7 @@ class SubscriptionController extends ApiController
|
|||
// check requirements
|
||||
$validity = $this->validateRequest($req, [
|
||||
'plan_id',
|
||||
'cv_id',
|
||||
]);
|
||||
|
||||
if (!$validity['is_valid']) {
|
||||
|
|
@ -78,6 +84,19 @@ class SubscriptionController extends ApiController
|
|||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// get customer vehicle
|
||||
$cv = $this->em->getRepository(CustomerVehicle::class)->find($req->request->get('cv_id'));
|
||||
|
||||
// check if it exists
|
||||
if ($cv == null) {
|
||||
return new ApiResponse(false, 'Vehicle does not exist.');
|
||||
}
|
||||
|
||||
// check if it's owned by customer
|
||||
if ($cv->getCustomer()->getID() != $cust->getID()) {
|
||||
return new ApiResponse(false, 'Invalid vehicle.');
|
||||
}
|
||||
|
||||
// initialize paymongo connector
|
||||
$this->initializeSubscriptionPayMongoConnector($pm);
|
||||
|
||||
|
|
@ -109,9 +128,20 @@ class SubscriptionController extends ApiController
|
|||
return new ApiResponse(false, 'Error retrieving payment intent. Please try again later.');
|
||||
}
|
||||
|
||||
// create subscription entity
|
||||
$obj = new Subscription();
|
||||
$obj->setCustomer($cust)
|
||||
->setCustomerVehicle($cv)
|
||||
->setStatus(SubscriptionStatus::PENDING)
|
||||
->setExtApiId($pm_sub['response']['data']['id'])
|
||||
->setMetadata($pm_sub['response']['data']);
|
||||
|
||||
$this->em->persist($obj);
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
//'subscription_id' => $pm_sub['response']['data']['id'],
|
||||
'subscription_id' => $obj->getID(),
|
||||
'payment_intent_id' => $pi['response']['data']['id'],
|
||||
'payment_intent_client_key' => $pi['response']['data']['attributes']['client_key'],
|
||||
'paymongo_public_key' => $this->getParameter('subscription_paymongo_public_key'),
|
||||
|
|
@ -141,4 +171,39 @@ class SubscriptionController extends ApiController
|
|||
'payment_intent' => $pi['response']['data'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function setSubscriptionAsPaid(Request $req, $sub_id, PayMongoConnector $pm)
|
||||
{
|
||||
// check requirements
|
||||
$validity = $this->validateRequest($req);
|
||||
|
||||
if (!$validity['is_valid']) {
|
||||
return new ApiResponse(false, $validity['error']);
|
||||
}
|
||||
|
||||
// initialize paymongo connector
|
||||
$this->initializeSubscriptionPayMongoConnector($pm);
|
||||
|
||||
// get subscription
|
||||
$obj = $this->em->getRepository(Subscription::class)->findOneBy([
|
||||
'id' => $sub_id,
|
||||
'status' => SubscriptionStatus::PENDING,
|
||||
'customer' => $this->session->getCustomer(),
|
||||
]);
|
||||
|
||||
if (empty($obj)) {
|
||||
return new ApiResponse(false, 'Invalid subscription provided.');
|
||||
}
|
||||
|
||||
// mark subscription as paid
|
||||
$obj->setStatus(SubscriptionStatus::ACTIVE)
|
||||
->setDateStart(new DateTime());
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'success' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue