Ga naar inhoud

Commerce Integreren in Mijn App

Gebruik de Proyecta SDK om klanten aan te maken, checkouts te starten, functies af te schermen en abonnementen op te zeggen vanuit je app-code.

Zodra je Stripe hebt verbonden en een product hebt aangemaakt, koppel je commerce aan je app via de proyecta.commerce SDK-resource. De vier meest voorkomende handelingen zijn: een klant aanmaken, een checkout starten, toegang tot functies controleren en een abonnement opzeggen.

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

Een klant is de factureerbare entiteit in je app — meestal een gebruiker, organisatie of project. Maak er een aan zodra de bijbehorende entiteit in je database wordt aangemaakt, en sla het teruggegeven id op zodat je de klant kunt raadplegen bij checkouts en toegangscontroles.

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

Je kunt ook een factuuradres meegeven (line1, line2, city, state, postal_code, country) bij het aanmaken of via customers.update().

commerce.checkout() maakt een door Stripe gehoste checkout-sessie aan en geeft een URL terug om de klant naartoe door te sturen. Na betaling stuurt Stripe de klant door naar jouw 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);

Meerdere line_items worden ondersteund. Voor abonnementen staat quantity voor het aantal licenties.

Voordat je een klant een premiumfunctie laat gebruiken, roep je commerce.check() aan om te verifiëren of ze toegang hebben:

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

Dit is de aanbevolen manier om premiumcontent af te schermen. Zie Producten & Functies voor het functie/rechtmodel.

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

Standaard behoudt de klant toegang tot het einde van de factureringsperiode.

Je hoeft dit niet met de hand te schrijven. Veelgebruikte 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."