Working with IFSC Code – CodeIgniter 4 Tutorial

Share this Article
Reading Time: 5 minutes
477 Views

Inside this article we will work with IFSC code. There are several queries we cover in this whole article. Queries like –

  • How to check a IFSC code is valid or not?
  • How to get Bank Details by IFSC code?
  • Check Bank code is valid or not
  • Get bank details by bank code

These all things we will see with detailed concept. To understand things in sequence, we will go with CodeIgniter 4 framework.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Composer Installation – IFSC Razorpay Package

There is composer package for IFSC, needs to install into CodeIgniter 4 application setup. This package saves a lot of time to cover all topics what we have discussed above.

Step #1

Add this dependency to composer.json file of application. This file you will find at project root.

"require": {
  ...
  "php-http/guzzle6-adapter":"^1.0",
}

Step #2

Install razorpay IFSC package

$ composer require razorpay/ifsc

Create Route

Open Routes.php from /app/Config folder. Add this route into it.

//.. Other routes

$routes->get("ifsc-data", "IfscController::index");

Create Controller

Open project into terminal and run this spark command.

$ php spark make:controller Ifsc --suffix

It will create a file with name IfscController.php at /app/Controllers folder.

Open IfscController.php and write this following code into it.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

// Import IFSC classes
use Razorpay\IFSC\Bank;
use Razorpay\IFSC\IFSC;

class IfscController extends BaseController
{
    public function index()
    {
        echo IFSC::validate('KKBK0000261'); // Returns 1 or true
		
		echo "<br/>"; // one line break
		
        echo IFSC::validate('BOTM0XEEMRA'); // Returns null or false
		
		echo "<br/>";
		
        echo IFSC::validateBankCode('PUNB'); // Returns 1 or true
		
		echo "<br/>";
		
        echo IFSC::validateBankCode('ABCD'); // Returns null or false
		
		echo "<br/>";
		
        echo IFSC::getBankName('PUNB'); // Returns 'Punjab National Bank'
		
		echo "<br/>";
		
        echo IFSC::getBankName('ABCD'); // Returns null
		
		echo "<br/>";
		
        echo IFSC::getBankName(Bank::PUNB); //Returns Punjab National Bank
		
		echo "<br/>";
		
        $bank_data = Bank::getDetails(Bank::PUNB);
        print_r($bank_data);
		
		echo "<br/>";
		
        $bank_data_2 = Bank::getDetails('PUNB');
        print_r($bank_data_2);
      
       /*
        // Returns an array:
        // [
        //    'code' => 'PUNB',
        //    'type' => 'PSB',
        //    'ifsc' => 'PUNB0244200',
        //    'micr' => '110024001',
        //    'iin' => '508568',
        //    'apbs' => true,
        //    'ach_credit' => true,
        //    'ach_debit' => true,
        //    'nach_debit' => true,
        //    'name' => 'Punjab National Bank',
        //    'bank_code' => '024',
        //    'upi' => true
        // ]
       */
    }
}

How to check a IFSC code is valid or not?

IFSC::validate('KKBK0000261');

How to get Bank Details by IFSC code?

Here, we need to install a composer package first else we will get some exception when we run application.

$ composer require php-http/message

Then at controller, code will be like this.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

// Import IFSC classes
use Razorpay\IFSC\Client;

class IfscController extends BaseController
{
    public function index()
    {
        $client = new Client();
        $res = $client->lookupIFSC('KKBK0000261');
      
        print_r($res); // get bank details
    }
}

Check Bank code is valid or not

IFSC::validateBankCode('PUNB');

Get bank details by bank code

Bank::getDetails('PUNB');

OR

Bank::getDetails(Bank::PUNB);

Application Testing

Start development server:

$ php spark serve

URL – http://localhost:8080/ifsc-data

We hope this article helped you to learn Working with IFSC Code – CodeIgniter 4 Tutorial in a very detailed way.

For more details here is the link, Click here.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment