PayUMoney: PHP integration using Codeigniter

Posted on July 3, 2017 by Suresh Kamrushi in PHP

Follow the below steps to integrate PayUMoney into Codeigniter:
To download the complete code click here. For core php integration Click here.

View More:Payumoney integration in Core PHP

Step 1: Login to PayUMoney.com as a merchant and get merchant key and salt which we need while integration.
Step 2: Update constant.php file, located inside “application/config” folder, with below code:

//PayUMoney Configurations
define('MERCHANT_KEY', 'gtKFFx');
define('SALT', 'eCwWELxi');
//define('PAYU_BASE_URL', 'https://test.payu.in');    //Testing url
define('PAYU_BASE_URL', 'https://secure.payu.in');  //actual URL
define('SUCCESS_URL', 'http://localhost/gst/transaction/order_success');  //have complete url
define('FAIL_URL', 'http://localhost/gst/transaction/order_fail');    //add complete url 

For single point change we have created constant for few of the values so that we can used them on various places of application.You can also define them as config variables in config.php file and access throughout the application.

Step 3: Create a new controller as “transaction” and add method as index add below code. Basically this method collecting all of your cart information and passing to payment getway. You need to submit (using post method) your cart on this method by mentioning action as “transaction/index” and pass values “email”, “mobile”,”firstname”,”lastname” and “totalcost”. This page will collect information and create a hash string and pass it with other information to PayUMoney page.

public function index() {
    //print_r($_POST);
    $data['txnid'] = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
    $data['email'] = $this->input->post('email');
    $data['mobile'] = $this->input->post('mobile');
    $data['firstName'] = $this->input->post('firstName');
    $data['lastName'] = $this->input->post('lastName');
    $data['totalCost'] = $this->input->post('totalCost');
    $data['hash']         = '';
    //Below is the required format need to hash it and send it across payumoney page. UDF means User Define Fields.
    //$hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
    $hash_string = MERCHANT_KEY."|".$data['txnid']."|".$data['totalCost']."|"."productinfo|".$data['firstName']."|".$data['email']."|||||||||||".SALT;
    $data['hash'] = strtolower(hash('sha512', $hash_string));
    $data['action'] = PAYU_BASE_URL . '/_payment';        
    $this->load->view('pum_index',$data);
}

The view file “pum_index” will be like below:

<form action="<?php echo $action; ?>" method="post" name="payuForm" id="payuForm" style="display: block">
    <input type="hidden" name="key" value="<?php echo MERCHANT_KEY ?>" />
    <input type="hidden" name="hash" value="<?php echo $hash ?>"/>
    <input type="hidden" name="txnid" value="<?php echo $txnid ?>" />
    <input name="amount" type="number" value="<?php echo $totalCost; ?>" />
    <input type="text" name="firstname" id="firstname" value="<?php echo $firstName; ?>" />
    <input type="email" name="email" id="email" value="<?php echo $email; ?>" />
    <input type="text" name="phone" value="<?php echo $mobile; ?>" />
    <textarea name="productinfo"><?php echo "productinfo"; ?></textarea>
    <input type="text" name="surl" value="<?php echo SUCCESS_URL; ?>" />
    <input type="text" name="furl" value="<?php echo  FAIL_URL?>"/>
    <input type="text" name="service_provider" value="payu_paisa"/>
    <input type="text" name="lastname" id="lastname" value="<?php echo $lastName ?>" />
</form>
<script type="text/javascript">
    var payuForm = document.forms.payuForm;
    payuForm.submit();
</script>

Step 4: Create method for success as below:

public function order_success() {
    $status = $this->input->post("status");
    $firstname = $this->input->post("firstname");
    $amount = $this->input->post("amount");
    $txnid = $this->input->post("txnid");
    $posted_hash = $this->input->post("hash");
    $key = $this->input->post("key");
    $productinfo = $this->input->post("productinfo");
    $email = $this->input->post("email");
    $salt = "GQs7yium";

    if ($this->input->post("additionalCharges")) {
        $additionalCharges = $this->input->post("additionalCharges");
        $retHashSeq = $additionalCharges . '|' . $salt . '|' . $status . '|||||||||||' . $email . '|' . $firstname . '|' . $productinfo . '|' . $amount . '|' . $txnid . '|' . $key;
    } else {

        $retHashSeq = $salt . '|' . $status . '|||||||||||' . $email . '|' . $firstname . '|' . $productinfo . '|' . $amount . '|' . $txnid . '|' . $key;
    }
    $hash = hash("sha512", $retHashSeq);

    if ($hash != $posted_hash) {
        $data['msg'] = "Invalid Transaction. Please try again";
    } else {
        $data['msg'] = "<h3>Thank You. Your order status is " . $status . ".</h3>";
        $data['msg'] .= "<h4>Your Transaction ID for this transaction is " . $txnid . ".</h4>";
        $data['msg'] .= "<h4>We have received a payment of Rs. " . $amount . ". Your order will soon be shipped.</h4>";
    }

    $this->load->view('pum_common', $data);
}

Once user pay the money, he will redirected to this method with other information. Here we are verifying the posted “hash” value with return “hash” value. if it is same transaction is success else error in processing.
It is recommendation to store the transaction details like “txnid” and “hash” in table after transaction is successful. (here i am not doing that)

Step 5: Create method for failure as below:

public function order_fail() {
    $status = $this->input->post("status");
    $firstname = $this->input->post("firstname");
    $amount = $this->input->post("amount");
    $txnid = $this->input->post("txnid");
    $posted_hash = $this->input->post("hash");
    $key = $this->input->post("key");
    $productinfo = $this->input->post("productinfo");
    $email = $this->input->post("email");
    $salt = "fGxoywOg8S";
    If ($this->input->post("additionalCharges")) {
        $additionalCharges = $this->input->post("additionalCharges");
        $retHashSeq = $additionalCharges . '|' . $salt . '|' . $status . '|||||||||||' . $email . '|' . $firstname . '|' . $productinfo . '|' . $amount . '|' . $txnid . '|' . $key;
    } else {
        $retHashSeq = $salt . '|' . $status . '|||||||||||' . $email . '|' . $firstname . '|' . $productinfo . '|' . $amount . '|' . $txnid . '|' . $key;
    }
    $hash = hash("sha512", $retHashSeq);
    if ($hash != $posted_hash) {
        $data['msg'] = "Invalid Transaction. Please try again";
    } else {
        $data['msg'] = "<h3>Your order status is " . $status . ".</h3>";
        $data['msg'] .= "<h4>Your transaction id for this transaction is " . $txnid . ". You may try making the payment by clicking the link below.</h4>";
    }
    $data['msg'] .= '<p><a href=http://sforsuresh.in/> Try Again</a></p>';
    $this->load->view('pum_common',$data);
}

If transaction fails somehow, it will return to this method with “status”,”txnid” and “hash”. Here I am not storing failed transactions in table, but it is recommendation to insert information into the table for future reference. This transaction is also being track in PayUMoney account.

Step 6: A common view for success and failure page as below:

<?php echo $msg; ?>
  Comments or questions are welcome  
Tagged as ,