【问题标题】:Error creating a payment intent for Stripe with Firebase in NodeJS在 NodeJS 中使用 Firebase 为 Stripe 创建付款意图时出错
【发布时间】:2020-07-21 15:23:58
【问题描述】:

我正在尝试使用 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
  });
}

关于我做错了什么有什么建议吗?

【问题讨论】:

    标签: javascript node.js firebase stripe-payments


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的及时回复!我使用了 Stripe 示例代码中的 .then 方法。我应该在数量上实现异步:calculateOrderAmount(items) 吗?
    • 我尝试了两种方案。 #1:我添加了 .then((price) => { return price;});为 const calculateOrderAmount = items =>。然后我尝试了 const calculateOrderAmount = async (items) => { 返回 return productPrice。两者都没有导致值被传递回 const paymentIntent。 ://
    【解决方案2】:

    所以道格是正确的。我编辑了代码以包含异步函数,它现在可以完美运行。最终代码如下所示:

    // 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
      });
    }

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 2020-12-01
      • 1970-01-01
      • 2020-08-12
      • 2018-07-15
      • 2019-04-15
      • 2021-11-28
      • 2022-01-03
      • 2022-01-26
      相关资源
      最近更新 更多