Agrega cobros y pagos a tu aplicación de Express.
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 expressnpm install @commet/node expressyarn add @commet/node expressCOMMET_API_KEY=ck_sandbox_xxximport { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: process.env.COMMET_API_KEY!,
})import { Router } from 'express'
import { commet } from '../commet'
const router = Router()
router.post('/subscribe', async (req, res) => {
const { customerId, email } = req.body
await commet.customers.create({ email, id: customerId })
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
res.json({ checkoutUrl: subscription.checkoutUrl ?? null })
})
export default routerrouter.get('/subscription/:customerId', async (req, res) => {
const subscription = await commet.subscriptions.getActive({ customerId: req.params.customerId })
if (!subscription) {
return res.status(404).json({ error: 'no_active_subscription' })
}
res.json({ status: subscription.status })
})
router.get('/features/:feature/:customerId', async (req, res) => {
const access = await commet.featureAccess.get({
code: req.params.feature,
customerId: req.params.customerId,
})
res.json({ allowed: access.allowed })
})router.post('/usage', async (req, res) => {
await commet.usage.track({
customerId: req.body.customerId,
featureCode: 'api_calls',
value: 1,
})
res.json({ tracked: true })
})El uso se agrega y se cobra al final del período.
router.get('/portal', async (req, res) => {
const portal = await commet.portal.getUrl({
customerId: req.user.customerId,
})
res.redirect(portal.portalUrl)
})import express from 'express'
import billingRoutes from './routes/billing'
const app = express()
app.use(express.json())
app.use('/billing', billingRoutes)
app.listen(3000)¿Cómo está esta guía?