我正在try 在我的应用程序中将条纹订阅计费与分级定价模式集成在一起,根据我的理解,我需要做两件事:
- 允许我的新用户还创建条带客户帐户(通过集成)
- 在客户订阅付款处于活动状态时,监控条带WebHook‘Events’以提供访问权限
我的用户流如下:
- 在我的应用程序中创建配置文件(保存到数据库)-&>;重定向到条带 checkout 门户以获取账单信息(保存到条带数据库)->;attempt将条带客户ID保存到我的数据库,以便我可以监控订阅状态
然而,我不知道如何在我的应用程序中保存CustomerID信息,因为req.user
和req.params
是空的,因为用户从条带计费门户发送back到我的应用程序
Controller function个
module.exports.stripeWebhook = async (req, res) => {
let data;
const webhookSecret = stripeWebhookSecret;
if (webhookSecret) {
let event;
let signature = req.headers["stripe-signature"];
try {
event = stripe.webhooks.constructEvent(
req.body,
signature,
webhookSecret
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`);
return res.sendStatus(400);
}
data = event.data;
eventType = event.type;
} else {
// retrieve the event data directly from the request body.
data = req.body.data;
eventType = req.body.type;
}
switch (eventType) {
case 'payment_intent.succeeded': {
console.log('PaymentIntent was successful!');
break;
}
case 'checkout.session.completed':
// Payment is successful and the subscription is created.
// You should provision the subscription and save the customer ID to your database.
console.log(data.object.customer); <- works
const user = await User.findById(req.user.id); <- comes back empty so my next two lines of code don't work
user.stripeId.push(data.object.customer);
await user.save();
break;
default:
}
res.sendStatus(200);
};
App.js个
app.use(bodyParser.raw({type: "application/json"}));
app.use(express.json({ limit: '1mb' }));
app.use(express.urlencoded({ extended: true }));
我之所以包含app.js代码,是因为bodyparser.raw
对我的控制器函数中主体的实现方式有影响.
我指望req.user或req.pars在我的数据库中找到该用户,但它不起作用.如何像条带注释所建议的那样将客户ID保存到我的数据库中?