Automated Action 0474b30c3f Implement complete bookstore management API with FastAPI
- Added comprehensive book management with CRUD operations
- Implemented inventory tracking with stock management and reservations
- Created order management system with status tracking
- Integrated Stripe payment processing with payment intents
- Added SQLite database with SQLAlchemy ORM and Alembic migrations
- Implemented health check and API documentation endpoints
- Added comprehensive error handling and validation
- Configured CORS middleware for frontend integration
2025-06-25 10:34:27 +00:00

48 lines
1.8 KiB
Python

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)}")