0Pricing
Stripe Payments & SaaS Billing Systems · 课时

订阅生命周期管理与事件

管理订阅的完整生命周期,包括暂停、恢复、取消和处理续订事件。

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

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

Subscription Lifecycle Overview

Managing subscriptions isn't just about creating them; it's about handling their entire journey! This journey, or lifecycle, includes various states like active, paused, or canceled.

Understanding how to transition between these states programmatically is crucial for building flexible and user-friendly subscription services.

  • Customer Experience: Allows users flexibility.
  • Retention: Pausing can prevent churn.
  • Business Logic: React to changes in subscription status.

Pausing Subscriptions

Sometimes, customers need a break without fully canceling. This is where pausing a subscription comes in handy!

When a subscription is paused, Stripe stops collecting payments for a specified period or until resumed. This can significantly improve customer retention by offering flexibility instead of forcing a full cancellation.

Pausing via Stripe API

You can pause a subscription using the Stripe API by updating the subscription and setting the pause_collection parameter. Here's a quick Java example:

import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Subscription;
import com.stripe.param.SubscriptionUpdateParams;

import java.util.HashMap;
import java.util.Map;

public class PauseSubscription {
  public static void main(String[] args) {
    Stripe.apiKey = "sk_test_YOUR_SECRET_KEY"; // Replace with your actual secret key

    String subscriptionId = "sub_12345"; // Replace with a real subscription ID

    try {
      SubscriptionUpdateParams params = SubscriptionUpdateParams.builder()
          .setPauseCollection(SubscriptionUpdateParams.PauseCollection.builder()
              .setBehavior(SubscriptionUpdateParams.PauseCollection.Behavior.MARK_UNPAID)
              .build())
          .build();

      Subscription subscription = Subscription.retrieve(subscriptionId);
      subscription = subscription.update(params);

      System.out.println("Subscription " + subscription.getId() + " paused. Status: " + subscription.getStatus());
    } catch (StripeException e) {
      System.err.println("Error pausing subscription: " + e.getMessage());
    }
  }
}

Understanding Pause Behavior

In the previous example, we used MARK_UNPAID for pause_collection.behavior. This means any invoices due during the pause period will be marked as unpaid.

Other behaviors include:

  • VOID: Invoices due during the pause are voided.
  • KEEP_AS_DRAFT: Invoices are created but remain in draft status.

Choose the behavior that best fits your business logic for how to handle billing during a pause.

Resuming Subscriptions

After a period of being paused, a customer might want to continue their subscription. Resuming a subscription brings it back to an active state, and billing will recommence according to its original schedule.

This is a straightforward process, often initiated by the customer through a self-service portal or by an administrator.

Resuming via Stripe API

To resume a paused subscription, you simply update it and set the pause_collection parameter to null. This tells Stripe to remove the pause and restart billing.

import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Subscription;
import com.stripe.param.SubscriptionUpdateParams;

public class ResumeSubscription {
  public static void main(String[] args) {
    Stripe.apiKey = "sk_test_YOUR_SECRET_KEY"; // Replace with your actual secret key

    String subscriptionId = "sub_12345"; // Replace with a real subscription ID

    try {
      SubscriptionUpdateParams params = SubscriptionUpdateParams.builder()
          .setPauseCollection(null) // Setting to null removes the pause
          .build();

      Subscription subscription = Subscription.retrieve(subscriptionId);
      subscription = subscription.update(params);

      System.out.println("Subscription " + subscription.getId() + " resumed. Status: " + subscription.getStatus());
    } catch (StripeException e) {
      System.err.println("Error resuming subscription: " + e.getMessage());
    }
  }
}

Cancelling Subscriptions

Eventually, some customers will want to cancel their subscription. Stripe offers two main ways to cancel:

  • Immediately: The subscription ends right away, and prorations might apply.
  • At Period End: The subscription remains active until the end of the current billing cycle, then cancels. This is often preferred for customer experience.

Cancelling via Stripe API

To cancel a subscription, you use the Subscription.cancel() method. You can specify whether to cancel immediately or at the end of the current period.

import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Subscription;
import com.stripe.param.SubscriptionCancelParams;

public class CancelSubscription {
  public static void main(String[] args) {
    Stripe.apiKey = "sk_test_YOUR_SECRET_KEY"; // Replace with your actual secret key

    String subscriptionId = "sub_12345"; // Replace with a real subscription ID

    try {
      // To cancel at the end of the current period:
      SubscriptionCancelParams params = SubscriptionCancelParams.builder()
          .setAtPeriodEnd(true)
          .build();

      Subscription subscription = Subscription.retrieve(subscriptionId);
      subscription = subscription.cancel(params);

      System.out.println("Subscription " + subscription.getId() + " status: " + subscription.getStatus() + ". Cancel at period end: " + subscription.getCancelAtPeriodEnd());

      // To cancel immediately (setAtPeriodEnd(false) or omit):
      // subscription = subscription.cancel();
      // System.out.println("Subscription " + subscription.getId() + " canceled immediately. Status: " + subscription.getStatus());

    } catch (StripeException e) {
      System.err.println("Error canceling subscription: " + e.getMessage());
    }
  }
}

Handling Renewal Events

Subscription renewals are key moments in the lifecycle. Stripe sends various webhook events that notify your application about these occurrences.

  • customer.subscription.updated: Fired when a subscription renews, changes, or is canceled.
  • invoice.payment_succeeded: Indicates a successful payment for a renewal invoice.
  • invoice.payment_failed: Signals a failed payment for a renewal invoice.

By listening to these events, you can update user access, send notifications, and log billing changes.

Quick Check: Subscription States

Which of the following actions can be performed to manage a subscription's lifecycle using Stripe's API?

Recap: Lifecycle Management

In this lesson, we explored how to manage the full lifecycle of a subscription using Stripe's API.

  • You learned to pause subscriptions for flexibility.
  • You saw how to resume paused subscriptions.
  • We covered canceling subscriptions, both immediately and at the end of the billing period.
  • Finally, we touched on the importance of webhook events for monitoring renewals and other critical lifecycle changes.

Mastering these concepts allows you to build robust and user-friendly subscription experiences!

常见问题解答

「订阅生命周期管理与事件」课时是免费的吗?

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

「订阅生命周期管理与事件」这节课中我会学到什么?

管理订阅的完整生命周期,包括暂停、恢复、取消和处理续订事件。 你通过在浏览器中直接运行的动手代码来练习 Stripe Payments & SaaS Billing Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「订阅生命周期管理与事件」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 处理试用期与方案升级
  2. 实现按比例计费与计量计费
  3. 订阅生命周期管理与事件
  4. 优惠券、折扣与促销代码
← 返回 Stripe Payments & SaaS Billing Systems