【发布时间】:2020-01-09 10:30:24
【问题描述】:
我是 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 });
});
【问题讨论】:
标签: node.js google-cloud-functions stripe-payments