PayFast Python Integration

245 views
Skip to first unread message

Tawanda Abraham Makunike

unread,
Jan 21, 2016, 6:42:56 PM1/21/16
to Gauteng Python User Group
Good day guys. 

I am working on an ecommerce platform built on Django and I have been trying to integrate with the PayFast  API. I am facing a problem when I submit the form to payfast. I cant seem to generate the signature in the way payfast accepts. The code for generating the signature below is part of my fork of the django-payfast app that I downloaded. I have been working on updating it as a lot has changed since its last update.

from hashlib import md5
import urllib2
import logging
from urllib import urlencode
from collections import OrderedDict

  
POSTBACK_URL = '/eng/query/validate'
POSTBACK_SERVER = 'https://www.payfast.co.za'
  

def _values_to_encode(data):
    return [
        (k, unicode(data[k]).strip().encode('utf8'),)
        for k in data if data[k] and k != 'signature'
    ]
  
def _signature_string(data):
    values = _values_to_encode(data)
    return urlencode(values)

def signature(data):
    """
   Calculates PayFast signature.
   'data' should be an OrderedDict instance.
    """
    text = _signature_string(data)
    print text
    return md5(text).hexdigest()


The data that I am passing into the signature method is an Ordered Dictionary so that the sequence of the keys is maintained, this is how I am building the dictionary

def get_data():
data = OrderedDict()
data['merchant_id'] = 'XXXXXXXXX'
data['merchant_key'] = 'XXXXXXXXX'
data['return_url'] = 'http://example.com/cart/success/'
data['cancel_url'] = 'http://example.com/cart/cancelled/'
data['notify_url'] = 'http://example.com/cart/process/'
data['email_address'] = 'paym...@example.com'
data['m_payment_id'] = '2'
data['amount'] = '234'
data['item_name'] = 'Order'
data['item_description'] = 'Some description here'

The order above is per PayFast specifications on their website. I am kindly asking for any assistance if there is someone who has worked with the api before, I am planning also on releasing the fork I am working on to the public once it has worked and hopefully rally some devs for active maintenance which I am committing to personally.

Any help is greatly appreciated.

Mandla Mbuli

unread,
Jan 22, 2016, 1:16:25 AM1/22/16
to gpu...@googlegroups.com
Hi Tawanda

PayFast? NICE, as consumer, I use it on Takealot all the time. Haven't used it as a dev though.
Looking at the docs for django-payfast, it sounds like you're not supposed to generate the signature
yourself.

> merchant_id’, ‘merchant_key’, ‘notify_url’ and ‘signature’ params are handled automatically.

Do you have link to the latest developer docs for payfast which lists the parameters for the API?

--
Leonard Mandla Mbuli
email:ma...@mandla.me
website: http://mandla.me

--
You received this message because you are subscribed to the Google Groups "Gauteng Python User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gpugsa+un...@googlegroups.com.
To post to this group, send email to gpu...@googlegroups.com.
Visit this group at https://groups.google.com/group/gpugsa.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages