How to add (backend) stripe payment method in Laravel?
In this complete tutorial, we will learn how to integrate Laravel stripe payments. We will look at a Laravel payment gateway integration example.
To add the Stripe payment method in Laravel, you can use the Stripe PHP library.
- First, you will need to install the Stripe PHP library in your Laravel project. You can do this using Composer by running the following command:
composer require stripe/stripe-php
- Next, you will need to sign up for a Stripe account and obtain your Stripe API keys. You can find your API keys in the Stripe dashboard under the “Developers” tab.
- In your Laravel application, you will need to configure the Stripe PHP library with your API keys. You can do this by adding the following code to your
.env
file:
STRIPE_SECRET=sk_test_0000000000000000 STRIPE_PUBLISHABLE=pk_test_00000000000
- To accept payment using Stripe, you will need to create a form that includes a credit card input field and a submit button. You can use Stripe Elements to create a customizable credit card form that is compatible with Stripe’s payment processing.
- When the form is submitted, you can use the Stripe PHP library to create a charge using the
\Stripe\Charge::create()
method. This method takes an array of parameters as an argument, including the amount to be charged, the credit card details, and the currency.
Here is a sample code that demonstrates how to accept a payment using Stripe in Laravel:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Stripe\Charge; use Stripe\Stripe; class PaymentController extends Controller { public function charge(Request $request) { // Set your secret key: remember to change this to your live secret key in production // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe::setApiKey(env('STRIPE_SECRET')); // Token is created using Checkout or Elements! // Get the payment token ID submitted by the form: $token = $request->stripeToken; // Charge the user's card: $charge = Charge::create([ 'amount' => 1000, 'currency' => 'usd', 'description' => 'Example charge', 'source' => $token, ]); return redirect()->route('home')->with('success', 'Payment successful!'); } }
Card numbers to test payment integration
You can use the following card data for testing purposes and ensure that your integration works as expected.
Number | Brand | CVC | Date |
---|---|---|---|
4242424242424242 | Visa | Any 3 digits | Any future date |
4000056655665556 | Visa (debit) | Any 3 digits | Any future date |
5555555555554444 | Mastercard | Any 3 digits | Any future date |
2223003122003222 | Mastercard (2-series) | Any 3 digits | Any future date |
5200828282828210 | Mastercard (debit) | Any 3 digits | Any future date |
5105105105105100 | Mastercard (prepaid) | Any 3 digits | Any future date |
378282246310005 | American Express | Any 4 digits | Any future date |
371449635398431 | American Express | Any 4 digits | Any future date |
6011111111111117 | Discover | Any 3 digits | Any future date |
6011000990139424 | Discover | Any 3 digits | Any future date |
3056930009020004 | Diners Club | Any 3 digits | Any future date |
36227206271667 | Diners Club (14 digit card) | Any 3 digits | Any future date |
3566002020360505 | JCB | Any 3 digits | Any future date |
6200000000000005 | UnionPay | Any 3 digits | Any future date |
Finally, we completed this basic Laravel with Stripe Payment Gateway. If you like my hard work, please share this tutorial with others. I would appreciate it.