首页 > 解决方案 > 条带错误:您不能将 PaymentMethod 用作客户的来源

问题描述

我是 NodeJS 的新手,我正在尝试使用 Firebase Cloud 功能集成 Stripe 支付。我按照以下步骤操作:

  1. 我从客户端获取令牌,将其存储在 Firestore 中,令牌如下所示:pm_1FFhvNDcXKDMgaqV ...
  2. 我在 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});
    }
    );
    
  3. 上面的代码有效。

  4. 现在我尝试使用教程和文档中指定的令牌添加支付源,但我不断收到以下错误:

错误:您不能将付款方式用作客户的来源。相反,使用支付方式 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.jsgoogle-cloud-functionsstripe-payments

解决方案


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。


推荐阅读