Payment Gateway Integration in PHP – PayUMoney

Dear Readers,

In this article i will show you how to create payment gateway system with payumoney in PHP.

  1. First of all you need to create account on PayUMoney .After your account creation you can start your payment processes.
  2. After login to your account you will get your Merchant Key & Salt:

payUmoneyLogin.JPG

You will also get Email of these details.

3. Now you can download the sample code from payumoney.com and update your Merchant key & Salt.

PayUMonay_form.php

<?php

  
                $merchant_key  = "<Your Merchant Key>";

                $salt          = "<Your Salt>";

                $payu_base_url = "https://test.payu.in"; // For Test environment

                $action        = '';

                $currentDir            = 'http://localhost/payumoney/';

                $posted = array();

               

                if(!empty($_POST)) {

                  foreach($_POST as $key => $value) {   

                    $posted[$key] = $value;

                  }

                }




                $formError = 0;

                if(empty($posted['txnid'])) {

                  $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);

                } else {

                  $txnid = $posted['txnid'];

                }




                $hash         = '';

                $hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";




                if(empty($posted['hash']) && sizeof($posted) > 0) {

                  if(

          empty($posted['key'])

          || empty($posted['txnid'])

          || empty($posted['amount'])

          || empty($posted['firstname'])

          || empty($posted['email'])

          || empty($posted['phone'])

          || empty($posted['productinfo'])

          || empty($posted['surl'])

          || empty($posted['furl'])

                  ){

                    $formError = 1;


                  } else {

                                $hashVarsSeq = explode('|', $hashSequence);

                    $hash_string = '';         

                                foreach($hashVarsSeq as $hash_var) {

                      $hash_string .= isset($posted[$hash_var]) ? $posted[$hash_var] : '';

                      $hash_string .= '|';

                    }

                    $hash_string .= $salt;

                    $hash = strtolower(hash('sha512', $hash_string));

                    $action = $payu_base_url . '/_payment';

                  }

                } elseif(!empty($posted['hash'])) {

                  $hash = $posted['hash'];

                  $action = $payu_base_url . '/_payment';

                }

?>

