How to Integrate PayUMoney Payment Gateway in Laravel 9

  • 21-08-2022
  • 2849
  • Laravel 9
  • Haresh Chauhan

Laravel 9 PayUMoney payment gateway integration, In this article we will teach you how to integrate PayUMoney payment gateway in your laravel 9 application. This is a very simple integration process. I have defined all the step in this post.

How to implement the PayUMoney payment gateway in laravel, First you need to generate your merchant key and salt key from PayUMoney. To get both access keys, you need to log in or sign up in your PayUMoney business merchant account. from there you will generate your both key.

So let's start implementing PayUMoney Payment gateway integration in the laravel 9 application.

Preview :

PayUMoney payment dashboard.

image

Card & Details fill payUMoney dashboard.

image

Step 1: Create Project

Let's start by cloning a new laravel application. Use the below-provided command, Also you may change the project name as per your requirement. Goto terminal and run the command.

composer create-project laravel/laravel payUMoney

Step 2: Create Your PayUMoney account

Go to PayUMoney and login/Sign Up as a business merchant account.

Step 3: Sign Up & Create

Enter your valid email address and create your business merchant account in the PayUMoney account. There you will be asked for some details about your business, Fill in all required business details.

Laravel Tap Payment Gateway Integration Tutorial Example

Add your business bank account details and submit, then generate your credential from your account.

Step 4: Route & Controller Create

Create a controller using the suggested command, This command will create a PayUMoney controller in your application.

php artisan make:controller PayUMoneyController

Goto your web.php file and paste the given route in your file, there are three routes. The first route for the payment show, the second route from the payUMponey response handled, the Third route for the failed response handled.

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PayUMoneyController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('pay-u-money-view',[PayUMoneyController::class,'payUMoneyView']);
Route::get('pay-u-response',[PayUMoneyController::class,'payUResponse'])->name('pay.u.response');
Route::get('pay-u-cancel',[PayUMoneyController::class,'payUCancel'])->name('pay.u.cancel');

Step 5: PayUMoneyController

This is the PayUMoneyController.php file here is a code for making a payment request, add your merchant id and salt id, you will get these ids from the payout money in your business account. Add some basic details of the user to make payment.

App\Http\Controllers\PayUMoneyController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PayUMoneyController extends Controller
{
    public function payUMoneyView()
    {
        $MERCHANT_KEY = "fB7m8s"; // TEST MERCHANT KEY
        $SALT = "eRis5Chv"; // TEST SALT

        $PAYU_BASE_URL = "https://test.payu.in";

        //$PAYU_BASE_URL = "https://secure.payu.in"; // PRODUCATION
        $name = 'Haresh Chauhan';
        $successURL = route('pay.u.response');
        $failURL = route('pay.u.cancel');
        $email = 'example@gmail.com';
        $amount = 1000;

        $action = '';
        $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
        $posted = array();
        $posted = array(
            'key' => $MERCHANT_KEY,
            'txnid' => $txnid,
            'amount' => $amount,
            'firstname' => $name,
            'email' => $email,
            'productinfo' => 'Webappfix',
            'surl' => $successURL,
            'furl' => $failURL,
            'service_provider' => 'payu_paisa',
        );

        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) {
            $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';
        }

        return view('pay-u',compact('action','hash','MERCHANT_KEY','txnid','successURL','failURL','name','email','amount'));
    }

    public function payUResponse(Request $request)
    {
        dd('Payment Successfully Done');
    }

    public function payUCancel(Request $request)
    {
        dd('Payment Failed');
    }
}

Step 6: Blade File

Load the view page and redirect to the PayUMoney payment page.

resources/views/pay-u.blade.php
<html>
<head>
</head>
<body onload="submitPayuForm()">
Processing Payment.....
<form action="{{$action}}" method="post" name="payuForm"><br />
    <input type="hidden" name="key" value="{{$MERCHANT_KEY}}" /><br />
    <input type="hidden" name="hash" value="{{$hash}}"/><br />
    <input type="hidden" name="txnid" value="{{$txnid }}" /><br />
    <input type="hidden" name="amount" value="{{$amount}}" /><br />
    <input type="hidden" name="firstname" id="firstname" value="{{$name}}" /><br />
    <input type="hidden" name="email" id="email" value="{{$email}}" /><br />
    <input type="hidden" name="productinfo" value="Webappfix"><br />
    <input type="hidden" name="surl" value="{{ $successURL }}" /><br />
    <input type="hidden" name="furl" value="{{ $failURL }}" /><br />
    <input type="hidden" name="service_provider" value="payu_paisa"  /><br />
    <?php
    if(!$hash) { ?>
        <input type="submit" value="Submit" />
    <?php } ?>
</form>
<script>
var payuForm = document.forms.payuForm;
payuForm.submit();
</script>
</body>
</html>
Conclusion

Here in this tutorial, how to learned how to integrate the payUMoney payment gateway in your laravel application from the scratch. Follow each step of this tutorial and integrate it into your laravel 9 application. Before it, you need to generate your merchant id and salt id from the PayUMoney.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :