Maxlego08

Stripe Webhook

Oct 2nd, 2021 (edited)
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Payment\Utils;
  4.  
  5. use App\Models\Payment\PaymentGateway;
  6. use Stripe\Exception\ApiErrorException;
  7. use Stripe\StripeClient;
  8.  
  9. class StripeWebhook
  10. {
  11.  
  12.     /**
  13.      * @var StripeClient
  14.      */
  15.     private $stripe;
  16.  
  17.     /**
  18.      * @var string
  19.      */
  20.     private $gatewayName = "stripe";
  21.  
  22.     /**
  23.      *
  24.      */
  25.     public function __construct()
  26.     {
  27.         $sk = env('STRIPE_SK');
  28.         $this->stripe = new StripeClient($sk);
  29.     }
  30.  
  31.     /**
  32.      * Permet de créer le webhook de stripe
  33.      *
  34.      * @throws ApiErrorException
  35.      */
  36.     public function make()
  37.     {
  38.         $gateway = PaymentGateway::where('name', $this->gatewayName)->first();
  39.         if ($this->endpointIsAlreadyCreate() && isset($gateway))
  40.             return;
  41.  
  42.         $this->deleteEndPointIfAlreadyExist();
  43.  
  44.         $response = $this->stripe->webhookEndpoints->create([
  45.             'url' => $this->getRouteEndPoint(),
  46.             'enabled_events' => [
  47.                 'checkout.session.completed',
  48.                 'checkout.session.async_payment_succeeded',
  49.                 'checkout.session.async_payment_failed',
  50.                 'charge.dispute.created',
  51.                 'charge.refunded',
  52.             ],
  53.             'description' => 'Webhook for groupez.xyz'
  54.         ]);
  55.         if (isset($gateway))
  56.             $gateway->update(['data' => $response->secret]);
  57.         else {
  58.             PaymentGateway::create([
  59.                 'name' => $this->gatewayName,
  60.                 'data' => $response->secret,
  61.             ]);
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Permet de vérifier si le endpoint existe ou pas
  67.      *
  68.      * @return bool
  69.      * @throws ApiErrorException
  70.      */
  71.     private function endpointIsAlreadyCreate(): bool
  72.     {
  73.         foreach ($this->stripe->webhookEndpoints->all() as $endpoint) {
  74.             if ($endpoint->url === $this->getRouteEndPoint())
  75.                 return true;
  76.         }
  77.         return false;
  78.     }
  79.  
  80.     /**
  81.      * Permet de supprimer les routes en trop
  82.      */
  83.     private function deleteEndPointIfAlreadyExist()
  84.     {
  85.         try {
  86.             foreach ($this->stripe->webhookEndpoints->all() as $endpoint) {
  87.                 if ($endpoint->url === $this->getRouteEndPoint())
  88.                     $this->stripe->webhookEndpoints->delete($endpoint->id);
  89.             }
  90.         } catch (ApiErrorException $e) {
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Permet de retourner la route
  96.      * @return string
  97.      */
  98.     private function getRouteEndPoint(): string
  99.     {
  100.         return route('api.v1.notification', ['gateway' => $this->gatewayName]);
  101.     }
  102.  
  103. }
  104.  
Add Comment
Please, Sign In to add comment