Stripe Payments & SaaS Billing Systems · บทเรียน

การนำการคำนวณตามสัดส่วนและการเรียกเก็บเงินตามการใช้งานมาใช้

ทำความเข้าใจและใช้การคำนวณตามสัดส่วนสำหรับการเปลี่ยนแปลงกลางรอบ และตั้งค่าการเรียกเก็บเงินตามการใช้งานสำหรับรูปแบบการสมัครสมาชิกที่คิดตามปริมาณใช้

บทเรียน 2 จาก 411 ขั้นตอน

การนำการคำนวณตามสัดส่วนและการเรียกเก็บเงินตามการใช้งานมาใช้ เป็นบทเรียน Stripe Payments & SaaS Billing Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Stripe Payments & SaaS Billing Systems และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Stripe Payments & SaaS Billing Systems มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Flexible Billing: Intro

Welcome to advanced subscription features! Many businesses need more flexible billing than just a fixed monthly or yearly fee.

Today, we'll dive into two powerful concepts: prorations and metered billing. These allow you to handle mid-cycle changes and usage-based pricing with ease.

What are Prorations?

Imagine a customer upgrades their subscription plan halfway through their billing cycle. What happens to the money they already paid for the cheaper plan?

Proration is the process of proportionally adjusting charges when a subscription changes mid-cycle. Stripe automatically calculates this for you.

  • Credit: For the unused portion of the old plan.
  • Charge: For the new plan's usage up to the end of the current cycle.

Stripe's Proration Logic

When you modify a subscription in Stripe (e.g., changing the price or quantity of a subscription item), Stripe automatically calculates the proration.

It creates line items on the customer's invoice: a credit for the unused time on the old plan and a charge for the new plan from the change date to the end of the current billing period.

This ensures fairness and accuracy for both you and your customers.

Implementing Prorated Changes

When updating a subscription via the Stripe API, prorations are typically handled by default. You can also explicitly control this behavior using the proration_behavior parameter.

CREATE_PRORATIONS ensures that proration items are added to the invoice immediately or at the end of the cycle.

Try running this conceptual Java code:

import com.stripe.Stripe;
import com.stripe.param.SubscriptionUpdateParams;

public class Main {
  public static void main(String[] args) {
    // In a real app, you'd set Stripe.apiKey and handle exceptions.
    // This snippet focuses on the update parameters.

    String subscriptionItemId = "si_EXISTING_ITEM_ID";
    String newPriceId = "price_NEW_PLAN_ID";

    SubscriptionUpdateParams params =
      SubscriptionUpdateParams.builder()
        .addItem(
          SubscriptionUpdateParams.Item.builder()
            .setId(subscriptionItemId) // ID of the existing subscription item
            .setPrice(newPriceId)     // The new price for the subscription
            .build()
        )
        .setProrationBehavior(SubscriptionUpdateParams.ProrationBehavior.CREATE_PRORATIONS)
        .build();

    System.out.println("Subscription update parameters created:");
    System.out.println("- New Price ID: " + newPriceId);
    System.out.println("- Proration Behavior: CREATE_PRORATIONS");
    System.out.println("Stripe will automatically calculate and apply prorations.");
  }
}

What is Metered Billing?

Metered billing (or usage-based billing) charges customers based on how much of a service they actually use, rather than a fixed amount.

Think of utility bills (electricity, water) or cloud services (data storage, API calls). Customers only pay for what they consume.

  • Examples: API requests, GB of storage, minutes of video streaming, number of active users.

Setting Up Metered Products

To implement metered billing, you first need to define a Product and a Price in Stripe with a usage_type set to metered.

This tells Stripe that the quantity for this price will be reported by your application, not fixed at subscription creation.

You'll also define the billing scheme (e.g., per_unit or tiered) and optional aggregation method.

Reporting Usage to Stripe

For metered billing, your application needs to track customer usage and report it to Stripe periodically, usually as it occurs or at regular intervals (e.g., daily).

This is done using the Usage Record API. You report the quantity of usage for a specific subscription_item.

Here's a conceptual Java example:

