【问题标题】:Stripe - Token vs Source, changing default sourceStripe - 令牌与源,更改默认源
【发布时间】:2019-08-03 14:57:38
【问题描述】:

我正在学习由 Google 开发人员制作的教程 here

在文章中说:

Stripe 提供了两种创建支付方式的方法:令牌和来源。令牌是一次性的。附加到客户时可以多次使用源。

我一直在做的是我一直在我的 Android 应用程序中通过stripe.createToken(cardToSave, object : TokenCallback {...} 创建一个Token,并将其保存到我的数据库中。这会触发一个云函数addPaymentSource,它会创建一个“支付源(卡)”并保存到我的数据库中:

addPaymentSource 云功能

exports.addPaymentSource = functions.database
    .ref('/stripe_customers/{userId}/sources/{pushId}/token').onWrite((change, context) => {
      const source = change.after.val();
      if (source === null){
        return null;
      }
      return admin.database().ref(`/stripe_customers/${context.params.userId}/customer_id`)
          .once('value').then((snapshot) => {
            return snapshot.val();
          }).then((customer) => {
            return stripe.customers.createSource(customer, {source:source});
          }).then((response) => {
            return change.after.ref.parent.set(response);
          }, (error) => {
            return change.after.ref.parent.child('error').set(userFacingMessage(error));
          }).then(() => {
            return reportError(error, {user: context.params.userId});
          });
        });

那么,这是一个我可以重复用于付款的“来源”吗?我认为这是因为我能够使用这个“源”创建多个charges

我感到困惑的部分是我使用了一个令牌来创建一个源(或者我认为它是一个源)。这是正确的吗?

另外,每当我添加另一个源/卡时,新卡都会添加到旧卡旁边的路径"stripe_customers/$currentUser/sources/"。现在当我点击付款时,它仍在向旧卡收费。如何将卡切换到新添加的卡?

编辑

在条纹网站上:

如果卡的所有者没有默认卡,则新卡将成为默认卡。但是,如果所有者已经有默认值,则它不会更改。要更改默认值,您应该更新客户以拥有新的 default_source,或更新收件人以拥有新的

所以我尝试更新default_source,但它不起作用。老实说,我对 JS 一无所知,而我只是在这里讨论一下……这段代码给了我一个错误

exports.addPaymentSource = functions.database
    .ref('/stripe_customers/{userId}/sources/{pushId}/token').onWrite((change, context) => {
      const source = change.after.val();
      if (source === null){
        return null;
      }
      return admin.database().ref(`/stripe_customers/${context.params.userId}/customer_id`)
          .once('value').then((snapshot) => {
            return snapshot.val();
          }).then((customer) => {
            return stripe.customers.createSource(customer, {source:source});
          }).then((customer) =>{ // look here
            return stripe.customers.update(customer.customer_id,{default_source: source}) // and here
          }).then((response) => {
            return change.after.ref.parent.set(response);
          }, (error) => {
            return change.after.ref.parent.child('error').set(userFacingMessage(error));
          }).then(() => {
            return reportError(error, {user: context.params.userId});
          });
        });

【问题讨论】:

    标签: javascript android firebase google-cloud-functions stripe-payments


    【解决方案1】:

    看看这里:https://stripe.com/docs/sources/customers

    您的代码采用 Stripe 令牌并使用它来创建一个 Source,然后将其保存在您的客户身上。然后,您可以重复使用 Source 为该客户付款。

    对于您的第二个问题,stripe.customers.createSource应该自动分离现有的源并将其替换为您创建的源。如果您没有看到这种行为,您可以使用 default_source 将您的新来源设置为您的客户的新默认值:https://stripe.com/docs/sources/customers#attaching-a-source-to-an-existing-customer-object

    【讨论】:

    • 你能看一下我的edit吗,我正在尝试设置新的default_source,但是我不确定我是否做得正确。我对 JS 知之甚少
    猜你喜欢
    • 2019-01-12
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2022-11-21
    • 2016-09-09
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多