how to integration stripe payment gateway in codeigniter – onlinecode

how to integration stripe payment gateway in codeigniter – onlinecode

In this post we will give you information about how to integration stripe payment gateway in codeigniter – onlinecode. Hear we will give you detail about how to integration stripe payment gateway in codeigniter – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we will tell you how to integrate Stripe payment gateway in Codeigniter Framework(Codeigniter stripe payment gateway integrate example).

The Stripe payment gateway is a popular payment gateway method and it is easily used for the project. so many developers prefer that payment gateway method.

Stripe is a secure payment method and it’s work-based using API. it provides two environments for the user like a sandbox(Test) and live.

Overview

Step 1: Install or Download stripe-PHP LibraryStep 2: Get and Set Stripe API Key and SECRETStep 3: Create ControllerStep 4: Create View File

Step 1: Install or Download stripe-PHP LibraryNow, we are going to install the stripe package using the below command and if you want to download the stripe-PHP library then you can download it on https://github.com/stripe/stripe-php. git repository.

composer require stripe/stripe-php

Step 2: Get and Set Stripe API Key and SECRETIn this step, we have to need the API Key and SECRET Key. so we will go to the official Stripe site and after then login gets API Key and SECRET Key after then we will set in a config file.application/config/config.php

$config['stripe_key'] = 'pk_test_xxxxxxxxxxxxxxxxxxx';
$config['stripe_secret'] = 'sk_test_xxxxxxxxxxxxxx';

Step 3: Create Controller

In this step, we will create a Stripe.php file in the “application/controller” directory and paste the below code in this controller.application/controller/Stripe.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
    
class Stripe 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('stripe_form');
    }
        
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function payment()
    {
      require_once('application/libraries/stripe-php/init.php');
     
      $stripeSecret = 'YOUR STRIPE SECRETE KEY';
 
      StripeStripe::setApiKey($stripeSecret);
      
        $stripe = StripeCharge::create ([
                "amount" => $this->input->post('amount'),
                "currency" => "usd",
                "source" => $this->input->post('tokenId'),
                "description" => "product 1"
        ]);
                   
        $data = array('success' => true, 'data'=> $stripe);
 
        echo json_encode($data);
    }
}
?>

Step 4: Create View File

So finally, we will create the stripe_form.php file in the “application/views/” directory.application/views/stripe_form.php

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>How To Integration Stripe Payment Gateway In Codeigniter - onlinecode</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <style>
   .container{
    padding: 0.5%;
   } 
</style>
</head>
<body>
  <div >
  
   <div >
      <div ><pre id="token_response"></pre></div>
    </div>
    <div >
      <div >
        <button  onclick="pay(100)">Pay $100</button>
      </div>
      <div >
        <button  onclick="pay(500)">Pay $500</button>
      </div>
      <div >
        <button  onclick="pay(1000)">Pay $10000</button>
      </div>
    </div>
</div>
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://checkout.stripe.com/checkout.js"></script>
  
<script type="text/javascript">
  
  function pay(amount) {
    var handler = StripeCheckout.configure({
      key: 'YOUR STRIPE KEY',
      locale: 'auto',
      token: function (token) {
        // You can access the token ID with 'token.id'.
        // Get the token ID to your server-side code for use.
        console.log('Token Created!!');
        console.log(token)
        $('#token_response').html(JSON.stringify(token));
  
        $.ajax({
          url:"<?php echo base_url(); ?>stripe/payment",
          method: 'post',
          data: { tokenId: token.id, amount: amount },
          dataType: "json",
          success: function( response ) {
            console.log(response.data);
            $('#token_response').append( '<br />' + JSON.stringify(response.data));
          }
        })
      }
    });
   
    handler.open({
      name: 'Demo Site',
      description: '2 widgets',
      amount: amount * 100
    });
  }
</script>
</body>
</html>

Now you can see the following card details:

Number: 4242 4242 4242 4242CSV: 123Expiration Month: Use the future monthExpiration Year: Use the future month

Please follow and like us:

Hope this code and post will helped you for implement how to integration stripe payment gateway in codeigniter – onlinecode. 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

For More Info See :: laravel And github

Leave a Comment

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

6  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US