integrate Instamojo payment gateway with CodeIgniter 3
To integrate Instamojo payment gateway with CodeIgniter 3, you can follow these steps:
Step 1: Install CodeIgniter 3
If you haven't already installed CodeIgniter 3, you can download it from the official website and set up your project.
Step 2: Get Instamojo API Credentials
Sign up for an Instamojo account and get your API credentials, including the API key and auth token. You'll need these credentials to communicate with Instamojo's API.
Step 3: Create a Controller
Create a controller in CodeIgniter to handle payment requests and responses. For example, you can create a controller named "Payment" using the following command in your terminal:
```bash
php index.php tools/make_controller Payment
```
Step 4: Configure Payment Gateway Settings
In your CodeIgniter project, open the `config.php` file located in `application/config` and set the `base_url` to your project's URL.
Step 5: Create Payment Form
Create a view file where users can fill in payment details and submit the payment request. For example, create a view file named `payment_form.php` in your `application/views` folder.
```php
<!-- application/views/payment_form.php -->
<form method="post" action="<?= base_url('payment/process_payment') ?>">
<input type="text" name="amount" placeholder="Amount">
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<button type="submit">Pay Now</button>
</form>
```
Step 6: Process Payment Request
In the `Payment` controller, process the payment request and redirect the user to Instamojo's payment page. Install the Instamojo PHP SDK using composer or manually download and include it in your project.
```bash
composer require instamojo/instamojo-php
```
Now, in your `Payment` controller:
```php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/Instamojo.php';
class Payment extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
public function index() {
$this->load->view('payment_form');
}
public function process_payment() {
$api_key = 'YOUR_INSTAMOJO_API_KEY';
$auth_token = 'YOUR_INSTAMOJO_AUTH_TOKEN';
$api = new Instamojo\Instamojo($api_key, $auth_token, 'https://www.instamojo.com/api/1.1/');
$data = array(
'purpose' => 'Payment for Order', // Purpose of the payment
'amount' => $this->input->post('amount'), // Amount to be paid
'buyer_name' => $this->input->post('name'), // Buyer's name
'email' => $this->input->post('email'), // Buyer's email
'send_email' => true,
'redirect_url' => base_url('payment/payment_success'),
'webhook' => base_url('payment/webhook'), // Webhook URL for handling payment updates
);
try {
$response = $api->paymentRequestCreate($data);
$payment_url = $response['longurl'];
redirect($payment_url);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
public function payment_success() {
// Handle successful payment here
echo 'Payment Successful';
}
public function webhook() {
// Handle webhook data here
}
}
```
Remember to replace `'YOUR_INSTAMOJO_API_KEY'` and `'YOUR_INSTAMOJO_AUTH_TOKEN'` with your actual API credentials obtained from your Instamojo account.
Step 7: Handling Payment Response
Create methods in the `Payment` controller to handle successful and failed payment responses. The `process_payment` method redirects the user to Instamojo's payment page, and after successful payment, the user will be redirected back to the `payment_success` method. In this method, you can handle the successful payment completion.
In the Instamojo dashboard, you can also set a webhook URL that Instamojo will use to send payment updates. Implement the `webhook` method to handle these updates.
Conclusion
This CodeIgniter 3 code demonstrates how to integrate the Instamojo payment gateway into your web application. Users can enter payment details in the form, and upon submitting, they will be redirected to the Instamojo payment page for secure payment processing. After successful payment, the user will be redirected back to the website, and you can handle the payment completion in the `payment_success` method. Additionally, you can implement the `webhook` method to handle payment updates from Instamojo, ensuring a seamless payment experience for your users.