Integrate Stripe Payment Gateway in Codeigniter

Integrate Stripe Payment Gateway in Codeigniter

Stripe is the most popular payment gateway that allows collecting payments from users and its integration into a website or application is very easy.

In this tutorial, we’ll discuss step by step how to integrate stripe card payment gateway in our Codeigniter version 3 based project.

Stripe payment gateway integration in Codeigniter

Here are steps to integrate stripe card payment gateway in CI:

  • Download Stripe Payment Gateway Library
  • Create Controller
  • Create View

1. Download Stripe Payment Gateway Library

There are two options for downloading or installing the 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');

2. Create Controller

Create stripe.php file in application/controller that handle payment process:

<?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('myProduct');
}

/**
* 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);
}
}

3. Create View

Create a myProduct.php file in the Application/Views/ folder to allow users to collect payments using Stripe:

<!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 - Tutsmake.com</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>

Here are the Stripe card testing credentials to make payments:

 Card No : 4242424242424242
 Month : any future month
 Year : any future Year
 CVV : 123 

Conclusion

In this tutorial, we have successfully integrated Stripe payment gateway into CodeIgniter application.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *