Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller;
- use App\Entity\Subscription;
- use App\Services\StripeClient;
- use App\Services\SubscriptionHelper;
- use Psr\Log\LoggerInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Routing\Annotation\Route;
- class WebhookController extends AbstractController
- {
- /**
- * @Route("/webhooks/stripe", name="webhook_stripe")
- */
- public function stripeWebhookAction(Request $request, StripeClient $stripeClient, SubscriptionHelper $subscriptionHelper, $shouldVerifyStripeEvent)
- {
- $data = json_decode($request->getContent(), true);
- if($data === null)
- {
- throw new \Exception("Bad JSON body from Stripe !");
- }
- $eventId = $data['id'];
- if($shouldVerifyStripeEvent)
- {
- $stripeEvent = $stripeClient->findEvent($eventId);
- }
- else
- {
- // test environment
- $stripeEvent = json_decode($request->getContent());
- }
- switch($stripeEvent->type)
- {
- case "customer.subscription.deleted":
- //Cancel complètement l'abonnement de l'utilisateur
- $stripeSubscriptionId = $stripeEvent->data->object->id;
- $subscription = $this->findSubscription($stripeSubscriptionId);
- $subscriptionHelper->fullyCancelSubscription($subscription);
- break;
- default:
- throw new \Exception("Unexpected webhook from Stripe ".$stripeEvent->type);
- }
- return new Response('Event handled: '.$stripeEvent->type);
- }
- private function findSubscription($stripeSubscriptionId)
- {
- $subscription = $this->getDoctrine()->getRepository(Subscription::class)
- ->findOneBy([
- 'stripeSubscriptionId' => $stripeSubscriptionId
- ]);
- if(!$subscription)
- {
- throw new \Exception("Il semblerait qu'il n'y ait pas d'id d'abonnement ".$stripeSubscriptionId);
- }
- return $subscription;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement