【问题标题】:Customer is not created on stripe account客户不是在条带帐户上创建的
【发布时间】:2020-03-25 03:43:38
【问题描述】:

我正在运行此 node.js 代码以在条带帐户上创建客户功能部署成功但未能在条带上创建客户我没有得到我所缺少的。 Fire base functions 文件夹也没有显示该功能。

const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    const stripe = require('stripe')("secret key here");
    var customer;

    stripe.customers.create(
      {
        email: 'customer@example.com',
      },
      {
        maxNetworkRetries: 2, 
      }
    );

【问题讨论】:

  • 你能显示你得到的错误吗?
  • 实际上我没有收到任何成功部署的错误,但是这段代码应该在我没有生成的条带帐户上创建客户
  • 检查正在发生的事情的唯一方法是检查呼叫的响应。因此,请检查响应,并将消息放在这里,以便我们为您提供帮助。 .then/.catchasync/await 并将其分配给变量
  • 我想问一下它是否应该在火基地的功能选项卡上创建一个功能?如果是,则此代码甚至不会在那里生成函数。

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


【解决方案1】:

当您将 API 用于您完全无法控制的服务(即 stripe.com 和 firebase)时,您必须检查错误。如果您错误地使用服务,错误将解释或至少提示您做错了什么。

stripe-node API documentation 建议您调用 stripe.customer.create() 作为等待函数,如下所示:

const customer = await stripe.customers.create({
         email: 'customer@example.com',
});

如果您从异步函数中调用它,这很容易。你应该在你的异步函数中使用这种代码来检查从 stripe.com 返回的错误。

try {
   const customer = await stripe.customers.create({
         email: 'customer@example.com',
    });
    /* here's your customer object */
}
catch (error) {
    console.error ('stripe', error);
}

如果你不从异步函数调用它,你可以使用 Promises 方案等待结果。

    stripe.customers.create({
         email: 'customer@example.com',
       })
    .then ( function (customer) {
             /* here's your customer object */
       })
    .catch ( function (error) {
        console.error ('stripe', error);
       });

如果您还没有弄清楚 async/await 或 Promises 是如何工作的,那么现在是时候这样做了。

【讨论】:

  • 我正在关注您在此处提到的条带文档,但我在使用 await 时发现了一个错误,错误是 await can only use with async。我对 js 很陌生
  • firebaseconifq 和 gcloud_project 环境变量丢失,当我在 cmd 中运行 node index.js 时,初始化 firebase-admin 将无法找到此错误
猜你喜欢
  • 2022-07-11
  • 1970-01-01
  • 2017-12-03
  • 2020-09-15
  • 2021-09-11
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多