Models
The django_midtrans.models module defines all database models for storing payment, invoice, subscription, and notification data.
All models inherit from TimeStampedModel which provides created_at and updated_at fields.
TimeStampedModel
Abstract base model providing timestamp fields.
Field |
Type |
Description |
|---|---|---|
|
|
Auto-set on creation, indexed. |
|
|
Auto-set on every save. |
MidtransPayment
The core payment model. Stores full transaction state from Midtrans.
Fields
Field |
Type |
Description |
|---|---|---|
|
|
Primary key (auto-generated UUID4). |
|
|
Unique order ID sent to Midtrans. Indexed. |
|
|
Midtrans-generated transaction UUID. |
|
|
Payment method (see |
|
|
Bank for VA payments (see |
|
|
Total charge amount. |
|
|
Currency code (default: |
|
|
Current status (see |
|
|
Fraud detection result (see |
|
|
Midtrans HTTP status code. |
|
|
Midtrans status message. |
|
|
Customer first name. |
|
|
Customer last name. |
|
|
Customer email. |
|
|
Customer phone number. |
|
|
Optional link to Django user. |
|
|
Virtual Account number (bank transfer). |
|
|
Mandiri bill key (e-channel). |
|
|
Mandiri biller code (e-channel). |
|
|
Convenience store payment code. |
|
|
3DS redirect URL (credit card). |
|
|
Mobile deeplink URL (GoPay, ShopeePay). |
|
|
QR code data string (QRIS). |
|
|
Transaction creation time from Midtrans. |
|
|
Settlement time from Midtrans. |
|
|
Payment expiry time. |
|
|
Total refunded amount (default: 0). |
|
|
Latest refund key. |
|
|
Full raw charge API response. |
|
|
Arbitrary metadata dict. |
|
|
Custom field 1. |
|
|
Custom field 2. |
|
|
Custom field 3. |
|
|
Record creation timestamp. |
|
|
Last update timestamp. |
Properties
Property |
Type |
Description |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Database Indexes
(transaction_status, payment_type)(customer_email)(created_at)
Example
from django_midtrans.models import MidtransPayment
# Get all paid payments
paid = MidtransPayment.objects.filter(
transaction_status__in=["capture", "settlement"]
)
# Check a specific payment
payment = MidtransPayment.objects.get(order_id="ORDER-20240101-ABC12345")
print(payment.is_paid) # True
print(payment.net_amount) # Decimal('95000.00')
MidtransPaymentItem
Line items for a payment.
Fields
Field |
Type |
Description |
|---|---|---|
|
|
Primary key. |
|
|
Parent payment. Related name: |
|
|
External item ID. |
|
|
Item name. |
|
|
Unit price. |
|
|
Quantity (default: 1). |
|
|
Brand name. |
|
|
Category name. |
|
|
Merchant name. |
|
|
Item URL. |
Properties
Property |
Type |
Description |
|---|---|---|
|
|
|
MidtransNotification
Stores incoming webhook notifications from Midtrans.
Fields
Field |
Type |
Description |
|---|---|---|
|
|
Primary key. |
|
|
Linked payment (nullable). Related name: |
|
|
Order ID from notification. Indexed. |
|
|
Transaction ID from notification. |
|
|
Status reported by Midtrans. |
|
|
Fraud status from notification. |
|
|
Payment type from notification. |
|
|
HTTP status code. |
|
|
Gross amount string. |
|
|
SHA-512 signature for verification. |
|
|
Processing status: |
|
|
Complete raw notification JSON. |
|
|
Error details if processing failed. |
MidtransInvoice
Invoice model for Midtrans invoice/payment link API.
Fields
Field |
Type |
Description |
|---|---|---|
|
|
Primary key. |
|
|
Unique invoice number. |
|
|
Unique order ID. |
|
|
Midtrans-generated invoice ID. |
|
|
|
|
|
Customer name. |
|
|
Customer email. |
|
|
Customer phone. |
|
|
External customer ID. |
|
|
Optional Django user. Related name: |
|
|
Invoice total. |
|
|
Default: |
|
|
Due date. |
|
|
Payment timestamp (nullable). |
|
|
Invoice notes. |
|
|
Reason for voiding. |
|
|
Linked payment (nullable). Related name: |
|
|
Raw Midtrans create response. |
|
|
Arbitrary metadata. |
Properties
Property |
Type |
Description |
|---|---|---|
|
|
|
MidtransInvoiceItem
Line items for an invoice.
Fields
Field |
Type |
Description |
|---|---|---|
|
|
Primary key. |
|
|
Parent invoice. Related name: |
|
|
External item ID. |
|
|
Item description. |
|
|
Quantity (default: 1). |
|
|
Unit price. |
Properties
Property |
Type |
Description |
|---|---|---|
|
|
|
MidtransSubscription
Recurring subscription model.
Fields
Field |
Type |
Description |
|---|---|---|
|
|
Primary key. |
|
|
Midtrans subscription ID. Unique, indexed. |
|
|
Subscription name. |
|
|
|
|
|
Recurring amount. |
|
|
Default: |
|
|
Card token or GoPay account ID. |
|
|
Billing interval count (default: 1). |
|
|
|
|
|
Max billing cycles (default: 12). |
|
|
Subscription start time (nullable). |
|
|
Current interval count. |
|
|
Retry interval count. |
|
|
Retry unit (default: |
|
|
Max retries (default: 3). |
|
|
|
|
|
Customer first name. |
|
|
Customer last name. |
|
|
Customer email. |
|
|
Customer phone. |
|
|
Optional Django user. Related name: |
|
|
Raw create response. |
|
|
Arbitrary metadata. |
Properties
Property |
Type |
Description |
|---|---|---|
|
|
Human-readable schedule, e.g. |
MidtransRefund
Refund record linked to a payment.
Fields
Field |
Type |
Description |
|---|---|---|
|
|
Primary key. |
|
|
Parent payment. Related name: |
|
|
Unique refund key. |
|
|
Refund amount. |
|
|
Refund reason. |
|
|
Whether this was a direct (online) refund. |
|
|
Refund status (default: |
|
|
Midtrans status code. |
|
|
Midtrans status message. |
|
|
Raw refund API response. |
Example
from django_midtrans.models import MidtransPayment
payment = MidtransPayment.objects.get(order_id="ORDER-123")
# Access related objects
for item in payment.items.all():
print(f"{item.name}: {item.subtotal}")
for notification in payment.notifications.all():
print(f"{notification.transaction_status} at {notification.created_at}")
for refund in payment.refunds.all():
print(f"Refund {refund.refund_key}: {refund.amount}")