node.js - 条带错误:您不能将 PaymentMethod 用作客户的来源
问题描述
我是 NodeJS 的新手,我正在尝试使用 Firebase Cloud 功能集成 Stripe 支付。我按照以下步骤操作:
- 我从客户端获取令牌,将其存储在 Firestore 中,令牌如下所示:pm_1FFhvNDcXKDMgaqV ...
我在 Stripe 上创建了一个客户
exports.createNewStripeCustomer = functions.auth.user().onCreate( async (user) => { const customer = await stripe.customers.create({email: user.email}) ; return admin.firestore() .collection('customers') .doc(user.uid) .set({customer_id: customer.id}); } );
上面的代码有效。
现在我尝试使用教程和文档中指定的令牌添加支付源,但我不断收到以下错误:
错误:您不能将付款方式用作客户的来源。相反,使用支付方式 API 将客户附加到支付方式。请参阅https://stripe.com/docs/api/payment_methods/attach
这是导致错误的代码:
exports.newPaymentSource = functions.firestore.document('customers/{userId}/tokens/{tokenId}').onWrite(async (change, context) => {
//Get token that strike sdk gave to the client...
const data = change.after.data();
if (data ===null) { return null }
const token = data.tokenId;
const snapshot = await firestore.collection('customers').doc(context.params.userId).get();
const customer = snapshot.data().customer_id;
//calling stripe api...
console.log(customer + ":" + token + ":" + context.params.userId);
const respFromStripe = await stripe.customers.createSource(customer, { source: token });
// console.log(respFromStripe);
return firestore.collection('users').doc(context.params.userId)
.collection('sources').doc(respFromStripe.card.fingerprint).set(respFromStripe, { merge: true });
});
解决方案
PaymentMethod 对象(代表您的用户卡)需要使用 /v1/payment_methods/pm_123/attach 端点或在 Stripe-Node 中附加到客户:
pm = await stripe.paymentMethods.attach('pm_678', {customer: 'cus_123'});
https://stripe.com/docs/api/payment_methods/attach?lang=node
您使用它的方式 ( customer.createSource()
) 适用于较旧的 Tokens (tok_123) 和 Source (src_123) 对象,而不是 PaymentMethods。
推荐阅读
- charts - Highcharts - 使用 addPoint 添加一个点会延长时间
- r - R中的R-CNN用于分析航拍照片
- ios - Admob 未显示任何广告
- java - 如何验证模拟方法是否引发了异常?
- sybase - 从 SQLBrowser for Sybase 运行 SQL 文件
- javascript - Merging two multidimensional arrays in Javascript by value
- scala - Spark Scala 计算数据框字段的长度
- java - 我可以记住java程序中的管理员权限吗?
- elasticsearch - 当持久队列大小较低时,Filebeat 仅将部分日志发送到 logstash。如何解决?
- javascript - 将 JavaScript 评估为 WKWebView 以获取视频 URL 不起作用