import com.stripe.Stripe;
import com.stripe.param.UsageRecordCreateParams;

public class Main {
  public static void main(String[] args) {
    // In a real app, you'd set Stripe.apiKey and handle exceptions.
    // This snippet focuses on creating a usage record.

    String subscriptionItemId = "si_METERED_ITEM_ID"; // The ID of the metered subscription item
    long quantityUsed = 10L; // The amount of usage to report

    UsageRecordCreateParams params =
      UsageRecordCreateParams.builder()
        .setQuantity(quantityUsed)
        .setTimestamp(System.currentTimeMillis() / 1000L) // Current time in seconds since epoch
        .setAction(UsageRecordCreateParams.Action.INCREMENT) // Add to existing usage
        .build();

    System.out.println("Usage record creation parameters created:");
    System.out.println("- Subscription Item ID: " + subscriptionItemId);
    System.out.println("- Quantity Reported: " + quantityUsed);
    System.out.println("- Action: INCREMENT (adds to previous usage)");
    System.out.println("This usage will be billed at the end of the current billing cycle.");
  }
}

How Metered Usage is Billed

Stripe aggregates all usage records reported for a specific metered subscription item within a billing period.

At the end of the billing cycle (or when an invoice is finalized), Stripe calculates the total usage and applies the corresponding price, adding it to the customer's invoice.

You can define how usage is aggregated (e.g., sum, last_ever, max) when creating the price.

Prorations vs. Metered: Recap

It's important to distinguish between prorations and metered billing:

  • Prorations: Adjust charges for changes to fixed-price subscription items mid-cycle (e.g., upgrading from Basic to Pro plan).
  • Metered Billing: Charges based on actual consumption of a service, with usage reported over time (e.g., paying per API call or GB of data).

Both offer powerful ways to make your billing more flexible and customer-friendly.

Quick Check: Flexible Billing

Which of the following scenarios would typically involve metered billing in Stripe?

Recap: Prorations & Metered

Great job! You've learned how to handle flexible billing scenarios with Stripe:

  • Prorations: Automatically adjust charges when subscriptions change mid-cycle, ensuring fair billing.
  • Metered Billing: Charge customers based on their actual consumption, requiring you to report usage via the Stripe API.

These features are crucial for building dynamic and scalable SaaS platforms. Keep exploring to master more advanced Stripe capabilities!

เริ่มต้นได้ฟรี

เรียนรู้ Stripe Payments & SaaS Billing Systems ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การนำการคำนวณตามสัดส่วนและการเรียกเก็บเงินตามการใช้งานมาใช้” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การนำการคำนวณตามสัดส่วนและการเรียกเก็บเงินตามการใช้งานมาใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Stripe Payments & SaaS Billing Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Stripe Payments & SaaS Billing Systems มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การนำการคำนวณตามสัดส่วนและการเรียกเก็บเงินตามการใช้งานมาใช้”

ทำความเข้าใจและใช้การคำนวณตามสัดส่วนสำหรับการเปลี่ยนแปลงกลางรอบ และตั้งค่าการเรียกเก็บเงินตามการใช้งานสำหรับรูปแบบการสมัครสมาชิกที่คิดตามปริมาณใช้ คุณปฏิบัติ Stripe Payments & SaaS Billing Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Stripe Payments & SaaS Billing Systems หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Stripe Payments & SaaS Billing Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การนำการคำนวณตามสัดส่วนและการเรียกเก็บเงินตามการใช้งานมาใช้” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Stripe Payments & SaaS Billing Systems นี้ได้ไหม

ได้ บทเรียน Stripe Payments & SaaS Billing Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจัดการช่วงทดลองใช้และการอัปเกรดแผน
  2. การนำการคำนวณตามสัดส่วนและการเรียกเก็บเงินตามการใช้งานมาใช้
  3. การจัดการวงจรชีวิตและเหตุการณ์ของการสมัครสมาชิก
  4. คูปอง ส่วนลด และรหัสส่งเสริมการขาย
← กลับไปที่ Stripe Payments & SaaS Billing Systems