how to integration stripe payment gateway in codeigniter
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 Library
Step 2: Get and Set Stripe API Key and SECRET
Step 3: Create Controller
Step 4: Create View File
Step 1: Install or Download stripe-PHP Library
Now, 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.
1 | composer require stripe/stripe-php |
Step 2: Get and Set Stripe API Key and SECRET
In 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
1 2 | $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
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 | <?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'; \Stripe\Stripe::setApiKey($stripeSecret); $stripe = \Stripe\Charge::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
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 65 66 67 68 69 70 71 72 73 74 | <!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 - XpertPhp</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: '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 4242
CSV: 123
Expiration Month: Use the future month
Expiration Year: Use the future month