@groundbrick/payment-service
v1.0.1
Published
Serviço de pagamento plugável com suporte a Stripe
Maintainers
Readme
@groundbrick/payment-service
Serviço de pagamento plugável com suporte a Stripe e provider mock para testes.
Instalação
pnpm add @groundbrick/payment-serviceUso básico
import { PaymentService } from '@groundbrick/payment-service';
const paymentService = new PaymentService({
provider: 'stripe',
config: {
secretKey: process.env.STRIPE_SECRET_KEY!,
webhookSecret: process.env.STRIPE_WEBHOOK_SECRET!
}
});
// 1. Sincronizar produto
const product = await paymentService.syncProduct({
id: 'agendapet_pro',
name: 'AgendaPet Pro',
description: 'Plano profissional para petshops'
});
// 2. Sincronizar preços
const monthlyPrice = await paymentService.syncPrice({
id: 'agendapet_pro_monthly',
productId: product.id,
currency: 'eur',
unitAmount: 1990,
recurring: { interval: 'month' }
});
// 3. Criar checkout session
const session = await paymentService.createCheckoutSession({
customerEmail: '[email protected]',
priceId: monthlyPrice.id,
successUrl: 'https://app.agendapet.pt/subscription/success',
cancelUrl: 'https://app.agendapet.pt/subscription/cancel',
metadata: { petshop_id: '123', plan_code: 'pro' }
});
// 4. Redirecionar cliente
// redirect(session.url);Webhooks (Express)
app.post('/webhooks/stripe', express.raw({ type: 'application/json' }), (req, res) => {
const result = paymentService.validateWebhook(
req.body,
req.headers['stripe-signature'] as string
);
if (!result.isValid) {
return res.status(400).send(result.error);
}
const event = result.event!;
if (event.type === 'checkout.session.completed') {
const { customerId, subscriptionId, metadata } = event.data.checkoutSession!;
// Ativar subscription do petshop_id no metadata
}
res.sendStatus(200);
});Providers
- Stripe: integração real com Stripe Checkout e webhooks.
- Mock: provider in-memory para testes e desenvolvimento.
Licença
MIT
