Codeigniter 4 Integrate Stripe Payment Gateway Tutorial

Apr 19, 2022 . Admin

Hi Dev,

Hello all! In this article, we will talk about integrate stripe payment gateway in codeigniter 4. In this article, we will implement a how to integrate stripe payment gateway in codeigniter 4 tutorial example. I’m going to show you about stripe payment gateway in codeigniter 4. This post will give you simple example of how to integrate stripe payment gateway.

I’m going to show you about stripe payment gateway in codeigniter 4.

we will discuss Codeigniter 4 Stripe payment gateway integration tutorial In this comprehensive example. you will learn you will learn how to implement Stripe Payment Gateway in Codeigniter using the Stripe PHP library and Stripe API key. I’m going to show you about secret. In this article, we will implement a you can understand a concept of how to integrate stripe payment gateway

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

First of all 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

In this step 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 application/config/database.php
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

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

                "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 app/Config/Routes.php
$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