Signals¶
The subscriptions package dispatches custom Django signals for every major lifecycle event. Connect to these signals to add custom behavior without modifying the package.
Connecting to Signals¶
# your_app/signals.py
from subscriptions.signals import subscription_activated, payment_success
def on_subscription_activated(sender, instance, **kwargs):
"""Called when a subscription becomes active."""
print(f"Subscription activated: {instance.user} - {instance.plan.name}")
def on_payment_success(sender, instance, **kwargs):
"""Called when a payment is successfully settled."""
print(f"Payment settled: {instance.order_id} - {instance.gross_amount}")
subscription_activated.connect(on_subscription_activated)
payment_success.connect(on_payment_success)
Or using the @receiver decorator:
from django.dispatch import receiver
from subscriptions.signals import subscription_created
@receiver(subscription_created)
def handle_new_subscription(sender, instance, **kwargs):
"""Send welcome email when subscription is created."""
send_welcome_email(instance.user)
Signal Reference¶
Subscription Lifecycle¶
Signal |
Sent When |
|
|---|---|---|
|
New subscription saved |
|
|
Status changes to |
|
|
Status changes to |
|
|
Status changes to |
|
|
(dispatched via |
|
|
Status changes to |
|
|
(dispatched manually when renewing) |
|
|
(available for custom use) |
|
Payment Lifecycle¶
Signal |
Sent When |
|
|---|---|---|
|
New payment saved |
|
|
Payment is settled ( |
|
|
Status is |
|
|
Status is |
|
Invoice Lifecycle¶
Signal |
Sent When |
|
|---|---|---|
|
New invoice saved |
|
|
Status changes to |
|
|
Status changes to |
|
|
Status changes to |
|
Webhook¶
Signal |
Sent When |
|
|---|---|---|
|
New webhook log created |
|
|
Webhook log status becomes |
|
Wallet¶
Signal |
Sent When |
|
|---|---|---|
|
Top-up status changes to |
|
|
Wallet transaction created with positive amount |
|
|
Wallet transaction created with negative amount |
|
How Signals Are Dispatched¶
Signals are dispatched via post_save handlers registered in subscriptions/signals.py:
Subscription: On
post_save, checks ifstatusis inupdate_fieldsand dispatches the corresponding signalPayment: On
post_save, checksis_paidor failure statesInvoice: On
post_save, checks status forpaid,overdue,voidWebhookLog: On
post_save, dispatcheswebhook_receivedon create andwebhook_processedon status updateTopUp: On
post_save, dispatchestopup_completedwhen status issuccessWalletTransaction: On
post_save, dispatcheswallet_creditedorwallet_debitedbased on amount sign
Example Use Cases¶
Send Slack notification on payment failure¶
@receiver(payment_failed)
def notify_slack_on_failure(sender, instance, **kwargs):
slack_webhook(f"Payment failed: {instance.order_id} ({instance.gross_amount} {instance.currency})")
Log subscription changes to analytics¶
@receiver(subscription_activated)
def track_activation(sender, instance, **kwargs):
analytics.track(instance.user.id, "subscription_activated", {
"plan": instance.plan.name,
"payment_type": instance.payment_type,
})
Auto-provision resources on activation¶
@receiver(subscription_activated)
def provision_resources(sender, instance, **kwargs):
features = instance.plan.features
create_user_workspace(instance.user, max_projects=features.get("max_projects", 1))