为什么我曾经从条带获得token_already_used,但我曾经使用过一次

时间:2019-10-05 06:53:44

标签: node.js stripe-payments

在我的应用程序中,我从前端获取条带令牌,并将其传递给后端服务器以创建客户。

我从Stripe收到响应,指出您不能多次使用Stripe令牌。

尽管我在创建客户时仅使用令牌一次。

这是我的完整后端代码:

router.post('/', (req, res) => {
    var code = rng(6)

    var validationError = {};
    if (!req.body.name) validationError['name'] = 'name is required';
    if (!req.body.email) validationError['email'] = 'email is required';
    if (!req.body.password) validationError['password'] = 'password is required';

    if (Object.keys(validationError).length > 0) {
        res.status(417).send({
            message: 'Validation Error',
            error: validationError
        })
    }
    else {
        stripe.customers.create({
            email: req.body.email,
            description: req.body.name,
            source: req.body.token // obtained with Stripe.js
        }, function (err, customer) {
            if (customer) {
                Model.getOneData({email: req.body.email}, (err, data) => {
                    if (!data) {
                        Model.createData({
                            name: req.body.name,
                            email: req.body.email,
                            password: req.body.password,
                            city: req.body.city,
                            phone: req.body.phone,
                            age: req.body.age,
                            condition: {
                                one: req.body.condition1,
                                two: req.body.condition2,
                                three: req.body.condition3
                            },
                            status: req.body.status ? req.body.status : "trial",
                            customerid: customer.id,
                            confirmationCode: code,
                            last4digit: customer.sources.data[0].last4
                        }, (err, newUser) => {
                            if (err) {
                                res.status(400).send(err)
                            }
                            else {
                                // sendEmail(req.body.email, "Verify your email", "Your verification code is " + code + "\nOr click on this url to activate your account: http://localhost:3000/users/verify/" + data._id + "/" + code);
                                const html = TemplateMaker({
                                    headline: 'Merci de vous être enregistré',
                                    subHeadline: 'Vous pouvez activer votre compte en cliquant sur le button ci-dessous :',
                                    buttonText: 'ACTIVER MON COMPTE',
                                    buttonUrl: constants.Api_endpoint + "verify-email/" + newUser._id + "/" + code,
                                    para1: 'Votre code de vérification est :' + code,
                                    para2: constants.Api_endpoint + "verify-email/" + newUser._id + "/" + code,
                                }, null);
                                Mailer.sendEmail(req.body.email, "Merci de vous être enregistré", html);
                                res.send(newUser)

                            }
                        })
                    }
                    else {
                        res.status(409).send({
                            error: "User already exists"
                        })
                    }
                })

            }
            else {
                console.log(err)
                res.status(402).send(err)
            }

        });
    }
})

我还检查了Webhook设置。对于以下事件,webhook似乎被击中了三次:

customer.created

payment_method.attached

customer.source.created

0 个答案:

没有答案