How to integration CCAvenue in codeigniter 3 - deesoft service

How to integration CCAvenue in codeigniter 3

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

Here's a basic outline of the steps you can follow to integrate CCAvenue payment gateway in CodeIgniter 3:

Register and get your CCAvenue merchant account.

Obtain the necessary API credentials, such as your merchant ID, working key, and access code.

Create a form that will collect the payment information from the customer.

In your CodeIgniter application, create a controller method to handle the form data and initiate the payment process.

Use the CCAvenue API to encrypt the form data and redirect the customer to the CCAvenue payment page.

Implement a callback function in your CodeIgniter application to receive and process the response from CCAvenue after the customer has made the payment.

Verify the authenticity of the response from CCAvenue and update your order status accordingly.

public function initiate_payment() {
    $data = array(
        'merchant_id' => 'your_merchant_id',
        'order_id' => 'your_unique_order_id',
        'amount' => 'the_order_amount',
        'redirect_url' => 'your_callback_url',
        'cancel_url' => 'your_cancel_url',
        'language' => 'EN',
    );
    
    // Encrypt the form data using the CCAvenue API
    // ...
    
    // Redirect the customer to the CCAvenue payment page
    // ...
}

// Step 5: Encrypt the form data using the CCAvenue API
$encrypted_data = $this->ccavenue->encrypt($data);

// Step 6: Redirect the customer to the CCAvenue payment page
$this->load->view('ccavenue_payment', array('encrypted_data' => $encrypted_data));

// Step 7: Implement a callback function to receive and process the response from CCAvenue
public function callback() {
    // Decrypt the response from CCAvenue
    $response = $this->ccavenue->decrypt($_POST['encResp']);
    
    // Verify the authenticity of the response
    if ($this->ccavenue->verify($response)) {
        // Update the order status
        $status = $response['order_status'];
        $order_id = $response['order_id'];
        // ...
    } else {
        // Handle the error
        // ...
    }
}

Here's the code for the CCAvenue library that you can use to encrypt and decrypt the form data, and verify the authenticity of the response:

class Ccavenue {
    private $working_key;
    private $access_code;
    
    public function __construct($params = array()) {
        $this->working_key = $params['working_key'];
        $this->access_code = $params['access_code'];
    }
    
    public function encrypt($plaintext) {
        $key = $this->working_key;
        $plaintext .= "&Checksum=" . $this->generateChecksum($plaintext, $key);
        $encrypted = base64_encode($this->aesEncrypt($plaintext));
        return $encrypted;
    }
    
    public function decrypt($encrypted) {
        $decrypted = $this->aesDecrypt(base64_decode($encrypted));
        $data = $this->getArray($decrypted);
        return $data;
    }
    
    public function verify($response) {
        $checksum = $this->generateChecksum($this->getSignatureString($response), $this->working_key);
        return $checksum == $response['checksum'];
    }
    
    private function aesEncrypt($plainText) {
        $secretKey = $this->getSecretKey();
        $initVector = $this->access_code;
        $openSSLString = "AES-128-CBC";
        $encryptedText = openssl_encrypt($plainText, $openSSLString, $secretKey, 0, $initVector);
        return $encryptedText;
    }
    
    private function aesDecrypt($encryptedText) {
        $secretKey = $this->getSecretKey();
        $initVector = $this->access_code;
        $openSSLString = "AES-128-CBC";
        $decryptedText = openssl_decrypt($encryptedText, $openSSLString, $secretKey, 0, $initVector);
        return $decryptedText;
    }
    
    private function generateChecksum($data, $key) {
        return hash_hmac
    }

    private function getArray($data) {
        $data = explode("&", $data);
        $arr = array();
        foreach ($data as $dat) {
            $d = explode("=", $dat);
            $arr[$d[0]] = $d[1];
        }
        return $arr;
    }

    private function getSignatureString($data) {
        $str = "";
        foreach ($data as $key => $value) {
            if ($key != 'checksum') {
                $str .= "$value|";
            }
        }
        return $str;
    }

    private function getSecretKey() {
        $hex = $this->working_key;
        $key = pack('H*', $hex);
        return $key;
    }
}

You also need to replace $params['working_key'] and $params['access_code'] with your actual CCAvenue API credentials.

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