src/Service/StripePaymentService.php line 15
<?phpnamespace App\Service;use Stripe\Stripe;use Stripe\Checkout\Session;use Symfony\Component\DependencyInjection\Attribute\Autowire;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;class StripePaymentService{private string $stripeSecretKey;private UrlGeneratorInterface $router;public function __construct(#[Autowire('%env(STRIPE_SECRET_KEY)%')] string $stripeSecretKey = 'sk_test_demo',UrlGeneratorInterface $router) {$this->stripeSecretKey = $stripeSecretKey;$this->router = $router;}public function createCheckoutSession(array $cartItems): ?string{// Stripe::setApiKey($this->stripeSecretKey);$lineItems = [];foreach ($cartItems as $item) {$lineItems[] = ['price_data' => ['currency' => 'eur','product_data' => ['name' => $item['product']->getName() . ' - Format ' . $item['format'],'description' => 'Impression PRO',],'unit_amount' => (int) ($item['print_unit_price'] * 100), // En centimes],'quantity' => $item['quantity'],];}// Simulation de création de session pour éviter l'erreur sans lib Stripe installée// Dans le réel :/*$session = Session::create(['payment_method_types' => ['card'],'line_items' => $lineItems,'mode' => 'payment','success_url' => $this->router->generate('app_payment_success', [], UrlGeneratorInterface::ABSOLUTE_URL),'cancel_url' => $this->router->generate('app_cart_index', [], UrlGeneratorInterface::ABSOLUTE_URL),]);return $session->url;*/// Retourne une URL fictive pour la démoreturn $this->router->generate('app_payment_success');}}