In this tutorial, We have share how to integrate razorpay payment gateway in codeigniter using cURL. Razorpay is the most polular payment Gateway in India. Razorpay provides clean, fast, secure payments services with hassle free integration with developer friendly APIs. It allows online business to accept and process payments payments modes like Cards, Netbanking, Wallets & UPI. Developer Friendly API, Fast Onboarding and No Setup. Razorpay payment gateway is the easiest option for the web developer to implement payment system on the Web Application.
Step 1: Create Razorpay Account
First we need to create account on Razorpay and generate KeyId and Secret Key. We will keep created Razorpay account in test mode to test payment functionality.
Step 2: Update Razorpay Config Details
Here in this example, we will use Test App to integrate Razorpay gateway. So we will update config.php with KeyID and Secret Key from Razorpay.
Step 3: Open a file constants
Open "application/config/constants.php" file and define constants:
Step 4: Create a Controller file Razorpay
Create a model file named "Razorpay.php" inside "application/controllers" folder.
$this->load->view('templates/footer');
?>
Step 7: Open a file routes
Open "application/config/routes.php" file and add code like as bellow:
Domestic
International
Step 1: Create Razorpay Account
First we need to create account on Razorpay and generate KeyId and Secret Key. We will keep created Razorpay account in test mode to test payment functionality.
Step 2: Update Razorpay Config Details
Here in this example, we will use Test App to integrate Razorpay gateway. So we will update config.php with KeyID and Secret Key from Razorpay.
Step 3: Open a file constants
Open "application/config/constants.php" file and define constants:
define('RAZOR_KEY_ID', 'XXXXXXXXXX');
define('RAZOR_KEY_SECRET', 'XXXXXXXXXX');
?>
Step 4: Create a Controller file Razorpay
Create a model file named "Razorpay.php" inside "application/controllers" folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* @package Razorpay : CodeIgniter Razorpay Gateway
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Razorpay Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Razorpay extends CI_Controller {
// construct
public function __construct() {
parent::__construct();
$this->load->model('Site', 'site');
}
// index page
public function index() {
$data['title'] = 'Razorpay | TechArise';
$data['productInfo'] = $this->site->getProduct();
$this->load->view('razorpay/index', $data);
}
// checkout page
public function checkout($id) {
$data['title'] = 'Checkout payment | TechArise';
$this->site->setProductID($id);
$data['itemInfo'] = $this->site->getProductDetails();
$data['return_url'] = site_url().'razorpay/callback';
$data['surl'] = site_url().'razorpay/success';;
$data['furl'] = site_url().'razorpay/failed';;
$data['currency_code'] = 'INR';
$this->load->view('razorpay/checkout', $data);
}
// initialized cURL Request
private function get_curl_handle($payment_id, $amount) {
$url = 'https://api.razorpay.com/v1/payments/'.$payment_id.'/capture';
$key_id = RAZOR_KEY_ID;
$key_secret = RAZOR_KEY_SECRET;
$fields_string = "amount=$amount";
//cURL Request
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $key_id.':'.$key_secret);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/ca-bundle.crt');
return $ch;
}
// callback method
public function callback() {
if (!empty($this->input->post('razorpay_payment_id')) && !empty($this->input->post('merchant_order_id'))) {
$razorpay_payment_id = $this->input->post('razorpay_payment_id');
$merchant_order_id = $this->input->post('merchant_order_id');
$currency_code = 'INR';
$amount = $this->input->post('merchant_total');
$success = false;
$error = '';
try {
$ch = $this->get_curl_handle($razorpay_payment_id, $amount);
//execute post
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result === false) {
$success = false;
$error = 'Curl error: '.curl_error($ch);
} else {
$response_array = json_decode($result, true);
// echo "";print_r($response_array);exit;
//Check success response
if ($http_status === 200 and isset($response_array['error']) === false) {
$success = true;
} else {
$success = false;
if (!empty($response_array['error']['code'])) {
$error = $response_array['error']['code'].':'.$response_array['error']['description'];
} else {
$error = 'RAZORPAY_ERROR:Invalid Response
'.$result;
}
}
}
//close connection
curl_close($ch);
} catch (Exception $e) {
$success = false;
$error = 'OPENCART_ERROR:Request to Razorpay Failed';
}
if ($success === true) {
if(!empty($this->session->userdata('ci_subscription_keys'))) {
$this->session->unset_userdata('ci_subscription_keys');
}
if (!$order_info['order_status_id']) {
redirect($this->input->post('merchant_surl_id'));
} else {
redirect($this->input->post('merchant_surl_id'));
}
} else {
redirect($this->input->post('merchant_furl_id'));
}
} else {
echo 'An error occured. Contact site administrator, please!';
}
}
public function success() {
$data['title'] = 'Razorpay Success | TechArise';
$this->load->view('razorpay/success', $data);
}
public function failed() {
$data['title'] = 'Razorpay Failed | TechArise';
$this->load->view('razorpay/failed', $data);
}
}
?>
Step 5: Create a view file index
Create a view file named "index.php" inside "application/views/razorpay" folder
$this->load->view('templates/header');
?>
Razorpay Payment Gateway Integration In Codeigniter using cURL
$this->load->view('templates/footer');
?>
Step 6: Create a view file checkout
Create a view file named "checkout.php" inside "application/views/razorpay" folder
$this->load->view('templates/header');
?>
Razorpay Payment Gateway Integration In Codeigniter using cURL
$productinfo = $itemInfo['description'];
$txnid = time();
$surl = $surl;
$furl = $furl;
$key_id = RAZOR_KEY_ID;
$currency_code = $currency_code;
$total = ($itemInfo['price']* 100);
$amount = $itemInfo['price'];
$merchant_order_id = $itemInfo['product_id'];
$card_holder_name = 'TechArise Team';
$email = 'info@techarise.com';
$phone = '9000000001';
$name = APPLICATION_NAME;
$return_url = site_url().'razorpay/callback';
?>
session->flashdata('msg'))){ ?>
session->flashdata('msg'); ?>
Image | Name | Price | Qty | Sub Total |
---|---|---|---|---|
1 |
$this->load->view('templates/footer');
?>
Step 7: Open a file routes
Open "application/config/routes.php" file and add code like as bellow:
// routes
$route['default_controller'] = 'razorpay/index';
$route['checkout/(:any)'] = "razorpay/checkout/$1";
?>
Domestic
Mastercard | Visa |
---|---|
5104_0155_5555_5558, 5104_0600_0000_0008 | 4111_1111_1111_1111 |
International
Mastercard | Visa |
---|---|
5555_5555_5555_4444, 5105_1051_0510_5100 | 4012_8888_8888_1881, 4000_1841_8621_8826 |