Welcome to dj-payfast’s documentation!

dj-payfast is a comprehensive Django library for integrating PayFast payment gateway into your Django applications. It provides a simple, secure, and Pythonic way to accept payments from South African customers.

PyPI version Python versions Django versions License

Overview

PayFast is South Africa’s leading payment gateway, trusted by thousands of businesses to process online payments securely. dj-payfast makes it easy to integrate PayFast into your Django projects with minimal configuration.

Key Features

  • Secure: Built-in signature verification and validation

  • Easy to Use: Simple API similar to dj-stripe and dj-paypal

  • Complete: Models, forms, views, and webhooks included

  • Test Mode: Sandbox support for development and testing

  • Admin Interface: Full Django admin integration

  • Webhooks: Automatic ITN (Instant Transaction Notification) handling

  • Database Tracking: Complete payment history and audit trail

  • Customizable: Support for custom fields and metadata

  • Mobile Ready: Works with PayFast’s mobile payment options

Quick Example

Install using pip:

pip install dj-payfast

Add payfast to your main projects:

INSTALLED_APPS += [
   'payfast'
]

Add App to Main Projects urls.py:

urlpatterns += [
   path('payfast/', include("payfast.urls"))
]

Added payfast application ( /payfast/payments ), perform a post request on, some fields are not required, This is a post request:

{
   "m_payment_id": "",
   "user": null,
   "amount": null,
   "item_name": "",
   "item_description": "",
   "name_first": "",
   "name_last": "",
   "email_address": "",
   "cell_number": "", // optional field
   "custom_str1": "", // optional field
   "custom_str2": "", // optional field
   "custom_str3": "", // optional field
   "custom_str4": "", // optional field
   "custom_str5": "", // optional field
   "custom_int1": null, // optional field
   "custom_int2": null, // optional field
   "custom_int3": null, // optional field
   "custom_int4": null, // optional field
   "custom_int5": null, // optional field
}

Note: The fields cell_number, custom_str1 through custom_str5, and custom_int1 through custom_int5 are optional.

Expected Response

{
   "amount": "99.99",
   "item_name": "Premium Subscription",
   "item_description": "1 month premium access",
   "name_first": "",
   "name_last": "",
   "email_address": "your.email@gmail.com",
   "m_payment_id": "3a280ceb-344c-48d1-8e9c-7945f3f1194a",
   "payfast_url": "http://127.0.0.1:2000/payfast/checkout/2"
}

Using Payfast within React or Android

To use the above Response just redirect to the payfast_url and pay.

Why dj-payfast?

PayFast Integration Made Simple

While PayFast provides excellent documentation, integrating it into Django applications requires handling:

  • Secure signature generation and verification

  • ITN webhook processing and validation

  • Payment tracking and database storage

  • Admin interface for payment management

  • Test and production environment configuration

dj-payfast handles all of this for you, letting you focus on building your application instead of dealing with payment gateway integration details.

Requirements

  • Python 3.8+

  • Django 3.2+

  • requests 2.25.0+

  • A PayFast merchant account (get one at payfast.co.za)

Installation

Install using pip:

pip install dj-payfast

Or install from source:

git clone https://github.com/carrington-dev/dj-payfast.git
cd dj-payfast
pip install -e .

Quick Start

  1. Add to INSTALLED_APPS

    INSTALLED_APPS = [
        ...
        'payfast',
    ]
    
  2. Configure Settings

    # PayFast Configuration
    PAYFAST_MERCHANT_ID = 'your_merchant_id'
    PAYFAST_MERCHANT_KEY = 'your_merchant_key'
    PAYFAST_PASSPHRASE = 'your_passphrase'  # Optional but recommended
    PAYFAST_TEST_MODE = True  # Set to False for production
    
  3. Add URLs

    urlpatterns = [
        ...
        path('payfast/', include('payfast.urls')),
    ]
    
  4. Run Migrations

    python manage.py migrate
    
  5. Start Accepting Payments!

    See the Usage Guide guide for detailed examples.

Documentation Contents

PayFast Resources

Community & Support

Contributing

We welcome contributions! Please see our contributing guide for details on:

  • Reporting bugs

  • Suggesting features

  • Submitting pull requests

  • Running tests

  • Code style guidelines

License

dj-payfast is released under the MIT License. See the LICENSE file for details.

Acknowledgments

This library is inspired by other excellent Django payment libraries:

Special thanks to PayFast for providing a reliable payment gateway for South African businesses.

Indices and tables