Skip to content

Webhook de Sub-conta

Configura webhook individual para uma sub-conta.

PUT /api/customer/{id}/webhook

Configura webhook para receber notificações de transações da sub-conta.

Endpoint

PUT https://api.divipay.com.br/api/customer/{id}/webhook

Headers

HeaderValorObrigatório
AuthorizationBearerSim
Content-Typeapplication/jsonSim

Path Parameters

ParâmetroTipoDescrição
idstringID da sub-conta

Body Parameters

CampoTipoDescriçãoObrigatório
urlstringURL para receber webhooksSim

Exemplo de Requisição

bash
curl -X PUT https://api.divipay.com.br/api/customer/sub_abc123/webhook \
  -H "Authorization: Bearer SEU_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://vendedor.com/webhook"
  }'
javascript
const response = await fetch(
  'https://api.divipay.com.br/api/customer/sub_abc123/webhook',
  {
    method: 'PUT',
    headers: {
      'Authorization': 'Bearer SEU_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      url: 'https://vendedor.com/webhook'
    })
  }
);

const result = await response.json();

Resposta de Sucesso

Status: 201 Created

json
{
  "id": "sub_abc123"
}

PUT /api/customer/webhook

Configura webhook da conta principal.

Endpoint

PUT https://api.divipay.com.br/api/customer/webhook

Exemplo

bash
curl -X PUT https://api.divipay.com.br/api/customer/webhook \
  -H "Authorization: Bearer SEU_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://plataforma.com/webhook"
  }'

Eventos Recebidos

Pagamento Confirmado

json
{
  "event": "charge.paid",
  "chargeId": "cob_abc123",
  "referenceId": "pedido-123",
  "amount": 100.00,
  "receivedAmount": 90.00,
  "paidAt": "2024-11-04T15:30:00.000Z",
  "customerId": "sub_abc123"
}

Saque Realizado

json
{
  "event": "withdraw.completed",
  "withdrawId": "wtd_xyz789",
  "customerId": "sub_abc123",
  "amount": 500.00,
  "completedAt": "2024-11-04T16:00:00.000Z"
}

Exemplo de Implementação

javascript
const express = require('express');
const app = express();

app.use(express.json());

// Webhook da sub-conta
app.post('/webhook/seller/:sellerId', async (req, res) => {
  const { sellerId } = req.params;
  const event = req.body;

  console.log(`Webhook recebido para vendedor ${sellerId}:`, event.event);

  try {
    switch (event.event) {
      case 'charge.paid':
        await handleSellerPayment(sellerId, event);
        break;

      case 'withdraw.completed':
        await handleSellerWithdraw(sellerId, event);
        break;

      default:
        console.log('Evento desconhecido:', event.event);
    }

    res.status(200).send('OK');

  } catch (error) {
    console.error('Erro ao processar webhook:', error);
    res.status(500).send('Error');
  }
});

async function handleSellerPayment(sellerId, event) {
  // Atualizar saldo do vendedor
  await db.sellers.update({
    where: { id: sellerId },
    data: {
      balance: {
        increment: event.receivedAmount
      }
    }
  });

  // Notificar vendedor
  await sendNotification(sellerId, {
    title: 'Pagamento Recebido',
    message: `Você recebeu R$ ${event.receivedAmount.toFixed(2)}`
  });
}

async function handleSellerWithdraw(sellerId, event) {
  // Registrar saque
  await db.withdraws.create({
    data: {
      sellerId,
      amount: event.amount,
      status: 'COMPLETED',
      completedAt: event.completedAt
    }
  });

  // Notificar vendedor
  await sendNotification(sellerId, {
    title: 'Saque Realizado',
    message: `Saque de R$ ${event.amount.toFixed(2)} foi concluído`
  });
}

app.listen(3000);

Próximos Passos

Documentação da API DiviPay