Codeigniter 4 Integrate Stripe Payment Gateway Example

Codeigniter 4 Integrate Stripe Payment Gateway Example

Stripe provides a PHP library that allows users to collect payments via online, card, UPI, etc. by installing it through Composer in CodeIgniter.

In this tutorial, you will learn how to integrate stripe payment gateway in Codeigniter 4 projects.

Here are steps to integrate Stripe Payment in Codeigniter 4:

Step 1: Setup Codeigniter 4 Project

Visit https://codeigniter.com/download and download Codeigniter 4 fresh new setup and unzip the setup in your local system xampp/htdocs/.

Step 2: Basic Configurations

Next, you will set some basic configuration on the app/config/app.php file, so let’s go to application/config/config.php and open this file on text editor.

Set Base URL like this

public $baseURL = 'http://localhost:8080';
To
public $baseURL = 'http://localhost/demo/';

Step 3: Configure Database

Edit app/Config/Database.php file and setup database credentials in it:

	public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'demo',
'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 stripe package Via Composer

To install stripe package using the following composer command:

composer require stripe/stripe-php

To use the bindings, use the Composer’s autoload

require_once('vendor/autoload.php');

Step 5: Create Controller

Create a controller name Stripe.php and methods into it that handle payment process:

<?php namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
class Stripe 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 = 'sk_test_j5k0976GOLSOtiRzbDLpKqat00og5iM3cY';
\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 successfull payment, you can store payment related information into your database

$data = array('success' => true, 'data'=> $stripe);
echo json_encode($data);
}
}

Step 6: Create Stripe Form View

Go to application/views/ and create a new file named home.php with a form that allows users to make payment through stripe card:

<!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>Codeigniter 4 Stripe Payment Gateway Integration - Tutsmake.com</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 class="container">

<div class="row">
<div class="col-md-12"><pre id="token_response"></pre></div>
</div>
<div class="row">
<div class="col-md-4">
<button class="btn btn-primary btn-block" onclick="pay(100)">Pay $100</button>
</div>
<div class="col-md-4">
<button class="btn btn-success btn-block" onclick="pay(500)">Pay $500</button>
</div>
<div class="col-md-4">
<button class="btn btn-info btn-block" 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: 'pk_test_5f6jfFP2ZV5U9TXQYG0vtqFJ00eFVWNoRX', // your publisher key id
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>

Step 7: Create Route

Edit app/Config/Routes.php file and add the following route in it:

$routes->get('/', 'Stripe::index');

Step 8: Test Application

Run the following command to start development server:

php spark serve

Then, Go to the browser and hit below the URL:

http://localhost:8080

Conclusion

Stripe payment integration in codeigniter 4. In this tutorial, you have learned how to implement stripe payment gateway integration in codeigniter 4 app.

If you have any questions or thoughts to share, use the comment form below to reach us.

Recommended CodeIgniter 4 Tutorial

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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