Providers API
Provider Base Class
- class fastapi_payments.providers.base.PaymentProvider(config)[source]
Bases:
ABCBase payment provider class.
Methods:
__init__(config)Initialize the payment provider.
Initialize the provider with configuration.
create_customer(email[, name, meta_info, ...])Create a customer in the provider's system.
retrieve_customer(provider_customer_id)Retrieve customer data from the provider.
update_customer(provider_customer_id, data)Update customer data in the provider's system.
delete_customer(provider_customer_id)Delete a customer from the provider's system.
create_payment_method(provider_customer_id, ...)Create a payment method for a customer.
create_setup_intent(provider_customer_id[, ...])Create a SetupIntent or equivalent object used to initiate client-side confirmation flows for saving payment methods (e.g. Stripe SetupIntent).
list_payment_methods(provider_customer_id)List payment methods for a customer.
delete_payment_method(payment_method_id)Delete a payment method.
create_product(name[, description, meta_info])Create a product in the provider's system.
create_price(product_id, amount, currency[, ...])Create a price for a product.
create_subscription(provider_customer_id, ...)Create a subscription for a customer.
retrieve_subscription(provider_subscription_id)Retrieve subscription details.
update_subscription(...)Update subscription details.
cancel_subscription(provider_subscription_id)Cancel a subscription.
process_payment(amount, currency[, ...])Process a one-time payment.
refund_payment(provider_payment_id[, amount])Refund a payment.
webhook_handler(payload[, signature])Handle webhook events from the provider.
record_usage(subscription_item_id, quantity)Record usage for usage-based billing.
- __init__(config)[source]
Initialize the payment provider.
- Parameters:
config – Configuration object or dictionary
- abstractmethod async create_customer(email, name=None, meta_info=None, address=None)[source]
Create a customer in the provider’s system.
- Parameters:
- Return type:
- Returns:
Customer data dictionary
- abstractmethod async retrieve_customer(provider_customer_id)[source]
Retrieve customer data from the provider.
- abstractmethod async update_customer(provider_customer_id, data)[source]
Update customer data in the provider’s system.
- abstractmethod async delete_customer(provider_customer_id)[source]
Delete a customer from the provider’s system.
- abstractmethod async create_payment_method(provider_customer_id, payment_details)[source]
Create a payment method for a customer.
- async create_setup_intent(provider_customer_id, usage=None, **kwargs)[source]
Create a SetupIntent or equivalent object used to initiate client-side confirmation flows for saving payment methods (e.g. Stripe SetupIntent).
Return a dict with at least ‘id’ and ‘client_secret’ keys when supported.
- abstractmethod async list_payment_methods(provider_customer_id)[source]
List payment methods for a customer.
- abstractmethod async create_product(name, description=None, meta_info=None)[source]
Create a product in the provider’s system.
- abstractmethod async create_price(product_id, amount, currency, interval=None, interval_count=None, meta_info=None)[source]
Create a price for a product.
- Parameters:
- Return type:
- Returns:
Price data
- abstractmethod async create_subscription(provider_customer_id, price_id, quantity=1, trial_period_days=None, meta_info=None)[source]
Create a subscription for a customer.
- Parameters:
- Return type:
- Returns:
Subscription data
- abstractmethod async retrieve_subscription(provider_subscription_id)[source]
Retrieve subscription details.
- abstractmethod async update_subscription(provider_subscription_id, data)[source]
Update subscription details.
- abstractmethod async cancel_subscription(provider_subscription_id, cancel_at_period_end=True)[source]
Cancel a subscription.
- abstractmethod async process_payment(amount, currency, provider_customer_id=None, payment_method_id=None, description=None, meta_info=None, mandate_id=None)[source]
Process a one-time payment.
- abstractmethod async webhook_handler(payload, signature=None)[source]
Handle webhook events from the provider.
Stripe Provider
- class fastapi_payments.providers.stripe.StripeProvider(config)[source]
Bases:
PaymentProviderStripe payment provider backed by the official SDK.
Attributes:
Methods:
Initialize Stripe with configuration.
create_customer(email[, name, meta_info, ...])Create a customer in Stripe.
retrieve_customer(provider_customer_id)Retrieve customer from Stripe.
update_customer(provider_customer_id, data)Update customer data in Stripe.
delete_customer(provider_customer_id)Delete a customer from Stripe.
create_payment_method(provider_customer_id, ...)Create a payment method in Stripe and attach it to the customer.
create_setup_intent(provider_customer_id[, ...])Create a Stripe SetupIntent to be confirmed client-side by the browser.
list_payment_methods(provider_customer_id)List payment methods for a customer in Stripe.
delete_payment_method(payment_method_id)Detach a payment method from Stripe.
create_product(name[, description, meta_info])Create a product in Stripe.
create_price(product_id, amount, currency[, ...])Create a price in Stripe.
create_subscription(provider_customer_id, ...)Create a subscription in Stripe.
retrieve_subscription(provider_subscription_id)Retrieve subscription details from Stripe.
retrieve_product(provider_product_id)Retrieve product from Stripe and return a normalized dict.
retrieve_price(provider_price_id)Retrieve price/price_id from Stripe and return normalized dict.
retrieve_payment(provider_payment_id)Retrieve a payment intent from Stripe and return normalized dict.
update_subscription(...)Update subscription in Stripe.
cancel_subscription(provider_subscription_id)Cancel a subscription in Stripe.
process_payment(amount, currency[, ...])Process a one-time payment with Stripe using PaymentIntents.
refund_payment(provider_payment_id[, amount])Refund a payment in Stripe.
webhook_handler(payload[, signature])Handle webhook events from Stripe.
record_usage(subscription_item_id, quantity)Record usage for metered billing with Stripe.
- ZERO_DECIMAL_CURRENCIES = {'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'}
- EVENT_TYPE_MAP = {'charge.refunded': 'payment.refunded', 'customer.subscription.created': 'subscription.created', 'customer.subscription.deleted': 'subscription.canceled', 'customer.subscription.updated': 'subscription.updated', 'invoice.payment_failed': 'invoice.payment_failed', 'invoice.payment_succeeded': 'invoice.payment_succeeded', 'payment_intent.payment_failed': 'payment.failed', 'payment_intent.succeeded': 'payment.succeeded'}
- async create_customer(email, name=None, meta_info=None, address=None)[source]
Create a customer in Stripe.
- async create_payment_method(provider_customer_id, payment_details)[source]
Create a payment method in Stripe and attach it to the customer.
- async create_setup_intent(provider_customer_id, usage=None, **kwargs)[source]
Create a Stripe SetupIntent to be confirmed client-side by the browser. Returns the SetupIntent id and client_secret so the client can call stripe.confirmCardSetup(…) to complete 3DS flows required in certain regions (e.g. India).
- async list_payment_methods(provider_customer_id)[source]
List payment methods for a customer in Stripe.
- async create_price(product_id, amount, currency, interval=None, interval_count=None, meta_info=None)[source]
Create a price in Stripe.
- async create_subscription(provider_customer_id, price_id, quantity=1, trial_period_days=None, meta_info=None)[source]
Create a subscription in Stripe.
- async retrieve_subscription(provider_subscription_id)[source]
Retrieve subscription details from Stripe.
- async retrieve_product(provider_product_id)[source]
Retrieve product from Stripe and return a normalized dict.
- async retrieve_price(provider_price_id)[source]
Retrieve price/price_id from Stripe and return normalized dict.
- async retrieve_payment(provider_payment_id)[source]
Retrieve a payment intent from Stripe and return normalized dict.
- async cancel_subscription(provider_subscription_id, cancel_at_period_end=True)[source]
Cancel a subscription in Stripe.
- async process_payment(amount, currency, provider_customer_id=None, payment_method_id=None, description=None, meta_info=None, mandate_id=None)[source]
Process a one-time payment with Stripe using PaymentIntents.
PayPal Provider
Adyen Provider
Provider Factory
- fastapi_payments.providers.get_provider(provider_name, provider_config)[source]
Factory function to get a payment provider instance.
- Parameters:
- Return type:
- Returns:
A PaymentProvider instance
- Raises:
ValueError – If provider is not supported or configuration is invalid