0Pricing
Stripe Payments & SaaS Billing Systems · 课时

Stripe Subscriptions API 入门

探索周期性支付的核心概念,以及 Stripe Subscriptions API 如何为 SaaS 提供可靠的订阅管理能力。

Stripe Subscriptions API 入门 是 CoddyKit 上的免费 Stripe Payments & SaaS Billing Systems 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Stripe Payments & SaaS Billing Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Stripe Payments & SaaS Billing Systems 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Welcome to Subscriptions!

Ever wondered how Netflix, Spotify, or your favorite SaaS app charges you regularly? That's recurring billing, powered by subscriptions!

In this lesson, we'll dive into how Stripe helps you build robust subscription services for your business.

Why Recurring Payments?

Subscriptions are key for many businesses, especially for Software as a Service (SaaS) models. They provide:

  • Predictable Revenue: Stable income streams.
  • Customer Loyalty: Encourages long-term relationships.
  • Simplified Billing: Automates regular charges.

Stripe's Subscriptions API makes setting this up straightforward.

Stripe's Core Subscription Building Blocks

Stripe uses a few key concepts to manage subscriptions:

  • Products: What you sell (e.g., 'Basic Plan', 'Premium Tier').
  • Prices: How much and how often you charge for a Product (e.g., '$10/month', '$100/year').
  • Customers: The individuals or businesses subscribing.
  • Subscriptions: The ongoing agreement between a Customer and a Price.

Understanding Products

A Product in Stripe represents something you sell. Think of it as the core offering.

For example, if you offer a 'Standard' and 'Pro' tier for your app, these would be two separate Products.

Products hold general information like a name and description, but not the actual price.

Defining Prices for Products

A Price defines how you charge for a Product. A single Product can have multiple Prices.

For example, your 'Pro' Product might have:

  • A 'Monthly' Price of $29.
  • An 'Annual' Price of $299.

Prices specify the currency, amount, and billing interval (e.g., day, week, month, year).

The Subscription Flow: High-Level

To create a subscription, you generally follow these steps:

  1. Create a Customer: Represent the user in Stripe.
  2. Collect Payment Method: Securely get card details (covered in other lessons).
  3. Create a Subscription: Link the Customer to a Price.

Stripe handles the recurring billing automatically after that!

API Example: Creating a Customer

Before a customer can subscribe, they need to exist in Stripe. Here's a basic Java example to create a Customer object:

Run this code to see a new customer ID generated!

import com.stripe.Stripe;
import com.stripe.model.Customer;
import com.stripe.param.CustomerCreateParams;

public class Main {
  public static void main(String[] args) {
    Stripe.apiKey = "sk_test_YOUR_SECRET_KEY"; // Replace

    try {
      CustomerCreateParams params =
        CustomerCreateParams.builder()
          .setEmail("testuser@example.com")
          .setName("Test User")
          .build();
      Customer customer = Customer.create(params);
      System.out.println(
        "New Customer ID: " + customer.getId()
      );
    } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
    }
  }
}

API Example: Creating a Subscription

Once you have a Customer and a Price (which you'd create via the dashboard or API, covered in Lesson 2), you can create a Subscription.

Note: Replace cus_... and price_... with actual IDs from your Stripe account.

import com.stripe.Stripe;
import com.stripe.model.Subscription;
import com.stripe.param.SubscriptionCreateParams;

public class Main {
  public static void main(String[] args) {
    Stripe.apiKey = "sk_test_YOUR_SECRET_KEY"; // Replace

    try {
      SubscriptionCreateParams params =
        SubscriptionCreateParams.builder()
          .setCustomer("cus_YOUR_CUSTOMER_ID") // From step 1
          .addItem(
            SubscriptionCreateParams.Item.builder()
              .setPrice("price_YOUR_PRICE_ID") // Your plan's price
              .build()
          )
          .build();
      Subscription subscription =
        Subscription.create(params);
      System.out.println(
        "Subscription ID: " + subscription.getId()
      );
    } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
    }
  }
}

Subscription Statuses

Subscriptions have different statuses reflecting their state:

  • active: The customer is currently subscribed and being billed.
  • past_due: A payment failed, and the subscription is awaiting a new payment.
  • canceled: The subscription has ended.
  • trialing: The customer is in a trial period (no charge yet).

Understanding these helps manage customer access.

Quick Check: Subscription Basics

Which of the following is responsible for defining how much and how often a customer is charged for a subscription?

Recap: Intro to Subscriptions

Great job! You've learned the fundamental concepts of Stripe Subscriptions:

  • The value of recurring payments for SaaS.
  • Stripe's core building blocks: Products, Prices, Customers, and Subscriptions.
  • The basic API flow for creating a customer and a subscription.
  • Key subscription statuses.

Next, we'll learn how to create and manage Products and Prices in detail!

常见问题解答

「Stripe Subscriptions API 入门」课时是免费的吗?

是的 — 「Stripe Subscriptions API 入门」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Stripe Payments & SaaS Billing Systems 课程的其余内容,请升级到 CoddyKit PRO。 Stripe Payments & SaaS Billing Systems 课程共包含 4 节课。

「Stripe Subscriptions API 入门」这节课中我会学到什么?

探索周期性支付的核心概念,以及 Stripe Subscriptions API 如何为 SaaS 提供可靠的订阅管理能力。 你通过在浏览器中直接运行的动手代码来练习 Stripe Payments & SaaS Billing Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Stripe Payments & SaaS Billing Systems 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Stripe Payments & SaaS Billing Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Stripe Subscriptions API 入门」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Stripe Payments & SaaS Billing Systems 课中编写并运行代码吗?

能。每节 Stripe Payments & SaaS Billing Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Stripe Subscriptions API 入门
  2. 为方案创建产品与价格
  3. 客户门户与基本账单管理
  4. 处理失败付款与催收
← 返回 Stripe Payments & SaaS Billing Systems