Phonepe integration in codeigniter3 - deesoft service

Phonepe integration in codeigniter3

Deepak Tailor Image
Deepak Tailor - Feb 10 2023
Phonepe integration in codeigniter3

1. Obtain the API key and Merchant ID from PhonePe

2. Create a controller in CodeIgniter to handle the payment processing

3. Create a view file to display the payment form

4. In the controller, make an API call to PhonePe's server to initiate the payment

5. Handle the response from PhonePe's server and update the payment status in your database

Here is an example source code for the controller:

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

class Payment extends CI_Controller {

    public function __construct() {

    public function index() {
        $data['amount'] = 100;
        $this->load->view('payment_form', $data);

    public function process() {
        $amount = $this->input->post('amount');

        // PhonePe API credentials
        $apiKey = 'your_api_key';
        $merchantId = 'your_merchant_id';

        // API URL
        $url = '';

        // API request data
        $data = [
            'apiKey' => $apiKey,
            'merchantId' => $merchantId,
            'amount' => $amount,

        // API call using cURL
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);

        // Handle the API response
        $responseData = json_decode($response, true);
        if ($responseData['status'] == 'success') {
            // Update the payment status in your database
            // ...

            // Redirect to success page
        } else {
            // Redirect to failure page

    public function success() {

    public function failure() {


Create a view file for payment form, for example payment_form.php. This view file should contain the payment form, with an input field for the amount and a submit button.

    <title>PhonePe Payment Form</title>
    <h2>PhonePe Payment Form</h2>
    <form action="<?=base_url('payment/process')?>" method="post">
      <label for="amount">Amount:</label>
      <input type="text" id="amount" name="amount" value="<?=$amount?>">
      <input type="submit" value="Pay with PhonePe">

Create view files for payment success and failure pages, for example payment_success.php and payment_failure.php.

    <title>Payment Success</title>
    <h2>Payment Successful</h2>
    <p>Your payment has been processed successfully.</p>

    <title>Payment Failure</title>
    <h2>Payment Failed</h2>
    <p>Your payment has failed. Please try again later.</p>

Update the routes.php file in application/config directory to map the URLs to the corresponding controller methods.

$route['payment'] = 'payment';
$route['payment/process'] = 'payment/process';
$route['payment/success'] = 'payment/success';
$route['payment/failure'] = 'payment/failure';
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