我用付款Gpay /卡(带有颤动和火力)创建了一个简单的应用程序。但是现在在firebase函数中,我遇到了这个错误:paymentIntent未定义。有人可以给小费如何处理这个问题吗?
这是我的职责
const functions = require('firebase-functions');
const stripe = require('stripe')('sk_test_');
exports.StripePI = functions.https.onRequest(async (req, res) => {
const fee = (req.query.amount/100) | 0;
const stripeVendorAccount = 'acct_';
stripe.paymentIntents.create({
amount: req.query.amount,
currency: req.query.currency,
payment_method: req.query.paym,
confirmation_method: 'automatic',
confirm: true,
payment_method_types: ['card'],
//application_fee_amount: fee,
description: req.query.description,
}, {
stripeAccount: stripeVendorAccount
},
function(err, paymentIntent) {
// asynchronously called
const paymentIntentReference = paymentIntent;
if (err !== null){
console.log('Error payment Intent: ', err);
res.send('error');
} else {
console.log('Created paymentintent: ', paymentIntent);
res.json({
paymentIntent: paymentIntent,
stripeAccount: stripeVendorAccount});
}});
console.log(paymentIntent.status);
});
答案 0 :(得分:1)
您的最终console.log(paymentIntent.status);
是在声明paymentIntent
的块之外定义的,因此无法到达它。
要解决此问题,请将console.log(paymentIntent.status);
移动到其上方的代码段中:
exports.StripePI = functions.https.onRequest(async (req, res) => {
const fee = (req.query.amount / 100) | 0;
const stripeVendorAccount = 'acct_';
stripe.paymentIntents.create({
amount: req.query.amount,
currency: req.query.currency,
payment_method: req.query.paym,
confirmation_method: 'automatic',
confirm: true,
payment_method_types: ['card'],
//application_fee_amount: fee,
description: req.query.description,
}, {
stripeAccount: stripeVendorAccount
},
function(err, paymentIntent) {
// asynchronously called
const paymentIntentReference = paymentIntent;
if (err !== null) {
console.log('Error payment Intent: ', err);
res.send('error');
} else {
console.log('Created paymentintent: ', paymentIntent);
res.json({
paymentIntent: paymentIntent,
stripeAccount: stripeVendorAccount
});
}
console.log(paymentIntent.status);
});
});
正如您可能在上面看到的那样,如果代码始终缩进,我发现更容易发现诸如此类的问题。如果您难以保持一致的样式,请考虑使用类似Prettier的工具或(如上所述)beautifier.io。