Example App Setup

Prerequisites

  • Python 3.10+

  • A Midtrans sandbox account (sign up)

Steps

1. Clone the Repository

git clone https://github.com/rissets/django-payment-midtrans.git
cd django-payment-midtrans

2. Create a Virtual Environment

python -m venv .venv
source .venv/bin/activate  # Linux/macOS
# .venv\Scripts\activate   # Windows

3. Install Dependencies

pip install -e ".[dev,unfold,docs]"

4. Configure Environment

Copy the example .env and fill in your Midtrans sandbox credentials:

cp .env.example .env

Edit .env:

MIDTRANS_SERVER_KEY=SB-Mid-server-xxxxxxxxxxxxxxxxxxxx
MIDTRANS_CLIENT_KEY=SB-Mid-client-xxxxxxxxxxxxxxxxxxxx
MIDTRANS_MERCHANT_ID=G000000000
SECRET_KEY=your-django-secret-key
DEBUG=True

Get your sandbox credentials from the Midtrans Dashboard → Settings → Access Keys.

5. Run Migrations

cd example
python manage.py migrate

6. Create a Superuser

python manage.py createsuperuser

7. Load Sample Data (Optional)

Create some products via the admin or Django shell:

python manage.py shell
from shop.models import Product

Product.objects.create(name="T-Shirt", price=150000, stock=50, description="Cotton t-shirt")
Product.objects.create(name="Hoodie", price=350000, stock=30, description="Premium hoodie")
Product.objects.create(name="Cap", price=75000, stock=100, description="Baseball cap")

8. Start the Server

python manage.py runserver

Visit:

  • Shop: http://localhost:8000/

  • Admin: http://localhost:8000/admin/

  • API: http://localhost:8000/midtrans/api/

9. (Optional) Start Celery

For background payment tasks:

# In a separate terminal
celery -A config worker -B -l info

10. (Optional) Expose for Webhooks

To receive Midtrans notifications locally:

ngrok http 8000

Then set the notification URL in your .env:

MIDTRANS_NOTIFICATION_URL=https://abc123.ngrok-free.app/midtrans/api/notification/

Or configure it directly in the Midtrans Dashboard → Settings → Payment → Notification URL.