<html>

  <head>

  

    var hash = '';

    function submitPayuForm() {

      if(hash == '') {

        return;

      }

      var payuForm = document.forms.payuForm;

      payuForm.submit();

    }

  

  </head>

  <body onload="submitPayuForm()">

    <h2>PayU Form</h2>

    <br/>

    <?php if($formError) { ?>

      <span style="color:red">Please fill all mandatory fields.</span>

      <br/>

      <br/>

    <?php } ?>

    <form action="<?php echo $action; ?>" method="post" name="payuForm">

      <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 ?>" />

      <table>

        <tr>

          <td><b style="color: red">Mandatory Parameters</b></td>

        </tr>

        <tr>

          <td>Amount <span class="mand">*</span>: </td>

          <td><input name="amount" type="number" value="<?php echo (empty($posted['amount'])) ? '' : $posted['amount'] ?>" /></td>

          <td>First Name <span class="mand">*</span>: </td>

          <td><input type="text" name="firstname" id="firstname" value="<?php echo (empty($posted['firstname'])) ? '' : $posted['firstname']; ?>" /></td>

        </tr>

        <tr>

          <td>Email <span class="mand">*</span>: </td>

          <td><input type="email" name="email" id="email" value="<?php echo (empty($posted['email'])) ? '' : $posted['email']; ?>" /></td>

          <td>Phone <span class="mand">*</span>: </td>

          <td><input type="text" name="phone" value="<?php echo (empty($posted['phone'])) ? '' : $posted['phone']; ?>" /></td>

        </tr>

        <tr>

          <td>Product Info <span class="mand">*</span>: </td>

          <td colspan="3"><textarea name="productinfo"><?php echo (empty($posted['productinfo'])) ? '' : $posted['productinfo'] ?></textarea></td>

        </tr>

        <tr>

          <td>Success URL <span class="mand">*</span>: </td>

          <td colspan="3"><input type="text" name="surl" value="<?php echo (empty($posted['surl'])) ? $currentDir.'success.php' : $posted['surl'] ?>" size="64" /></td>

        </tr>

        <tr>

          <td>Failure URL <span class="mand">*</span>: </td>

          <td colspan="3"><input type="text" name="furl" value="<?php echo (empty($posted['furl'])) ? $currentDir.'failure.php' : $posted['furl'] ?>" size="64" /></td>

        </tr>




        <tr>

          <td colspan="3"><input type="hidden" name="service_provider" value="" size="64" /></td>

        </tr>




        <tr>

          <td><b>Optional Parameters</b></td>

        </tr>

        <tr>

          <td>Last Name: </td>

          <td><input type="text" name="lastname" id="lastname" value="<?php echo (empty($posted['lastname'])) ? '' : $posted['lastname']; ?>" /></td>

          <td>Cancel URI: </td>

          <td><input type="text" name="curl" value="" /></td>

        </tr>

        <tr>

          <td>Address1: </td>

          <td><input type="text" name="address1" value="<?php echo (empty($posted['address1'])) ? '' : $posted['address1']; ?>" /></td>

          <td>Address2: </td>

          <td><input type="text" name="address2" value="<?php echo (empty($posted['address2'])) ? '' : $posted['address2']; ?>" /></td>

        </tr>

        <tr>

          <td>City: </td>

          <td><input type="text" name="city" value="<?php echo (empty($posted['city'])) ? '' : $posted['city']; ?>" /></td>

          <td>State: </td>

          <td><input type="text" name="state" value="<?php echo (empty($posted['state'])) ? '' : $posted['state']; ?>" /></td>

        </tr>

        <tr>

          <td>Country: </td>

          <td><input type="text" name="country" value="<?php echo (empty($posted['country'])) ? '' : $posted['country']; ?>" /></td>

          <td>Zipcode: </td>

          <td><input type="text" name="zipcode" value="<?php echo (empty($posted['zipcode'])) ? '' : $posted['zipcode']; ?>" /></td>

        </tr>

        <tr>

          <td>UDF1: </td>

          <td><input type="text" name="udf1" value="<?php echo (empty($posted['udf1'])) ? '' : $posted['udf1']; ?>" /></td>

          <td>UDF2: </td>

          <td><input type="text" name="udf2" value="<?php echo (empty($posted['udf2'])) ? '' : $posted['udf2']; ?>" /></td>

        </tr>

        <tr>

          <td>UDF3: </td>

          <td><input type="text" name="udf3" value="<?php echo (empty($posted['udf3'])) ? '' : $posted['udf3']; ?>" /></td>

          <td>UDF4: </td>

          <td><input type="text" name="udf4" value="<?php echo (empty($posted['udf4'])) ? '' : $posted['udf4']; ?>" /></td>

        </tr>

        <tr>

          <td>UDF5: </td>

          <td><input type="text" name="udf5" value="<?php echo (empty($posted['udf5'])) ? '' : $posted['udf5']; ?>" /></td>

          <td>PG: </td>

          <td><input type="text" name="pg" value="<?php echo (empty($posted['pg'])) ? '' : $posted['pg']; ?>" /></td>

        </tr>

        <tr>

          <?php if(!$hash) { ?>

            <td colspan="4"><input type="submit" value="Submit" /></td>

          <?php } ?>

        </tr>

      </table>

    </form>

  </body>

</html>

Access the PayUMonay_form.php then you can see the below screen:

payUmoney Form.JPG

This is the sample form and by using its reference you can integrate the fields with your project.

Some fields are mandatory to submit the form:

  1. Amount
  2. First Name
  3. Email
  4. Phone Number
  5. Product Info
  6. Success URL
  7. Failure URL

Remaining all fields are optional and you can also add your own custom fields.

success.php

If payment successfully processed then PayUMonay Server will navigate to the Success URL (surl).

<?php

  $status      =$_POST["status"];

  $firstname   =$_POST["firstname"];

  $amount      =$_POST["amount"];

  $txnid       =$_POST["txnid"];

  $posted_hash =$_POST["hash"];

  $key         =$_POST["key"];

  $productinfo =$_POST["productinfo"];

  $email       =$_POST["email"];

  $salt        ="eCwWELxi";


  If (isset($_POST["additionalCharges"])) {

    $additionalCharges =$_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) {

    echo "Invalid Transaction. Please try again";

  } else {

    echo "<h3>Thank You. Your order status is ". $status .".</h3>";

    echo "<h4>Your Transaction ID for this transaction is ".$txnid.".</h4>";

    echo "<h4>We have received a payment of Rs. " . $amount . ". Your order will soon be shipped.</h4>";

  }        

?>

failure.php

If payment process failsed then PayUMonay Server will navigate to the Failure URL (furl).

<?php

 echo "<h1>Payment Has been cancelled!</h1>";

?>

After submitting the form it will navigate you to the PayUMonay Test Server:

payUmoney payment.JPG

You can test your transaction using Dummy account provided by PayUMonay.

Thanks.

For more details, check documentation.

Source Code is also available on GitHub.

Do comment for any query.

 


Leave a comment