Configuration
Configuration File
Create a JSON configuration file (e.g., payment_config.json):
{
"providers": {
"stripe": {
"api_key": "sk_test_your_stripe_key",
"webhook_secret": "whsec_your_webhook_secret",
"sandbox_mode": true,
"additional_settings": {
"api_version": "2023-10-16"
}
},
"paypal": {
"api_key": "your_paypal_client_id",
"api_secret": "your_paypal_secret",
"sandbox_mode": true
}
},
"database": {
"url": "postgresql+asyncpg://user:password@localhost/payments",
"echo": false,
"pool_size": 5,
"max_overflow": 10
},
"rabbitmq": {
"url": "amqp://guest:guest@localhost/",
"exchange": "payments",
"queue_prefix": "payment_",
"consumer_count": 2,
"prefetch_count": 10
},
"pricing": {
"default_currency": "USD",
"default_pricing_model": "subscription",
"round_to_decimal_places": 2,
"tax": {
"default_rate": 0.0,
"included_in_price": false
}
},
"default_provider": "stripe",
"logging_level": "INFO"
}
Environment Variables
You can also configure the library using environment variables:
# Default provider
export PAYMENT_DEFAULT_PROVIDER=stripe
# Stripe configuration
export PAYMENT_PROVIDERS__STRIPE__API_KEY=sk_test_your_key
export PAYMENT_PROVIDERS__STRIPE__SANDBOX_MODE=true
# Database configuration
export PAYMENT_DATABASE__URL=postgresql+asyncpg://user:password@localhost/payments
# RabbitMQ configuration
export PAYMENT_RABBITMQ__URL=amqp://guest:guest@localhost/
Configuration Schema
The full configuration schema includes:
Provider Configuration:
api_key: API key for the providerapi_secret: API secret (if required)sandbox_mode: Boolean indicating test/sandbox modewebhook_secret: Secret for webhook signature verificationadditional_settings: Provider-specific additional settings
Database Configuration:
url: Database connection URLecho: Enable SQL query loggingpool_size: Connection pool sizemax_overflow: Maximum number of connections
RabbitMQ Configuration:
url: RabbitMQ connection URLexchange: Exchange namequeue_prefix: Prefix for queue namesconsumer_count: Number of consumers to runprefetch_count: Number of messages to prefetch
Pricing Configuration:
default_currency: Default currency for pricingdefault_pricing_model: Default pricing modelround_to_decimal_places: Number of decimal places for roundingtax: Tax configuration
General Settings:
default_provider: Default payment providerretry_attempts: Number of retry attempts for API callsretry_delay: Delay between retries (seconds)logging_level: Logging level (DEBUG, INFO, WARNING, ERROR)
Message Broker Configuration
Configure the message broker for event-driven architecture. FastAPI Payments supports multiple message brokers:
RabbitMQ (Default):
"messaging": {
"broker_type": "rabbitmq",
"url": "amqp://guest:guest@localhost/",
"exchange_name": "payments",
"queue_prefix": "payment_",
"consumer_count": 2,
"exchange_type": "topic",
"exchange_durable": true
}
Kafka:
"messaging": {
"broker_type": "kafka",
"url": "kafka://localhost:9092",
"topic_prefix": "payments.",
"group_id": "payment-service",
"auto_offset_reset": "earliest"
}
Redis:
"messaging": {
"broker_type": "redis",
"url": "redis://localhost",
"stream_maxlen": 1000,
"consumer_group": "payment-service"
}
NATS:
"messaging": {
"broker_type": "nats",
"url": "nats://localhost:4222",
"subject_prefix": "payments.",
"queue_group": "payment-service"
}
Memory (For testing only):
"messaging": {
"broker_type": "memory",
"url": "memory://",
"exchange_name": "payments"
}
Environment variables can also be used:
# RabbitMQ
export PAYMENT_MESSAGING__BROKER_TYPE=rabbitmq
export PAYMENT_MESSAGING__URL=amqp://guest:guest@localhost/
# Kafka
export PAYMENT_MESSAGING__BROKER_TYPE=kafka
export PAYMENT_MESSAGING__URL=kafka://localhost:9092
# Redis
export PAYMENT_MESSAGING__BROKER_TYPE=redis
export PAYMENT_MESSAGING__URL=redis://localhost