In this blog, we will see how to integrate Django and PayU Payment Gateway. To integrate with PayU, we have package called "django-payu" - a pluggable Django application.

GitHub Repository: django-payu

Documentaion: django-payu.readthedocs.org

Install:

$ pip install django-payu

Integration Process:

1. To start the integartion process, you need test merchant account and test credit card credentials to have the experience of overall transaction flow.

  • Once you create the account with PayU, they will provide SALT and KEY, we need this two credentials for the integration.

    NOTE: Here, you need to make the transaction request to the test-server and not on the production-server. Once you are ready and understood the entire payment flow, you can move to the production server.

2. To initialise the transaction, you need to generate a post request to the below urls with the parameters mentioned below

  • For PayU Test Server: POST URL: https://test.payu.in/_payment
  • For PayU Production (LIVE) Server: POST URL: https://secure.payu.in/_payment
  • Parameters to be posted by Merchant to PayU in transaction request are:
    • key (Mandatory), txnid (Mandatory), amount(Mandatory), productinfo(Mandatory), firstname(Mandatory), email (Mandatory), phone (Mandatory), lastname, udf1-udf5, address1, address2, city, state, country, zipcode, surl(Mandatory), furl(Mandatory), curl(Mandatory), hash(Checksum)(Mandatory): sha512(key|txnid|amount|productinfo|firstname|email| udf1|udf2|udf3|udf4|udf5||||||SALT)
  • NOTE:
    • udf : User defined field
    • surl : Sucess URL (Success redirection URL)
    • furl : Failure URL (Failure redirection URL)
    • curl : Cancel URL (Cancel redirection URL)
    • hash(Checksum) : A checksum is generated by a mathematical function using the Merchant ID(key), message and the Salt as input. The Checksum algorithm used is SHA512.

3. Add the following information in the setting file using the details from your PayU account:

PAYU_MERCHANT_KEY = "Your MerchantID",

PAYU_MERCHANT_SALT = "Your MerchantSALT",

# Change the PAYU_MODE to 'LIVE' for production.
PAYU_MODE = "TEST"

4. When the user click on the checkout button in your template, generate the mandatory parameter named "hash" using the get_hash() method.

from payu import get_hash
from uuid import uuid4

data = {
    'txnid':uuid4().hex, 'amount':10.00, 'productinfo': 'Sample Product',
    'firstname': 'test', 'email': 'test@example.com', 'udf1': 'Userdefined field',
}
hash_value = get_hash(data)

5. Then, send a post request to the PayU server using the HTML form filled with the data submitted by the buyer containing all the fields mentioned above.

6. When the transaction "post request" hits the PayU server, a new transaction is created in the PayU Database. For every new transaction in the PayU Database, a unique identifier is created every time at PayU’s end. This identifier is known as the PayU ID (or MihPayID).

7. Then the customer would be re-directed to PayU’s payment page. After the entire payment process, PayU provides the transaction response string to the merchant through a "post response". In this response, you would receive the status of the transaction and the hash.

8. Similar to Step 4, you need verify this hash value at your end and then only you should accept/reject the invoice order. You can verfy this hash using check_hash() method.

from django.http import HttpResponse
from payu import check_hash
from uuid import uuid4

def success_response(request):
    if check_hash(request.POST):
        return HttpResponse("Transaction has been Successful.")

In this "django-payu" package, there are many other functions to Capture, Refund, Cancel etc., For the detailed documentation, see here.

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties
Latest Comments
Related Articles
Deploying Your Django app on Heroku Swetha Naretla

Heroku is a platform as a service (PaaS) that enables developers to build and run applications entirely in the cloud.
1. installation
2. Creating and ...

Continue Reading...
Securing Django with multi factor authentication using Django-MFA Vinisha Naladala

Use Django Multi-Factor Authentication method to verify user identity with more than one authentication methods. It can be used for user login, any transactional methods ...

Continue Reading...
Django Unit Test cases with Forms and Views Swetha Naretla

By writing unit test cases, you can evaluate each code component in the initial stage itself and it'll improve your app/code performance. Which is the ...

Continue Reading...