Hello World, Welcome to projectsplaza.com. Today i am going to create a tutorial about stripe payment with laravel 7. This is a small and very easy tutorial. It is very easy to integrate stripe with laravel. I have divided this tutorial into the following parts.
- Setup stripe account
- Install stripe package in laravel
- Integrate stripe with laravel
- Make payment
I have assumed that you have average knowledge of laravel.
Setup stripe account
The first step is to create a stripe account on their official website and get the test credentials.
Install stripe package in laravel
Run the following command to install stripe package.
composer require stripe/stripe-php
For more information about this package, check this link.
Integrate stripe with laravel
- Add the follownig code in view file.
-
<form method="post" action="{{url('payment')}}"> <input type="hidden" name="_token" value="{{ csrf_token() }}" /> <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="pk_test_lwVKeaf8FRSWCseAkSQo4q1L00ydSoPvPM" data-amount="100", data-name="Example 1" data-description="Example 1 Description" data-image="https://stripe.com/img/documentation/checkout/marketplace.png" data-locale="auto" data-currency="usd" data-label="Pay Now" > </script> </form>
- The above code will show the stripe button.
- Create the PaymentController and add the following method in it.
-
function paymentProcess(){ \Stripe\Stripe::setApiKey('your-api-key-whick-start-with-sk'); $token=$_POST['stripeToken']; $charge=\Stripe\Charge::create([ 'amount'=>100, 'currency'=>'usd', 'description'=>'This is an example', 'source'=>$token ]); if($charge->paid==true){ return response('Payment has been successfull'); }else{ return response('Some Error Occured!!'); } }
- Add the following code in routes/web.php file
-
Route::post('payment','PaymentController@paymentProcess');
Make Payment
- Now open the URL with the defined route in the browser. You will see the following screen.
- You can test the payment with the following card detail
Card Number: 4111 1111 1111 1111 Expiry: 06/2024 CVV: 123
I hope this tutorial helps you. Please add your feedback in the comment section. Thank you 🙂