Working with IFSC Code – CodeIgniter 4 Tutorial

Reading Time: 5 minutes
3,053 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.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

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

//...

$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

Open project terminal and start development server via command:

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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.