Add RazorPay Credentials
RAZORPAY_KEY=rzp_test_XXXXXXX
RAZORPAY_SECRET=XXXXXXXXXXXXXX
If you already have Razorpay account, login into the Razorpay dashboard or create a new Razorpay account. Then go to settings from the left sidebar, and you will see the API KEYS tab.
Install the Composer Package of RazorPay
composer require razorpay/razorpay
Create Route
Route::get('product',[RazorpayController::class,'index']);
Route::post('razorpay-payment',[RazorpayController::class,'store'])->name('razorpay.payment.store');
Create Migration and Model
php artisan make:model Payment --migration
Schema::create('payments',function(Blueprint $table) {
$table->increments('id');
$table->string('r_payment_id');
$table->string('method');
$table->string('currency');
$table->string('user_email');
$table->string('amount');
$table->longText('json_response');
$table->timestamps();
});
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model {
use HasFactory;
protected $table = 'payments';
protected $guarded = ['id'];
}
Create Controller
public function store(Request $request) {
$input = $request->all();
$api = new Api (env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
$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']));
$payment = Payment::create([
'r_payment_id' => $response['id'],
'method' => $response['method'],
'currency' => $response['currency'],
'user_email' => $response['email'],
'amount' => $response['amount']/100,
'json_response' => json_encode((array)$response)
]);
} catch(Exceptio $e) {
return $e->getMessage();
Session::put('error',$e->getMessage());
return redirect()->back();
}
}
Session::put('success',('Payment Successful');
return redirect()->back();
}
Create View File
<div class="card card-default">
<div class="card-header">
Laravel - Razorpay Payment Gateway Integration
</div>
<div class="card-body text-center">
<form action="{{ route('razorpay.payment.store') }}" method="POST" >
@csrf
<script src="https://checkout.razorpay.com/v1/checkout.js"
data-key="{{ env('RAZORPAY_KEY') }}"
data-amount="10000"
data-buttontext="Pay 100 INR"
data-name="GeekyAnts official"
data-description="Razorpay payment"
data-image="/images/logo-icon.png"
data-prefill.name="ABC"
data-prefill.email="abc@gmail.com"
data-theme.color="#ff7529">
</script>
</form>
</div>
</div>