我想知道是否存在某种“约定”或其他将 Stripe 支付数据存储在数据库中的最佳实践。网上确实缺乏信息,因为我是自己做的,所以我想从其他人那里得到一些反馈。
我的网站的基本设置是有一个商店有多种产品。还有一个订阅选项,每月接收这些产品。
所以我设置了两条邮路。一种用于购买产品,一种用于订阅。这些使用 Stripe API。对于订单,我使用 stripe.charge
,对于订阅,我从 stripe.customer
创建了一个客户并为他们订阅计划。
对于 stripe.charges
,我将返回的对象保存到数据库中。这包含所有收费数据。有一个 webhook 作为 charge.succeeded
发送,如果 charge succeeded
.我不存储这些数据。也许我应该在这里存储费用 id,而不是整个费用对象。我将其存储如下:
订购路线
module.exports = async (req, res) => {
try {
const charge = await stripe.charges.create({
amount: req.session.cart.totalPrice,
currency: 'usd',
source: req.body.id
});
const order = await new Order({
user: req.user,
cart: req.session.cart,
stripeCharge: charge
}).save();
req.session.cart = new Cart({});
res.sendStatus(200);
} catch (e) {
console.log(e);
}
};
对于订阅,它有点复杂,因为创建的客户对象不包含任何费用 ID。所以我将客户 ID 保存到该用户模型中。如果订阅处理正常,Stripe 会触发 6 个 webhook。我店
customer.subscription.created
, customer.created
, charge.succeeded
, 和 invoice.created
.我像这样存储客户对象:订阅路线
module.exports = async (req, res) => {
try {
if(!req.user.hasSubscription) {
const customer = await stripe.customers.create({
email: req.body.email,
source: req.body.id,
plan: 'pro'
});
await new Subscription({
user: req.user,
customerId: customer.id
}).save();
req.user.hasSubscription = true;
await req.user.save();
res.send(req.user);
}
} catch (e) {
console.log(e);
}
};
如您所见,我在 Mongoose 上设置了一个 bool 值
User
型号, hasSubscription
.如果这是真的,则不会创建客户,因此不会设置订阅。如果为 false,我会从创建的客户对象中保存客户 ID。然后在 webhook 路由中,我将上面的 4 个事件保存到正确的用户,按客户 ID 匹配。我认为我可以在这里节省更少的数据,可能会记录任何订阅和任何取消。我将 webhook 事件保存如下:WEBHOOKS 路线
module.exports = async (req, res) => {
try {
const data = {};
if (req.body.type === 'customer.subscription.created') {
await Subscription.findOneAndUpdate({ customerId:
req.body.data.object.customer }, {
$set: {
'stripe_data.customer_subscription_created':
req.body.data.object
}
}, {
new: true
});
res.sendStatus(200);
};
…//OTHER STRIPE EVENTS SIMILAR TO ABOVE…
}
为了安全起见,我存储的数据比我可能需要的要多,而且我在某处读到建议存储足够的数据有利于遵守法律。但是由于 Stripe 将所有内容都存储在仪表板中,我可以理解您的观点,即像费用 ID 这样的简单识别信息就足够了。
我仍处于测试模式,但我认为 Stripe 只发回卡的最后 4 位数字,到期年份如下:
exp_year: 2042
last4: '4242'
关于将 secret 信息保存到数据库:我没有看到任何更敏感的信息被暴露。卡片本身由 Stripe 创建的 id 引用。
最佳答案
在 Stripes security docs ,它提到 Out-of-scope card data that can be safely stored
. Jake T.的评论确实是答案——从 API 返回的所有内容都可以存储。
卡类型、有效期和最后四位数字不受 PCI 合规性限制 .
关于stripe-payments - 在数据库中存储 Stripe 付款的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49122736/