PayU India Python Integration

payu_websdk (GitHub only) · active · verified Wed Mar 25

PayU India payment gateway. PACKAGE NAME CONFUSION: 'payu' on PyPI (1.2.1) is an unrelated HPC workflow tool — NOT PayU payments. Official PayU India Python SDK is 'payu_websdk' (from payu-india/web-sdk-python on GitHub, not on PyPI). 'payu-sdk' (1.2.0 on PyPI) is unofficial/community. Most Python integrations use manual hash generation without any SDK. Core requirement: SHA512 hash must be generated server-side for every transaction. Amount as string in rupees. Test environment: test.payu.in, Live: secure.payu.in.

Warnings

Install

Imports

Quickstart

PayU India — manual hash generation and payment form data preparation.

import hashlib
import requests
import uuid

# PayU credentials from dashboard.payu.in
KEY = 'your_merchant_key'
SALT = 'your_merchant_salt'
ENV = 'test'  # 'test' or 'prod'

BASE_URL = 'https://test.payu.in' if ENV == 'test' else 'https://secure.payu.in'

def generate_hash(key, txnid, amount, productinfo, firstname, email, salt, **udf):
    udf1 = udf.get('udf1', '')
    udf2 = udf.get('udf2', '')
    udf3 = udf.get('udf3', '')
    udf4 = udf.get('udf4', '')
    udf5 = udf.get('udf5', '')
    hash_str = f'{key}|{txnid}|{amount}|{productinfo}|{firstname}|{email}|{udf1}|{udf2}|{udf3}|{udf4}|{udf5}||||||{salt}'
    return hashlib.sha512(hash_str.encode()).hexdigest().lower()

# Create payment params
txnid = str(uuid.uuid4())[:20]
amount = '500.00'  # string, rupees

hash_val = generate_hash(
    KEY, txnid, amount,
    'Widget', 'John', 'john@example.com',
    SALT
)

payment_data = {
    'key': KEY,
    'txnid': txnid,
    'amount': amount,
    'productinfo': 'Widget',
    'firstname': 'John',
    'email': 'john@example.com',
    'phone': '9999999999',
    'surl': 'https://yourapp.com/payment/success',  # success URL
    'furl': 'https://yourapp.com/payment/failure',  # failure URL
    'hash': hash_val,
}
# POST this form data to BASE_URL/_payment
print('POST to:', f'{BASE_URL}/_payment')
print('Params:', payment_data)

view raw JSON →