Skip to main content

Stripe payment gateway integration using cUrl - Codeigniter 3

 


Stripe payment gateway integration using cUrl - Codeigniter 3

  • Sign up for a Stripe account at https://stripe.com/
  • Obtain Stripe api key
  • Setup Codeigniter 3 Project
  • Create a library for Stripe integration
Create library inside libraries folder  

application => libraries => Stripe_library.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Stripe_library {       
protected $CI; // Default Constructor public function __construct() { $this->CI =& get_instance(); // Load necessary libraries and helpers here if needed } // Create function for checkout public function create_checkout_session($amount, $currency, $success_url, $cancel_url) { $stripe_secret_key = 'Stripe_secret_key'; $api_url = 'https://api.stripe.com/v1/checkout/sessions'; $stripeamount = $amount * 100; $data = [ 'payment_method_types' => ['card'], 'line_items' => [ [ 'price_data' => [ 'currency' => $currency, 'product_data' => [ 'name' => 'Document Translation', ], 'unit_amount' => $stripeamount, ], 'quantity' => 1, ], ], 'mode' => 'payment', 'success_url' => $success_url, 'cancel_url' => $cancel_url, ]; $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_USERPWD, $stripe_secret_key . ':'); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); } } ?>

Create controller inside controller folder
<?php defined('BASEPATH') OR exit('No direct script access allowed');
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, OPTIONS");
class Payment extends CI_Controller{
    
    public function __construct()
    {
       parent::__construct();
       $this->load->library('stripe_library');
    }

    public function checkout() {
        $amount = "Amount"; // Enter Amount
        $productname = "Payment for"; // Enter Product Name
        $currency = 'inr'; // Enter Currency
        $success_url = base_url('payment/success');
        $cancel_url = base_url('payment/cancel');
$session = $this->stripe_library->create_checkout_session($amount, $currency, $success_url, $cancel_url); echo json_encode($session['url']); }     public function success() { // Handle successful payment here // You can retrieve payment details from $_GET or $_POST } public function cancel() { // Handle cancelled payment here }
Create payment.php file inside views.
Send payment checkout request using ajax. so we have create function paynow on button click. 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Stripe Payment Integration</title>
</head>
<body>
    <form id="payment-form">
        <input type="text" id="amount" placeholder="Enter Amount">
        <div id="card-element"></div>
        <button id="submit" onclick="paynow()">Pay</button>
    </form>

    <script>
        function paynow(){
        var amount = $('#total_amount').text();
        $.ajax({
            url: "<?php echo base_url('payment/checkout');?>",
            dataType: "json",
            type: "POST",
            data: {amount:amount},
            success: function(response){
                window.open(response, '_blank');
            }
        })
        }

    </script>
</body>
</html>




Comments

Popular posts from this blog

Create rest api to send curl request from codeigniter controller to google cloud for text to speech convert using codeigniter 3.

  Create Google Cloud account https://cloud.google.com/ You can create account using existing mail id. Obtain api key for Text to Speech. Create Rest Api to convert text to speech.  We are using cUrl method to send a request. Create Api.php and extend REST_Controller  <?php defined('BASEPATH') OR exit('No direct script access allowed'); require APPPATH . '/libraries/REST_Controller.php'; class Api extends REST_Controller{ public function __construct(){ parent::__construct(); } public function index_get() { $text = "Hi, how are you doing? Very well thank you. How about you? Not so well. But thank you for asking. How is the weather there? Oh, its very warm out here."; $audioContent = $this->generateSpeech($text); $this->response(array( "status" => 1, "message" => "question 1", "data" => $audioContent ), REST_Controller::HTTP_OK); } private function generateSpeech(...

Building Dynamic Web Applications with CodeIgniter 3

Introduction:                                 CodeIgniter 3 is a powerful PHP framework that simplifies web application development. Its simplicity and flexibility make it a popular choice among developers for creating dynamic and robust web applications. In this blog post, we'll explore some essential concepts and features of CodeIgniter 3 and show you how to get started with building your web applications.  Installation and Setup:   Begin by downloading and installing CodeIgniter 3.  Configure your development environment (e.g., Apache, MySQL, PHP).  Create a new CodeIgniter project structure.  Model-View-Controller (MVC) Architecture:  Understand the MVC design pattern used in CodeIgniter.  Learn how to structure your application using models, views, and controllers.  Explore the benefits of separating application logic.  Routing and URLs:  ...

Codeigniter registration form with validation using Ajax: Codeigniter Club

Codeigniter registration form with validation using Ajax In this tutorial, we will understand how to create a simple registration form using CodeIgniter.  Before starting we have to create a  Unique constraint (email id is a unique constraint in the database table) because if we enter a  Duplicate email id the  database will through a duplicate entry error and we can print an error. Otherwise, we can use another method using programming. we can check email-id already exist or not.  Create database table using Mysql database CREATE TABLE `registration` ( `id` int(11) NOT NULL, `username` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `country` varchar(100) NOT NULL, `password` varchar(200) NOT NULL, `agree` varchar(10) NOT NULL, `created_at` datetime NOT NULL DEFAULT current_timestamp(), `update_at` datetime NOT NULL DEFAULT current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Insert Data  INSERT INTO `registratio...