我正在尝试使用 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/