Stripe Subscriptions using php – Download Full Source Code

Hi Guys,

In This blog article i am going to explain about STRIPE SUBSCRIPTIONS USING PHP and making available the source code also. by extending this code you can make a complete subscription management script.

Here in this article about

stripe subscriptions using php ,

i am going to create a pricing table where 3 type of plans are defined that plan will create on stripe if plan not exists on stripe panel and then create a customer and create a subscription in that customers reference.

Note: the Stripe account is the pre requisite for you , where we will require stripe publishable key and secret key.

and the stripe php library will be require for including in config file so dont worry about the stripe php library it will be available in full source code download of this article.

Step 1: Create a config file and put following code :

<?php

require_once('stripe-php/init.php');

$stripe = array(
  "secret_key"      => "sk_test_456456546hgjhg",
  "publishable_key" => "pk_test_567hjhgjghgjhg"
);

\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

Note : you need to change the publishable key and secret key in this config file

Step 2:  Create a file index.php where pricing table and stripe client side code will come as following :

<?php 
  // Load Stripe's PHP library and set your secret keys
  require_once("config.php"); 
  // Create the subscription
  require_once("create-subscription.php"); 
?>
<html>
  <head>
    <style>
      .spacing {
        margin-top:20px;
      }
      .success {
        color: #fff;
        background-color: green;
      }
      .error {
        color: #fff;
        background-color: red;
      }

    </style>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <?php if (isset($success)): ?>
      <div class='success'><?php echo $success; ?></div>
    <?php else: ?>
      <?php if (isset($error)): ?>
        <div class='error'><?php echo $error; ?></div>
      <?php endif ?>       
      
<style>
* {
    box-sizing: border-box;
}
body {
    margin:50px 0; 
    padding:0;
    text-align:center;
}

.content {
     
    margin:0 auto;
    text-align:left;
    padding:15px;
     
}

.columns {
    float: left;
    width: 33.3%;
    padding: 8px;
}

.price {
    list-style-type: none;
    border: 1px solid #eee;
    margin: 0;
    padding: 0;
    -webkit-transition: 0.3s;
    transition: 0.3s;
}

.price:hover {
    box-shadow: 0 8px 12px 0 rgba(0,0,0,0.2)
}

.price .header {
    background-color: #111;
    color: white;
    font-size: 25px;
}

.price li {
    border-bottom: 1px solid #eee;
    padding: 20px;
    text-align: center;
}

.price .grey {
    background-color: #eee;
    font-size: 20px;
}

.button {
    background-color: #4CAF50;
    border: none;
    color: white;
    padding: 10px 25px;
    text-align: center;
    text-decoration: none;
    font-size: 18px;
}

@media only screen and (max-width: 600px) {
    .columns {
        width: 100%;
    }
}
</style>
 
<div class="content" style="width:60%;">
<h2 style="text-align:center">Stripe Subscriptions using php</h2>
<p style="text-align:center">with Responsive Pricing Tables</p>

<div class="columns">
  <ul class="price">
    <li class="header">Basic</li>
    <li class="grey">$ 9.99 / year</li>
    <li>10GB Storage</li>
    <li>10 Emails</li>
    <li>10 Domains</li>
    <li>1GB Bandwidth</li>
    <li class="grey">

        <form action="" method="POST" class="spacing">
        <input name="plan" type="hidden" value="Basic" />         
        <input name="interval" type="hidden" value="year" />         
        <input name="price" type="hidden" value="9.99" />         
        <input name="currency" type="hidden" value="usd" />         
        <script
          src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<?php echo $stripe['publishable_key']; ?>"
          data-image="https://www.w3school.info/wp-content/uploads/2017/08/w3school_logo.png"
          data-name="BASIC PLAN"
          data-description="Start with basic plan"
          data-panel-label="Subscribe Now"
          data-label="Subscribe Now"
          data-locale="auto">
        </script>
      </form>
   

    </li>
  </ul>
</div>

