Manage Recurring Payments using Stripe Billing in PHP

Last modified on July 1st, 2019 by Vincy.

Stripe Billing is a backend engine which supports to create and manage recurring payments. Integrating this engine with a PHP application will be very easy. It allows raising invoice billings against the subscribers. In a previous tutorial, we have seen how to manage recurring payments in PHP with PayPal Subscriptions.

Manage Recurring Payments using Stripe Billing in PHP

Stripe Billing allows both manual and automatic invoice generation. The automatic method of invoicing is used to enable recurring payments for subscriptions. In this tutorial, we are going to see the steps to implement Stripe Billing to manage recurring payments with an example. In a previous tutorial, we have seen how to integrate stripe billing for single payment.

Download and install Stripe API PHP library to integrate recurring payments by using this method. The library can be installed via the composer or by downloading the compressed code available online. By integrating this library, you can start invoking the Stripe API to proceed with the automatic payments implementation steps. 

Stripe Billing to Create and Manage Recurring Payments Invoices Automatically

The implementation of automatic invoices generation on recurring payments in Stripe platform can be done by following the three steps listed below.

  • Step 1: Create Product (goods and services) and plan
  • Step 2: Create a Customer
  • Step 3: Subscribe the customer for the created plan 

Prerequisites for Implementing Stripe Recurring Payments

First, you have to get the Stripe account API keys. Stripe provides two standard authentication keys publishable key and secret key. These keys can be found in the account setting pages with the URL The Stripe account publishable and secret key will be used while accessing the Stripe API. While implementing PHP payment gateway integration, we have created a developer account to get the API keys for authentication.

Download and install Stripe PHP client library for sending the API requests for the recurring payments implementation. The downloadable source code added this article is containing the PHP library in the vendor folder.

In the following sections, we are going to see the code to create Product Plan, Customer and Subscription. Before that, the API keys have to be set with the Stripe library class instance. The following code shows how to set the Stripe API keys using the library method.

$stripe = [
    "secret_key"      => "YOUR SECRET KEY",
    "publishable_key" => "YOUR PUBLISHABLE KEY",

Step 1: Create Product (goods and services) and Plan

While implementing the subscription-based recurring payment using the Stripe platform, the first step is for creating the product and plan. Stripe API requires specification to be set while accessing the API to create the product, plan, customer and subscription objects.

While creating the product, we need to specify the type of the product. The product can be goods or SaaS. In this example, I have added the Software product as a service and created a subscription plan to let the user subscribe to access my service.

In the below code, the product and the plan are created by accessing the Stripe API with the required options. In this code, the Product name and type is specified as an array.

$plan = \Stripe\Plan::create(array(
      "product" => [
          "name" => "PHP Tutorials with Examples"
      "nickname" => "PHP Tutorials Public Access",
      "interval" => "day",
      "interval_count" => "1",
      "currency" => "usd",
      "amount" => "30",

This code will return the Plan object JSON array with unique plan id. This plan id will be used while creating the subscription. The following JSON object array will be returned by executing the above code.

  "id": "",
  "object": "plan",
  "amount": 30,
  "billing_scheme": "per_unit",
  "created": 1545280570,
  "currency": "usd",
  "interval": "month",
  "interval_count": 1,
  "livemode": false,
  "metadata": {},
  "nickname": "PHP Tutorials Public Access",
  "product": "",
  "tiers": null,
  "tiers_mode": null,
  "transform_usage": null,
  "trial_period_days": null,
  "usage_type": "licensed"

Step 2: Create Customer

The following code is used to create the customer via API. Customer details can be passed via form elements or by using the Stripe Checkout.

After receiving the customer details, the Stripe API will post the data to the payment form action endpoint. In this code, the email represents the customer email address and the source is to denote the token reference for the stored customer payment details.

$customer = \Stripe\Customer::create([
  'email' => <customer_email>,
  'source'  => <src_token>,

By creating the customer object, the Stripe API will return the customer id. This id will be mapped with the reference of the subscription plan while creating the subscription for the customer.

Step 3: Create Subscription by Mapping the Plan to Customer

In this step, we are going to see how to create the subscription for a customer. In this code, the customer id and the unique service plan id is specified with the array of parameters passed to the subscription creation method. By invoking this method, the subscription object will be returned by the API. This JSON response data will have the details about the customer’s subscription created.

$subscription = \Stripe\Subscription::create([
    'customer' => '',
    'items' => [['plan' => '']],

As the customer subscribed for accessing the service, the subscription billing will be made by creating the invoice. The invoice is created on each billing cycle at a periodic interval. The billing cycle, interval, interval count parameters are set while creating the plan object.

Stripe Recurring Payment PHP Example

In this example, the subscribe button is generated and rendered into a landing page. In the landing page, the user can see the subscription plan caption in a tile interface.

By clicking the “Subscribe now” button the plan detail could be posted via a HTML form to the PHP endpoint. In this example, the subscription form will not have any hidden field. Instead, I have stated the plan and the product details directly on the PHP file where the three-step subscription is processed.

In charge.php, the subscription token and customer email will be received by using the post array. The posted values and the subscriptions keys are used to set the options required to proceed with the three-steps as we have seen before.

Below code shows the landing page and PHP endpoint that create stripe charges.


<?php require_once('./config.php'); ?>
.frmStripePayment {
    border: #E0E0E0 1px solid;
    padding: 20px 30px;
    width: 180px;
    text-align: center;
    background: #ececec;
    margin: 60px auto;
    font-family: Arial;
.plan-caption {
    margin-bottom: 30px;
    font-size: 1.2em;
    width: 180px;
<form action="charge.php" method="post" class="frmStripePayment">
  <div class="plan-caption">Want to Read PHP Video Tutorials?</div>
  <script src="" class="stripe-button"
          data-key="<?php echo $stripe['publishable_key']; ?>"
          data-description="ABONNEMENT 1 MOIS"
          data-label="Subscribe Now"




  $token  = $_POST['stripeToken'];
  $email  = $_POST['stripeEmail'];

  $plan = \Stripe\Plan::create(array(
      "product" => [
          "name" => "PHP Tutorials with Examples",
          "type" => "service"
      "nickname" => "PHP Tutorials Public Access",
      "interval" => "month",
      "interval_count" => "1",
      "currency" => "usd",
      "amount" => "3000",

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

   $subscription = \Stripe\Subscription::create(array(
      "customer" => $customer->id,
      "items" => array(
              "plan" => $plan->id,

  echo '<h1>Successfully charged $30.00!</h1>';

Stripe Recurring Payment Example Output

This screenshot shows the output of the landing page by executing this stripe recurring payment PHP example.



Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top