How to implement stripe payment gateway using CodeIgniter 4?

Hello developers,

In this blog post, We will learn how the Stripe payment gateway is used in the CodeIgniter application. This is also known as Stripe session checkout using CodeIgniter 4. Mostly, a payment gateway is used to receive payments from customers. Nowadays, payment gateways are required for all e-commerce sites. With the payment gateway, customers can pay online for their purchases. And vendors get their payments directly as well. Stripe session checkout allows us to make card payments, bank payments, etc. So customers can pay easily. Here, we have to follow a few steps. With the below-mentioned steps, we will develop stripe payment gateway integration.


Create a stripe account, complete the steps which are required for the stripe, after completing copy the Publishable key and Secret key, and save it for future use.


From Codeigniter 4, we can install the Codeigniter application using the CLI installation. Use the following commands in your terminal -

composer create-project codeigniter4/appstarter ithelpershub


After successfully installing your Codeigniter application, copy the env file and rename it to .env and remove # from the beginning of each required env variable.

CI_ENVIRONMENT = development

Install stripe package for the use of inbuild libraries, Run the bellow mentioned command in your terminal.

composer require stripe/stripe-php

Add the bellow-mentioned code into app/Config/Routes.php


namespace Config;

$routes = Services::routes();


$routes->get('/', 'Home::index');
$routes->get('/payment', 'Home::payment');
$routes->get('/payment/success', 'Home::payment_success');
$routes->get('/payment/cancel', 'Home::payment_cancel');

if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';

Now add the bellow-mentioned code into app/Controllers/Home.php, and here will implement ta payment logic.


namespace App\Controllers;

class Home extends BaseController
    public function index() {
        return view('product_list');

    public function payment() {

        //your total payment amount
        $total_payment_amount = 42;

        \Stripe\Stripe::setApiKey('your stripe secret key');

        $checkout_session = \Stripe\Checkout\Session::create([
            'payment_method_types' => ['card','us_bank_account'],
            'line_items' => [[
                'price_data' => [
                    'currency' => 'usd',
                    'product_data' => [
                        'name' => 'Smart Watch',
                    'unit_amount' => $total_payment_amount * 100,
                'quantity' => 1,
            'mode' => 'payment',
            'success_url' => base_url() . "payment/success?stripe_session_id={CHECKOUT_SESSION_ID}",
            'cancel_url' =>  base_url() . "payment/cancel?stripe_session_id={CHECKOUT_SESSION_ID}",

        $redirect_url = $checkout_session->url;
        return redirect()->to($redirect_url);

    public function payment_success() {
        \Stripe\Stripe::setApiKey('your stripe secret key');
        $stripe_session_id = $_REQUEST['stripe_session_id'];
        $session = \Stripe\Checkout\Session::retrieve($stripe_session_id);

        if($session->status=='complete') {
            //your payment success logic, validate payment status and amount_total
            echo "Payment success";

    public function payment_cancel() {
        //your payment cancel logic
        echo "Payment canceled";

Now create a file in the  app/Views/product_list.php and add the code for the payment checkout.

<!DOCTYPE html>
        <title>Cart List</title>
        <link href="" rel="stylesheet">
        <div class="container">  
            <table class="table">
                    <th>Product Name</th>
                    <td>Smart Watch</td>
                        <a href="<?php echo base_url(); ?>payment" class="btn btn-primary">Buy Now</a>

Run the below-mentioned command in your terminal to start the project.

php spark serve

Click on the Buy Now button it will redirect to the payment page.

Payment page - on this page, the customer will input their payment details by selecting payment method..

Hope this tutorial helps you to learn how to implement stripe payment gateway using CI4 and how stripe session checkout will work.

Post a Comment

Previous Post Next Post