Stack:Angular,Node.js,Express,MongoDB 使用angular-stripe
尝试为测试卡充电时,我收到此错误
angular.js:13424 TypeError: Cannot read property 'amount' of null
at config.js:688
at processQueue (angular.js:15757)
at angular.js:15773
at Scope.$eval (angular.js:17025)
at Scope.$digest (angular.js:16841)
at Scope.$apply (angular.js:17133)
at done (angular.js:11454)
at completeRequest (angular.js:11652)
at XMLHttpRequest.requestLoaded (angular.js:11593)
config.js:697 Other error occurred, possibly with your API Cannot read property 'amount' of null
以下是代码:
Node.js的
app.post('/payment', function(req, res){
var stripeToken = req.body.token;
var charge = stripe.charges.create({
amount: 19999, // amount in cents, again
currency: "usd",
source: stripeToken,
description: "Example charge"
}, function(err, charge) {
if (err && err.type === 'StripeCardError') {
console.log(err);// The card has been declined
} else {
res.send({
success:true,
data: charge
});
}
});
});
角:
$scope.stripeCallback = function (card) {
return stripe.card.createToken(card)
.then(function(response){
console.log('token created for card ending in ', response.card.last4, response.id);
var payment = response;
payment.card = response.card;
payment.token = response.id;
console.log(payment);
return $http.post('/payment', payment);
})
.then(function (payment) {
console.log(payment);
console.log('successfully submitted payment for $', payment.data.data.amount);
$scope.successfulPayment = payment.data.data.amount;
})
.catch(function (err) {
if (err.type && /^Stripe/.test(err.type)) {
$scope.unsuccessfulPayment('Stripe error: ', err.message);
}
else{
console.log('Other error occurred, possibly with your API', err.message);
}
});
};
表格:
<form class="boxForm" name="checkoutForm" layout="column" layout-align="center center" stripe-form="stripeCallback">
<md-input-container>
<label>Card Number</label>
<input type="text" ng-model="card.number" payments-format="card" payments-validate="card" payments-type-model="type"/>
</md-input-container>
<md-input-container>
<label>Expiry Month</label>
<input type="text" ng-model="card.exp_month" payments-format="expiry" payments-validate="expiry" />
</md-input-container>
<label>Expiry year</label>
<input type="text" ng-model="card.exp_year" payments-format="expiry" payments-validate="expiry" />
</md-input-container>
<md-input-container>
<label>Card CVC</label>
<input type="text" ng-model="card.cvc" payments-validate="cvc" payments-format="cvc" payments-type-model="type"/>
</md-input-container>
<button ng-click="stripeCallback(card)" type="submit">Submit</button>
</form>
根据错误,它在第688行,所以我在第687行为$ http响应console.log(付款)添加了一个console.log,这很明显。尽管成功,服务器端响应不会在计费对象中发送任何数据。也就是说,尽管从客户端(cvc,card,exp_month,exp_year,stripetoken)传递了所有必填字段并且成功请求,但我似乎无法从服务器传递数据。这段代码以前工作过,让我更加困惑。似乎在服务器端,传递给异步函数的计费对象是没有数据是问题。
这显示了在传递给服务器之前根据客户端的请求创建的令牌。非常困惑,感谢任何帮助。
token created for card ending in 4242 tok_18h90xHXm9b68JK2Enr7JT9Q