如何使用 wix-http-functions 为 Stripe 创建 Wix Webhook 端点?



>我在以下部分使用本指南:

"6:确认付款成功"以从 Stripe 管理平台测试 Webhook。

更新:我已经在Wix的包管理器中安装了条纹,快递和正文解析器。这是我在Wix中为http-function.js的最新代码:

import {ok, badRequest, response} from 'wix-http-functions';
import stripe from 'stripe';
import express from 'express';
import wixData from 'wix-data';
import bodyparser from 'body-parser';
const key = require('stripe')('sk_test_XXXX'); //use your own Secret Key
const endpointSecret = 'whsec_XXXXXX'; //stripe webhook signing secret
const app = require('express')();
const bodyParser = require('body-parser');
let responseBody = {
"status": 200,
};
export function post_checkoutUpdate(request) {

//console.log("Running the function"); // this one works
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
const sig = request.headers['stripe-signature'];
console('Inside app.post'); //the code doesn't reach here
let event;
try {
event = key.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
return response.status(400).send(`Webhook Error: ${err.message}`);
}

// Handle the checkout.session.completed event
if (event.type === 'checkout.session.completed') {
const session = event.data.object;
console('Checked'); //the code doesn't reach here too
}
// Return a response to acknowledge receipt of the event
response.json({received: true});
});
app.listen(8000, () => console.log('Running on port 8000'));
return response(responseBody);
}

从 Stripe 管理平台,我向这个 webhook 端点发送了一个测试事件:https://www<myowndomain.com>/_functions/checkoutUpdate

现在,测试网络钩子已成功发送,我可以从服务器接收 HTTP 状态代码:200(正常(到 Stripe。但是,代码无法 app.post 运行,我从Wix站点监视工具收到以下错误:

"jsonPayload":{
"message":"["(node:1) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead."]"
}

任何帮助将不胜感激,谢谢!

您可以将 try-catch 合并到代码中,并打开站点监控以开始记录后端日志。一旦我们得到更详细的响应,我们就可以开始调试正在发生的事情。

你不需要快递,我是这样做的:

import { ok, badRequest, response } from 'wix-http-functions';
import stripe from 'stripe';
import wixData from 'wix-data';
const key = require('stripe')('sk_test_XXXX'); //use your own Secret Key
const endpointSecret = 'whsec_XXXXXX'; //stripe webhook signing secret
let responseBody = {
"status": 200,
};
export async function post_checkoutUpdate(request) {
const sig = request.headers['stripe-signature'];
let event;
const rawBody = await request.body.text()
try {
event = key.webhooks.constructEvent(rawBody, sig, endpointSecret);
} catch (err) {
return response.status = 400
}
// Handle the checkout.session.completed event
if (event.type === 'checkout.session.completed') {
const session = event.data.object;
console('Checked');
}
// Return a response to acknowledge receipt of the event
response.status = 200
return response(responseBody);
}

相关内容

  • 没有找到相关文章

最新更新