尝试在异步函数中传递值时发生错误(条带计费过程)

问题描述 投票:0回答:1

我正在尝试将Stipe集成到我的项目中。为了创建费用,我需要创建一些值并将其传递给下一个函数。我目前有一个异步函数链可以这样做,但是由于某种原因,值(创建的令牌)之一没有被传递(错误:未定义)。我通过传递函数的返回值以及将所需的值保存在对象中然后传递给对象来进行尝试。客户和令牌都可以正确生成,但是只有客户可以传递给addCardToCustomer函数。有人可以在这里发现一个错误吗?

router.post("/checkout", async function (req, res, next) {
  if (!req.session.cart) {
    return res.redirect("/shopping-cart");
  }

  let cart = new Cart(req.session.cart);
  let customerId = {};

  let createCustomer = async function () {
    var param = {};
    param.email = req.body.email;
    param.name = req.body.name;
    param.description = "";
    return stripe.customers.create(param, function (err, customer) {
      if (err) {
        console.log("err:" + err);
      }
      if (customer) {
        console.log("success: " + JSON.stringify(customer, null, 2));
        customerId.id = customer.id;
      } else {
        console.log("something went wrong");
      }
    });
//CUSTOMER CREATED

  };

  let createToken = async function () {
    let param = {};

    param.card = {
      number: req.body.card,
      exp_month: req.body.exp_month,
      exp_year: req.body.exp_year,
      cvc: req.body.security
    }
    return stripe.tokens.create(param, function (err, token) {
      if (err) {
        console.log("err:" + err);

      }
      if (token) {
        console.log("success: " + JSON.stringify(token, null, 2));
        console.log(req.body);
        customerId.t_Id = token.id;
        console.log(customerId.t_Id)
      } else {
        console.log("something went wrong");
      }
    });
  };

//TOKEN CREATED


  let addCardToCustomer = async function (createdToken) {

    return stripe.customers.createSource(customerId.id, {
      source: createdToken
    }, function (err, card) {
      if (err) {
        console.log("err:" + err);
        console.log(customerId.id)
        //CUSTOMER IS DEFINED
        console.log(customerId.t_id);
        //TOKEN UNDEFINED
      }
      if (card) {
        console.log("success: " + JSON.stringify(card, null, 2));
      } else {
        console.log("something went wrong");
      }
    });
  };

  //CUSTOMER.ID WORKS; TOKEN ID NOT

  let chargeCustomerThroughCustomerID = async function () {
    let param = {
      amount: cart.totalPrice * 100,
      currency: 'eur',
      description: 'First payment',
      customer: customerId.id
    }

   return stripe.charges.create(param, function (err, charge) {
      if (err) {
        console.log("err: " + err);
      }
      if (charge) {
        console.log("success: " + JSON.stringify(charge, null, 2));
      } else {
        console.log("Something wrong")
      }
    })
  }


  try {
    const createdCustomer = await createCustomer(); 
    const createdToken = await createToken();
    const addedCardToCustomer = await addCardToCustomer(createdToken); 
    const chargedCustomerThroughCustomerID = await chargeCustomerThroughCustomerID(); 


    res.send("success");

  } catch (e) {
    console.log(`error ${e}`)
  };



});
javascript async-await stripe-payments
1个回答
0
投票

我已经回答了这个问题:您的函数缺少async和参数。

在此处查看答案:Stripe.create functions not in correct order

© www.soinside.com 2019 - 2024. All rights reserved.