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() {
        parent::__construct();
        $this->load->library('session');
    }

    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 = 'https://www.phonepe.com/api/v1/payment/initiate';

        // 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);
        curl_close($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
            redirect('payment/success');
        } else {
            // Redirect to failure page
            redirect('payment/failure');
        }
    }

    public function success() {
        $this->load->view('payment_success');
    }

    public function failure() {
        $this->load->view('payment_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.

<html>
  <head>
    <title>PhonePe Payment Form</title>
  </head>
  <body>
    <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?>">
      <br><br>
      <input type="submit" value="Pay with PhonePe">
    </form>
  </body>
</html>

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

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


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

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 deepaktailor10@yahoo.in