Codeigniter 4 Integrate Stripe Payment Gateway Tutorial

Apr 19, 2022



Codeigniter 4 Integrate Stripe Payment Gateway Tutorial

Hi Dev,

Today, I will let you know example of how to integrate stripe payment gateway in codeigniter 4 tutorial example. This article will give you simple example of stripe payment gateway in codeigniter 4. I would like to show you integrate stripe payment gateway in codeigniter 4. I would like to show you how to integrate stripe payment gateway.

Codeigniter 4 Stripe payment gateway integration tutorial; In this comprehensive example, you will learn how to implement Stripe Payment Gateway in Codeigniter using the Stripe PHP library and Stripe API key, secret.

Stripe is a notable payment processing for online business; It offers an outstanding infrastructure for payment APIs, which amplifies the transaction of payments for every business type regardless of the company’s size.

So let's start to the example.

Step 1: Install Codeigniter 4

This is optional; however, if you have not created the codeigniter app, then you may go ahead and execute the below command:

composer create-project codeigniter4/appstarter ci-news

After Download successfully, extract clean new Codeigniter 4 application.

Step 2 : Basic Configurations

So, we will now set basic configuration on the app/config/app.php file, so let’s implement to application/config/config.php and open this file on text editor.


public $baseURL = 'http://localhost:8080';


public $baseURL = 'http://localhost/example/';

Step 3 : Database Configurations


public $default = [

'DSN' => '',

'hostname' => 'localhost',

'username' => 'root',

'password' => '',

'database' => 'demo',

'DBDriver' => 'MySQLi',

'DBPrefix' => '',

'pConnect' => false,

'DBDebug' => (ENVIRONMENT !== 'production'),

'cacheOn' => false,

'cacheDir' => '',

'charset' => 'utf8',

'DBCollat' => 'utf8_general_ci',

'swapPre' => '',

'encrypt' => false,

'compress' => false,

'strictOn' => false,

'failover' => [],

'port' => 3306,


Step 4 : Install stripe package Via Composer

composer require stripe/stripe-php

To use the bindings, use the Composer’s autoload


Step 5 : Set Up Controller

Further, you need to generate a new controller that manages the online stripe transaction, hence create a StripePayment file and append the example code in..


namespace App\Controllers;

use CodeIgniter\Controller;

use CodeIgniter\HTTP\RequestInterface;

class StripePaymentController extends Controller



* Write code on Method


* @return response()


public function index()


return view('home');



* Get All Data from this method.


* @return Response


public function payment()



$stripeSecret = 'sk_test_j5k0976GOLSOtiRzbDLpKqat00og5iM3cY';


$stripe = \Stripe\Charge::create ([

"amount" => $this->request->getVar('amount'),

"currency" => "usd",

"source" => $this->request->getVar('tokenId'),

"description" => "Test payment from"


$data = array('success' => true, 'data'=> $stripe);

echo json_encode($data);



Step 5 : Add Routes


$routes->get('/', 'Stripe::index');

Step 6 : Set Up View

Head over to application/views/ folder, create a new home file. Likewise, open and add the suggested code example in application/views/home.php file:


<!DOCTYPE html>



<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 4 Stripe Payment Gateway Integration -</title>

<link rel="stylesheet" href="" />

<script src=""></script>



padding: 0.5%;





<div class="container">

<div class="row">

<div class="col-md-12"><pre id="token_response"></pre></div>


<div class="row">

<div class="col-md-4">

<button class="btn btn-primary btn-block" onclick="pay(100)">Pay $100</button>


<div class="col-md-4">

<button class="btn btn-success btn-block" onclick="pay(500)">Pay $500</button>


<div class="col-md-4">

<button class="btn btn-info btn-block" onclick="pay(1000)">Pay $10000</button>




<script src=""></script>

<script src=""></script>

<script src=""></script>

<script type="text/javascript">






function pay(amount) {

var handler = StripeCheckout.configure({

key: 'pk_test_5f6jfFP2ZV5U9TXQYG0vtqFJ00eFVWNoRX',

locale: 'auto',

token: function (token) {

console.log('Token Created!!');




url:"<?php echo base_url(); ?>stripe/payment",

method: 'post',

data: { tokenId:, amount: amount },

dataType: "json",

success: function( response ) {


$('#token_response').append( '<br />' + JSON.stringify(;





name: 'Demo Site',

description: '2 widgets',

amount: amount * 100






Step :7 Run Codeigniter App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Codeigniter app:

php spark serve

Now, Go to your web browser, type the given URL and view the app output:


I hope it can help you...

#Codeigniter 4