ผสานระบบ Commerce เข้ากับแอปของฉัน
ใช้ Proyecta SDK เพื่อสร้าง customer เริ่ม checkout ควบคุมการเข้าถึงฟีเจอร์ และยกเลิก subscription จากโค้ดแอปของคุณ
เมื่อเชื่อมต่อ Stripe และสร้าง product แล้ว คุณสามารถผสาน commerce เข้ากับแอปผ่าน SDK resource proyecta.commerce ได้เลย การดำเนินการที่พบบ่อยที่สุดมีสี่อย่าง ได้แก่ สร้าง customer เริ่ม checkout ตรวจสอบสิทธิ์การเข้าถึงฟีเจอร์ และยกเลิก subscription
import Proyecta from '@proyecta-ai/sdk';
const proyecta = new Proyecta({ apiKey: process.env.PROYECTA_API_KEY });1. สร้าง customer
หัวข้อที่มีชื่อว่า “1. สร้าง customer”customer คือ entity ที่ถูกเรียกเก็บเงินในแอปของคุณ — โดยทั่วไปคือ user, org หรือ project สร้าง customer ทันทีที่ entity ที่เกี่ยวข้องถูกสร้างในฐานข้อมูลของคุณ แล้วเก็บ id ที่ได้รับกลับมาไว้ เพื่อใช้อ้างอิง customer ใน checkout และการตรวจสอบสิทธิ์การเข้าถึง
const customer = await proyecta.commerce.customers.create({ email: 'alice@example.com', name: 'Alice Liddell',});
await db.users.update({ id: userId, proyectaCustomerId: customer.id });คุณยังสามารถระบุที่อยู่สำหรับการเรียกเก็บเงิน (line1, line2, city, state, postal_code, country) ได้ตอนสร้าง หรือจะใช้ customers.update() ในภายหลังก็ได้
2. เริ่ม checkout
หัวข้อที่มีชื่อว่า “2. เริ่ม checkout”commerce.checkout() สร้าง checkout session ที่โฮสต์โดย Stripe และคืนค่า URL สำหรับ redirect customer ไปยังหน้าชำระเงิน หลังจากชำระเงินเสร็จ Stripe จะ redirect customer กลับมาที่ success_url ของคุณ
const { url } = await proyecta.commerce.checkout({ customer_id: customer.id, line_items: [{ variant_id: 'var_pro_monthly', quantity: 1 }], success_url: 'https://myapp.com/welcome', cancel_url: 'https://myapp.com/pricing',});
return Response.redirect(url);รองรับ line_items หลายรายการ สำหรับ subscription ค่า quantity แทนจำนวน seat
3. ตรวจสอบสิทธิ์การเข้าถึงฟีเจอร์
หัวข้อที่มีชื่อว่า “3. ตรวจสอบสิทธิ์การเข้าถึงฟีเจอร์”ก่อนอนุญาตให้ customer ใช้ฟีเจอร์ premium ให้เรียก commerce.check() เพื่อยืนยันว่ามีสิทธิ์เข้าถึง
const { has_access } = await proyecta.commerce.check({ customer_id: customer.id, resource_id: 'feat_pro_features',});
if (!has_access) { return Response.json({ error: 'Upgrade required' }, { status: 402 });}นี่คือวิธีที่แนะนำสำหรับการควบคุมการเข้าถึงเนื้อหา premium ดูเพิ่มเติมได้ที่ Products & Features สำหรับโมเดล feature/entitlement
4. ยกเลิก subscription
หัวข้อที่มีชื่อว่า “4. ยกเลิก subscription”await proyecta.commerce.cancel({ customer_id: customer.id, subscription_id: 'sub_123', cancellation_timing: 'at_billing_period_end', // หรือ 'immediate'});โดยค่าเริ่มต้น customer จะยังคงมีสิทธิ์เข้าถึงจนถึงสิ้นสุดรอบการเรียกเก็บเงิน
ให้ AI จัดการทุกอย่างให้
หัวข้อที่มีชื่อว่า “ให้ AI จัดการทุกอย่างให้”คุณไม่จำเป็นต้องเขียนโค้ดนี้ด้วยตัวเอง ตัวอย่าง prompt ที่ใช้ได้:
"Create a Proyecta customer when a user signs up. Store the proyecta_customer_id on the user record.""Build a /pricing page that shows my Proyecta products and starts a checkout when a button is clicked.""Gate the /admin route — only allow users whose Proyecta customer has access to feat_admin.""When a user clicks Cancel Subscription, call proyecta.commerce.cancel with at_billing_period_end."