# Razorpay IPN Django Handler
A Django app for handling Razorpay Instant Payment Notification (IPN) webhook events, designed to manage payment, order, and subscription notifications seamlessly with support for signal-based event tracking.
## Features
- Full support for handling Razorpay webhook events
- Built-in models for storing payment, subscription, refund, and other event data
- Signal-based notifications for valid and invalid IPN events
- Signature verification for secure processing
## Installation
Install the package via pip:
```bash
pip install razorpay-ipn-django-handler
```
## Configuration
### Step 1: Add to Installed Apps
In your Django `settings.py`, add `razorpay_ipn_django_handler`:
```python
INSTALLED_APPS = [
...,
"razorpay_ipn_django_handler",
]
```
### Step 2: Configure URLs
In your project’s `urls.py`, include the app’s URL configuration for webhook notifications:
```python
from django.urls import path, include
urlpatterns = [
...,
path("payment/razorpay/", include("razorpay_ipn_django_handler.urls")),
]
```
For example, if your server is `https://yourdomain.com`, Razorpay notifications will be processed at `https://yourdomain.com/payment/razorpay/webhook/`.
> **Note**: Customize the `"payment/razorpay/"` URL to suit your structure if needed.
### Step 3: Set Environment Variables
Add your Razorpay credentials in `settings.py` using environment variables:
```python
RAZORPAY_WEBHOOK_SECRET = "your_webhook_secret_here"
RAZORPAY_API_KEY = "your_api_key_here"
RAZORPAY_API_SECRET = "your_api_secret_here"
```
Replace the placeholders with your actual credentials from the Razorpay Dashboard.
### Step 4: Migrate Database
Run migrations to create the necessary database tables:
```bash
python manage.py makemigrations
python manage.py migrate
```
## Signal Setup
The app provides signals for handling valid and invalid IPN events, allowing custom processing based on event types.
### Setting Up Signal Handlers
In one of your app files, such as `signals.py`, register handlers for IPN events:
```python
from django.dispatch import receiver
from razorpay_ipn_django_handler.signals import valid_razorpay_ipn_received, invalid_razorpay_ipn_received
from razorpay_ipn_django_handler.models import RazorpayIPN
# Handle valid IPN events
@receiver(valid_razorpay_ipn_received)
def handle_valid_ipn(sender, instance, **kwargs):
print("Received valid IPN event:", instance.event)
# Process the IPN instance as needed
# Handle invalid IPN events
@receiver(invalid_razorpay_ipn_received)
def handle_invalid_ipn(sender, **kwargs):
print("Invalid IPN received")
# Log or handle invalid IPN events
```
Here, `instance` provides a `RazorpayIPN` object containing event data like `event_type`, `payment_id`, and other Razorpay IPN-related information.
## Models Overview
The following models are included to manage event data and related details effectively:
### 1. **RazorpayIPN**
Represents an IPN event with fields to track event type, account ID, signature verification, and related details.
### 2. **Payment**
Tracks payment details including amount, status, method, and optional metadata such as UPI, card, or wallet details.
### 3. **Subscription**
Captures subscription events, including plan ID, quantity, status, schedule changes, and metadata.
### 4. **Refund**
Records refund details, including refund amount, status, batch ID, and additional notes.
### 5. **Order**
Stores order-specific details such as amount, status, receipt number, and custom notes.
> These models make it easier to track Razorpay events, allowing you to link each IPN event to associated payments, subscriptions, refunds, and orders as per your requirements.
## Example Usage
To verify a Razorpay webhook signature, use the `verify_signature` method on a `RazorpayIPN` instance:
```python
ipn_instance = RazorpayIPN.objects.create(payload=payload_data)
signature_valid = ipn_instance.verify_signature(body=request_body, signature=header_signature)
if signature_valid:
# Process the event
else:
# Handle invalid signature
```
### Example Event Processing
In your signal handler, you can access the event data for custom processing:
```python
@receiver(valid_razorpay_ipn_received)
def process_payment_event(sender, instance, **kwargs):
if instance.event_type == 'payment.authorized':
# Add logic to handle authorized payment events
elif instance.event_type == 'subscription.activated':
# Add logic to handle subscription activation
```
## Contributing
We welcome contributions to enhance the functionality of this library. Feel free to open issues, suggest features, or submit pull requests on [GitHub](https://github.com/your-repo).
## License
This project is licensed under the MIT License. See the LICENSE file for more details.
## About the Author
I am [Arpan Sahu], a developer specializing in web development, payment integrations, and more. Feel free to check out my portfolio to learn more about my work:
[Visit My Portfolio](https://arpansahu.me)
If you have any questions or are interested in collaborating, please reach out via my portfolio contact form or directly through [LinkedIn](https://linkedin.com/in/arpansahu).
Raw data
{
"_id": null,
"home_page": "https://github.com/arpansahu/razorpay-ipn-django-handler",
"name": "razorpay-ipn-django-handler",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": "Arpan Sahu",
"author_email": "arpanrocks95@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/85/8b/4a12f489acffb82f032f56a10054dcc257f66f64ffed4ce9e718d020ba9b/razorpay-ipn-django-handler-1.0.0.tar.gz",
"platform": null,
"description": "\n# Razorpay IPN Django Handler\n\nA Django app for handling Razorpay Instant Payment Notification (IPN) webhook events, designed to manage payment, order, and subscription notifications seamlessly with support for signal-based event tracking.\n\n## Features\n\n- Full support for handling Razorpay webhook events\n- Built-in models for storing payment, subscription, refund, and other event data\n- Signal-based notifications for valid and invalid IPN events\n- Signature verification for secure processing\n\n## Installation\n\nInstall the package via pip:\n\n```bash\npip install razorpay-ipn-django-handler\n```\n\n## Configuration\n\n### Step 1: Add to Installed Apps\n\nIn your Django `settings.py`, add `razorpay_ipn_django_handler`:\n\n```python\nINSTALLED_APPS = [\n ...,\n \"razorpay_ipn_django_handler\",\n]\n```\n\n### Step 2: Configure URLs\n\nIn your project\u2019s `urls.py`, include the app\u2019s URL configuration for webhook notifications:\n\n```python\nfrom django.urls import path, include\n\nurlpatterns = [\n ...,\n path(\"payment/razorpay/\", include(\"razorpay_ipn_django_handler.urls\")),\n]\n```\n\nFor example, if your server is `https://yourdomain.com`, Razorpay notifications will be processed at `https://yourdomain.com/payment/razorpay/webhook/`.\n\n> **Note**: Customize the `\"payment/razorpay/\"` URL to suit your structure if needed.\n\n### Step 3: Set Environment Variables\n\nAdd your Razorpay credentials in `settings.py` using environment variables:\n\n```python\nRAZORPAY_WEBHOOK_SECRET = \"your_webhook_secret_here\"\nRAZORPAY_API_KEY = \"your_api_key_here\"\nRAZORPAY_API_SECRET = \"your_api_secret_here\"\n```\n\nReplace the placeholders with your actual credentials from the Razorpay Dashboard.\n\n### Step 4: Migrate Database\n\nRun migrations to create the necessary database tables:\n\n```bash\npython manage.py makemigrations\npython manage.py migrate\n```\n\n## Signal Setup\n\nThe app provides signals for handling valid and invalid IPN events, allowing custom processing based on event types.\n\n### Setting Up Signal Handlers\n\nIn one of your app files, such as `signals.py`, register handlers for IPN events:\n\n```python\nfrom django.dispatch import receiver\nfrom razorpay_ipn_django_handler.signals import valid_razorpay_ipn_received, invalid_razorpay_ipn_received\nfrom razorpay_ipn_django_handler.models import RazorpayIPN\n\n# Handle valid IPN events\n@receiver(valid_razorpay_ipn_received)\ndef handle_valid_ipn(sender, instance, **kwargs):\n print(\"Received valid IPN event:\", instance.event)\n # Process the IPN instance as needed\n\n# Handle invalid IPN events\n@receiver(invalid_razorpay_ipn_received)\ndef handle_invalid_ipn(sender, **kwargs):\n print(\"Invalid IPN received\")\n # Log or handle invalid IPN events\n```\n\nHere, `instance` provides a `RazorpayIPN` object containing event data like `event_type`, `payment_id`, and other Razorpay IPN-related information.\n\n## Models Overview\n\nThe following models are included to manage event data and related details effectively:\n\n### 1. **RazorpayIPN**\n\nRepresents an IPN event with fields to track event type, account ID, signature verification, and related details.\n\n### 2. **Payment**\n\nTracks payment details including amount, status, method, and optional metadata such as UPI, card, or wallet details.\n\n### 3. **Subscription**\n\nCaptures subscription events, including plan ID, quantity, status, schedule changes, and metadata.\n\n### 4. **Refund**\n\nRecords refund details, including refund amount, status, batch ID, and additional notes.\n\n### 5. **Order**\n\nStores order-specific details such as amount, status, receipt number, and custom notes.\n\n> These models make it easier to track Razorpay events, allowing you to link each IPN event to associated payments, subscriptions, refunds, and orders as per your requirements.\n\n## Example Usage\n\nTo verify a Razorpay webhook signature, use the `verify_signature` method on a `RazorpayIPN` instance:\n\n```python\nipn_instance = RazorpayIPN.objects.create(payload=payload_data)\nsignature_valid = ipn_instance.verify_signature(body=request_body, signature=header_signature)\nif signature_valid:\n # Process the event\nelse:\n # Handle invalid signature\n```\n\n### Example Event Processing\n\nIn your signal handler, you can access the event data for custom processing:\n\n```python\n@receiver(valid_razorpay_ipn_received)\ndef process_payment_event(sender, instance, **kwargs):\n if instance.event_type == 'payment.authorized':\n # Add logic to handle authorized payment events\n elif instance.event_type == 'subscription.activated':\n # Add logic to handle subscription activation\n```\n\n## Contributing\n\nWe welcome contributions to enhance the functionality of this library. Feel free to open issues, suggest features, or submit pull requests on [GitHub](https://github.com/your-repo).\n\n## License\n\nThis project is licensed under the MIT License. See the LICENSE file for more details.\n\n## About the Author\n\nI am [Arpan Sahu], a developer specializing in web development, payment integrations, and more. Feel free to check out my portfolio to learn more about my work:\n\n[Visit My Portfolio](https://arpansahu.me)\n\nIf you have any questions or are interested in collaborating, please reach out via my portfolio contact form or directly through [LinkedIn](https://linkedin.com/in/arpansahu).\n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A Django app for handling Razorpay Instant Payment Notifications (IPN) in webhooks.",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/arpansahu/razorpay-ipn-django-handler"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9e93dee8d669cc20a5e0d3fd733d74fc625032a733866691dc66bb5c12fde929",
"md5": "018006e696a538069c064905f7798738",
"sha256": "a357cc8ea0c89fe7e60d435d81b0cfba49659ea370a9ee9bf0982b828727b0b4"
},
"downloads": -1,
"filename": "razorpay_ipn_django_handler-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "018006e696a538069c064905f7798738",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 20133,
"upload_time": "2024-11-03T19:02:07",
"upload_time_iso_8601": "2024-11-03T19:02:07.620564Z",
"url": "https://files.pythonhosted.org/packages/9e/93/dee8d669cc20a5e0d3fd733d74fc625032a733866691dc66bb5c12fde929/razorpay_ipn_django_handler-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "858b4a12f489acffb82f032f56a10054dcc257f66f64ffed4ce9e718d020ba9b",
"md5": "e3ff8ccee2c0deb398c6f976352d9682",
"sha256": "1bca8f2d4e2ecea8bac080f83124d0244209a21c3a70b452e4a2591c7828b49d"
},
"downloads": -1,
"filename": "razorpay-ipn-django-handler-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "e3ff8ccee2c0deb398c6f976352d9682",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 19619,
"upload_time": "2024-11-03T19:02:09",
"upload_time_iso_8601": "2024-11-03T19:02:09.230117Z",
"url": "https://files.pythonhosted.org/packages/85/8b/4a12f489acffb82f032f56a10054dcc257f66f64ffed4ce9e718d020ba9b/razorpay-ipn-django-handler-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-03 19:02:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "arpansahu",
"github_project": "razorpay-ipn-django-handler",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "Django",
"specs": [
[
"==",
"5.1.2"
]
]
}
],
"lcname": "razorpay-ipn-django-handler"
}