Stripe Integration in Laravel


by Samarth Sinha on 2017-02-25 06:59:44

Stripe is a US technology company, operating in over 25 countries, that allows both private individuals and businesses to accept payments over the Internet.

For integrating Stripe in Laravel, first add the Cashier package for Stripe to your composer.json file and run the composer update command:

"laravel/cashier": "~7.0"

Next, register the Laravel\Cashier\CashierServiceProvider service provider in your config/app.php configuration file.

Now we need to prepare the database. We need to add columns for stripe_id in our Users table which will save the Stripe's customer_id of the User. We can also add fields for save card info i.e. card_brandcard_last_fourtrial_ends_at.

So create migration with following code:

Schema::table('users', function ($table) {

Now create a form in your view file:

{!! Form::open(['route' => 'payment', 'id' => 'payment-form']) !!}
    {!! Form::text('card_holder_name', '', ['id' => 'card_holder_name', 'placeholder' => 'Card Holder Name']) !!}
    {!! Form::text('card_number', '', ['id' => 'card_number', 'placeholder' => 'Card Number']) !!}
    {!! Form::text('exp_month', '', ['id' => 'expiry_date', 'placeholder' => 'Expiry Month']) !!}
    {!! Form::text('exp_year', '', ['id' => 'expiry_date', 'placeholder' => 'Expiry Year']) !!}
    {!! Form::password('cvc', ['id' => 'cvc', 'placeholder' => 'CVC']) !!}
    {!! Form::hidden('amount', '21') !!}
    {!! Form::submit('Pay $21', ['id' => 'submit']) !!}
{!! Form::close() !!}

Now in Controller file we do the code for making payment:

    $token = Stripe\Token::create(array(
        "card" => array(
        "number" => $request->input('card_number'),
        "exp_month" => $request->input('exp_month'),
        "exp_year" => $request->input('exp_year'),
        "cvc" => $request->input('cvc')
    ));// Create Stripe Token
    if ($stripe_cust_id == "")
        $cust = Stripe\Customer::create(array(
            "description" => "Customer for ".$request->card_holder_name,
            "source" => $token->id
        $stripe_cust_id = $cust->id;
        $user = User::find($user_id);
        $user->stripe_id = $cust->id;
        $user->save();// Create customer if not exist and save stripe customer id
        $cus = Stripe\Customer::retrieve($stripe_cust_id); // stored in your application
        $cus->source = $token->id; // obtained with Checkout
        $cus->save();// Update token info of the customer
    $stripe = Stripe\Charge::create(array(
        "customer" => $stripe_cust_id,
        "amount" => ($request->input('amount')*100),
        "currency" => "usd"
    ));// Charge the customer and get all the details in $stripe variable
catch(Exception $e)
    return redirect()->with('msgError', $e->getMessage());

The $stripe variable contain all the details of our payment.

About the Author

  • avatar
    Samarth Sinha

    My name Samarth Sinha. I work on developing Web Applications using PHP and MySQL and in different frameworks of PHP like Laravel, CakePHP, CodeIgnitor and Smarty. In databases I have worked in MySQL and Postgres. I started this blog to share my knowledge to those who wants to start developing Web Applications using PHP.