1. Criar cliente
curl -X POST https://api.billing.upag.io/v1/customers \
-H "Authorization: Bearer sk_test_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john.doe@example.com",
"phone": "+5511999999999",
"taxId": "12345678900"
}'
import { Upag } from 'upag';
const upag = new Upag('sk_test_your_api_key');
const customer = await upag.customers.create({
name: 'John Doe',
email: 'john.doe@example.com',
phone: '+5511999999999',
taxId: '12345678900',
});
2. Cadastrar cartão
curl -X POST https://api.billing.upag.io/v1/customers/cus_ahwDXrgYvur89iPs/payment-methods \
-H "Authorization: Bearer sk_test_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"type": "credit_card",
"card": {
"number": "4242424242424242",
"expiryMonth": "12",
"expiryYear": "2032",
"cvv": "123",
"holderName": "JOHN DOE"
}
}'
const paymentMethod = await upag.paymentMethods.create(customer.id, {
type: 'credit_card',
card: {
number: '4242424242424242',
expiryMonth: '12',
expiryYear: '2032',
cvv: '123',
holderName: 'JOHN DOE',
},
});
3. Criar assinatura
curl -X POST https://api.billing.upag.io/v1/subscriptions \
-H "Authorization: Bearer sk_test_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"customer": "cus_ahwDXrgYvur89iPs",
"paymentMethod": "pm_abc123xyz",
"items": [{ "price": "price_def456ghi", "quantity": 1 }]
}'
const subscription = await upag.subscriptions.create({
customer: customer.id,
paymentMethod: paymentMethod.id,
items: [{ price: 'price_def456ghi', quantity: 1 }],
});
console.log(subscription.id);
4. Próxima cobrança
curl -G https://api.billing.upag.io/v1/invoices/upcoming \
-H "Authorization: Bearer sk_test_your_api_key" \
-d "subscription=sub_abc123xyz"
const preview = await upag.invoices.upcoming({
subscription: subscription.id,
});
console.log('Próximo valor (centavos):', preview.amountDue);
5. Webhooks
Escute renovações e falhas de cobrança:app.post('/webhooks/billing', express.json(), (req, res) => {
const { event, data } = req.body;
if (event === 'invoice.paid') {
console.log('Fatura paga:', data.id, 'subscription', data.subscriptionId);
}
if (event === 'subscription.past_due') {
console.log('Assinatura em atraso:', data.id);
}
res.sendStatus(200);
});
subscription.*, invoice.paid — ver Webhooks.