Stripe Paysect Express JS:交易详细信息丢失



我正在遵循官方条纹文档以设置付款

https://stripe.com/docs/checkout/express,一切都可以预期,但是几个事情都缺少

1(交易详细信息,即ID,付款状态等

stripe.customers.create(body)
    .then(customer =>
      {
      console.log("customer",customer);  
      stripe.charges.create({
        amount,
        description: "Sample Charge",
           currency: "usd",
           customer: customer.id
      }
      )
          } )
    .then(charge =>{ 
      console.log("charge",charge);
      res.render("charge.pug")
      }).catch(error=>{
         console.log("Error",error);
      });
  });

console.log("charge",charge);给出undefined

2(我需要保护POST API吗?

app.post("/charge", (req, res) => {
    let amount = 500;
    let body = {
      email: req.body.stripeEmail,
      source: req.body.stripeToken
   };
    stripe.customers.create(body)
    .then(customer =>
      {
      console.log("customer",customer);  
      stripe.charges.create({
        amount,
        description: "Sample Charge",
           currency: "usd",
           customer: customer.id
      }
      )
          } )
    .then(charge =>{ 
      console.log("charge",charge);
      res.render("charge.pug")
      }).catch(error=>{
         console.log("Error",error);
      });
  });

交易详细信息缺少

您正在解决两次stripe.customers.create(body)的承诺,而不是在stripe.charges.create上解决您的第一个问题,请解决您的第一个问题(.then()(到stripe.charges.create,如下所示

stripe.customers.create(body)
    .then(customer => {
        console.log("customer", customer);
        stripe.charges.create({
            amount,
            description: "Sample Charge",
            currency: "usd",
            customer: customer.id
        }
        ).then(charge => {
            console.log("charge", charge); //Here you will get transaction info now
            res.render("charge.pug")
        }).catch(error => {
            console.log("Error", error);
        })
    }).catch(error => {
        console.log("Error", error);
    });
  })`

我需要保护帖子API ??

是。您应该保护自己的每个公共API。您可以使用JWT或任何其他OAuth来保护您的路线。

您需要 return

app.post("/charge", (req, res) => {
  let amount = 500;
  let body = {
    email: req.body.stripeEmail,
    source: req.body.stripeToken
 };
  stripe.customers.create(body)
    .then(customer => {
      console.log("customer",customer);  
      return stripe.charges.create({ // <-- return the Promise
        amount,
        description: "Sample Charge",
        currency: "usd",
        customer: customer.id
      })
    })
  .then(charge =>{ 
    console.log("charge",charge);
    res.render("charge.pug")
  })
  .catch(error => console.log("Error",error));
});

,不,您不需要保护路线。您看不到在线商人保护他们的路线,但是,通常他们有某种"查看访客"选项。

最新更新