Integrate Stripe Payment Gateway in Codeigniter

Integrate Stripe Payment Gateway in Codeigniter

Codeigniter 3 stripe payment gateway integration example tutorial. This tutorial demonstrates to you, how you can integrate stripe payment gateway in your Codeigniter based projects or application.

In this tutorial, we’ll discuss step by step how to integrate stripe card payment gateway in our Codeigniter version 3 based project. Stripe is the most popular payment gateway which is integrated into many websites, Stripe payment is easy to integrate and use. Stripe is a very simple and most powerful and flexible tool.

This tutorial has the purpose to explain a simple and easy way to integrate a stripe card payment gateway in the Codeigniter framework.

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

1. 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');

2. 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);
    }
}

3. Create View

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 - 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>

In this tutorial, we will use the Striped JS library to create a token. When the user hits any of the above buttons our payment() function is called and here we will initialize the striped object and open the stripe popup. Popup will be the default popup of Stripe. You do not have to worry about verification and the security bar 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.

Recommended Tutorials

  1. Razorpay Payment Gateway Integration In Laravel 6
  2. Laravel 6 Instamojo Payment Gateway Integration Example
  3. Laravel 6 Paytm Payment Gateway Integration E.g. Tutorial
  4. Angular 8 – Stripe Payment Gateway Example

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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