{"id":897,"date":"2022-08-27T13:29:31","date_gmt":"2022-08-27T13:29:31","guid":{"rendered":"https:\/\/appfinz.com\/blogs\/?p=897"},"modified":"2024-08-22T11:09:38","modified_gmt":"2024-08-22T11:09:38","slug":"how-to-integrate-razorpay-payment-gateway-in-laravel","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/how-to-integrate-razorpay-payment-gateway-in-laravel\/","title":{"rendered":"How to Integrate Razorpay Payment Gateway in Laravel"},"content":{"rendered":"\n<p>Hello Guyz, In this youtube tutorial, I am showing you how to integrate the Razorpay payment gateway in laravel 9.\u00a0But first, let me introduce you to <a href=\"https:\/\/razorpay.com\/\">Razorpay Payment Gateway<\/a>, Razorpay\u00a0is the only\u00a0payment\u00a0solution provider in India which allows businesses and individuals to accept, process, and disburse\u00a0payments.\u00a0<\/p>\n\n\n\n<p>Razorpay payment gateway accepts&nbsp;all payment modes with domestic and international credit &amp; debit cards, EMIs ( Credit\/Debit Cards &amp; Cardless), PayLater Options Available, Netbanking from 58 banks, and  UPI (<strong>Unified Payments Interface<\/strong>) and also 8 types of mobile wallets, Razorpay provides the most extensive set of payment methods available in India today. <\/p>\n\n\n\n<p>Razorpay Payment Gateway is very easy as compared to any other payment gateways, The developer docs in very user friendly and easy to understand. so here i am demonstrating the Razorpay Payment Gateway integration in Laravel 9 here.<\/p>\n\n\n\n<p>So, let&#8217;s see Razorpay payment gateway integration in laravel 9 and laravel 8 and laravel 7,&nbsp;how to integrate Razorpay payment gateway in laravel&nbsp;9,&nbsp;Razorpay payment gateway&nbsp;integration&nbsp;in laravel&nbsp;9<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">Step 1 : Create Razorpay Account in Razorpay Payment Gateway Website\nStep 2 : Install Razorpay Package in Laravel 9\nStep 3 : Add API Key and Secret Key \nStep 4 : Create Route\nStep 5 : Create Controller\nStep 6 : Create View File<\/pre><\/div>\n\n\n\n<p><strong>Step 1 :&nbsp;Create Razorpay Account in&nbsp;Razorpay<\/strong><\/p>\n\n\n\n<p>First, we need to signup\/signin into the razorpay and verify our account verfication process is very simple.<\/p>\n\n\n\n<p><strong>Step 2 : Install Razorpay Package in Laravel 9<\/strong><\/p>\n\n\n\n<p>Now, we need to install&nbsp;<strong>razorpay\/razorpay<\/strong>&nbsp;package&nbsp;in laravel. &amp; copy the below command and run it in your terminal.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">composer require razorpay\/razorpay<\/pre><\/div>\n\n\n\n<p><strong>Step 3 : Add Key And Secret Key<\/strong><\/p>\n\n\n\n<p>Now, we need to add key and secret key in the&nbsp;<strong>.env<\/strong>&nbsp;file for&nbsp;razorpay API integration, you can find these key from the setting menu of the razorpay dashboard. go to setting -&gt; API Key and generate API Keys it will generate key secret id and secret key<\/p>\n\n\n\n<p>Go Here:&nbsp;<a href=\"https:\/\/dashboard.razorpay.com\/app\/keys\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/dashboard.razorpay.com\/app\/keys<\/a>.<\/p>\n\n\n\n<p>Next you can get account key id and secret and add on .env file as like bellow:<\/p>\n\n\n\n<p><strong>.env<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">RAZORPAY_KEY=rzp_test_XXXXXXXXX\nRAZORPAY_SECRET=XXXXXXXXXXXXXXXX<\/pre><\/div>\n\n\n\n<p><strong>Step 4 : Create Route<\/strong><\/p>\n\n\n\n<p>&nbsp;In this step, we are creating two routes&nbsp;in the&nbsp;<strong>web.php<\/strong>&nbsp;file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">Route::get('\/', [App\\Http\\Controllers\\RazorpayController::class, 'razorpay'])-&gt;name('razorpay');\nRoute::post('razorpaypayment', [App\\Http\\Controllers\\RazorpayController::class, 'payment'])-&gt;name('payment');<\/pre><\/div>\n\n\n\n<p><strong>Step 5 : Create Controller<\/strong><\/p>\n\n\n\n<p>In this step, we are creating&nbsp;<strong>RazorpayController&nbsp;<\/strong>in this location<strong>&nbsp;app\/Http\/Controllers.<\/strong> through artisan<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:controller RazorpayController<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;RazorpayController.php&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\nuse Razorpay\\Api\\Api;\nuse Session;\nuse Redirect;\n\nclass RazorpayController extends Controller\n{\n    public function razorpay()\n    {        \n        return view('welcome');\n    }\n\n    public function payment(Request $request)\n    {        \n        $input = $request-&gt;all();        \n        $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));\n        $payment = $api-&gt;payment-&gt;fetch($input['razorpay_payment_id']);\n\n        if(count($input)  &amp;&amp; !empty($input['razorpay_payment_id'])) \n        {\n            try \n            {\n                $response = $api-&gt;payment-&gt;fetch($input['razorpay_payment_id'])-&gt;capture(array('amount'=&gt;$payment['amount'])); \n\n            } \n            catch (\\Exception $e) \n            {\n                return  $e-&gt;getMessage();\n                \\Session::put('error',$e-&gt;getMessage());\n                return redirect()-&gt;back();\n            }            \n        }\n        \n        \\Session::put('success', 'Payment successful, your order will be despatched in the next 48 hours.');\n        return redirect()-&gt;back();\n    }\n}<\/pre><\/div>\n\n\n\n<p><strong>Step 6 : Create View<\/strong><\/p>\n\n\n\n<p>create a view from where you will process your payment process<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Integrate Payment Gateway in Laravel - Appfinz Technologies&lt;\/title&gt;\n    &lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;\n    &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/jquery\/3.6.0\/jquery.min.js&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/css\/bootstrap.min.css&quot; integrity=&quot;sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW\/dAiS6JXm&quot; crossorigin=&quot;anonymous&quot;&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div class=&quot;container&quot;&gt;\n    &lt;div class=&quot;row&quot;&gt;\n        &lt;div class=&quot;col-md-12&quot;&gt;\n            @if($message = Session::get('error'))\n                &lt;div class=&quot;alert alert-danger alert-dismissible fade in&quot; role=&quot;alert&quot;&gt;\n                    &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;alert&quot; aria-label=&quot;Close&quot;&gt;\n                        &lt;span aria-hidden=&quot;true&quot;&gt;\u00d7&lt;\/span&gt;\n                    &lt;\/button&gt;\n                    &lt;strong&gt;Error!&lt;\/strong&gt; {{ $message }}\n                &lt;\/div&gt;\n            @endif\n\n            @if($message = Session::get('success'))\n                &lt;div class=&quot;alert alert-info alert-dismissible fade in&quot; role=&quot;alert&quot;&gt;\n                    &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;alert&quot; aria-label=&quot;Close&quot;&gt;\n                        &lt;span aria-hidden=&quot;true&quot;&gt;\u00d7&lt;\/span&gt;\n                    &lt;\/button&gt;\n                    &lt;strong&gt;Success!&lt;\/strong&gt; {{ $message }}\n                &lt;\/div&gt;\n            @endif\n\n            &lt;div class=&quot;panel panel-default&quot; style=&quot;margin-top: 30px;&quot;&gt;\n                &lt;div class=&quot;panel-heading&quot;&gt;\n              &lt;h1&gt; How to integrate payment gateway in laravel &lt;\/h1&gt;\n                    &lt;h2&gt;Pay With Razorpay&lt;\/h2&gt;\n\n                    &lt;form action=&quot;{{ route('payment') }}&quot; method=&quot;POST&quot; &gt;\n                    @csrf\n                        &lt;script src=&quot;https:\/\/checkout.razorpay.com\/v1\/checkout.js&quot;\n                                data-key=&quot;{{ env('RAZORPAY_KEY') }}&quot;\n                                data-amount=&quot;1000&quot;\n                                data-buttontext=&quot;Pay 10 INR&quot;\n                                data-name=&quot;Appfinz&quot;\n                                data-description=&quot;Payment&quot;\n                                data-prefill.name=&quot;Kishan Kumar&quot;\n                                data-prefill.email=&quot;kkmishra459@gmail.com&quot;\n                                data-theme.color=&quot;#ff7529&quot;&gt;\n                        &lt;\/script&gt;\n                    &lt;\/form&gt;\n                &lt;\/div&gt;\n            &lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n\n<p>And after successful completion of payment, you can see transaction details in the Razorpay dashboard.<\/p>\n\n\n\n<p>you can get a testing card for razorpay from here:&nbsp;<a href=\"https:\/\/razorpay.com\/docs\/payment-gateway\/test-card-details\/\" target=\"_blank\" rel=\"noreferrer noopener\">Click Here<\/a><\/p>\n\n\n\n<p>Now you can run and check.<\/p>\n\n\n\n<p>I hope it can help you&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Razorpay payment integration in Laravel<\/p>\n","protected":false},"author":1,"featured_media":898,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,21,211],"tags":[],"class_list":["post-897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-laravel","category-payment-gateway"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=897"}],"version-history":[{"count":7,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/897\/revisions"}],"predecessor-version":[{"id":907,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/897\/revisions\/907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/898"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}