Stripe Payment Gateway Integration in CodeIgniter 4

Reading Time: 9 minutes
8,851 Views

As we all know in most web application either in CodeIgniter 4 or any of the PHP Framework, we sometimes need to integrate payment gateway.

Inside this article, we will see Stripe payment Integration in codeIgniter 4. Stripe payment system settings in CodeIgniter 4. Easy and in complete detailed concept. This article contains a classified information.

Learn More –

Let’s get started – Stripe payment gateway in CodeIgniter 4


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Stripe PHP Package Installation

We will install stripe package via composer into CodeIgniter 4 setup. Open CodeIgniter 4 project in terminal and hit this composer command.

$ composer require stripe/stripe-php

Autoload Stripe Library

Open Autoload.php from /app/Config. Find $psr4 and add stripe library.

Autoload.php

Set Stripe API Key and SECRET

Open up Constants.php from /app/Config folder of CodeIgniter 4 application.

Into the file, set stripe api key and secret.

Constants.php

This is our stripe sandbox detail, you can replace it with your own. If you are wondering to find stripe test account keys. Follow these steps –

  • You need to enable Viewing test data to use Test account details
  • Go to Developer >> Click on API keys

Create Routes

Open Routes.php from /app/Config folder. Add these routes into it.

Routes.php

Create Controller File

Next, we need to create a controller file.

$ php spark make:controller Stripe --suffix

It will create a controller file StripeController.php inside /app/Controllers folder

Open StripeController.php and write this given code.

StripeController.php

Create Template File

Let’s create template file for setting the layout of payment details for user.

Create a file stripe.php inside /app/Views folder. Open file and paste the given code.

stripe.php

This code will will create a UI for payment inputs and added javascript code will help to process the stripe payment.


Application Testing

Now, start development server.

$ php spark serve

Here you can test with these given test card details.

URL: http://localhost:8080/stripe

Name: Sample
Number: 4242 4242 4242 4242
CSV: 123
Expiration Month: 04
Expiration Year: 2024

We hope this article helped you to learn about Stripe Payment Gateway Integration in CodeIgniter 4 in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

2 thoughts on “Stripe Payment Gateway Integration in CodeIgniter 4”

  1. Brilliant article, tough payment section is returning the following error

    Class ‘Stripe\Stripe’ not found

    PHP: 7.3.26 — CodeIgniter: 4.1.1

    any idea

Comments are closed.