Stripe.create 函数的顺序不正确 [英] Stripe.create functions not in correct order

查看:22
本文介绍了Stripe.create 函数的顺序不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Stripe 集成到我的应用程序中.我已经在 req.body 中收集了所有必要的数据.现在,在创建客户 ID 并尝试将其传递给 addCustomerToCard 后,会抛出未定义的错误.之后,createToken 就被成功登录了.

I want to integrate Stripe into my application. I have collected all necessary Data in the req.body. Now an undefined error is being thrown after creating the customer Id and while trying to pass it to addCustomerToCard. After that, createToken is being successfully logged.

所以两个问题:1.为什么没有按照我的预期调用函数的顺序?2.addCustomerToCard中为什么没有传入customer?

So two questions: 1.Why is the order of functions not being invoked as I would expect? 2.Why does the customer not get passed in addCustomerToCard?

router.post("/checkout", async function (req, res, next) {
  if (!req.session.cart) {
    return res.redirect("/shopping-cart");
  }
  let createCustomer = 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));
      } else {
        console.log("something went wrong");
      }
    });


  };

  let createToken = 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);
        console.log(param);
      }
      if (token) {
        console.log("success: " + JSON.stringify(token, null, 2));
        console.log(req.body);
      } else {
        console.log("something went wrong");
      }
    });
  };




  let addCardToCustomer = function () {
    console.log(createdCustomer);
   return stripe.customers.createSource(customer.id, {source: token.id}, function (err, card) {
     if (err) {
       console.log("err:" + err);
       console.log(param);
     }
     if (card) {
       console.log("success: " + JSON.stringify(card, null, 2));
     } else {
       console.log("something went wrong");
     }
   });
 };

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

   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(); // promise 1
    const createdToken = await createToken();
    const addedCardToCustomer = await addCardToCustomer(createdCustomer,createdToken ); // 
    // const chargeCustomerThroughCustomerID = await chargeCustomerThroughCustomerID(); // promise 4


    res.send("success");

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



});

//LOG OUTPUT


//success
//error ReferenceError: createdCustomer is not defined
//success

推荐答案

对于初学者,您应该将其分解为更小的部分以验证您所期望的行为.这里发生了很多事情.

You should break this down into smaller pieces to verify the behaviour you're expecting, for starters. There's quite a lot going on here.

一个问题是您正在尝试 await 不是 async 的函数——您应该标记每个函数:

One problem is that you are trying to await functions which are not async -- you should have each function marked:

let createCustomer = async function () {...}

这很可能是序列不是您所期望的原因.

This is likely the reason the sequence is not what you expect.

您在 addCardToCustomer 中遇到错误,因为 createCustomer作用域.你需要有一个参数:

You're hitting an error in addCardToCustomer because createCustomer is undefined in the scope of that function. You need to have a parameter:

let addCardToCustomer = async function (createdCustomer) { ... }

这篇关于Stripe.create 函数的顺序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