javascript - 在 NodeJS 中使用 Firebase 为 Stripe 创建支付意图时出错

标签 javascript node.js firebase stripe-payments

我正在尝试使用 Firebase 在 NodeJS 中创建 Stripe 付款意图。服务器函数从我的 iOS 应用程序接收 JSON,正确检索产品并获取产品的价格(由控制台中的正确值确认),但在最后一步它没有正确传递价格值。

这是我在 Firebase 控制台中收到的错误:

Error: Invalid integer: {:domain=>{:domain=>"", :_eventsCount=>"1"}}
    at Function.generate (/srv/node_modules/stripe/lib/Error.js:38:16)
    at IncomingMessage.res.once (/srv/node_modules/stripe/lib/StripeResource.js:175:33)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

这是代码:

// New Stripe Payment Intent
const newPaymentIntent = express();
newPaymentIntent.use(bodyParser.urlencoded({ extended: true }));
newPaymentIntent.post('/', (req, res) => { createPaymentIntent(req, res); });
function paymentIntent(req, res) { }
exports.paymentIntent = functions.https.onRequest(newPaymentIntent);

const calculateOrderAmount = items => {
    let price = admin.database().ref('/productAds').orderByChild('code').equalTo(items['code']).once('value').then((snapshot) => {
            var productPrice = 99;

            snapshot.forEach((childSnapshot) => {
          var childData = childSnapshot.val();

                productPrice += childData.price;
                console.log(childData.price);
        });

            console.log(productPrice);
            return productPrice;
    });

    return price;
};

// Create Stripe Customer
async function createPaymentIntent(req, res) {
  const { items, currency } = req.body;

    const paymentIntent = await stripe.paymentIntents.create({
      amount: calculateOrderAmount(items),
      currency: 'aud',
    });
    const clientSecret = paymentIntent.client_secret

    // Send publishable key and PaymentIntent details to client
  res.send({
    publishableKey: 'pk_test_ikLVo1vJSDi89gcfwMiBTDDw',
    clientSecret: clientSecret
  });
}

对我做错了什么有什么建议吗?

最佳答案

您的函数calculateOrderAmount不返回数字。它返回一个 promise ,该 promise 将使用您传递给 then 的函数返回的值进行解析。

您应该使用另一个then来等待最终值,然后才调用stripe API。或者使用异步。 (如果您能够使用异步,您可能还应该在 calculateOrderAmount 函数中使用它,而不是使用 then,因为它会更容易阅读和推理。 )

关于javascript - 在 NodeJS 中使用 Firebase 为 Stripe 创建支付意图时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61113849/

相关文章:

javascript - 使用 IndexedDB 时,如何使用不是键的索引删除多条记录?

java - 未捕获的类型错误 : undefined is not a function Chrome not working

node.js - 如何将用户数据传递到所有 View 中?

node.js 和 websocket 模块聊天没有返回数据

iOS - Apple 会喜欢 Firebase 吗?

javascript - 文本出现在图像悬停上,图像在一系列中

javascript - native Ui 组件与 native 模块

javascript - 我的 NODE 代码不工作是因为我使用的是 Windows 而不是 Linux?

ios - 如何从 firebase 获取数据?

javascript - Firebase 的云函数 : How to access an array I have just got from firebase?