
- Created FastAPI application with Stripe payment processing - Added payment intent creation, retrieval, and webhook handling - Implemented SQLite database with payments and payment_methods tables - Set up Alembic for database migrations - Added comprehensive API endpoints for payment management - Configured CORS middleware for cross-origin requests - Added health check and service information endpoints - Created complete project documentation with setup instructions - Integrated Ruff for code formatting and linting
145 lines
3.5 KiB
Markdown
145 lines
3.5 KiB
Markdown
# 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:
|
|
|
|
```bash
|
|
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key_here
|
|
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
2. Run database migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
3. Start the development server:
|
|
```bash
|
|
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
|
```
|
|
|
|
## API Endpoints
|
|
|
|
### Base Endpoints
|
|
- `GET /` - Service information
|
|
- `GET /health` - Health check
|
|
- `GET /docs` - Interactive API documentation
|
|
- `GET /redoc` - Alternative API documentation
|
|
|
|
### Payment Endpoints
|
|
- `POST /api/v1/payments/create-payment-intent` - Create a new payment intent
|
|
- `GET /api/v1/payments/payment-intent/{payment_intent_id}` - Get payment intent details
|
|
- `GET /api/v1/payments/payment/{payment_intent_id}` - Get payment record from database
|
|
- `POST /api/v1/payments/webhook` - Handle Stripe webhooks
|
|
|
|
## Usage Example
|
|
|
|
### Create a Payment Intent
|
|
|
|
```bash
|
|
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
|
|
|
|
```json
|
|
{
|
|
"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 key
|
|
- `stripe_payment_intent_id` - Stripe payment intent ID
|
|
- `amount` - Payment amount
|
|
- `currency` - Payment currency
|
|
- `status` - Payment status
|
|
- `customer_email` - Customer email
|
|
- `customer_name` - Customer name
|
|
- `description` - Payment description
|
|
- `metadata` - Additional metadata (JSON)
|
|
- `created_at` - Creation timestamp
|
|
- `updated_at` - Last update timestamp
|
|
- `is_webhook_processed` - Webhook processing status
|
|
|
|
### Payment Methods Table
|
|
- `id` - Primary key
|
|
- `stripe_payment_method_id` - Stripe payment method ID
|
|
- `customer_email` - Customer email
|
|
- `type` - Payment method type
|
|
- `card_brand` - Card brand (for card payments)
|
|
- `card_last_four` - Last four digits of card
|
|
- `is_default` - Default payment method flag
|
|
- `created_at` - Creation timestamp
|
|
|
|
## Development
|
|
|
|
### Code Formatting
|
|
The project uses Ruff for code formatting and linting:
|
|
|
|
```bash
|
|
ruff check .
|
|
ruff format .
|
|
```
|
|
|
|
### Database Migrations
|
|
To create a new migration:
|
|
|
|
```bash
|
|
alembic revision --autogenerate -m "Description of changes"
|
|
```
|
|
|
|
To apply migrations:
|
|
|
|
```bash
|
|
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
|