src/Service/StripePaymentService.php line 15

  1. <?php
  2. namespace App\Service;
  3. use Stripe\Stripe;
  4. use Stripe\Checkout\Session;
  5. use Symfony\Component\DependencyInjection\Attribute\Autowire;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. class StripePaymentService
  8. {
  9.     private string $stripeSecretKey;
  10.     private UrlGeneratorInterface $router;
  11.     public function __construct(
  12.         #[Autowire('%env(STRIPE_SECRET_KEY)%')] string $stripeSecretKey 'sk_test_demo',
  13.         UrlGeneratorInterface $router
  14.     ) {
  15.         $this->stripeSecretKey $stripeSecretKey;
  16.         $this->router $router;
  17.     }
  18.     public function createCheckoutSession(array $cartItems): ?string
  19.     {
  20.         // Stripe::setApiKey($this->stripeSecretKey);
  21.         $lineItems = [];
  22.         foreach ($cartItems as $item) {
  23.             $lineItems[] = [
  24.                 'price_data' => [
  25.                     'currency' => 'eur',
  26.                     'product_data' => [
  27.                         'name' => $item['product']->getName() . ' - Format ' $item['format'],
  28.                         'description' => 'Impression PRO',
  29.                     ],
  30.                     'unit_amount' => (int) ($item['print_unit_price'] * 100), // En centimes
  31.                 ],
  32.                 'quantity' => $item['quantity'],
  33.             ];
  34.         }
  35.         // Simulation de création de session pour éviter l'erreur sans lib Stripe installée
  36.         // Dans le réel :
  37.         /*
  38.         $session = Session::create([
  39.             'payment_method_types' => ['card'],
  40.             'line_items' => $lineItems,
  41.             'mode' => 'payment',
  42.             'success_url' => $this->router->generate('app_payment_success', [], UrlGeneratorInterface::ABSOLUTE_URL),
  43.             'cancel_url' => $this->router->generate('app_cart_index', [], UrlGeneratorInterface::ABSOLUTE_URL),
  44.         ]);
  45.         return $session->url;
  46.         */
  47.         // Retourne une URL fictive pour la démo
  48.         return $this->router->generate('app_payment_success');
  49.     }
  50. }