Skip to main content

A flexible and extensible payment library for FastAPI

Project description

Work in Progress (Not ready for usage)

FastAPI Payments Library

A flexible and extensible payment library for FastAPI applications supporting multiple payment providers and pricing models.

Features

  • Multiple Payment Providers: Support for Stripe, PayPal, Adyen, PayU hosted checkout, and more
  • Flexible Pricing Models:
    • Subscription
    • Usage-based
    • Tiered pricing
    • Per-user/seat pricing
    • Freemium
    • Dynamic pricing
    • Hybrid models
  • Asynchronous Architecture: Built on FastAPI and SQLAlchemy 2.0 with async support
  • Event-Driven: RabbitMQ integration via FastStream for reliable payment event messaging
  • Highly Configurable: Extensive configuration options to customize for your needs
  • Extensible: Easy to add new payment providers or custom pricing models

Provider Functionality Status

Provider Customer Products Plans Payments & Subscription
Stripe
PayPal ⚠️
Adyen
PayU ⚠️ ⚠️ ⚠️

Provider Pricing Model Completion

Provider Subscription Usage-based Tiered pricing Per-user/seat pricing Freemium Dynamic pricing Hybrid models
Stripe
PayPal ⚠️
Adyen
PayU ⚠️

Installation

pip install fastapi-payments

Quick Start

from fastapi import FastAPI
from fastapi_payments import FastAPIPayments, create_payment_module
import json

# Create FastAPI app
app = FastAPI()

# Load payment configuration
with open("config/payment_config.json") as f:
    config = json.load(f)

# Initialize payments module
payments = FastAPIPayments(config)

# Include payment routes
payments.include_router(app, prefix="/api")

# Start server
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Configuration

Create a payment_config.json file:

{
  "providers": {
    "stripe": {
      "api_key": "sk_test_your_stripe_key",
      "webhook_secret": "whsec_your_webhook_secret",
      "sandbox_mode": true
    }
  },
  "database": {
    "url": "postgresql+asyncpg://user:password@localhost/payments"
  },
  "rabbitmq": {
    "url": "amqp://guest:guest@localhost/"
  },
  "pricing": {
    "default_currency": "USD",
    "default_pricing_model": "subscription"
  },
  "default_provider": "stripe"
}

PayU Hosted Checkout

To enable PayU hosted checkout add a payu provider:

"providers": {
  "payu": {
    "api_key": "your_merchant_key",
    "api_secret": "your_merchant_salt",
    "sandbox_mode": true,
    "additional_settings": {
      "success_url": "https://merchant.test/payu/success",
      "failure_url": "https://merchant.test/payu/failure",
      "cancel_url": "https://merchant.test/payu/cancel"
    }
  }
}

When calling POST /payments include the hosted checkout fields inside meta_info so the library can prepare the HTML form payload:

{
  "customer_id": "cust_123",
  "amount": 10.0,
  "currency": "INR",
  "meta_info": {
    "payu": {
      "firstname": "John",
      "email": "john@example.com",
      "phone": "9999999999",
      "productinfo": "Order #1001",
      "surl": "https://merchant.test/payu/success",
      "furl": "https://merchant.test/payu/failure"
    }
  }
}

The payment response includes meta_info.provider_data.payu.redirect containing the action URL and signed fields for rendering the hosted form. Webhooks posted by PayU can be sent using either JSON or form-encoded bodies and are verified automatically via the published hash logic.

Documentation

For complete documentation, visit https://fastapi-payments.readthedocs.io/

Example Project

Example project implements payment processing functionality using FastAPI.

It provides endpoints for handling payment transactions, including creation, processing, and status checking. The implementation supports various payment providers and includes error handling and logging.

For a complete example project demonstrating payment integration with FastAPI, refer to: https://github.com/innerkorehq/fastapi-payments-example

Usage:

  • Import the necessary classes and functions.
  • Configure payment provider settings.
  • Use the provided endpoints in your FastAPI application.

"""

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_payments-0.1.3.tar.gz (72.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_payments-0.1.3-py3-none-any.whl (86.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_payments-0.1.3.tar.gz.

File metadata

  • Download URL: fastapi_payments-0.1.3.tar.gz
  • Upload date:
  • Size: 72.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for fastapi_payments-0.1.3.tar.gz
Algorithm Hash digest
SHA256 19a693c18594c2c1677ae1272b8df192a1242e8df7e4afab0fca8cef45e96eb6
MD5 d368912bd3a9a7eb398f43926e341752
BLAKE2b-256 c70036adea29674eb3e17f22be36a8fe9363fd6cae26ac4fad325c1dd46c50cd

See more details on using hashes here.

File details

Details for the file fastapi_payments-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_payments-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bc63e38f883e8b2d9776088af05c41adc20866aa9b2960cda468d1ce26635e97
MD5 8912d14b5994f794433d83ef34fb6727
BLAKE2b-256 d0dd952001c5b5703e14d9c3f193a930069e6430d271479362be1c07abba58a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page