简体   繁体   English

付款成功后我想做点什么

[英]I want to do something after payment is successful

I created a payment function in my code using Stripe. 我使用Stripe在代码中创建了付款功能。 The payment works well, it accept the money, but I want to introduce into an array some data after the payment is created otherwise if it fails to return to the page with a message. 付款效果很好,可以接受付款,但是我想在创建付款后将一些数据引入数组,否则,如果无法返回带有消息的页面,则将这些数据引入数组。

I just created the function but I do not know what I should do after. 我只是创建了函数,但不知道以后该怎么做。

exports.postPayment = async (req,res,next) => {
    const loggedinUser = req.session.user._id;



const token = req.body.stripeToken;
    const charge = stripe.charges.create({
amount:200,
currency:'usd',
description:'Negative Comment',
source:token
    });


}

Should I create a then block or how I check if the payment was made. 我应该创建一个then块还是我应该如何检查是否付款。 Now I'm in the test mode so all the payments will be accepted. 现在我处于测试模式,因此所有付款都将被接受。 Please help me, thank you! 请帮帮我,谢谢!

You're not returning. 你不回来了 Ie calling one of the return methods on the response 'res' parameter. 即在响应“ res”参数上调用返回方法之一。

Note!! 注意!! The first snipped makes the call to make payment, and returns immediately, ie the payment could succeed or fail, this snippet would not wait for the result, the second example would, utilising a promise. 第一个片段拨打电话进行付款,并立即返回,即付款成功或失败,此片段将不等待结果,第二个示例将利用承诺。

exports.postPayment = async (req,res,next) => {
    const loggedinUser = req.session.user._id;

    const token = req.body.stripeToken;
    const charge = stripe.charges.create({
        amount:200,
        currency:'usd',
        description:'Negative Comment',
        source:token
    });

    return res.status(200).json({message: "It works!"});
}

Unsure if charges.create returns a promise, but if it does, i'd do it like this: 不确定charges.create是否返回承诺,但是如果这样做,我会这样做:

exports.postPayment = async (req,res,next) => {
    const loggedinUser = req.session.user._id;

    const token = req.body.stripeToken;

    try {
        await const charge = stripe.charges.create({
            amount:200,
            currency:'usd',
            description:'Negative Comment',
            source:token
        });
    } catch (e) {
        return res.status(500).json({message: "An error occured!", error: e});
    }


    return res.status(200).json({message: "It works!"});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM