Icegate E Payment Gateway-Integrating Stripe Payment Gateway with CodeIgniter for Indian Market: A Comprehensive Guide
Integrating Stripe with CodeIgniter for the Indian market involves setting up Stripe’s API in your CodeIgniter application to handle payments. Stripe is a popular choice for online payment processing due to its simplicity and the variety of features it offers. Below is a comprehensive guide to help you integrate Stripe with CodeIgniter for use in the Indian market.
### Prerequisites:
1. **CodeIgniter installed**: You should have CodeIgniter set up and running.
2. **Stripe Account**: Create an account on Stripe and set up your API keys.
3. **PHP cURL**: Ensure PHP cURL is enabled on your server as it’s required for making HTTP requests to Stripe’s API.
### Step 1: Install Stripe PHP Library
You can install the Stripe PHP library using Composer. If you haven’t already set up Composer, you’ll need to do that first.
In your project root, run:
“`bash
composer require stripe/stripe-php
“`
### Step 2: Configure Stripe API Keys
After installing the Stripe PHP library, you need to configure your Stripe API keys. These keys can be found in your Stripe Dashboard.
Create a new file `stripe.php` in the `application/config` directory of your CodeIgniter application with the following content:
“`php
load->config(‘stripe’);
“`
### Step 4: Create a Payment Method
To charge a customer, you first need to create a payment method. This usually involves collecting the user’s credit card details securely. Stripe provides a JavaScript library called Stripe.js to handle this.
Include Stripe.js in your view file:
“`html
“`
Then, create a form to collect card details:
“`html
Credit or debit card
“`
Initialize Stripe.js with your publishable key and mount the card element:
“`javascript
var stripe = Stripe(‘config->item(‘stripe_key_publishable’); ?>’);
var elements = stripe.elements();
var card = elements.create(‘card’);
card.mount(‘#card-element’);
// Add event listener to the form
var form = document.getElementById(‘payment-form’);
form.addEventListener(‘submit’, function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById(‘card-errors’);
errorElement.textContent = result.error.message;
} else {
// Send the token to your server
stripeTokenHandler(result.token);
}
});
});
function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to your server
var hiddenInput = document.createElement(‘input’);
hiddenInput.setAttribute(‘type’, ‘hidden’);
hiddenInput.setAttribute(‘name’, ‘stripeToken’);
hiddenInput.setAttribute(‘value’, token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
“`
### Step 5: Handle the Payment on the Server
In your controller, create a function to handle the payment:
“`php
public function charge() {
$this->load->config(‘stripe’);
require_once APPPATH . ‘third_party/stripe/stripe-php/init.php’;
// Retrieve the request’s body and parse it as JSON
$body = @file_get_contents(‘php://input’);
$json = json_decode($body, true);
// Set API key
\Stripe\Stripe::setApiKey($this->config->item(‘stripe_key_secret’));
// Get the credit card token submitted by Stripe.js
$token = $json[‘stripeToken’];
// Charge the user’s card
try {
$charge = \Stripe\Charge::create(array(
‘amount’ => 1000, // amount in paise, for INR currency
‘currency’ => ‘inr’,
‘description’ => ‘Example charge’,
‘source’ => $token,
));
echo json_encode(array(“status” => “success”, “charge” => $charge));
} catch(\Stripe\Error\Card $e) {
// The card has been declined
echo json_encode(array(“status” => “declined”, “error” => $e->getMessage()));
}
}
“`
Make sure to replace `1000` with the actual amount in paise (1 INR = 100 paise) and `’inr’` with the currency code you are using.
### Step 6: Test Your Integration
Before going live, thoroughly test your payment flow in the test environment provided by Stripe. Ensure that you handle all possible scenarios, including declined cards and network errors.
### Step 7: Go Live
Once you’ve tested your integration and are ready to go live, replace your test API keys with your live API keys and make sure your application is secure, especially since you’re handling sensitive payment information.
Remember to comply with the Reserve Bank of India’s guidelines and other regulations that apply to online payment transactions in India.
### Security and Compliance
– Always use HTTPS to protect card data.
– Follow PCI DSS guidelines to ensure that your handling of card data is secure.
– Do not store card details on your server; let Stripe handle that securely.
This guide provides a basic outline for integrating Stripe with CodeIgniter for the Indian market. Depending on your specific requirements, you may need to add additional functionality or handle more complex scenarios. Always refer to the Stripe documentation for the most up-to-date and detailed information.