Any business project cannot be said to be complete until you are able to sell it. To make the sale, you should be able to receive the payment.
There are two types of payment that are mostly used in any business project:
- Single payment: A single payment is what needs to be made only once. For example, making a payment to purchase through an ecommerce website.
- Recurring payment: A recurring payment is that which you have to make more than once in a particular period of time. For example, subscribing to web hosting services for a website.
In this blog, I will tell you how you can implement the recurring payment process in laravel. The blog will focus on implementing the stripe payment.
If you are into web development, this blog will be a very handy guide. You might also want to check out how you can set up stripe connect marketplace account.
Let's get started.
Implementing recurring payment with stripe
Step 1: Install Laravel Fresh Project
To begin, use the command below to install Laravel as a new application, then open your command prompt and run it:
composer create-project --prefer-dist laravel/laravel stripeRecurringPayment
Step 2: Install Stripe package
composer require cartalyst/stripe-laravel
After that, you need to register the provider and aliases. Go to the app/config/app.php and enter the lines below:
‘providers’ => [
..........
Cartalyst\Stripe\Laravel\StripeServiceProvider::class
],
‘aliases’ => [
..........
'Stripe' => Cartalyst\Stripe\Laravel\Facades\Stripe::class
],
Step 3: Set Secret Credential
Now open .env file and set the secret credential provided by a stripe payment gateway
STRIPE_KEY=pk_test_xxxxxxxxxxxxxxxxxxx
STRIPE_SECRET=sk_test_xxxxxxxxxxxxxx
Next step, you need to set up the Stripe API key, to do this open or create the config/services.php file, and add or update the 'stripe' array:
1
2
3
'stripe' => [
'secret' => env('STRIPE_SECRET'),
],
Step 4: Make Route
Route::get('stripe', 'StripeController@index');
Route::post('store', 'StripeController@store');
Step 5: Create Controller
In this step, you need to create the controller name StripeController using the below command.
php artisan make:controller StripeController
Go to app/Http/Controller/StripeController and enter the below code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use Stripe;
class StripeController extends Controller
{
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function stripe()
{
return view('stripe');
}
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function stripePost(Request $request)
{
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
//create customer
$customer = \Stripe\Customer::create([
'name' => 'your name',
'address' => [
'line1' => 'your address',
'postal_code' => 'your postal code',
'city' => 'Your City,
'state' => 'Your State Code',
'country' => 'Your Country Code',
],
'email' => 'your email',
'source' => $request->stripeToken
]);
$stripe = new \Stripe\StripeClient(
env('STRIPE_SECRET')
);
$customer_id = $customer->id;
//create product
$product = $stripe->products->create([
'name' => 'Diamond',
'id' => '123',
'metadata' => [
'name' => "silver",
'last-date' => '30-7-2021'
]
]);
$product_id = $product->id;
//define product price and recurring interval
$price = $stripe->prices->create([
'unit_amount' => 2000,
'currency' => 'usd',
'recurring' => ['interval' => 'month'],
'product' => $product_id,
]);
$price_id = $price->id;
//CREATE SUBSCRIPTION
$subscription = $stripe->subscriptions->create([
'customer' => $customer_id,
'items' => [
['price' => $price_id],
],
'metadata' => [
'start_date' => '30-7-2021',
'total_months' => '11',
'end_date' => '30-5-2022'
]
]);
Session::flash('success', 'Payment successful!');
return back();
}
}
In the above example, you can see that there are basically three things that are done with the stripe code.
- Creating customers
- Creating a recurring product
- Creating a subscription and assigning that subscription to the above created customer with the above created product.
How to create blade view file?
To create a frontend view you can simply use the Stripe code which shows the popup form and a form to store all the required information.
To create a blade views file, go to app/resources/views/ and create one file named stripe.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="csrf-token" content="">
<title>Stripe Payment Gateway Integrate - Tutsmake.com</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
<style>
.mt40{
margin-top: 40px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12 mt40">
<div class="text-center">
<h2>Pay for Event</h2>
<br>
</div>
</div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> Something went wrong<br>
<ul>
@foreach ($errors->all() as $error)
<li></li>
@endforeach
</ul>
</div>
@endif
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<form accept-charset="UTF-8" action="" class="require-validation"
data-cc-on-file="false"
data-stripe-publishable-key="test_public_key"
id="payment-stripe" method="post">
<div class='row'>
<div class='col-xs-12 form-group'>
<label class='control-label'>Name on Card</label> <input
class='form-control' size='4' type='text'>
</div>
</div>
<div class='row'>
<div class='col-xs-12 form-group'>
<label class='control-label'>Card Number</label> <input
autocomplete='off' class='form-control' size='20'
type='text' name="card_no">
</div>
</div>
<div class='row'>
<div class='col-xs-4 form-group'>
<label class='control-label'>CVC</label> <input autocomplete='off'
class='form-control' placeholder='ex. 311' size='3'
type='text' name="ccv">
</div>
<div class='col-xs-4 form-group'>
<label class='control-label'>Expiration</label> <input
class='form-control' placeholder='MM' size='2'
type='text' name="expiry_month">
</div>
<div class='col-xs-4 form-group'>
<label class='control-label'> </label> <input
class='form-control' placeholder='YYYY' size='4'
type='text' name="expiry_year">
</div>
</div>
<div class='row'>
<div class='col-md-12'>
<div class='form-control total btn btn-info'>
Total: <span class='amount'>$20</span>
</div>
</div>
</div>
<div class='row'>
<div class='col-md-12 form-group'>
<button class='form-control btn btn-primary submit-button'
type='submit' style="margin-top: 10px;">Pay »</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
How to start development server?
In this step, you can use the php artisan serve command . It will start your server locally.
php artisan serve
Testing Card Credential
Card No : 4242424242424242
Month : any future month
Year : any future Year
CVV : 123
And there you have it. Use your demo stripe keys to test the payment process.
Now, you can use these simple steps and implement this recurring payment process on your own.
To access the full code you can download it from my git repo.
https://github.com/iqbaldhillon116/laravelStripePayment.git
Wrapping Up
In this blog, I showed you how to do recurring deposits with Stripe. I believe the steps mentioned above are clear and will assist you in achieving your objectives.
Still, if you require assistance with website development or the creation of a professional website, Contact Us!
Editor: Amrutha