Handling the Redirect URLs

The surl and furl parameters in the Collect Payment API refer to the success and failure URLs respectively. These URLs are used to redirect the customer to your website after a successful or failed transaction.

To handle surl and furl parameters with the Collect Payment API (_payment), you can include them as part of the request payload. Here’s an example of how to do this:

{
    "txnid": "12345",
    "amount": "100.00",
    "productinfo": "Test Product",
    "firstname": "John",
    "email": "[email protected]",
    "phone": "9999999999",
    "surl": "http://example.com/success",
    "furl": "http://example.com/failure"
}

In the above example, the surl parameter is set to β€œhttp://example.com/success” and the furl parameter is set to β€œhttp://example.com/failureβ€œ. These URLs will be used by the PayU India API to redirect the customer to your website after a successful or failed transaction.

πŸ“˜

Note:

The URLs you provide for the surl & furl parameters must be accessible and valid.

The following sample code that demonstrates how to handle surl and furl parameters with the Collect Payment API:

import hashlib
import requests

def make_payment():
    api_key = "your_api_key"
    salt = "your_salt"
    txn_id = "your_transaction_id"
    amount = "100.00"
    product_info = "Test Product"
    first_name = "John"
    email = "[email protected]"
    phone = "9999999999"
    surl = "http://example.com/success"
    furl = "http://example.com/failure"

    hash_string = f"{api_key}|{txn_id}|{amount}|{product_info}|{first_name}|{email}|||||||||||{salt}"
    hash = hashlib.sha512(hash_string.encode()).hexdigest()

    url = "https://test.payu.in/_payment"
    payload = {
        "key": api_key,
        "txnid": txn_id,
        "amount": amount,
        "productinfo": product_info,
        "firstname": first_name,
        "email": email,
        "phone": phone,
        "surl": surl,
        "furl": furl,
        "hash": hash
    }

    response = requests.post(url, data=payload)
    print(response.text)
<?php
function makePayment() {
    $apiKey = "your_api_key";
    $salt = "your_salt";
    $txnId = "your_transaction_id";
    $amount = "100.00";
    $productInfo = "Test Product";
    $firstName = "John";
    $email = "[email protected]";
    $phone = "9999999999";
    $surl = "http://example.com/success";
    $furl = "http://example.com/failure";
    $hashString = "$apiKey|$txnId|$amount|$productInfo|$firstName|$email|||||||||||$salt";
    $hash = hash('sha512', $hashString);

    $data = array(
        'key' => $apiKey,
        'txnid' => $txnId,
        'amount' => $amount,
        'productinfo' => $productInfo,
        'firstname' => $firstName,
        'email' => $email,
        'phone' => $phone,
        'surl' => $surl,
        'furl' => $furl,
        'hash' => $hash,
    );

    $url = "https://test.payu.in/_payment";

    $options = array(
        'http' => array(
            'header' => "Content-type: application/x-www-form-urlencoded\r\n",
            'method' => 'POST',
            'content' => http_build_query($data),
        ),
    );

    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    echo $result;
}
?>
function makePayment() {
    var apiKey = "your_api_key";
    var salt = "your_salt";
    var txnId = "your_transaction_id";
    var amount = "100.00";
    var productInfo = "Test Product";
    var firstName = "John";
    var email = "[email protected]";
    var phone = "9999999999";
    var surl = "http://example.com/success";
    var furl = "http://example.com/failure";
    var hashString = apiKey + "|" + txnId + "|" + amount + "|" + productInfo + "|" + firstName + "|" + email + "|||||||||||" + salt;
    var hash = sha512(hashString);

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "https://test.payu.in/_payment", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            console.log(xhr.responseText);
        }
    };

    var formData = new FormData();
    formData.append("key", apiKey);
    formData.append("txnid", txnId);
    formData.append("amount", amount);
    formData.append("productinfo", productInfo);
    formData.append("firstname", firstName);
    formData.append("email", email);
    formData.append("phone", phone);
    formData.append("surl", surl);
    formData.append("furl", furl);
    formData.append("hash", hash);

    xhr.send(formData);
}

function sha512(value) {
    var hash = CryptoJS.SHA512(value);
    return hash.toString(CryptoJS.enc.Hex);
}
import java.io.*;
import java.net.*;
import java.util.*;

public class PayUIndiaPaymentAPI {
    public static void main(String[] args) {
        try {
            String apiKey = "your_api_key";
            String salt = "your_salt";
            String txnId = "your_transaction_id";
            String amount = "100.00";
            String productInfo = "Test Product";
            String firstName = "John";
            String email = "[email protected]";
            String phone = "9999999999";
            String surl = "http://example.com/success";
            String furl = "http://example.com/failure";
            String hashString = apiKey + "|" + txnId + "|" + amount + "|" + productInfo + "|" + firstName + "|" + email + "|||||||||||" + salt;
            String hash = hashCal("SHA-512", hashString);

            URL url = new URL("https://test.payu.in/_payment");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);

            Map<String, String> parameters = new LinkedHashMap<>();
            parameters.put("key", apiKey);
            parameters.put("txnid", txnId);
            parameters.put("amount", amount);
            parameters.put("productinfo", productInfo);
            parameters.put("firstname", firstName);
            parameters.put("email", email);
            parameters.put("phone", phone);
            parameters.put("surl", surl);
            parameters.put("furl", furl);
            parameters.put("hash", hash);
            String parameterString = getPostDataString(parameters);

            DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
            dataOutputStream.writeBytes(parameterString);
            dataOutputStream.flush();
            dataOutputStream.close();

            InputStream inputStream = connection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                response.append(line);
            }
            bufferedReader.close();
            inputStream.close();
            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String hashCal(String algorithm, String value) throws Exception {
        byte[] messageDigest = MessageDigest.getInstance(algorithm).digest(value.getBytes());
        StringBuilder stringBuilder = new StringBuilder();
        for (byte digestByte : messageDigest) {
            stringBuilder.append(Integer.toString((digestByte & 0xff) + 0x100, 16).substring(1));
        }
        return stringBuilder.toString();
    }

    public static String getPostDataString(Map<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (first) {
                first = false;
            } else {
                result.append("&");
            }
            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }
        return result.toString();
    }
}

In the above code, you need to replace the placeholders your_api_key, your_salt, your_transaction_id, http://example.com/success, and http://example.com/failure with your actual values.

  • Java: This code uses the HttpURLConnection class to send a POST request to the endpoint.
  • JavaScript: This code uses the XMLHttpRequest class to make a POST request to the endpoint. The FormData class is used to create the request payload, which includes surl and furl parameters. The sha512 function calculates the hash of the request parameters. Note that this code uses the CryptoJS library to calculate the hash.
  • Python: This code uses the hashlib library to calculate the hash of the request parameters. The requests library is used to make a POST request to the endpoint. The payload dictionary is used to create the request payload, which includes the surl and furl parameters. The response object contains the response from the API endpoint.
  • PHP: This code uses the hash() function to calculate the hash of the request parameters. The file_get_contents() function is used to make a POST request to the endpoint. The $options array is used to create the request payload, which includes the surl and furl parameters. The $result variable contains the response from the API endpoint.