const express = require('express');
const app = express();
app.use(express.json());
app.post('/webhook/payout', (req, res) => {
const { id, invoice, status, updated_at, status_detail } = req.body;
console.log(`Received payout webhook for ID: ${id}`);
console.log(`Invoice: ${invoice}`);
console.log(`Status: ${status.name} (${status.id})`);
console.log(`Updated at: ${updated_at}`);
// Handle different statuses
switch (status.id) {
case 3: // Paid
console.log('✓ Cash-out paid successfully');
handleSuccessfulPayout(id, invoice);
break;
case 6: // Cancelled
console.log('✗ Cash-out cancelled');
if (status_detail) {
console.log(`Reason: ${status_detail.code} - ${status_detail.detail}`);
}
handleCancelledPayout(id, invoice, status_detail);
break;
case 4: // Failed
console.log('✗ Cash-out failed');
handleFailedPayout(id, invoice);
break;
case 5: // Rejected
console.log('✗ Cash-out rejected');
handleRejectedPayout(id, invoice);
break;
default:
console.log(`Status: ${status.name}`);
}
// Always respond 200 OK
res.status(200).json({ received: true });
});
function handleSuccessfulPayout(id, invoice) {
// Update your database
// Send confirmation email
// Update order status
}
function handleCancelledPayout(id, invoice, statusDetail) {
// Log cancellation reason
// Notify admin
// Update records
}
function handleFailedPayout(id, invoice) {
// Retry logic
// Alert admin
}
function handleRejectedPayout(id, invoice) {
// Review compliance issue
// Contact support if needed
}
app.listen(3000, () => {
console.log('Webhook server listening on port 3000');
});