Agrega cobros y pagos a tu aplicación de Next.js con Commet.
Instala la Commet Skill para que tu agente integre el SDK actual y valide su trabajo contra el contrato vivo de la API.
npx skills add commet-labs/skills --skill commetpnpm add @commet/node @commet/nextnpm install @commet/node @commet/nextyarn add @commet/node @commet/nextbun add @commet/node @commet/nextCOMMET_API_KEY=ck_sandbox_xxximport { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: process.env.COMMET_API_KEY!,
})customers.create es idempotente — si ya existe un cliente con el mismo id, retorna el registro existente.
'use server'
import { redirect } from 'next/navigation'
import { commet } from '@/lib/commet'
export async function subscribe(customerId: string) {
await commet.customers.create({
email: 'user@example.com',
id: customerId,
})
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
const checkoutUrl = subscription.checkoutUrl
if (checkoutUrl) {
redirect(checkoutUrl)
}
}El cliente es redirigido al checkout para completar el pago. Cuando checkoutUrl es null no hace falta pago y la suscripción ya está activa.
export async function getSubscription(customerId: string) {
const subscription = await commet.subscriptions.getActive({ customerId })
return subscription
}export async function canUseFeature(customerId: string, feature: string) {
const access = await commet.featureAccess.get({ code: feature, customerId })
return access.allowed
}export async function trackApiCall(customerId: string) {
await commet.usage.track({
customerId,
featureCode: 'api_calls',
value: 1,
})
}El uso se agrega y se cobra al final del período.
import { CustomerPortal } from '@commet/next'
export const GET = CustomerPortal({
apiKey: process.env.COMMET_API_KEY!,
getCustomerId: async (req) => {
return 'user_123'
},
})<a href="/api/commet/portal">Gestionar Cobros y Pagos</a>¿Cómo está esta guía?