stripe payment gateway integration in codeigniter

In this post we will give you information about Stripe Payment Gateway Integration in Codeigniter Example. Hear we will give you detail about Stripe Payment Gateway Integration in Codeigniter ExampleAnd how to use it also give you demo for it if it is necessary.

If you want to add credit card payment gateway in your php codeigniter 3 app, then i will suggest you to use stripe payment gateway in your codeigniter website. In this this tutorial i will explain how to integrate stripe payment gateway in codeigniter 3.

Stripe Payment Gateway Integration in Codeigniter Example

I write step by step tutorial of stripe payment gateway integration in php codeigniter. you need to just follow few steps and you have stripe payments api integrated.

Stripe is a fastest online payment processing for internet businesses. stripe provide credit card payment, subscription payment. the most advantage is a they prevent fraud payment.

we will use php code library for stripe payment gateway. before you integrate stripe payment gateway you have stripe account or stripe developer account so, you can easily get stripe api key and secret. then you can use it with this example.

So, let’s follow bellow few steps and you can make payment with codeigniter project.

we will learn about Stripe Payment Gateway Integration In PHP With DEMO Examples with an example.

Read Also:  Laravel 6 Clear cache Example

Step 1: Download stripe-php Library

In this step we need to download stripe-php library from here: https://github.com/stripe/stripe-php.

After download, you have to extract that folder into “application/libraries” folder and make sure rename folder name “stripe-php”.

Step 2: Set Stripe API Key and SECRET

Now, we need to set stripe key and secret. so first you can go on Stripe website and create development stripe account key and secret and add bellow:

application/config/config.php

$config['stripe_key'] = 'pk_test_reFxwbsm*********CKASd';

$config['stripe_secret'] = 'sk_test_o*************4w';

Step 3: Create Routes

In this step, we will create two routes for get request and another for post request. So, let’s add new route on that file.

application/config/routes.php

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

Step 4: Create Controller File

in next step, now we have create new controller as StripeController and write both method on it like as bellow, So let’s create both controller:

application/controller/StripeController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class StripeController extends CI_Controller {
/**
* Get All Data from this method.
*
* @return Response
*/
public function __construct() {
parent::__construct();
$this->load->library("session");
$this->load->helper('url');
}
/**
* Get All Data from this method.
*
* @return Response
*/
public function index()
{
$this->load->view('dashboard_stripe_view');
}
/**
* Get All Data from this method.
*
* @return Response
*/
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 pakainfo." 
]);
$this->session->set_flashdata('success', 'Payment made successfully.');
redirect('/my-stripe', 'refresh');
}
}

Step 5: Create View File

In Last step, let’s create dashboard_stripe_view.php(application/views/dashboard_stripe_view.blade.php) for layout and write code of jquery to get token from stripe here and put following code:

Read Also:  Laravel File Upload Validation Rules Example

application/views/dashboard_stripe_view.php

<!DOCTYPE html>
<html>
   <head>
      <title>Codeigniter Stripe Payment Integration Example - www.pakainfo.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>
         <h2>Codeigniter Stripe Payment Integration Example <br/> www.pakainfo.com</h2>
         <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>

Now you can check with following card details for testing Purpose:

Read Also:  PHP Image Upload and Crop in jQuery Example

Name: Test
Number: 4242 4242 4242 4242
CSV: 123
Expiration Month: 12
Expiration Year: 2024

I hope it can help you…

Hope this code and post will helped you for implement Stripe Payment Gateway Integration in Codeigniter Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 6 Guzzle PHP HTTP client Request
  2. Read Also:  how to disable browser back button in php?
  3. Read Also:  Laravel 6 Set Extra where condition Examples
  4. Read Also:  PHP Ajax Image Upload Resize Crop using jQuery
  5. Read Also:  sql having clause Examples
  6. Read Also:  Remove Html tags from string PHP - PHP strip_tags() Function
  7. Read Also:  Stripe API Webhook upgrade and downgrade Subscriptions
  8. Read Also:  Laravel File Upload Validation Rules Example
  9. Read Also:  Laravel 6 Guzzle PHP HTTP client Request
  10. Read Also:  how to disable browser back button in php?
CLOSEX