Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Codeigniter 4 Stripe Payment Gateway Integrate Example

Codeigniter 4 Stripe Payment Gateway Integrate Example

Posted on September 19, 2021February 17, 2022 By XpertPhp

In this article, we will explain to you how to integrate the stripe payment gateway in the Codeigniter application(Codeigniter 4 Stripe Payment Gateway Integration Example). we can easily implement Using the stripe package in CodeIgniter 4.

In this example, we will install the stripe PHP package library in Codeigniter. so you can see our following example.

Overview
Step 1: Download Codeigniter
Step 2: Basic Configurations
Step 3: Connect to Database
Step 4: Install Stripe Package
Step 5: Create Controller
Step 6: Create Views Files
Step 7: Run The Application

Step 1: Download Codeigniter
If you want to download or install the latest version of CodeIgniter 4 then you can go to Codeigniter’s official site and download the latest version of Codeigniter 4. after the downloaded you can configure in “xampp/htdocs/” directory.

Step 2: Basic Configurations
If you want to Basic Configurations in your project then you can below Url.
Codeigniter 4 Removing Index.Php From Url

Step 3: Connect to Database
Go to the “app/Config/Database.php” folder and open the database.php file some changes in this file like hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public $default = [
'DSN'      => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter4_pagination',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug'  => (ENVIRONMENT !== 'production'),
'cacheOn'  => false,
'cacheDir' => '',
'charset'  => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre'  => '',
'encrypt'  => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port'     => 3306,
];

Step 4: Install Strip Package
In this step, We will install the dompdf package for the generate pdf in codeigniter 4 application. so you can follow our composer command.

1
composer require stripe/stripe-php

After that, open the app/config/autoload.php file and set vendor directory path, so you can follow configuration.

1
$config['composer_autoload'] = 'vendor/autoload.php';

Step 5: Create Controller
In this step, we will create the “Stripe.php” controller and create the index() and payment() methods. so you can see following example.
app/Controllers/Stripe.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
 
class StripeController extends Controller
{
 
 
    public function index()
    {
        return view('home');
    }
      
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function payment()
    {
      require_once('application/libraries/stripe-php/init.php');
    
      $stripeSecret = 'YOUR STRIPE SECRETE KEY';
 
      \Stripe\Stripe::setApiKey($stripeSecret);
    
        $stripe = \Stripe\Charge::create ([
                "amount" => $this->request->getVar('amount'),
                "currency" => "usd",
                "source" => $this->request->getVar('tokenId'),
                "description" => "Test payment from tutsmake.com."
        ]);
            
       // after successful payment, you can store payment-related information into your database
            
        $data = array('success' => true, 'data'=> $stripe);
 
        echo json_encode($data);
    }
 
}

Step 6: Create Views Files
Finally, we will create the payment.php in the app/views directory.
app/views/payment.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Codeigniter 4 Stripe Payment Gateway Integration Example - XpertPhp</title>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
 
<body>
  <div class="container">
 
    <pre id="getTokenRes"></pre>
 
    <div class="d-grid mb-3">
      <button class="btn btn-dark" onclick="handlePayment(50)">Pay $50</button>
    </div>
    <div class="d-grid mb-3">
      <button class="btn btn-danger" onclick="handlePayment(100)">Pay $100</button>
    </div>
    <div class="d-grid">
      <button class="btn btn-warning" onclick="handlePayment(200)">Pay $200</button>
    </div>
    
  </div>
 
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="https://checkout.stripe.com/checkout.js"></script>
  <script type="text/javascript">
    function handlePayment(amount) {
      var handler = StripeCheckout.configure({
        key: 'pk_test_51H7bbSxxxxxxxxxxxxx',
        locale: 'auto',
        token: function (token) {
          console.log('Token Generated' + token);
          $('#getTokenRes').html(JSON.stringify(token));
          $.ajax({
            url: "<?php echo site_url('stripe/payment');?>",
            method: 'post',
            data: {
              tokenId: token.id,
              amount: amount
            },
            dataType: "json",
            success: function (response) {
              console.log(response.data);
              $('#getTokenRes').append('<br />' + JSON.stringify(response
                .data));
            }
          })
        }
      });
      handler.open({
        name: 'book1',
        description: '2 Widgets',
        amount: amount * 50
      });
    }
  </script>
</body>
 
</html>

Step 7: Run The Application
We can start the server and run the Codeigniter 4 application using the below command.

1
php spark serve

Now we will run our example using the below Url in the browser.

1
http://localhost:8080/stripe

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • how to Integrate Razorpay payment gateway in Codeigniter
  • Login With Facebook Using CodeIgniter
  • How to do form validation in codeigniter
  • How to create Pagination with Codeigniter and MySql
  • Codeigniter 4 Removing Index.Php From Url
Codeigniter

Post navigation

Previous Post: Codeigniter 4 cURL Delete Request Example Tutorial
Next Post: Node JS Axios HTTP GET Request Example

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com