Stripe Payment API – Create Monthly Subscription Plan, Create Customer & Assign Customer to the Plan

Stripe API request and response code to create monthly subscription plan, create customer and assign customer to the plan to be charged every month, posted by stripe checkout.js

/*
API Installation Links:
https://stripe.com/docs/libraries
https://github.com/stripe/stripe-php

API Usage Links
https://stripe.com/docs/api
https://stripe.com/docs/checkout/tutorial
*/

<?php require_once('vendor/autoload.php'); // check the link on github above on how to get it

// ****** One time charge, posted by stripe checkout.js
// charge card (not relevant for this topic)
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$charge_response = \Stripe\Charge::create(array(
				"amount" 	=> 1000,
				"currency" 	=> "usd",
				"source"		=> $token,
				"description" => $email
));
// end of one time charge


// ***** Create monthly subscription plan, customer & 
// ***** assign customer to the plan to be charged every month
// ***** posted by stripe checkout.js

// create plan
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$plan = \Stripe\Plan::create(array(
  "name" => "Xyz Monthly Plan",
  "id" => "xyz-monthly",
  "interval" => "month",
  "currency" => "usd",
  "amount" => 1000,
)); 


// create customer
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$customer_response = \Stripe\Customer::create(array(
  "email" => $email,
  "source" => $token,
)); 	

// response after customer is created
echo $customer_response;  // response
echo $customer_response->id; // customer id
echo $customer_response->email; // email
echo $customer_response->data->brand; // credit card type
echo $customer_response->data->last4; // credit card last 4 digits
echo $customer_response->data->exp_month; // expiry month
echo $customer_response->data->exp_year;  // expiry year

// assign customer to plan to be charged every month
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxxx");
$assign_response = \Stripe\Subscription::create(array(
  "customer" => $customer_response->id,
  "plan" => "xyz-monthly",
)); 

?>

Please check the comments within the code for better understanding. In case you run into any problems please check the Events and/or Logs on the Stripe dashboard.

Hope it helps!