Signals Reference
django_midtrans fires Django signals at key lifecycle events. Connect receivers to react to payment, invoice, and subscription changes.
Connecting a Signal
# yourapp/signals.py
from django.dispatch import receiver
from django_midtrans.signals import payment_settled
@receiver(payment_settled)
def handle_settled(sender, payment, notification, payload, **kwargs):
print(f"Payment {payment.order_id} settled!")
Register in your app config:
# yourapp/apps.py
class YourAppConfig(AppConfig):
name = "yourapp"
def ready(self):
import yourapp.signals # noqa: F401
Signal Arguments
All payment signals send the same keyword arguments:
Argument |
Type |
Description |
|---|---|---|
|
class |
|
|
|
The updated payment instance |
|
|
The notification record |
|
|
Raw webhook payload from Midtrans |
Invoice and subscription signals send:
Argument |
Type |
Description |
|---|---|---|
|
class |
The service class that triggered the signal |
|
Model instance |
The relevant model |
Payment Signals
Signal |
Fired When |
|---|---|
|
A new pending payment is created |
|
Payment is successfully settled or captured |
|
Payment is denied by Midtrans or the bank |
|
Payment is cancelled |
|
Payment expires (customer didn’t pay in time) |
|
Payment is refunded (full or partial) |
|
Payment fails for any other reason |
Usage Examples
from django_midtrans.signals import (
payment_received,
payment_settled,
payment_denied,
payment_cancelled,
payment_expired,
payment_refunded,
payment_failed,
)
@receiver(payment_received)
def on_payment_received(sender, payment, notification, payload, **kwargs):
"""New payment is pending — show customer waiting instructions."""
send_payment_instructions(payment)
@receiver(payment_settled)
def on_payment_settled(sender, payment, notification, payload, **kwargs):
"""Payment confirmed — fulfill the order."""
order = Order.objects.get(midtrans_payment=payment)
order.status = "paid"
order.save()
send_receipt_email(order)
@receiver(payment_denied)
def on_payment_denied(sender, payment, notification, payload, **kwargs):
"""Payment denied by bank/fraud check."""
log_denied_payment(payment)
@receiver(payment_cancelled)
def on_payment_cancelled(sender, payment, notification, payload, **kwargs):
"""Payment was cancelled."""
restore_stock(payment)
@receiver(payment_expired)
def on_payment_expired(sender, payment, notification, payload, **kwargs):
"""Payment expired — restore inventory."""
restore_stock(payment)
@receiver(payment_refunded)
def on_payment_refunded(sender, payment, notification, payload, **kwargs):
"""Refund processed."""
send_refund_confirmation(payment)
@receiver(payment_failed)
def on_payment_failed(sender, payment, notification, payload, **kwargs):
"""Payment failed."""
notify_support_team(payment)
Invoice Signals
Signal |
Fired When |
|---|---|
|
A new invoice is created |
|
Invoice payment is confirmed |
|
Invoice is voided/cancelled |
from django_midtrans.signals import invoice_created, invoice_paid, invoice_voided
@receiver(invoice_paid)
def on_invoice_paid(sender, invoice, **kwargs):
send_invoice_receipt(invoice)
Subscription Signals
Signal |
Fired When |
|---|---|
|
A new subscription is created |
|
Recurring payment charged successfully |
|
Subscription is disabled (paused) |
|
Subscription is permanently cancelled |
from django_midtrans.signals import subscription_created, subscription_cancelled
@receiver(subscription_cancelled)
def on_subscription_cancelled(sender, subscription, **kwargs):
revoke_premium_access(subscription.user)
All Signals at a Glance
Signal |
Category |
Arguments |
|---|---|---|
|
Payment |
payment, notification, payload |
|
Payment |
payment, notification, payload |
|
Payment |
payment, notification, payload |
|
Payment |
payment, notification, payload |
|
Payment |
payment, notification, payload |
|
Payment |
payment, notification, payload |
|
Payment |
payment, notification, payload |
|
Invoice |
invoice |
|
Invoice |
invoice |
|
Invoice |
invoice |
|
Subscription |
subscription |
|
Subscription |
subscription |
|
Subscription |
subscription |
|
Subscription |
subscription |