Dear Reader, let us know that “Stripe Payment Gateway Integration in Codeigniter“. So in this Tutorial, We Learn Step By Step Stripe Payment Gateway Integration Process in PHP.
The Stripe Payment Gateway provides us with the very easy and needs the full way to collect the payments from web Application.
The Stripe is one of the best Payment gateways in the world to accept the payments using Credit Card and Debit Card. If you want to receive the payment by Credit card then The Stripe is for you.
In this tutorial, we are going to aware of how to integrate the stripe payment gateway in CodeIgniter.
Let’s start the Stripe payment gateway Integration step by step.
First, we see a Snapshot of the Product Pay now page.
And you can also see Live “Demo” by click on below Demo button
Step 1:- First get the registration from the https://stripe.com/
Please fill the required fields and click on the “Create your Stripe Account”
Step 2:- Now you will redirect on the dashboard and click on the “Developers” Menu on the Left side menu and Under the Developers Menu click the “API Keys”, Now you can see the “Publish Key” and “Secret Key” and mention in the below snapshot
Step 3:- First we need to create a database like “ci_demo” and paste the below SQL code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
CREATE TABLE `orders` ( `id` int(11) NOT NULL, `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `card_num` bigint(20) NOT NULL, `card_cvc` int(5) NOT NULL, `card_exp_month` varchar(2) COLLATE utf8_unicode_ci NOT NULL, `card_exp_year` varchar(5) COLLATE utf8_unicode_ci NOT NULL, `item_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `item_number` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `item_price` float(10,2) NOT NULL, `item_price_currency` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'usd', `paid_amount` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `paid_amount_currency` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `txn_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `payment_status` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `created_date` datetime NOT NULL, `created_by` int(11) NOT NULL, `modified_date` datetime NOT NULL, `modified_by` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
Step 4:- Now we need to Install the Codeigniter from
How to Install CodeIgniter Framework in Windows Operating System
if you already Installed then Please Ignore this
Step 5:- Now we need to set “API Key” and “Secret Key” in “config.php” by pasting the below code
1 2 3 4 |
/* for stripe payment gateway */ $config['stripe_key'] = 'pk_test_qWEbr1PrSelCDxAk1qTW6NXT'; $config['stripe_secret'] = 'sk_test_dBQ5Iwbo8OeWFsaaZcR1tKPJ'; /* for stripe payment gateway */ |
Step 6:- Now Create the Controller “Payments.php” and paste the below code
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 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Payments extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('stripe/Payment','Payment'); } public function index() { $this->load->helper('url'); $this->load->view('header'); $this->load->view('stripe/stripe'); $this->load->view('footer'); } public function payment() { require_once('application/libraries/stripe-php/init.php'); \Stripe\Stripe::setApiKey($this->config->item('stripe_secret')); $charge = \Stripe\Charge::create ([ "amount" => 55 * 100, "currency" => "usd", "source" => $this->input->post('stripeToken'), "description" => "Test payment from webpreparations" ]); $chargeJson = $charge->jsonSerialize(); /* for insert data in database start */ $item_name = "Premium Script Webpreparations"; $item_number = "PS123456"; $item_price = 55 * 100; $currency = "usd"; $order_id = "SKA92712382139"; $amount = $chargeJson['amount']; $balance_transaction = $chargeJson['balance_transaction']; $currency = $chargeJson['currency']; $status = $chargeJson['status']; $insert_data = array( 'name' => $this->input->post('name'), 'email' => $this->input->post('email'), 'card_num' => $this->input->post('card_num'), 'card_cvc' => $this->input->post('card_cvc'), 'card_exp_month' => $this->input->post('card_exp_month'), 'card_exp_year' => $this->input->post('card_exp_year'), 'item_name' => $item_name, 'item_number' => $item_number, 'item_price' => $item_price, 'item_price_currency' => $currency, 'paid_amount' => $amount, 'paid_amount_currency' => $currency, 'payment_status' => $status, 'created_by' => 1, 'created_date' => date('Y-m-d H:i:s') ); $this->Payment->insert($insert_data); /* for insert data in database close */ $this->session->set_flashdata('success', 'Payment made successfully.'); redirect('/stripe/Payments/index', 'refresh'); } } |
Step 7:- Create the Model “Payment.php” and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); //This is the Book Model for CodeIgniter CRUD using Ajax Application. class Payment extends CI_Model { var $table = 'orders'; // for construct public function __construct() { parent::__construct(); } /* for insert payment details in database start*/ public function insert($data) { //print_r($data); $this->db->insert($this->table, $data); return $this->db->insert_id(); } /* for insert payment details in database close*/ } |
Step 8:- Create the View “stripe.php” and paste the below code
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' /> <script src="<?php echo base_url();?>assets/js/jquery.min.js"></script> <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <!-- <h2 class="alert alert-info">How to integrate Stripe Payment Gateway in PHP</h2> --> <?php if($this->session->flashdata('success')){ ?> <div class="alert alert-success text-center"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <p><?php echo $this->session->flashdata('success'); ?></p> </div> <?php } ?> <!--<h1 class="alert alert-info">Charge $55 with Stripe</h1>--> <!-- display errors returned by createToken --> <span class="payment-errors alert"></span> <!-- stripe payment form --> <form action="<?php base_url(); ?>payment" method="POST" id="paymentFrm"> <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-4"> <div class="form-group"> <label>Name</label> <input type="text" name="name" size="50" class="form-control" placeholder="Please enter name"/> </div> </div> <div class="col-sm-4"> </div> </div> <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-4"> <div class="form-group"> <label>Email</label> <input type="text" name="email" size="50" class="form-control" placeholder="Please enter email"/> </div> </div> <div class="col-sm-4"> </div> </div> <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-4"> <div class="form-group"> <label>Card Number</label> <input type="text" name="card_num" size="20" autocomplete="off" class="card-number form-control" placeholder="Please enter card number"/> </div> </div> <div class="col-sm-4"> </div> </div> <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-4"> <div class="form-group"> <label>CVC</label> <input type="text" name="card_cvc" size="4" autocomplete="off" class="card-cvc form-control" placeholder="Please enter cvc"/> </div> </div> <div class="col-sm-4"> </div> </div> <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-4"> <div class="col-sm-6"> <div class="form-group"> <label>Card Expiry Month</label> <input type="text" name="card_exp_month" size="2" class="card-expiry-month form-control" placeholder="Please enter month"/> </div> </div> <div class="col-sm-6"> <div class="form-group"> <label>Card Expiry Year</label> <input type="text" name="card_exp_year" size="4" class="card-expiry-year form-control" placeholder="Please enter year"/> </div> </div> </div> <div class="col-sm-4"> </div> </div> <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-4"> <div class="form-group"> <button type="submit" id="payBtn" class="btn btn-success">Pay Now</button> </div> </div> <div class="col-sm-4"> </div> </div> </form> </div> <!-- for back to tutorial button start --> <br> <br> <br> <br> <a href="http://localhost/webpreprations/how-to-create-login-and-registration-in-codeigniter-2/"> <button class="btn btn-warning" style="margin-left: 110px"><i class="fa fa-mail-reply"></i> Back to Tutorial</button></a> <br> <br> <!-- for back to tutorial button close --> <!-- Stripe JavaScript library --> <script type="text/javascript" src="https://js.stripe.com/v2/"></script> <!-- jQuery is used only for this example; it isn't required to use Stripe --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript"> //set your publishable key Stripe.setPublishableKey('pk_test_qWEbr1PrSelCDxAk1qTW6NXT'); //callback to handle the response from stripe function stripeResponseHandler(status, response) { if (response.error) { //enable the submit button $('#payBtn').removeAttr("disabled"); //display the errors on the form $(".payment-errors").html('<div class="alert alert-danger">'+response.error.message+'</div>'); } else { var form$ = $("#paymentFrm"); //get token id var token = response['id']; //insert the token into the form form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />"); //submit form to the server form$.get(0).submit(); } } $(document).ready(function() { //on form submit $("#paymentFrm").submit(function(event) { //disable the submit button to prevent repeated clicks $('#payBtn').attr("disabled", "disabled"); //create single-use token to charge the user Stripe.createToken({ number: $('.card-number').val(), cvc: $('.card-cvc').val(), exp_month: $('.card-expiry-month').val(), exp_year: $('.card-expiry-year').val() }, stripeResponseHandler); //submit from callback return false; }); }); </script> </body> </html> |
Step 9:- Test Payment detail is below:-
Testing URL: https://uatsys.agsindia.com:8006
Card Number : 4242 4242 4242 4242
Expiry Year: 2020
Expiry Month: 12
CVV: 123
Step 10:- Now run our project, so we need to type in browser localhost/your_project_name/controller
like
http://localhost/webpreparations/ci_demo/stripe/Payments then you can see the result as in below snapshot,
Step 11:- Now we need to fill the below form as mention in the below snapshot
Step 12:- Now we need to Click on the “Pay Now” button then we can see the below snapshot
Congratulations you have successfully created “Stripe Payment Gateway Integration in Codeigniter”, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below, Thanks