Best way to use stripe payment gateway in codeigniter 3

sripe-payment-gateway-integration-in-codeigniter

If You Want to integrate Payment Gateway in your php Codeigniter3 Applicarion,then I’ll recomonded to you Stripe Payment Gateway in Your Codeigniter3 Application.

In This artical I’ll Explain this Step by step.
You need to just follow this tutorial for stripe payment gateway integration in codeigniter.

stripe is the most popular payment gateway for card payment and subscription or recurring payments.

Before We use This payment integration using php stripe-php library,We need to create developer account in stripe.so we can get published_key and secret_key for integration.

Let’s Follow Few Steps.

1.Download Library for Stripe

download stripe-php  library and placed in Library folder of codeigniter and renamed it as stripe-php

Path:application/library

2.Set Config File for stripe keys

  •  for getting published key and secret for stripe you should have account in  stripe developer mode.
  • set published_key and secret_key for stripe in config file
  • Path: application config/config.php
$config['stripe_key'] = 'pk_test_ghjfndghfht';
$config['stripe_secret'] = 'sk_test_fgdhesateju';

3.Set Routing

Path: application/config/route.php

$route['my-stripe'] = "StripeController";
$route['stripePost']['post'] = "StripeController/stripePost";

4.Controller For Stripe Payment Gateway

In This Step,We Are Creating Controller for stripe Payment Gateway in codeigniter framework of php.

Path:app;ication/controller/StripeController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class StripeController extends CI_Controller {
    public function __construct() {
       parent::__construct();
       $this->load->library("session");
       $this->load->helper('url');
    }
    public function index()
    {
        $this->load->view('my_stripe');
    }
    public function stripePost()
    {
        require_once('application/libraries/stripe-php/init.php');
        StripeStripe::setApiKey($this->config->item('stripe_secret'));
        StripeCharge::create ([
                "amount" => 100 * 100,
                "currency" => "usd",
                "source" => $this->input->post('stripeToken'),
                "description" => "Test payment from csssofttech.com." 
        ]);
        $this->session->set_flashdata('success', 'Payment made successfully.');
        redirect('/my-stripe', 'refresh');
    }
}
 

5.View

Path : application/view/my_stripe.php

<!DOCTYPE html>
<html>
<head>
    <title>Codeigniter Stripe Payment Integration Example - csssofttech.com</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <style type="text/css">
        .panel-title {
        display: inline;
        font-weight: bold;
        }
        .display-table {
            display: table;
        }
        .display-tr {
            display: table-row;
        }
        .display-td {
            display: table-cell;
            vertical-align: middle;
            width: 61%;
        }
    </style>
</head>
<body>
<div>
    <h1>Codeigniter Stripe Payment Integration Example <br/> csssofttech.com</h1>
    <div>
        <div>
            <div>
                <div >
                    <div >
                        <h3 >Payment Details</h3>
                        <div >                            
                            <img src="http://i76.imgup.net/accepted_c22e0.png">
                        </div>
                    </div>                    
                </div>
                <div>
                    <?php if($this->session->flashdata('success')){ ?>
                    <div>
                            <a href="#" data-dismiss="alert" aria-label="close">×</a>
                            <p><?php echo $this->session->flashdata('success'); ?></p>
                        </div>
                    <?php } ?>
                    <form role="form" action="/stripePost" method="post"
                                                     data-cc-on-file="false"
                                                    data-stripe-publishable-key="<?php echo $this->config->item('stripe_key') ?>"
                                                    id="payment-form">
                        <div class='form-row row'>
                            <div class='col-xs-12 form-group required'>
                                <label class='control-label'>Name on Card</label> <input
                                    class='form-control' size='4' type='text'>
                            </div>
                        </div>
                        <div class='form-row row'>
                            <div class='col-xs-12 form-group card required'>
                                <label class='control-label'>Card Number</label> <input
                                    autocomplete='off' class='form-control card-number' size='20'
                                    type='text'>
                            </div>
                        </div>
                        <div class='form-row row'>
                            <div class='col-xs-12 col-md-4 form-group cvc required'>
                                <label class='control-label'>CVC</label> <input autocomplete='off'
                                    class='form-control card-cvc' placeholder='ex. 311' size='4'
                                    type='text'>
                            </div>
                            <div class='col-xs-12 col-md-4 form-group expiration required'>
                                <label class='control-label'>Expiration Month</label> <input
                                    class='form-control card-expiry-month' placeholder='MM' size='2'
                                    type='text'>
                            </div>
                            <div class='col-xs-12 col-md-4 form-group expiration required'>
                                <label class='control-label'>Expiration Year</label> <input
                                    class='form-control card-expiry-year' placeholder='YYYY' size='4'
                                    type='text'>
                            </div>
                        </div>
                        <div class='form-row row'>
                            <div class='col-md-12 error form-group hide'>
                                <div class='alert-danger alert'>Please correct the errors and try
                                    again.</div>
                            </div>
                        </div>
                        <div>
                            <div>
                                <button type="submit">Pay Now ($100)</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>        
        </div>
    </div>
</div>
</body>  
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
$(function() {
    var $form         = $(".require-validation");
  $('form.require-validation').bind('submit', function(e) {
    var $form         = $(".require-validation"),
        inputSelector = ['input[type=email]', 'input[type=password]',
                         'input[type=text]', 'input[type=file]',
                         'textarea'].join(', '),
        $inputs       = $form.find('.required').find(inputSelector),
        $errorMessage = $form.find('div.error'),
        valid         = true;
        $errorMessage.addClass('hide');
        $('.has-error').removeClass('has-error');
    $inputs.each(function(i, el) {
      var $input = $(el);
      if ($input.val() === '') {
        $input.parent().addClass('has-error');
        $errorMessage.removeClass('hide');
        e.preventDefault();
      }
    });
    if (!$form.data('cc-on-file')) {
      e.preventDefault();
      Stripe.setPublishableKey($form.data('stripe-publishable-key'));
      Stripe.createToken({
        number: $('.card-number').val(),
        cvc: $('.card-cvc').val(),
        exp_month: $('.card-expiry-month').val(),
        exp_year: $('.card-expiry-year').val()
      }, stripeResponseHandler);
    }
  });
  function stripeResponseHandler(status, response) {
        if (response.error) {
            $('.error')
                .removeClass('hide')
                .find('.alert')
                .text(response.error.message);
        } else {
            var token = response['id'];
            $form.find('input[type=text]').empty();
            $form.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
            $form.get(0).submit();
        }
    }
});
</script>
</html>

6.Card details for stripe payment gateway integration incodeigniter

Card Name: Test

Card Number: 4242 4242 4242 4242

Card CSV: 123

Card Expiration Month: 12

Card Expiration Year: 2024

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top