Subscribing Customers

Subscribe customers to your plans to bill them on a recurring basis. If you need help after reading this, check out our answers to common questions or chat live with other developers in #stripe on freenode.

Having created one or more plans, you can now subscribe customers to plans. In order to bill customers for subscriptions, you’ll need to collect their payment details using Checkout or Stripe.js. (Even with subscriptions, you still need to securely obtain the customer’s payment information.)

Assuming you’ve used Checkout or Stripe.js to fetch a Stripe token that represents the customer’s payment information, you can then create a customer and subscribe her to a plan in one step:

# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

Stripe::Customer.create(
  :source => token, # obtained from Stripe.js
  :plan => "basic_monthly",
  :email => "[email protected]"
)
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

stripe.Customer.create(
  source=token, # obtained from Stripe.js
  plan="basic_monthly",
  email="[email protected]"
)
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

$customer = \Stripe\Customer::create(array(
  "source" => $token, // obtained from Stripe.js
  "plan" => "basic_monthly",
  "email" => "[email protected]"
));
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_BQokikJOvBiI2HlWgH4olfQ2";

Map<String, Object> customerParams = new HashMap<String, Object>();
customerParams.put("source", token); // obtained with Stripe.js
customerParams.put("plan", "basic_monthly");
customerParams.put("email", "[email protected]");

Customer customer = Customer.create(customerParams);
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

stripe.customers.create({
  source: token, // obtained with Stripe.js
  plan: "basic_monthly",
  email: "[email protected]"
}, function(err, customer) {
  // asynchronously called
});

The code above creates a customer, associates the previously provided payment info with that customer, and subscribes the customer to the “Basic Monthly” plan. (Note that the plan value should be its ID, not its name.)

Just as you would when attempting a charge, you should wrap that code within a try...catch block in order to handle any thrown exceptions. If the payment method was invalid (i.e., the customer’s card was declined), if the plan does not exist, or if anything else went wrong, you’ll need to handle the error.

If successful, the resulting Customer object will reflect that the customer was successfully subscribed to the plan. The default_card property will point to the default payment method. The subscriptions property will contain the subscription that you created.

You should store the id property in your database along with the other customer info that you’ve collected, such as login credentials, for later reference.

Next steps