Codeigniter 4 Stripe Payment Gateway Integrate Example
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 |