【问题标题】:Firebase functions not being invoked未调用 Firebase 函数
【发布时间】:2021-02-24 23:02:20
【问题描述】:

我正在尝试使用 Firebase 在我的 web 应用中集成 Stripe 付款。我已经从这里的存储库中克隆了代码:https://github.com/firebase/functions-samples/tree/master/stripe 并遵循了这里的文档:https://firebase.google.com/docs/use-cases/payments

通过阅读文档,我假设当客户通过 firebase 身份验证登录时,他们的详细信息将被添加到 firestore 中的 stripe_customer 集合中。我意识到情况并非如此,并手动添加了一个用户来测试保存卡功能。然后我收到以下错误:“stripe.confirmCardSetup Invalid value for stripe.confirmCardSetup intent secret: value should be a client_secret string. You specified: undefined”

我有一个火力基地的火焰计划并已配置。按照文档中的步骤,我认为这是可行的。很抱歉这个问题太含糊了,但似乎在每个角落我都遇到了另一个问题。是否有一些非常明显的我遗漏了阻止此代码工作的东西?我正在尝试为朋友的业务实现这一点,并且对 Firebase 感到非常困惑。我正在使用 Angularjs 进行编码。非常感谢您对此的任何帮助!

这是创建客户的函数的代码

exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => {
        const customer = await stripe.customers.create({ email: user.email });
        const intent = await stripe.setupIntents.create({
        customer: customer.id,
  });
  await admin.firestore().collection('stripe_customers').doc(user.uid).set({
    customer_id: customer.id,
    setup_secret: intent.client_secret,
  });
  return;
});

这是在控制器中调用的代码:

const firebaseUI = new firebaseui.auth.AuthUI(firebase.auth());
const firebaseUiConfig = {
  callbacks: {
    signInSuccessWithAuthResult: function (authResult, redirectUrl) {
      // User successfully signed in.
      // Return type determines whether we continue the redirect automatically
      // or whether we leave that to developer to handle.
      return true;
    },
    uiShown: () => {
      document.getElementById('loader').style.display = 'none';
    },
  },
  signInFlow: 'popup',
  signInSuccessUrl: '/checkout.html',
  signInOptions: [
    firebase.auth.GoogleAuthProvider.PROVIDER_ID,
    firebase.auth.EmailAuthProvider.PROVIDER_ID,
  ],
  credentialHelper: firebaseui.auth.CredentialHelper.NONE,
  // Your terms of service url.
  tosUrl: 'https://example.com/terms',
  // Your privacy policy url.
  privacyPolicyUrl: 'https://example.com/privacy',
};
firebase.auth().onAuthStateChanged((firebaseUser) => {
  if (firebaseUser) {
    currentUser = firebaseUser;
    firebase
      .firestore()
      .collection('stripe_customers')
      .doc(currentUser.uid)
      .onSnapshot((snapshot) => {
        if (snapshot.data()) {
          customerData = snapshot.data();
          startDataListeners();
          document.getElementById('loader').style.display = 'none';
          document.getElementById('content').style.display = 'block';
        } else {
            console.warn(
              `No Stripe customer found in Firestore for user: ${currentUser.uid}`
            );
        }
      });
  } else {
    document.getElementById('content').style.display = 'none';
    firebaseUI.start('#firebaseui-auth-container', firebaseUiConfig);
  }
});

【问题讨论】:

  • 嘿,您是否在云功能的配置中设置了您的条带键?您是在本地测试吗?如果是这样,您是否使用这些值更新了本地商店?
  • 嘿,非常感谢您的帮助!我已经按照文档中的说明在 CLI 中设置了我的条带键。我在本地进行测试,但我不确定你的意思,但我用这些值更新我的商店。我在控制台中更新了我的 stripe_customers 集合以包含一个新用户,但是当用户登录时代码是否应该自动执行此操作?对不起,如果这些是非常基本的问题。
  • 一切都好。我在下面添加了一个答案来澄清。如果它有效,请接受它。要将您的值输出到本地存储,即 runtimeconfig.json 文件,请运行以下命令:firebase functions:config:get > .runtimeconfig.json
  • 您是否检查过您的 Stripe 仪表板以确保您创建客户和设置意图的请求有效?

标签: angularjs firebase google-cloud-firestore stripe-payments payment-gateway


【解决方案1】:

您提供的错误(如下)意味着您的配置中的密钥没有被拉入您的代码中。如果您在本地运行此程序,则需要在更改函数时运行以下内容:配置值。

 firebase functions:config:get > .runtimeconfig.json

查看doc's 了解如何在本地运行您的函数:

错误

“stripe.confirmCardSetup Intent Secret 的值无效:值应该 是一个 client_secret 字符串。您指定:未定义”

【讨论】:

  • 非常感谢您的帮助,我现在收到此错误:Uncaught TypeError: firebase.firestore(...).useEmulator is not a function。我在网上找不到这个模拟器特有的错误,非常感谢任何帮助!
  • 我不太确定您要达到什么目标,或者您为了得到这个错误而运行了什么。要在本地调用 onCreate 函数,请使用 myFirestoreFunction({foo: ‘new’}) 您可以使用 firebase functions:shell 启动 shell。不要忘记为您尝试访问的 Firestore 导出您的凭据。查看此页面以获取帮助,firebase.google.com/docs/functions/…
猜你喜欢
  • 1970-01-01
  • 2019-12-19
  • 2018-03-28
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 2019-01-18
  • 1970-01-01
相关资源
最近更新 更多