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.


Step:1

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.


Step:2

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

Setp:3

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

CI_ENVIRONMENT = development

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

composer require stripe/stripe-php


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

<?php

namespace Config;

$routes = Services::routes();

$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();

$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';
}

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

<?php

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";
    }
}

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

<!DOCTYPE html>
<html>
    <head>
        <title>Cart List</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container">  
            <table class="table">
                <thead>
                <tr>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Quantiy</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>Smart Watch</td>
                    <td>$42</td>
                    <td>1</td>
                    <td>
                        <a href="<?php echo base_url(); ?>payment" class="btn btn-primary">Buy Now</a>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

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

php spark serve

Output
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