如何将条带标记从angularjs传递到nodejs或修复错误nodejs帖子?

时间:2014-11-14 09:10:38

标签: javascript angularjs node.js stripe-payments

我正在尝试设置条带付款但遇到错误: TypeError:无法读取未定义的属性'id'在/Users/andreykondratyuk/websites/talllly/src/server/app.js:56:37
在Layer.handle [as handle_request] ...

表格:

<form stripe-form="stripeCallback" name="checkoutForm">
        <input ng-model="number" placeholder="Card Number" payments-format="card" payments-validate="card" name="card" />
        <input ng-model="expiry" placeholder="Expiration" payments-format="expiry" payments-validate="expiry" name="expiry" />
        <input ng-model="cvc" placeholder="CVC" payments-format="cvc" payments-validate="cvc" name="cvc" />
        <button type="submit">Submit</button>
        <div ng-if="checkoutForm.card.$invalid"> Error: invalid card number! </div>
      </form>

在控制器中

// Stripe Response Handler
       $scope.stripeCallback = function (code, result) {
         console.log(result);
          if (result.error) {
            window.alert('it failed! error: ' + result.error.message);
          } else {


  // Simple POST request example (passing data) :
    $http.post('/charge', result)
    .success(function(data, status, headers, config) {
      alert(data);
    })
    .error(function(data, status, headers, config) {
      alert(data);
    });
      }
  };

在后端app.js

app.post('/charge', function(req, res) {
console.log(req);
    var stripeToken = req.body.token.id;

    var charge = stripe.charges.create({
        amount: 10000, // amount in cents, again
        currency: "usd",
        card: stripeToken,
        description: "email@email.com"
    }, function(err, charge) {
        if (err && err.type === 'StripeCardError') {
            console.log(JSON.stringify(err, null, 2));
        }
        res.send("completed payment!");
    });
});

我做错了什么?

1 个答案:

答案 0 :(得分:2)

令牌ID在响应主体上为id,stripe.js调用没有返回令牌对象,请检查docs

所以在Node你的行:

var stripeToken = req.body.token.id;

应该是:

var stripeToken = req.body.id;