AI Powered SaaS: Stripe + Auth + Billing + Deploy · บทเรียน

การสร้างผลิตภัณฑ์และราคา

เรียนรู้การกำหนดและจัดการผลิตภัณฑ์กับรูปแบบราคาที่เกี่ยวข้องในแดชบอร์ด Stripe และผ่าน API

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

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

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

What are Stripe Products?

In Stripe, a Product represents what you sell. This could be a physical good, a service, or, in our case, a feature or access level for your SaaS application.

A Price defines how much a product costs, in what currency, and whether it's a one-time payment or recurring subscription. Together, they form the foundation for processing payments.

Creating Products via Dashboard

For simple setups, you can define products directly in the Stripe Dashboard. This is great for getting started quickly and understanding the concepts.

  • Navigate to Products in your Stripe Dashboard.
  • Click + Add product.
  • Enter a Name (e.g., "Premium AI Access") and optionally a Description and Image.

Attaching Prices via Dashboard

Once a product is created, you need to add a price to it. A product can have multiple prices.

  • On the product details page, scroll down to the Pricing section.
  • Click + Add another price.
  • Specify the Amount, Currency (e.g., USD), and set Billing period to "One-time" for this lesson.

Automating with the Stripe API

While the dashboard is useful, a real SaaS application needs to create and manage products and prices programmatically. This allows for:

  • Automation: Create products on the fly.
  • Dynamic Pricing: Adjust prices based on user segments or promotions.
  • Integration: Link directly with your application's backend logic.

API Setup: Keys & Client

To interact with Stripe's API, you need your secret API key and the official Stripe client library. We'll use Python for our examples.

Ensure you have your STRIPE_SECRET_KEY from the Stripe Dashboard (Developers > API keys).

import stripe
import os

# In a real app, use environment variables!
# For this demo, replace 'sk_test_YOUR_KEY' with your actual secret key
stripe.api_key = os.getenv("STRIPE_SECRET_KEY", "sk_test_YOUR_KEY")

def main():
    if stripe.api_key == "sk_test_YOUR_KEY":
        print("Stripe API key is a placeholder. Update it!")
    else:
        print("Stripe API client initialized!")

if __name__ == "__main__":
    main()

API: Create Your First Product

Now, let's create a product using the Stripe API. We'll specify its name, description, and set it as active.

Run this code to see a new product appear in your Stripe Dashboard!

import stripe
import os

stripe.api_key = os.getenv("STRIPE_SECRET_KEY", "sk_test_YOUR_KEY")

def create_product(name, description):
    try:
        product = stripe.Product.create(
            name=name,
            description=description,
            active=True
        )
        print(f"Product created! ID: {product.id}")
        print(f"Name: {product.name}")
        return product
    except stripe.error.StripeError as e:
        print(f"Error creating product: {e}")
        return None

def main():
    print("Attempting to create a sample product...")
    my_product = create_product(
        "Pro AI Feature Pack",
        "Unlock advanced AI capabilities for your users." 
    )
    if my_product:
        print("Product creation successful!")
    else:
        print("Product creation failed.")

if __name__ == "__main__":
    main()

API: Attach a Price to Product

After creating a product, we need to define a price for it. For one-time payments, we specify the unit_amount in cents and set recurring=None.

The price must be linked to an existing product using its ID.

import stripe
import os

stripe.api_key = os.getenv("STRIPE_SECRET_KEY", "sk_test_YOUR_KEY")

# Helper to create product and get its ID
def create_product_for_price(name, description):
    try:
        product = stripe.Product.create(
            name=name,
            description=description,
            active=True
        )
        return product.id
    except stripe.error.StripeError as e:
        print(f"Error creating product for price: {e}")
        return None

def create_one_time_price(product_id, amount_in_cents, currency="usd"):
    try:
        price = stripe.Price.create(
            product=product_id,
            unit_amount=amount_in_cents, 
            currency=currency,
            recurring=None, # This specifies a one-time payment
        )
        print(f"Price created! ID: {price.id}")
        print(f"Amount: {price.unit_amount / 100} {price.currency.upper()}")
        return price
    except stripe.error.StripeError as e:
        print(f"Error creating price: {e}")
        return None

def main():
    product_id = create_product_for_price(
        "Ultimate AI Access",
        "Access to all AI features, forever."
    )
    if product_id:
        print(f"Product ID for price: {product_id}")
        print("Creating a one-time price for the product...")
        my_price = create_one_time_price(product_id, 4999) # $49.99
        if my_price:
            print("Price creation successful!")
        else:
            print("Price creation failed.")

if __name__ == "__main__":
    main()

Key Price Parameters

When creating a price via API, these parameters are crucial:

  • product: The ID of the product this price belongs to.
  • unit_amount: The price amount in the smallest currency unit (e.g., cents for USD).
  • currency: The three-letter ISO currency code (e.g., 'usd', 'eur').
  • recurring: Set to None for one-time payments. For subscriptions, this would be an object with details like interval.
  • lookup_key (optional): A custom string you can use to retrieve prices without knowing their Stripe ID.

Retrieving & Updating

You can retrieve products and prices by their IDs using stripe.Product.retrieve(product_id) and stripe.Price.retrieve(price_id).

Important: Stripe Prices are immutable. Once created, you cannot change their amount or currency. If you need to change a price, you must create a new one and deactivate the old one.

Products, however, can be updated (e.g., their name or description).

Product & Price Check

Let's check your understanding of creating products and prices in Stripe.

Recap: Products & Prices

Great job! You've learned how to define and manage products and their associated one-time prices in Stripe.

  • Products represent what you sell.
  • Prices define how much something costs.
  • You can manage them via the Stripe Dashboard or programmatically via the API.
  • Key API parameters include product ID, unit_amount (in cents), currency, and setting recurring=None for one-time charges.
  • Remember that Stripe Prices are immutable!

Next, we'll use these products and prices to implement Stripe Checkout sessions.

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

เรียนรู้ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วย AI tutor — ฟรี

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

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

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

บทเรียน “การสร้างผลิตภัณฑ์และราคา” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การสร้างผลิตภัณฑ์และราคา”

เรียนรู้การกำหนดและจัดการผลิตภัณฑ์กับรูปแบบราคาที่เกี่ยวข้องในแดชบอร์ด Stripe และผ่าน API คุณปฏิบัติ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy หรือไม่

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

บทเรียน “การสร้างผลิตภัณฑ์และราคา” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy นี้ได้ไหม

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

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

  1. บัญชี Stripe และคีย์ API
  2. การสร้างผลิตภัณฑ์และราคา
  3. การนำเซสชันชำระเงินมาใช้
  4. การจัดการเงินคืนและข้อพิพาท
← กลับไปที่ AI Powered SaaS: Stripe + Auth + Billing + Deploy