条带错误:您不能将PaymentMethod用作客户的来源

时间:2019-09-06 14:18:52

标签: node.js google-cloud-functions stripe-payments

我是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 });
});

1 个答案:

答案 0 :(得分:1)

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())的方式适用于较早的令牌(tok_123)和源(src_123)对象,而不适用于PaymentMethods。