Integrate Stripe Payment Gateway in Codeigniter
integrate stripe – This tutorial will show you how to add a payment method to your Codeigniter project using Stripe. Stripe is a popular payment gateway that’s used by many websites. It’s easy to use and integrate with your application.
We’ll go through the steps to integrate Stripe’s card payment gateway into your Codeigniter version 3 project. Stripe is a simple, powerful, and flexible tool that makes online payments easy.
The aim of this tutorial is to show you a simple and straightforward method to add a stripe card payment gateway to your Codeigniter framework.
integrate stripe – Stripe payment gateway integration in Codeigniter
Follow the below given steps and easily integrate stripe card payment gateway in Codeigniter:
- Download Stripe Payment Gateway Library
- Create Controller
- Create View
Download Stripe Payment Gateway Library
There are two option for download or install stripe library for codeigniter.
1. First download and extract stripe
First of all, we need to download the stripe card payment gateway library for Codeigniter. And place this library into your Codeigniter project.
You can download the Stripe payment gateway library here => Stripe card Payment Gateway library for Codeigniter.
After download, you have to extract that folder into the “application/libraries” folder and make sure the rename folder name “stripe-php”.
2. Install stripe package Via Composer
The second option to install a stripe package via the composer for Codeigniter.
You can also install the stripe package via Composer. You can use the below command for that:
composer require stripe/stripe-php
To use the bindings, use the Composer’s autoload
require_once('vendor/autoload.php');
Create Controller
First of all, we need to create a new Stripe controller, So go application/controller and create a new controller name Stripe.php. In the Stripe controller, we need to create two methods, index(), payment().
Explanation of the following methods:
- index() method would display the product information.
- The payment() method helps to done payment using the stripe payment gateway library.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Stripe extends CI_Controller {
/**
* Get All Data from this method.
*
* @return Response
*/
public function __construct() {
parent::__construct();
$this->load->library("session");
$this->load->helper('url');
}
/**
* Get All Data from this method.
*
* @return Response
*/
public function index()
{
$this->load->view('stripe/index');
}
/**
* Get All Data from this method.
*
* @return Response
*/
public function payment()
{
require_once('application/libraries/stripe-php/init.php');
$stripeSecret = 'sk_test_j5k0976GOLSOtiRzbDLpKqat00og5iM3cY';
\Stripe\Stripe::setApiKey($stripeSecret);
$stripe = \Stripe\Charge::create ([
"amount" => $this->input->post('amount'),
"currency" => "usd",
"source" => $this->input->post('tokenId'),
"description" => "Test payment from tutsmake.com."
]);
// after successfull payment, you can store payment related information into your database
$data = array('success' => true, 'data'=> $stripe);
echo json_encode($data);
}
}
Create Views
In this step, we need to create one folder named stripe.
After that, we need to create views files where we will show the product listing and payment-related information.
First of all, go to application/views/stripe and create a new file inside the stripe folder named index.php. Then update the HTML code below in your index.php file:
<!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="{{ csrf_token() }}" />
<title>Codeigniter Stripe Payment Gateway Integration - torqueprogramming.co.in</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
.container {
padding: 0.5%;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12"><pre id="token_response"></pre></div>
</div>
<div class="row">
<div class="col-md-4">
<button class="btn btn-primary btn-block" onclick="pay(100)">Pay $100</button>
</div>
<div class="col-md-4">
<button class="btn btn-success btn-block" onclick="pay(500)">Pay $500</button>
</div>
<div class="col-md-4">
<button class="btn btn-info btn-block" onclick="pay(1000)">Pay $10000</button>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://checkout.stripe.com/checkout.js"></script>
<script type="text/javascript">
function pay(amount) {
var handler = StripeCheckout.configure({
key: "pk_test_5f6jfFP2ZV5U9TXQYG0vtqFJ00eFVWNoRX", // your publisher key id
locale: "auto",
token: function (token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
console.log("Token Created!!");
console.log(token);
$("#token_response").html(JSON.stringify(token));
$.ajax({
url: "<?php echo base_url(); ?>stripe/payment",
method: "post",
data: { tokenId: token.id, amount: amount },
dataType: "json",
success: function (response) {
console.log(response.data);
$("#token_response").append("<br />" + JSON.stringify(response.data));
},
});
},
});
handler.open({
name: "Demo Site",
description: "2 widgets",
amount: amount * 100,
});
}
</script>
</body>
</html>
In this tutorial, we will use the Striped JS library to create a token. When the user clicks on any of the buttons above, our payment() function will be called. In this function, we’ll set up the Stripe object and open the Stripe popup. The Stripe popup is the default one and you don’t need to worry about security because it will handle everything for you.
Note:- Stripe Payment Gateway
The stripe card test credentials for payment is given below. Use this credential for make test payment.
Card No : 4242424242424242
Month : any future month
Year : any future Year
CVV : 123
Conclusion
In this tutorial, We have successfully integrated the stripe payment gateway in codeigniter Application. Our examples run quickly.
You May Also Like
Integrate Razorpay with PHP Codeigniter
First Codeigniter 3 CRUD (Create,Read,Update,Delete) via Mysql

