import express from 'express';
import crypto from 'crypto';
const app = express();
app.use(express.json());
app.post('/api/paygate-webhook', async (req, res) => {
const payload = req.body;
console.log('Webhook received:', payload.type);
switch (payload.type) {
case 'payment.completed':
await handlePaymentCompleted(payload);
break;
case 'feature.unlocked':
await handleFeatureUnlocked(payload);
break;
case 'product.created':
case 'product.updated':
case 'product.deleted':
await handleProductChange(payload);
break;
}
// Always respond with 200 to acknowledge receipt
res.json({ received: true });
});
async function handlePaymentCompleted(payload) {
const { productId, walletAddress, txHash, amount } = payload;
// Grant access in your database
await db.access.create({
productId,
walletAddress,
txHash,
grantedAt: new Date()
});
// Send confirmation email/notification
await notifyUser(walletAddress, `Payment of ${amount} USDC confirmed!`);
}
async function handleFeatureUnlocked(payload) {
const { accessToken, featureIds, walletAddress } = payload;
// Store access token for user
await db.users.update({
where: { wallet: walletAddress },
data: {
accessToken,
features: featureIds,
upgradedAt: new Date()
}
});
}
async function handleProductChange(payload) {
// Sync product data with your system
console.log(`Product ${payload.productId} ${payload.type.split('.')[1]}`);
}