razorpay laravel integration

11 Nov

razorpay laravel integration

In order to do razorpay laravel integration. install the razorpay library in laravel.
add the following line the composer json file

“razorpay/razorpay”: “^2.5”
now run composer update command.

it will install razorpay library in your laravel website.

you can also use command

composer require “razorpay/razorpay”

Now add following line at the top section of your controller.

use Razorpay\Api\Api;

Now add two public variable $razor_key and $razor_secret in the controller. You can obtain it from razorpay website.

razor pay shows razor_secret only once at the time of generating keys. you should keept it somewhere save to get it back.

here in the example we will add some in the users wallet. first we well receive money and then update the users wallets with that money.

now in the route/web.php add the following routes

Route::post(‘/wallet’, ‘MembersController@wallet’);
Route::post(‘/payment-success’, ‘MembersController@payment_success’);

Below the exmple of MembersController.

class MembersController extends Controller {

public $razor_key = 'rzp_test_tZrZOPdIvxxxx';
public $razor_secret = 'lRXlfw7Rdk9FKN55guxxxxx';

public function wallet(Request $request) {
        $user = Auth::user();
$user_id = $user->id;

if ($request->isMethod('post')) { $validatedData = $this->validate($request,[ 'wallet_amount' => 'required|numeric|min:10' ]); $UsersWallet = new UsersWallet(); $where = array(); $where[] = array('user_id','=',$user_id); $wallet_row = $UsersWallet->where($where)->first(); $wallet_amount = $request->wallet_amount; $api = new Api($this->razor_key, $this->razor_secret); $arr = array( 'receipt' => 'order_rcptid_' . $user_id, 'amount' => $wallet_amount, // amount in the smallest currency unit 'currency' => 'INR',// <a href="https://razorpay.freshdesk.com/support/solutions/articles/11000065530-what-currencies-does-razorpay-support" target="_blank">See the list of supported currencies</a>.) 'payment_capture' => '0' ); $orderId = $api->Order->create($arr); return view('members.paymentform',compact('rows','wallet_row', 'user', 'wallet_amount','orderId')); }else { $UsersWallet = new UsersWallet(); $where = array(); $where[] = array('user_id','=',$user_id); $wallet_row = $UsersWallet->where($where)->first(); return view('members.wallet',compact('rows','wallet_row')); } } //payment_success method is called when payemtn ge success function payment_success(Request $request) { $user = Auth::user(); $user_id = $user->id; $api = new Api($this->razor_key, $this->razor_secret); $payment = $api->payment->fetch($request->razorpay_payment_id); if(!empty($request->razorpay_payment_id)) { try { $response = $api->payment->fetch($request->razorpay_payment_id)->capture(array('amount' => $payment['amount'])); //below parameters are received from razor pay. you can save it a database table. $response['id']; $response['entity']; $response['amount']; $response['currency']; $response['status']; $response['order_id']; $response['invoice_id']; $response['international']; $response['method']; $response['amount_refunded']; $response['refund_status']; $response['captured']; $response['description']; $response['card_id']; $response['bank']; $response['wallet']; $response['vpa']; $response['email']; $response['contact']; $response['fee']; $response['tax']; $response['error_code']; $response['error_description']; return redirect('/wallet')->with('success','Amount added successfull!'); } catch (\Exception $e) { return $e->getMessage(); \Session::put('error',$e->getMessage()); return redirect()->back(); } // Do something here for store payment details in database... } return redirect()->back(); }

create a file wallet.blade.php and add below form.

<form action="{{ url('wallet') }}" method="POST" class="main-header-form" id="sform" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="box">
<div class="box-body" >
@if ($errors->any())
<div class="alert alert-danger">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endif <div class="row">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="form-group">
<label for="name"><strong>Add money in wallet</strong></label>
<input type="number" name="wallet_amount" id="wallet_amount" value="" class="form-control" placeholder="Enter amounts">

<div class="box-footer">
<button type="submit" class="btn btn-info">Add</button>


Leave a Reply

Your email address will not be published. Required fields are marked *

10 + 3 =