การผสานรวม Payment Intents API
เรียนรู้การใช้ Payment Intents API สำหรับขั้นตอนการชำระเงินแบบไดนามิก พร้อมจัดการธุรกรรมในสถานการณ์ต่าง ๆ อย่างปลอดภัยและมีประสิทธิภาพ
การผสานรวม Payment Intents API เป็นบทเรียน Stripe Payments & SaaS Billing Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Stripe Payments & SaaS Billing Systems และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Stripe Payments & SaaS Billing Systems มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Payment Intents?
Welcome! Today, we're diving into Stripe's Payment Intents API. This is a powerful tool for managing complex payment flows.
Before Payment Intents, simple charges worked for basic payments. But what about payments that need extra steps, like 3D Secure authentication?
Payment Intents solve this by representing your intention to collect a payment, allowing for dynamic, multi-step processes.
The Payment Intent Object
A Payment Intent is an object that tracks the lifecycle of a customer's payment attempt. It guides you through the necessary steps to complete a payment.
- Created: The intent is ready to start.
- Requires Action: Customer needs to authenticate (e.g., 3D Secure).
- Succeeded: Payment is complete.
- Canceled: The payment attempt was abandoned.
This object helps manage everything from initial creation to final confirmation.
Server-Side: Creating an Intent
The first step is always to create a Payment Intent on your server. This ensures sensitive details like the amount and currency are controlled server-side.
You specify the amount, currency, and payment method types you'll accept (e.g., 'card'). Stripe then returns a PaymentIntent object, which includes a client_secret.
Try running this simplified server-side example (Java):
import java.util.List;
import java.util.Map;
// This is a simplified example to illustrate server-side intent creation.
// In a real application, this would involve calling Stripe's API.
public class Main {
public static void main(String[] args) {
long amount = 1000L; // Example: $10.00 in cents
String currency = "usd";
String paymentMethodType = "card";
System.out.println("--- Server-Side Payment Intent Creation ---");
System.out.println("Creating Payment Intent for: " + amount + " " + currency);
System.out.println("Payment method type: " + paymentMethodType);
System.out.println("\nStripe API call (conceptual, using Java client library):\n");
System.out.println(" PaymentIntent.create(Map.of(");
System.out.println(" \"amount\", " + amount + "L,");
System.out.println(" \"currency\", \"" + currency + "\",");
System.out.println(" \"payment_method_types\", List.of(\"" + paymentMethodType + "\")");
System.out.println(" ));");
System.out.println("\nStripe would return an object including:\n");
System.out.println(" \"id\": \"pi_mockPaymentIntent123\",");
System.out.println(" \"client_secret\": \"pi_mockPaymentIntent123_secret_mockSecretString\"");
System.out.println("\nThe client_secret is then sent securely to the client-side.");
}
}Client-Side: Confirming the Payment
Once your server sends the client_secret to your frontend, you use Stripe.js to confirm the Payment Intent. This is where the customer interacts with the payment form.
Stripe.js handles the collection of payment details and any required authentication (like 3D Secure) automatically, using the client_secret to link to the server-created intent.
- 1. Collect details: Customer enters card info.
- 2. Call
stripe.confirmCardPayment(): Pass theclient_secretand card details. - 3. Handle result: Stripe returns the final status.
Understanding Payment Statuses
The status field of a Payment Intent is crucial for understanding its state. Here are the common ones:
requires_payment_method: Needs payment details.requires_confirmation: Ready to be confirmed.requires_action: Customer needs to complete an action (e.g., 3D Secure).processing: Payment is being processed.succeeded: Payment was successful!canceled: Payment was canceled.
Your application logic should react to these statuses.
Handling 3D Secure & SCA
Strong Customer Authentication (SCA), often involving 3D Secure, is a regulatory requirement in Europe for many online card payments. Payment Intents are designed to handle this seamlessly.
If a payment requires SCA, the Payment Intent's status will change to requires_action. Stripe.js will then automatically prompt the customer to complete the authentication step (e.g., redirect to their bank's page).
Your client-side confirmation call will then complete once authentication is done.
Securing the client_secret
The client_secret is a unique key that ties a client-side action to a specific Payment Intent on your server. It's safe to expose on the client, but only for a limited time.
- Generate on server: Always create the Payment Intent and its
client_secreton your backend. - Pass securely: Send the
client_secretto your frontend (e.g., via a secure API endpoint). - Use promptly: Use it immediately on the client to confirm the payment.
Never generate or store the client_secret directly on the client side.
Retrieving & Updating Intents
You can retrieve a Payment Intent at any time using its ID from your server-side code. This is useful for checking its current status or details.
You can also update a Payment Intent (e.g., change the amount or currency) as long as it hasn't been confirmed yet. This offers flexibility for dynamic pricing or order changes before final payment.
However, once confirmed, a Payment Intent cannot be modified.
Payment Method Attachment
When a Payment Intent succeeds, Stripe can optionally save the customer's payment method for future use. This is done by attaching the payment method to a Customer object.
This is useful for recurring payments or one-click checkouts. You'd set the setup_future_usage parameter when creating the Payment Intent, or explicitly create a Setup Intent (a topic for another lesson!).
Always ensure you have customer consent to save payment details.
Quick Check: Payment Intents
You've learned about the Payment Intents API. Let's test your understanding!
Recap: Mastering Payment Intents
Great job! You've learned the fundamentals of Stripe's Payment Intents API.
- Payment Intents manage payment lifecycles, especially for multi-step or authenticated payments.
- You create them server-side, get a
client_secret, and confirm them client-side with Stripe.js. - Statuses like
requires_actionhelp you handle dynamic flows like 3D Secure.
This API is essential for building robust and compliant payment experiences. Keep practicing!
คำถามที่พบบ่อย
บทเรียน “การผสานรวม Payment Intents API” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผสานรวม Payment Intents API” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Stripe Payments & SaaS Billing Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Stripe Payments & SaaS Billing Systems มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวม Payment Intents API”
เรียนรู้การใช้ Payment Intents API สำหรับขั้นตอนการชำระเงินแบบไดนามิก พร้อมจัดการธุรกรรมในสถานการณ์ต่าง ๆ อย่างปลอดภัยและมีประสิทธิภาพ คุณปฏิบัติ Stripe Payments & SaaS Billing Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Stripe Payments & SaaS Billing Systems หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Stripe Payments & SaaS Billing Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การผสานรวม Payment Intents API” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Stripe Payments & SaaS Billing Systems นี้ได้ไหม
ได้ บทเรียน Stripe Payments & SaaS Billing Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผสานรวม Payment Intents API
- การจัดการ Webhook สำหรับเหตุการณ์แบบอะซิงโครนัส
- การจัดการเงินคืนและข้อพิพาทอย่างมีประสิทธิภาพ
- การนำการทำงานซ้ำได้อย่างปลอดภัยไปใช้กับ API การชำระเงินที่เชื่อถือได้