我正在尝试创建一个 webhook 来监听 paypal 结帐购买。
当用户购买某些商品时,贝宝应该将数据发送到我的服务器(我需要的只是一封电子邮件)
我使用express.js创建了一个nodejs webhook POST请求,它确实多次仅从webhook模拟器接收数据,即使我只单击它一次(https://developer.paypal.com/dashboard/webhooksSimulator)
这是我的源代码:
app.post('/paypal-webhook', (req, res) => {
console.log('Webhook event received')
const rawBody = JSON.stringify(req.body);
console.log(rawBody)
const signature = req.headers['paypal-transmission-sig'];
console.log(req.body)
const verified = crypto.createHmac('sha256', paypalWebhookSecret)
.update(rawBody)
.digest('hex');
if (signature === verified) {
const event = req.body;
// Check if the event is a payment sale completed event
if (event.event_type === 'PAYMENT.SALE.COMPLETED') {
console.log('Payment sale completed:', event);
// Your course fulfillment logic here
}
res.status(200).send('Webhook received successfully.');
} else {
console.error('Webhook verification failed.');
res.status(403).send('Unauthorized');
}
});
我遇到的问题是,在 paypal 结账时,当用户购买东西时,它不会向我的 webhook 发送任何内容。它仅从 webhooks 模拟器发送数据。
您使用什么集成方式进行 PayPal Checkout?您是否订阅了developer.paypal.com/dashboard/applications 中同一应用程序/客户端 ID 的所有事件?
PAYMENT.SALE.COMPLETED 用于订阅付款。根据您的集成,事件名称将是其他名称。不过,如果这是基本的 v2/结账/订单集成,则您不应该让代码依赖于 Webhooks(而是使用即时服务器端捕获 API 响应)