Android Firestore Stripe - 添加支付来源

问题描述 投票:0回答:1

底部更新

我正在尝试在我的Android应用中建立一个注册页面,通过Stripe让用户注册订阅。我卡住的是,通过云功能,从安卓系统中添加一个支付源,并从Stripe接收一个令牌。

目前我已经解决了,自动将新创建的用户添加到Stripe中。以及创建订阅时(/users/{userId}/membership/token)被写入,或改变。

在安卓系统上,我是可以通过输入来获取信用卡数据的。

PaymentMethodCreateParams.Card card = cardInputWidget.getPaymentMethodCard();

接下来我需要通过使用......将其提交给我的云函数。

mFunctions = FirebaseFunctions.getInstance();
mFunctions.getHttpsCallable("addPaymentSource")
          .call()
          .addOnCompleteListener(task -> {
          ...

由于我找不到这方面的信息,以下是我对这个云功能的全部内容(Javascript)

exports.addPaymentSource = functions.https.onCall((data, context) =>{

      const pm = await stripe.paymentMethods.attach('pm_678', {customer: 'cus_123'});
      return admin.firestore().collection('users').doc(user.uid).get('membership').set({token: token});

}

我需要获取保存在- users{user.uid}customerId'的客户号。以及通过我的http数据调用来传递支付方式,并通过获取user_id(在这之前很久就已经创建了)。

我是看了这个youtube视频,然后把我的代码转换过来的。使用Stripe、Angular和Firebase进行订阅支付。

我也参考了不少Stripe的云函数例子。有一个问题是大家似乎都在用这个代码(如下图),在我的实现中无法使用。在大多数指南examples没有用于订阅的情况下。

// Add a payment source (card) for a user by writing a stripe payment source token to Cloud Firestore
exports.addPaymentSource = functions.firestore.document('/stripe_customers/{userId}/tokens/{pushId}').onCreate(async (snap, context) => {
  const source = snap.data();
  const token = source.token;
  if (source === null){
    return null;
  }

  try {
    const snapshot = await admin.firestore().collection('stripe_customers').doc(context.params.userId).get();
    const customer =  snapshot.data().customer_id;
    const response = await stripe.customers.createSource(customer, {source: token});
    return admin.firestore().collection('stripe_customers').doc(context.params.userId).collection("sources").doc(response.fingerprint).set(response, {merge: true});
  } catch (error) {
    await snap.ref.set({'error':userFacingMessage(error)},{merge:true});
    return reportError(error, {user: context.params.userId});
  }
});

更新了一下。


我做了一些小的改动,试图让这个工作... ...

exports.addPaymentSource = functions.https.onCall((data, context) =>{
    ///users/{userId}/membership/token

    // Create Payment Method
    const paymentMethod = stripe.paymentMethods.create(
        {
            type: 'card',
            card: {
              number: '4242424242424242',
              exp_month: 5,
              exp_year: 2021,
              cvc: '314',
            },
    }).then(pm => {

        console.log('paymentMethod: ', paymentMethod.id);

        return stripe.paymentMethods.attach(paymentMethod.id, { customer: 'cus_HCQNxmI5CSlIV5' })
        .then(pm => {

        return admin.firestore().collection('users').doc(user.uid).get('membership').set({token: pm.id});

        });
    });
});

我正在接近,问题是paymentMethod.id是'undefined'。

android firebase google-cloud-firestore google-cloud-functions stripe-payments
1个回答
1
投票

虽然我不是Firebase专家,但在你的Android端,你要调用你的云函数,参数为Customer ID和PaymentMethod ID,以便传递给你的云函数。

传递参数如图所示。https:/stackoverflow.coma5629821310654456

然后在你的云功能中,你要把PaymentMethod附加到Customer上(就像你用stripe-node做的那样),并让它成为Customer对Subscriptions的默认,如图所示。https:/stripe.comdocsbillingsubscriptionspayment#signup-3。

然后,你应该为某一计划在客户上创建一个订阅,同样使用stripe-node,如这里所示。https:/stripe.comdocsbillingssubscriptionspayment#signup-4。


0
投票

这里我有我的功能代码。(我使用了一些占位符数据来填充变量)

exports.addPaymentSource = functions.https.onCall((data, context) =>{

    // Create Payment Method
    stripe.paymentMethods.create( {

        type: 'card',
        card: {
          number: '4242424242424242',
          exp_month: 5,
          exp_year: 2021,
          cvc: '314',
        },
    }) 
    .then(pm => {

         return stripe.paymentMethods.attach(pm.id, { customer: 'cus_HCCNMAAwRhNM3c' })

    })
    .then(pm => {

        console.log('final step');
        console.log('paymentMethod: ', pm.id);
        admin.firestore().collection('users').doc('LzgbQBtk0QSZi7QISIbV').set({token: pm.id});
        return admin.firestore().collection('users').doc(user.uid).collection('membership').set({token: pm.id});

        })
    .catch(error => { return null });
});

于是我手动粘贴了一些变量,确认我的功能是否正常。客户ID和卡的详细信息需要从安卓应用中传入。这些卡信息是我在订阅时唯一需要的信息。

pm'是返回的Payment Method对象,其中id是需要附加到用户身上的变量,最后pm.id是必须保存到firestore里面的token。

最后pm.id是必须保存到firestore里面的token。这样做会触发我的订阅设置云函数(未显示)。

显示的代码显示了如何避免嵌套的 then 语句,以及 Android firestore 的直接函数调用。虽然也没有显示,但数据字段可以调用任何变量的关键词 "data.cardnbr"。

该方法避免了任何SetupIntents的使用。虽然这对于基于订阅的收费来说是非常有效的,但对于直接收费来说可能并不是最佳实践。

© www.soinside.com 2019 - 2024. All rights reserved.