
- Added app/__init__.py for main app module - Added app/services/__init__.py for services module - Ensures proper Python package structure
Stripe Payment Integration Service
A FastAPI service for handling Stripe payments with complete payment processing capabilities.
Features
- Create and manage Stripe payment intents
- Process payments securely
- Handle Stripe webhooks for payment status updates
- Store payment records in SQLite database
- RESTful API with automatic OpenAPI documentation
- Health check endpoint
Environment Variables
You need to set the following environment variables:
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key_here
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here
Installation
- Install dependencies:
pip install -r requirements.txt
- Run database migrations:
alembic upgrade head
- Start the development server:
uvicorn main:app --reload --host 0.0.0.0 --port 8000
API Endpoints
Base Endpoints
GET /
- Service informationGET /health
- Health checkGET /docs
- Interactive API documentationGET /redoc
- Alternative API documentation
Payment Endpoints
POST /api/v1/payments/create-payment-intent
- Create a new payment intentGET /api/v1/payments/payment-intent/{payment_intent_id}
- Get payment intent detailsGET /api/v1/payments/payment/{payment_intent_id}
- Get payment record from databasePOST /api/v1/payments/webhook
- Handle Stripe webhooks
Usage Example
Create a Payment Intent
curl -X POST "http://localhost:8000/api/v1/payments/create-payment-intent" \
-H "Content-Type: application/json" \
-d '{
"amount": 2000,
"currency": "usd",
"customer_email": "customer@example.com",
"customer_name": "John Doe",
"description": "Payment for services"
}'
Response
{
"client_secret": "pi_xxxxx_secret_xxxxx",
"payment_intent_id": "pi_xxxxx",
"amount": 2000,
"currency": "usd",
"status": "requires_payment_method"
}
Database Schema
The service uses SQLite with the following tables:
Payments Table
id
- Primary keystripe_payment_intent_id
- Stripe payment intent IDamount
- Payment amountcurrency
- Payment currencystatus
- Payment statuscustomer_email
- Customer emailcustomer_name
- Customer namedescription
- Payment descriptionmetadata
- Additional metadata (JSON)created_at
- Creation timestampupdated_at
- Last update timestampis_webhook_processed
- Webhook processing status
Payment Methods Table
id
- Primary keystripe_payment_method_id
- Stripe payment method IDcustomer_email
- Customer emailtype
- Payment method typecard_brand
- Card brand (for card payments)card_last_four
- Last four digits of cardis_default
- Default payment method flagcreated_at
- Creation timestamp
Development
Code Formatting
The project uses Ruff for code formatting and linting:
ruff check .
ruff format .
Database Migrations
To create a new migration:
alembic revision --autogenerate -m "Description of changes"
To apply migrations:
alembic upgrade head
Webhook Configuration
Configure your Stripe webhook endpoint URL in the Stripe Dashboard:
- URL:
https://yourdomain.com/api/v1/payments/webhook
- Events:
payment_intent.succeeded
,payment_intent.payment_failed
Security Notes
- Always use HTTPS in production
- Store Stripe keys securely as environment variables
- Verify webhook signatures for security
- Never expose secret keys in client-side code
Description
Languages
Python
97.4%
Mako
2.6%