How to integration Instamojo in codeigniter 3 - deesoft service

How to integration Instamojo in codeigniter 3

Deepak Tailor Image
Deepak Tailor - Feb 05 2023
How to integration Instamojo in codeigniter 3

Create an account on Instamojo and obtain your API Key and Auth Token.

Install the CodeIgniter framework.

Create a new controller file in the "controllers" folder and name it "Payment.php".

Add the following code to the Payment.php file:

defined('BASEPATH') OR exit('No direct script access allowed');

class Payment extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('instamojo');
    }

    public function index()
    {
        $api = new Instamojo\Instamojo('API_KEY', 'AUTH_TOKEN');

        try {
            $response = $api->paymentRequestCreate(array(
                "purpose" => "Test Payment",
                "amount" => "100",
                "buyer_name" => "John Doe",
                "phone" => "9999999999",
                "send_email" => true,
                "send_sms" => true,
                "email" => "test@example.com",
                "redirect_url" => "http://localhost/codeigniter/payment/success"
                ));
            print_r($response);
        }
        catch (Exception $e) {
            print('Error: ' . $e->getMessage());
        }
    }

    public function success()
    {
        echo 'Payment successful';
    }
}

Create a library file in the "libraries" folder and name it "Instamojo.php".

Add the following code to the Instamojo.php file:

class Instamojo
{
    private $api_key;
    private $auth_token;
    private $endpoint;

    public function __construct($api_key, $auth_token)
    {
        $this->api_key = $api_key;
        $this->auth_token = $auth_token;
        $this->endpoint = 'https://www.instamojo.com/api/1.1/payments/';
    }

    public function paymentRequestCreate($data)
    {
        $url = $this->endpoint;

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
                    array("X-Api-Key:$this->api_key",
                            "X-Auth-Token:$this->auth_token"));
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

        $response = curl_exec($ch);
        curl_close($ch)
    }
}

Add the following routes in the "config/routes.php" file:

$route['payment'] = 'payment';
$route['payment/success'] = 'payment/success';

Load the Instamojo library in the controller constructor:

$this->load->library('instamojo');

Replace the placeholder values in the Payment.php controller file with your actual API Key and Auth Token:

$api = new Instamojo\Instamojo('API_KEY', 'AUTH_TOKEN');

Test the payment integration by accessing the "http://localhost/codeigniter/payment" URL in your browser.

Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in