how to integrate stripe payment gateway in php – stripe php [stripe integration in php]

how to integrate stripe payment gateway in php? – Stripe Manage Subscription Payment using Codeigniter – Recurring payments also known as AutoPay are automatic payments to authorize and collect all the Recurring charges immediately, Stripe Subscriptions supports you the platform to set up and manage recurring payments.

A Guide to Enable Stripe Subscription Payment Integration in CodeIgniter. and Get the database subscription forms & arrange them under a dropdown.

how to integrate stripe payment gateway in php – Stripe Payment Gateway Integration in Codeigniter Example

first of all Make a Stripe account and get API keys and then Fetch subscription plans from the database and list them under a dropdown. and then Open a file constants and update here Make controllers now The controller handles the Subscription process. and then Make a view(header) , Make a view(footer), Make views and You add Stripe Javascript code on your page. and last Make a views file thankyou page.

stripe php
stripe php

Stripe Manage Subscription Payment using Codeigniter

__stripe-manage-subscription-payment-using-codeigniter
___application
____config
_____autoload.php
_____constants.php
_____routes.php
____controllers
_____Subscription.php
____models
____views
_____subscription
______index.php
______thankyou.php
_____templates
_______header.php
_______footer.php
__system
__index.php
__assets
__css
___style.css

application/config/constants.php

  <?php
    define('STRIPE_PUBLISHABLE_KEY', 'pk_test_fg9jghfdLtjlkLFJGKDJfaLy99qyh55x6h');
    define('STRIPE_SECRET_KEY', 'sk_test_gkfMjWggHrMKj6iLKJDzqL89CxsUjdkj');
  ?>

Also Read : stripe integration in php

application/controllers/Subscription.php

<?php
defined('BASEPATH') OR
    exit('No direct script access allowed');

class Subscription extends CI_Controller {
  public function __construct() {
        parent::__construct();
        $this->load->library('stripe');
    }

  public function index() {
        $data['metaDescription'] = 'Open up the Stripe docs and go down the page until you find subscriptions.';
        $data['metaKeywords'] = 'Stripe Billing is a backend engine which supports to create and manage recurring payments.';
        $data['title'] = "stripe payment subscription integration with php, html & css";
        $data['breadcrumbs'] = array('Stripe Subscription Payment Integration in PHP' => '#');
        $this->load->view('subscription/index', $data);
    }

    // make subscription
    public function create() {
        $data['metaDescription'] = 'Stripe Subscription Payment Integration in PHP · Create an HTML form to select a subscription plan and provide credit card information.';
        $data['metaKeywords'] = 'Install Stripe PHP Library — Stripe official libraries help to access API from the application. Download the Stripe PHP library from Github.';
        $data['title'] = "In this stripe tutorial you will learn stripe checkout subscription or recurring payment integration using PHP";
        $data['breadcrumbs'] = array('Integrate Stripe payment Subscription with PHP, HTML5 & CSS3 for website & developers.' => '#');
         
        \Stripe\Stripe::setApiKey(STRIPE_SECRET_KEY);

        $token  = $this->input->post('stripeToken');
        $email  = $this->input->post('stripeEmail');
        $plan  = $this->input->post('plan');
        $interval  = $this->input->post('interval');
        $price  = $this->input->post('price');
        $currency  = $this->input->post('currency');

        $time = time();
        $plan = \Stripe\Plan::create(array( 
            "product" => [
                "name" => $plan,
                "type" => "service"
            ],
            "nickname" => $plan,
            "interval" => $interval,
            "interval_count" => "1",
            "currency" => $currency,
            "amount" => ($price*100) ,
        ));

        $customer = \Stripe\Customer::create([
            'email' => $email,
            'source'  => $token,
        ]);

        $subscription = \Stripe\Subscription::create(array(
            "customer" => $customer->id,
            "items" => array(
                array(
                    "plan" => $plan->id,
                ),
            ),
        ));
        $data['price'] = $price;
        $this->session->set_flashdata('price', $price);  
        redirect('subscription/thankyou');
    }

