代码之家  ›  专栏  ›  技术社区  ›  AndrewLeonardi

条纹-产生电荷

  •  0
  • AndrewLeonardi  · 技术社区  · 7 年前

    正在集成Stipe。在前端似乎一切正常,但在服务器端代码上,令牌是空的,无法成功地向条带充电。似乎不知道我哪里出错了。

       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 回复  |  直到 7 年前
        1
  •  0
  •   jitendra kumar    7 年前

    以前 创建费用 ,你应该 创建客户 . 充电后工作。

    样本代码。(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
            });
    
    
    }
    

    我希望工作顺利。

        2
  •  0
  •   karllekko    7 年前

    在您的前端代码中, other question 你以

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

    这意味着条带令牌实际上位于 token post参数,不是 stripeToken . 所以你需要这么做

    const token = req.body.token;

    相反。

    推荐文章