A robust payment gateway integration API built with FastAPI and Razorpay. This API handles order creation, payment signature verification, and webhook events.
- Create Order: Generate Razorpay orders with dynamic amounts and currency.
- Verify Payment: Securely verify payment signatures on the server side.
- Webhooks: Handle asynchronous payment events (e.g.,
payment.captured). - Secure: Uses environment variables for API keys and secrets.
payment-api-python/
├── app/
│ ├── main.py # Application entry point
│ ├── config.py # Configuration & Environment loading
│ ├── routes.py # API Endpoints
│ └── utils.py # Razorpay client & helpers
├── .env # Environment variables (git-ignored)
├── requirements.txt # Python dependencies
└── README.md # Project documentation
- Python 3.8+
- Razorpay Account (Key ID & Key Secret)
-
Clone the repository:
git clone https://github.com/sambhavi298/payment-api-python.git cd payment-api-python -
Create and activate a virtual environment:
python -m venv venv # Windows venv\Scripts\activate # macOS/Linux source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the root directory. -
Add your Razorpay credentials and configuration:
PORT=4000 RAZORPAY_KEY_ID=your_actual_key_id RAZORPAY_KEY_SECRET=your_actual_key_secret RAZORPAY_WEBHOOK_SECRET=your_webhook_secret
Start the development server with hot-reload:
uvicorn app.main:app --reload --port 4000The API will be available at http://localhost:4000.
POST /api/payments/create-order
Creates a new order in Razorpay.
Body:
{
"amount": 50000,
"currency": "INR"
}(Note: Amount is in paise, so 50000 = ₹500.00)
POST /api/payments/verify-signature
Verifies the payment signature after a successful transaction on the client side.
Body:
{
"razorpay_order_id": "order_...",
"razorpay_payment_id": "pay_...",
"razorpay_signature": "signature_string"
}POST /api/payments/webhook
Receives events from Razorpay (e.g., payment.captured). Ensure the RAZORPAY_WEBHOOK_SECRET matches your dashboard setting.
You can test the endpoints using Postman or the built-in Swagger UI at:
http://localhost:4000/docs