इसे छोड़कर कंटेंट पर जाएं

अपने App में Commerce जोड़ें

अपने app code से customers बनाने, checkouts शुरू करने, features को gate करने, और subscriptions cancel करने के लिए Proyecta SDK का उपयोग करें।

एक बार जब आप Stripe को connect कर लें और कोई product बना लें, तो proyecta.commerce SDK resource के ज़रिए अपने app में commerce को wire करें। चार सबसे सामान्य operations हैं: customer बनाना, checkout शुरू करना, feature access check करना, और subscription cancel करना।

import Proyecta from '@proyecta-ai/sdk';
const proyecta = new Proyecta({ apiKey: process.env.PROYECTA_API_KEY });

एक customer आपके app में billable entity होती है — आमतौर पर कोई user, org, या project। जैसे ही आपके database में संबंधित entity बने, तुरंत एक customer बनाएं, और फिर returned id को store करें ताकि आप checkouts और access checks में उस customer को reference कर सकें।

const customer = await proyecta.commerce.customers.create({
email: 'alice@example.com',
name: 'Alice Liddell',
});
await db.users.update({ id: userId, proyectaCustomerId: customer.id });

आप billing address (line1, line2, city, state, postal_code, country) को creation के समय या customers.update() के ज़रिए भी pass कर सकते हैं।

commerce.checkout() एक Stripe-hosted checkout session बनाता है और customer को redirect करने के लिए एक URL return करता है। Payment के बाद, Stripe customer को आपके success_url पर redirect करता है।

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);

Multiple line_items support किए जाते हैं। Subscriptions के लिए, quantity seat count को दर्शाता है।

किसी customer को premium feature इस्तेमाल करने देने से पहले, यह verify करने के लिए commerce.check() call करें कि उनके पास access है या नहीं:

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 content को gate करने का यही recommended तरीका है। Feature/entitlement model के लिए Products & Features देखें।

await proyecta.commerce.cancel({
customer_id: customer.id,
subscription_id: 'sub_123',
cancellation_timing: 'at_billing_period_end', // or 'immediate'
});

By default, customer का access billing period के अंत तक बना रहता है।

AI से सब कुछ wire करवाएं

Section titled “AI से सब कुछ wire करवाएं”

यह सब आपको खुद लिखने की ज़रूरत नहीं है। कुछ सामान्य prompts:

  • "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."