    // successfully pay
    public function thankyou() {
        $data['metaDescription'] = 'Stripe Payment Gateway Integration using PHP';
        $data['metaKeywords'] = 'Integrate Recurring Stripe Subscription Payment with PHP';
        $data['title'] = "How To Integrate Stripe In PHP Applications?";
        $data['breadcrumbs'] = array('How to Integrate Stripe Payment Gateway in Codeigniter?' => '#');
        
        $data['price'] = $this->session->flashdata('price');
        $this->load->view('subscription/thankyou', $data);   
    }
}

?>

application/views/templates/header.php

Read Also:  Data Export to Excel with PHP and MySQL

  <?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  ?>
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title><?php print $title; ?> - How to Integrate Stripe Payment Gateway in Codeigniter?</title>
    <link rel="icon" type="image/ico" href="<?php print HTTP_IMAGE_PATH; ?>favicon.ico">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" />
    <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">

    <link href="<?php print HTTP_CSS_PATH; ?>style.css" rel="stylesheet">
  </head>
  <body>

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top header-bg-dark" style="background: ##FFFFFF!;">
    <div class="container">
      <a class="navbar-brand font-weight-bold" href="https://www.pakainfo.com"><h2>Www.pakainfo.com</h2></a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
      <div class="collapse navbar-collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="https://www.pakainfo.com">Home
                  <span class="sr-only">(current)</span>
                </a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="https://www.pakainfo.com/php-free-script-demos/">Live Demo</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>


<b>application/views/templates/footer.php</b>


    <!-- Footer -->
    <footer class="footer bg-light footer-bg-dark">
      <div class="container">
        <div class="row">
          <div class="col-lg-6 h-100 text-center text-lg-left my-auto">
            <ul class="list-inline mb-2">
              <li class="list-inline-item">
                <a href="#">About</a>
              </li>
              <li class="list-inline-item">⋅</li>
              <li class="list-inline-item">
                <a href="#">Contact</a>
              </li>
              <li class="list-inline-item">⋅</li>
              <li class="list-inline-item">
                <a href="#">Terms of Use</a>
              </li>
              <li class="list-inline-item">⋅</li>
              <li class="list-inline-item">
                <a href="#">Privacy Policy</a>
              </li>
            </ul>
            <p class="text-muted small mb-4 mb-lg-0">Copyright ©  2022 - <?php print date('Y', time());?> <a href="https://www.pakainfo.com/">www.pakainfo.COM</a> All rights reserved.</p>
          </div>
          <div class="col-lg-6 h-100 text-center text-lg-right my-auto">
            <ul class="list-inline mb-0">
              <li class="list-inline-item mr-3">
                <a href="#">
                  <i class="fab fa-facebook fa-2x fa-fw"></i>
                </a>
              </li>
              <li class="list-inline-item mr-3">
                <a href="#">
                  <i class="fab fa-twitter-square fa-2x fa-fw"></i>
                </a>
              </li>
              <li class="list-inline-item">
                <a href="#">
                  <i class="fab fa-instagram fa-2x fa-fw"></i>
                </a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </footer>
    <script>
      var baseurl = "<?php print site_url();?>";
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>  
  </body>
  </html>

application/views/subscription/index.php

<?php $this->load->view('templates/header');?>
 <style type="text/css">
.planswebsite .card {
  border: none;
  border-radius: 1rem;
  transition: all 0.2s;
  box-shadow: 0 0.5rem 1rem 0 rgba(0, 0, 0, 0.1);
}

.planswebsite hr {
  margin: 1.5rem 0;
}

.planswebsite .card-title {
  margin: 0.5rem 0;
  font-size: 20px;
  letter-spacing: .1rem;
  font-weight: bold;
}

.planswebsite .card-price {
  font-size: 3rem;
  margin: 0;
}

.planswebsite .card-price .period {
  font-size: 0.8rem;
}

.planswebsite ul li {
  margin-bottom: 1rem;
}

.planswebsite .text-muted {
  opacity: 0.7;
}

