import stripe import os from typing import Dict, Any from fastapi import HTTPException stripe.api_key = os.getenv("STRIPE_SECRET_KEY") class StripeService: @staticmethod def create_payment_intent(amount: float, currency: str = "usd", metadata: Dict[str, Any] = None) -> Dict[str, Any]: try: intent = stripe.PaymentIntent.create( amount=int(amount * 100), # Stripe expects amount in cents currency=currency, metadata=metadata or {}, automatic_payment_methods={'enabled': True} ) return { "client_secret": intent.client_secret, "payment_intent_id": intent.id, "amount": intent.amount / 100, "status": intent.status } except stripe.error.StripeError as e: raise HTTPException(status_code=400, detail=f"Stripe error: {str(e)}") @staticmethod def confirm_payment_intent(payment_intent_id: str) -> Dict[str, Any]: try: intent = stripe.PaymentIntent.retrieve(payment_intent_id) return { "payment_intent_id": intent.id, "status": intent.status, "amount": intent.amount / 100 } except stripe.error.StripeError as e: raise HTTPException(status_code=400, detail=f"Stripe error: {str(e)}") @staticmethod def cancel_payment_intent(payment_intent_id: str) -> Dict[str, Any]: try: intent = stripe.PaymentIntent.cancel(payment_intent_id) return { "payment_intent_id": intent.id, "status": intent.status } except stripe.error.StripeError as e: raise HTTPException(status_code=400, detail=f"Stripe error: {str(e)}")