条纹-创建费用

时间:2018-10-30 10:36:01

标签: node.js express stripe-payments payment-gateway payment

正在整合刀柄。一切似乎都在前端工作,但在服务器端代码上,令牌为空,并且未成功向Stripe充电。似乎无法弄清楚我要去哪里。

   app.post('/apple-pay', function(req, res, next) {

    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    var stripe = require("stripe")("sk_test_XXX");

    // Token is created using Checkout or Elements!
    // Get the payment token ID submitted by the form:
    const token = req.body.stripeToken;
    console.log(token)
     const charge = stripe.charges.create({
      amount: 999,
      currency: 'usd',
      description: 'Example charge',
       source: token,

    }, function(err, charge) {
         if(err){
                req.flash("error", err.message);
                res.redirect("back");
            } else {

            }
    });
    });

2 个答案:

答案 0 :(得分:0)

创建费用之前,您应创建客户。充电后有效。

样本代码。 (ES6)

let customer = await payStripe.customers.create({
            email: req.body.stripeEmail,
            source: req.body.stripeToken
});

//After Created Customer...

if(customer){

 let charge = await payStripe.charges.create({

            amount: req.body.amount,

            description: req.body.description,

            currency: 'usd',

            customer: customer.id
        });


}

我希望一切正常。

答案 1 :(得分:0)

other question中的前端代码中,您将POST正文传递为

JSON.stringify({token: ev.token.id})

,这意味着Stripe令牌实际上在token POST参数中,而不在stripeToken中。所以你需要做

const token = req.body.token;

相反。