.planswebsite .btn {
  font-size: 80%;
  border-radius: 5rem;
  letter-spacing: .1rem;
  font-weight: bold;
  padding: 1rem;
  opacity: 0.7;
  transition: all 0.2s;
}


@media (min-width: 992px) {
  .planswebsite .card:hover {
    margin-top: -.25rem;
    margin-bottom: .25rem;
    box-shadow: 0 0.5rem 1rem 0 rgba(0, 0, 0, 0.3);
  }
  .planswebsite .card:hover .btn {
    opacity: 1;
  }
}
.pb-5, .py-5{
      padding-bottom: 15px!important;
      padding-top: 15px!important;
}

.strp-btn-bonoza-el  {
    background-image: linear-gradient(#007bff,#007bff 85%,#007bff);
    margin-left: 31px;
    border-radius: 50px;
  }
  
  .strp-btn-bonoza-el span {
    width: 227px;  
    position: relative;
    height: 49px;
    line-height: 49px;
    background: #007bff;
    background-image: -webkit-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: -moz-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: -ms-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: -o-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: -webkit-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: -moz-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: -ms-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: -o-linear-gradient(#007bff,#007bff 85%,#007bff);
    background-image: linear-gradient(#007bff,#007bff 85%,#007bff);
    font-size: 16px;
    color: #FFFFFF;
    font-weight: bold;
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    text-shadow: 0 -1px 0 rgba(0,0,0,0.25);
    -webkit-box-shadow: inset 0 1px 0 rgba(255,255,255,0.25);
    -moz-box-shadow: inset 0 1px 0 rgba(255,255,255,0.25);
    -ms-box-shadow: inset 0 1px 0 rgba(255,255,255,0.25);
    -o-box-shadow: inset 0 1px 0 rgba(255,255,255,0.25);
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.25);
    -webkit-border-radius: 0px;
    -moz-border-radius: 0px;
    -ms-border-radius: 0px;
    -o-border-radius: 0px;
    border-radius: 0px;
     font-size: 80%;
    border-radius: 5rem;
    letter-spacing: .1rem;
    font-weight: bold;
    transition: all 0.2s;
    padding: .0px;
  }


 </style>
 <section class="showcase">
   <div class="container">
   <div class="pb-2 mt-4 mb-2 border-bottom">
        <h2>Stripe Manage Subscription Payment using Codeigniter</h2>
      </div>
    <section class="planswebsite py-5">
    <div class="container">
      <div class="row">
        <div class="col-lg-4">
          <div class="card mb-5 mb-lg-0">
            <div class="card-body">
              <h5 class="card-title text-primary text-uppercase text-center">DSP Zone</h5>
              <h6 class="card-price text-center">$333<span class="period">/per Month</span></h6>
              <hr>
              <ul class="fa-ul">
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Single User</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>5GB Storage</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>DEDICATED Public Apps</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Community Access</li>
                <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>DEDICATED Private Apps</li>
                <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Dedicated Phone Support</li>
                <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Free Subdomain</li>
                <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Monthly Status Reports</li>
              </ul>
              <form action="<?php print site_url();?>subscription/create" method="post" class="frmStripePayment">
                <input name="plan" type="hidden" value="SHARED Package<" />         
                <input name="interval" type="hidden" value="Month" />         
                <input name="price" type="hidden" value="333.00" />         
                <input name="currency" type="hidden" value="usd" />
                <script src="https://checkout.stripe.com/checkout.js" class="strp-btn-bonoza"
                    data-key="<?php echo STRIPE_PUBLISHABLE_KEY; ?>"
                    data-name="DSP Zone"
                    data-description="DSP Zone"
                    data-panel-label="Pay Now"
                    data-label="Sign Up"
                    data-locale="auto">
                </script>
            </form>
            </div>
          </div>
        </div>
        <div class="col-lg-4">
          <div class="card mb-5 mb-lg-0">
            <div class="card-body">
              <h5 class="card-title text-success text-uppercase text-center">HOME</h5>
              <h6 class="card-price text-center">$248<span class="period">/per Month</span></h6>
              <hr>
              <ul class="fa-ul">
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span><strong>5 Users</strong></li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>50GB Storage</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>DEDICATED Public Apps</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Community Access</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>DEDICATED Private Apps</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Dedicated Phone Support</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Free Subdomain</li>
                <li class="text-muted"><span class="fa-li"><i class="fas fa-times text-danger"></i></span>Monthly Status Reports</li>
              </ul>
              <form action="<?php print site_url();?>subscription/create" method="post" class="frmStripePayment">
                <input name="plan" type="hidden" value="SHARED Package<" />         
                <input name="interval" type="hidden" value="Month" />         
                <input name="price" type="hidden" value="248.00" />         
                <input name="currency" type="hidden" value="usd" />
                <script src="https://checkout.stripe.com/checkout.js" class="strp-btn-bonoza"
                    data-key="<?php echo STRIPE_PUBLISHABLE_KEY; ?>"
                    data-name="HOME PACKAGE"
                    data-description="HOME PACKAGE"
                    data-panel-label="Pay Now"
                    data-label="Sign Up"
                    data-locale="auto">
                </script>
            </form>
            </div>
          </div>
        </div>
        <div class="col-lg-4">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title text-warning text-uppercase text-center">eCommerce</h5>
              <h6 class="card-price text-center">$999<span class="period">/per Month</span></h6>
              <hr>
              <ul class="fa-ul">
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span><strong>DEDICATED Users</strong></li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Service Storage</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>DEDICATED Public Apps</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Community Access</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>DEDICATED Private Apps</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Dedicated Phone Support</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span><strong>DEDICATED</strong> Free Subdomains</li>
                <li><span class="fa-li"><i class="fas fa-check text-success"></i></span>Monthly Status Reports</li>
              </ul>
              <form action="<?php print site_url();?>subscription/create" method="post" class="frmStripePayment">
                <input name="plan" type="hidden" value="SHARED Package<" />         
                <input name="interval" type="hidden" value="Month" />         
                <input name="price" type="hidden" value="999.00" />         
                <input name="currency" type="hidden" value="usd" />
                <script src="https://checkout.stripe.com/checkout.js" class="strp-btn-bonoza"
                    data-key="<?php echo STRIPE_PUBLISHABLE_KEY; ?>"
                    data-name="eCommerce PACKAGE"
                    data-description="eCommerce PACKAGE"
                    data-panel-label="Pay Now"
                    data-label="Sign Up"
                    data-locale="auto">
                </script>
            </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
  </div>
</section>
<?php $this->load->view('templates/footer');?>

Stripe Javascript source code

Read Also:  PHP Compare Two Arrays Values

  <form action="charge.php" method="post" class="frmStripePayment">
    <input name="plan" type="hidden" value="SHARED Package<" />         
    <input name="interval" type="hidden" value="Month" />         
    <input name="price" type="hidden" value="248.00" />         
    <input name="currency" type="hidden" value="usd" />
    <script src="https://checkout.stripe.com/checkout.js" class="strp-btn-bonoza"
        data-key="<?php echo STRIPE_PUBLISHABLE_KEY; ?>"
        data-name="NAME PACKAGE"
        data-description="NAME PACKAGE"
        data-panel-label="Pay Now"
        data-label="Get started"
        data-locale="auto">
    </script>
  </form>

Don’t Miss : stripe php
application/views/subscription/thankyou.php

<?php $this->load->view('templates/header');?>
<section class="showcase">
   <div class="container">
    <div class="text-center">
      <h1 class="display-3">Thank You!</h1>
      <?php if(!empty($price)) { ?>
        <h3>Successfully charged $<?php print $price;?>!</h3>
      <?php } ?>
      <p class="lead"><strong>Please check your email</strong> for further instructions on how to complete your account setup.</p>
      <hr>
      <p>
        Having trouble? <a href="mailto:[email protected]">Contact us</a>
      </p>
      <p class="lead">
        <a class="btn btn-primary btn-sm" href="<?php print site_url();?>" role="button">Continue to Pakainfo.com</a>
      </p>
    </div>
    </div>
</section>
<br><br><br><br><br><br>
<?php $this->load->view('templates/footer');?>