<div class="columns">
  <ul class="price">
    <li class="header" style="background-color:#4CAF50">Pro</li>
    <li class="grey">$ 24.99 / year</li>
    <li>25GB Storage</li>
    <li>25 Emails</li>
    <li>25 Domains</li>
    <li>2GB Bandwidth</li>
    <li class="grey">
       <form action="" method="POST" class="spacing">
         
        <input name="plan" type="hidden" value="Pro" />         
        <input name="interval" type="hidden" value="year" />         
        <input name="price" type="hidden" value="24.99" />         
        <input name="currency" type="hidden" value="usd" />               
        <script
          src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<?php echo $stripe['publishable_key']; ?>"
          data-image="https://www.w3school.info/wp-content/uploads/2017/08/w3school_logo.png"
          data-name="Pro PLAN"
          data-description="Start with Pro plan"
          data-panel-label="Subscribe Now"
          data-label="Subscribe Now"
          data-locale="auto">
        </script>
      </form>
    </li>
  </ul>
</div>

<div class="columns">
  <ul class="price">
    <li class="header">Premium</li>
    <li class="grey">$ 49.99 / year</li>
    <li>50GB Storage</li>
    <li>50 Emails</li>
    <li>50 Domains</li>
    <li>5GB Bandwidth</li>
    <li class="grey">
<form action="" method="POST" class="spacing">     
        <input name="plan" type="hidden" value="Premium" />         
        <input name="interval" type="hidden" value="year" />         
        <input name="price" type="hidden" value="49.99" />         
        <input name="currency" type="hidden" value="usd" />           
        <script
          src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<?php echo $stripe['publishable_key']; ?>"
          data-image="https://www.w3school.info/wp-content/uploads/2017/08/w3school_logo.png"
          data-name="Premium PLAN"
          data-description="Start with Premium plan"
          data-panel-label="Subscribe Now"
          data-label="Subscribe Now"
          data-locale="auto">
        </script>
      </form>
    </li>
  </ul>
</div>

  <?php endif ?>  

</div>
  </body>
</html>

Note : here you have 3 plans listed with different subscription plan and data(like plan name,plan price,plan interval [month/year/week/day] and currency)  which are defined in hidden fields in every stripe form tag inside every pricing options.

Step 3: Now the Stripe Server Side code where after inputing the card details Actyual payment need to be done

So create a file create-subscription.php which is already included in index.php code in top lines, and this file wil have following Code:

<?php 
  // Check for a POSTed stripeToken and subscription
  if (isset($_POST['stripeToken']) && isset($_POST['plan'])){
    try {
	
	
	$plan = \Stripe\Plan::create(array(
			  "name" => $_POST['plan'],
			  "id" => "plan-".$_POST['plan'],
			  "interval" => $_POST['interval'],
			  "currency" => $_POST['currency'],
			  "amount" => $_POST['price']*100,
			));

	$customer = \Stripe\Customer::create(array(
	  "email" => $_POST['stripeEmail'],
	  "source" => $_POST['stripeToken'], // The token submitted from Checkout
	));
	
	\Stripe\Subscription::create(array(
	  "customer" => $customer->id,
	  "items" => array(
		array(
		  "plan" => "plan-".$_POST['plan'],
		),
	  ),
	));

     
      $success = "Thanks! You've subscribed to the " . $_POST['plan'] .  " plan.";
    }
    catch(\Stripe\Error\Card $e) {
      // Since it's a decline, \Stripe\Error\Card will be caught
      $body = $e->getJsonBody();
      $error  = $body['error']['message'];
    } 
    // Probably want to log all of these for later or send yourself a notification
    catch (\Stripe\Error\RateLimit $e) {
      $error = "Sorry, we weren't able to authorize your card. You have not been charged.";
    } catch (\Stripe\Error\InvalidRequest $e) {
      $error = "Sorry, we weren't able to authorize your card. You have not been charged.";
    } catch (\Stripe\Error\Authentication $e) {
      $error = "Sorry, we weren't able to authorize your card. You have not been charged.";
    } catch (\Stripe\Error\ApiConnection $e) {
      $error = "Sorry, we weren't able to authorize your card. You have not been charged.";
    } catch (\Stripe\Error\Base $e) {
      $error = "Sorry, we weren't able to authorize your card. You have not been charged.";
    } catch (Exception $e) {
      $error = "Sorry, we weren't able to authorize your card. You have not been charged.";
    }
  }
?>

Now you are Ready  it will look like this :

stripe subscriptions preview
stripe subscriptions preview

and if we click on any subscription button it will prompt a stripe popup like this

stripe subscriptions credit card promt by stripe
stripe subscriptions credit card prompt by stripe

Click HERE to download full source code

Note: this is completely Free script.

if you have any trouble in download this then please contact me at [email protected] or ask query in comments below