Razorpay Payment Gateway Integration In Laravel 5.5

By : PHPErrorCode October 23, 2017 253 Views laravel

Today, in this post I share with you how to use the Integration Razorpay Payment Gateway in the laravel 5.5 Web Application with easy example.

When you want to use payment gateway in countries such as India, this Razorpay payment gateway will be useful becose the Razorpay payment gateway can also be used in Rupees currency, which is not available in another payment gateway such as stripe and paypal. And it's very easy to use in any programming language. Let's today, we let' use it in laravel 5.5 web application

Following the simple steps below, you simply integrate Razorpay Payment Gateway into a laravel 5.5 web application.

Create Razorpay Account

First of all, we need to create an Razorpay account, please go to this link https://dashboard.razorpay.com . And get key_id and key_secret from the account setting.

After creating an account, look for the following screenshots to get key_id and key_scricket

screenshot 1
Razorpay Payment Gateway create key and key secret
Next screenshot 2
Razorpay Payment Gateway generate key and key secret
Next screenshot 3
Razorpay Payment Gateway get key id and key secret

Step:1 Install razorpay package

after create account, than you can install razorpay/razorpay below command

composer require razorpay/razorpay

Step:2 Create new Config File

After installing razporay you need to create a custom.php file. Now go to the config folder and create a new file named custom.php. Then add the following two line in this file and set the razor_key and razor_secret.

return [
	'razor_key' => 'rzp_test_razor_key',
	'razor_secret' => 'rzp_test_razor_secret'
];

Step:3 Create Route

Now, after install razorpay package, than go to routes/web.php and open web.php to add 2 route from below .


// Show Payment Form
Route::get('payment', 'PaymentController@payWithRazorpay')->name('get.payment');
// Send Payment Request
Route::post('payment', 'PaymentController@payment')->name('post.payment');

Step:4 Create Controller

Now, You go to the app/HTTP/Controllers and create a new file named PaymentController.php and add below code

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

use Session;
use Redirect;
use Razorpay\Api\Api;

class PaymentController extends Controller
{    
    public function payWithRazorpay()
    {        
        return view('payment');
    }

    public function payment()
    {
        //Input form item
        $input = Input::all();
        //get razor_key_id and razor_key_secret
        $api = new Api(config('custom.razor_key'), config('custom.razor_secret'));
        //Fetch payment information by razorpay_payment_id
        $payment = $api->payment->fetch($input['razorpay_payment_id']);

        if(count($input)  && !empty($input['razorpay_payment_id'])) 
        {
            try {
                $response = $api->payment
                                ->fetch($input['razorpay_payment_id'])
                                ->capture(array('amount'=>$payment['amount'])); 

            } catch (\Exception $e) {
                return  $e->getMessage();
                \Session::put('error',$e->getMessage());
                return redirect()->back();
            }

            // Do something here for store payment details in database...
        }
        
        \Session::put('success', 'Payment successful');
        return redirect()->back();
    }
}

Step:5 Create View

Now, you have to create a view file for the form show. Go to resources/views/ for it and create Paypal.blade.php file. Have the paypal.blade.php file open and add the code below

<!DOCTYPE html>
<html>
<head>
    <title>Razorpay Payment Gateway Integration In Laravel 5.5</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            @if($message = Session::get('error'))
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <strong>Error!</strong> {{ $message }}
                </div>
            @endif
            {!! Session::forget('error') !!}
            @if($message = Session::get('success'))
                <div class="alert alert-info alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <strong>Success!</strong> {{ $message }}
                </div>
            @endif
            {!! Session::forget('success') !!}
            <div class="panel panel-default">
                <div class="panel-heading">Pay With Razorpay</div>

                <div class="panel-body text-center">
                    <form action="{!!route('payment')!!}" method="POST" >
                        <!-- Note that the amount is in paise = 50 INR -->
                        <!--amount need to be in paisa-->
                        <script src="https://checkout.razorpay.com/v1/checkout.js"
                                data-key="{{ Config::get('custom.razor_key') }}"
                                data-amount="500"
                                data-buttontext="Pay 5 INR"
                                data-name="PHPErrorCode"
                                data-description="Order Value"
                                data-image="yout_logo_url"
                                data-prefill.name="name"
                                data-prefill.email="email"
                                data-theme.color="#ff7529">
                        </script>
                        <input type="hidden" name="_token" value="{!!csrf_token()!!}">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Now we are ready to run our laravel cms using bellow command:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000

I hope this post will be useful to you. Please comment below if you find an error