【发布时间】:2019-12-01 22:20:44
【问题描述】:
我想使用 Stripe 检索付款的最后 4 位数字。为此,我必须运行一个异步函数,并在该函数内运行另一个异步函数。
之后我想用最后 4 个数字更新数据库。但我想在解决这两个函数后更新数据库,如果没有得到undefined。
if (intent.status === "succeeded") {
const user = res.locals.decodedClaims;
const uid = user.uid;
let last4; //trying to set last4 to global var so that i can change it inside a function
// The payment didn’t need any additional actions and completed!
// Handle post-payment fulfillment
//get payment last4 digits
stripe.paymentIntents.retrieve(intent.id, function(err, paymentIntent) {
// asynchronously called
if (err) {
return console.log("PI retrieve error: ", err);
}
stripe.charges.retrieve(paymentIntent.charges.data[0].id, function(
err,
charge
) {
if (err) {
return console.log("Charge retrieve error: ", err);
}
last4 = charge.payment_method_details.card.last4;
console.log("charge: ", last4); // works, eg: 4242
});
});
//update status in database
const userRef = db.collection("users").doc(uid);
let updatedStatus = userRef.update({
paid: true,
last4: last4 //undefined
});
updatedStatus.catch(err => {
console.log(err);
});
//send success message to client
return {
success: true,
hello: 1
};
预期结果:
//update status in database
const userRef = db.collection("users").doc(uid);
let updatedStatus = userRef.update({
paid: true,
last4: last4 //undefined
});
updatedStatus.catch(err => {
console.log(err);
});
//send success message to client
return {
success: true,
hello: 1
所有这一切都在我得到 last4 的值之后运行。
【问题讨论】:
标签: javascript node.js asynchronous google-cloud-firestore stripe-payments