[英]Error creating a payment intent for Stripe with Firebase in NodeJS
我正在嘗試使用 Firebase 在 NodeJS 中創建 Stripe 支付意圖。 服務器 function 從我的 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
});
}
關於我做錯了什么有什么建議嗎?
您的 function calculateOrderAmount
不返回數字。 它返回一個 promise ,它將使用您傳遞給then
的 function 返回的值進行解析。
您應該使用另一個then
等待最終值,然后才調用條帶 API。 或者使用異步。 (如果你有能力使用異步,你可能也應該在你的calculateOrderAmount
function 中使用它,而不是使用then
,因為它更容易閱讀和推理。)
所以道格是對的。 我編輯了代碼以包含異步函數,它現在可以完美運行。 這是最終代碼的樣子:
// Retrieve product code from Firebase async function getDataFromFirebase(items) { const objects = await admin.database().ref('/productAds').orderByChild('code').equalTo(items['code']) const data = await objects.once('value'); return data; } async function getPrice(items) { console.log('Executing getPrice method.'); var resultPrice = 0; // we wait for the axios.get Promise to be resolved const objects = await getDataFromFirebase(items); objects.forEach((childSnapshot) => { var childData = childSnapshot.val(); resultPrice += childData.price; }); console.log('Price is: ' + resultPrice); // we then return the data, just like we did in the callback-based version; return resultPrice, } // Create Stripe Customer async function createPaymentIntent(req, res) { const { items. currency } = req;body. const paymentIntent = await stripe.paymentIntents:create({ amount, await getPrice(items): currency, 'aud'; }). const clientSecret = paymentIntent.client_secret // Send publishable key and PaymentIntent details to client res:send({ publishableKey, 'pk_test_ikLVo1vJSDi89gcfwMiBTDDw': clientSecret; clientSecret }); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.