将 Stripe API 集成到我的 Meteor 应用程序时无法返回有效结果

Trouble returning a valid result while integrating Stripe API into my Meteor app

下面是 运行:

这是我的方法在服务器上的样子:

createCustomer: function(token, email, plan){
  try{
    let createCustomer = Meteor.wrapAsync(stripe.customers.create);
    let result = createCustomer({
      source: token,
      email: email,
      plan: plan
    });

    let subscription = {
      customer: result.id,
      sub: result.subscriptions.data[0].id,
      plan: result.subscriptions.data[0].plan.name
    };

    Meteor.users.update({_id: Meteor.userId()}, {$set: subscription});

  } catch(error){
  if(error.code === "incorrect_cvc"){
    throw new Meteor.Error("incorrect_cvc", error.message);
  }
// More of such errors follows
  }
}

这是它在客户端上的样子:

 Stripe.card.createToken({
    number: number,
    cvc: cvc,
    exp_month: exp,
    exp_year: exp_year,
    address_zip: zip,
    address_country: country
  }, function(status, response){
    if(response.error){
      console.log("Make sure all fields are filled before submitting order.");
    } else{
      let token = response.id;
      Meteor.call("createCustomer", token, email, plan, function(error, result){
        if(result){
        console.log("Congratulations, everything worked!");
        } else{
          if(error.error === "incorrect_cvc"){
            console.log("oops, the CSV is incorrect");
          } 

          // More of such errors follow..
        }
      })
    }
  });

因此,当出现真正的错误时,一切正常,它会在服务器 + 客户端上正常运行。当用户使用卡时,会创建费用并始终创建订阅。 HOWEVER,当成功并且一切正常时,我仍然通过回调在客户端收到错误,结果永远不会 true 或触发。不知道为什么。

不是 100% 使用 Meteor,但在我看来你的 createCustomer 方法实际上并没有 return 任何东西,所以你的 (err, result) 中的 result里面可能永远不会有任何东西?

如评论中所述,您可能希望将步骤分开并将每个步骤包装在自己的 try-catch 集中,以便更好地隔离问题。

此外,我觉得您可以将服务器端错误代码概括为如下内容:

throw new Meteor.Error(error.error, error.message);

我什至可能想做这样的事情,至少在 testing/development 期间 - 这样你实际上可以 console.log() 浏览器中的原始错误:

throw new Meteor.Error(error.error, error.message, JSON.stringify(error));