Skip to main content

A pluggable Django application for integrating payment system.

Project description

A pluggable Django application for integrating payment system.


$ pip install django-payfast


$ easy_install django-payfast


$ hg clone
$ cd django-payfast
$ python install

Then add ‘payfast’ to INSTALLED_APPS and execute

$ python syncdb

or (if South is in use)

$ python migrate payfast


Specify your credentials in


If your web server is behind reverse proxy you should also specify PAYFAST_IP_HEADER option. It’s a request.META key with client ip address (default is ‘REMOTE_ADDR’).

There is an option with PayFast server IP addresses (PAYFAST_IP_ADDRESSES). It is a list with current PayFast servers’ ip addresses. If they will change then override this option in your

You also have to setup your PayFast account on Login into the admin panel, go to ‘My Account -> Integration’, enable the Instant Transaction Notification (ITN) and provide the Notify URL.


Payment form

payfast.forms.PayFastForm can be used to construct the html form. It is a helper form for html output and it shouldn’t perform any validation.

Pass all the fields to form ‘initial’ argument. Form also has an optional ‘user’ parameter: it is the User instance the order is purchased by. If ‘user’ is specified, ‘name_first’, ‘name_last’ and ‘email_address’ fields will be filled automatically if they are not passed with ‘initial’.



from django.shortcuts import get_object_or_404
from django.views.generic.simple import direct_to_template
from django.contrib.auth.decorators import login_required

from payfast.forms import PayFastForm

def pay_with_payfast(request, order_id):

    # Order model have to be defined by user, it is not a part
    # of django-payfast
    order = get_object_or_404(Order, pk = order_id)

    form = PayFastForm(initial={
        # required params:
        'item_name': 'the name of the item being charged for',

        # optional params:
        # 'return_url' : '',
        # 'cancel_url' : ''
        # ... etc.
    }, user=order.user)

    return direct_to_template(request, 'pay_with_payfast.html', {'form': form})

Please refer to PayFast docs ( for more options. ‘merchant_id’, ‘merchant_key’, ‘notify_url’ and ‘signature’ params are handled automatically.

The template:

{% extends 'base.html' %}

{% block content %}
    <form action="{{ }}" method="POST">
        <p>{{ form.as_p }}</p>
        <p><input type="submit" value="Buy Now"></p>
{% endblock %}

The {{ form.as_p }} output will be a number of <input type='hidden'> tags.

PayFastForm has a ‘target’ attribute with PayFast server URL.

Please note that it’s up to you to implement the order processing logic. Order handling should be performed in signal handler.

payfast.signals.notify signal

When PayFast posts data to the Notify URL payfast.signals.notify signal is sent. This signal won’t be sent for suspicious data (when request is coming from untrusted ip, form validation fails or the payment is duplicate).

Signal subscribers will get an ‘order’ argument with PayFastOrder instance.


import payfast.signals

def data_received(sender, **kwargs):
    payfast_order = kwargs['order']

    if payfast_order.payment_status == 'COMPLETE':

        # The order is paid (merchant_id check and check for duplicate
        # payments is already handled by django-payfast)

        amount = payfast_order.amount
        # your business logic
        # ...
    else: # the order is not paid
        # your business logic
        # ...


In order to get Notify URL up and running, include payfast.urls in your

urlpatterns = patterns('',
    url(r'^payfast/', include('payfast.urls')),

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-payfast, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size django-payfast-0.1.2.tar.gz (10.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page