您好,我是 AngularJS 的新手,正在开发一个项目,在该项目中我正在创建带有 Stripe 的付款表单。我已经创建了表单并创建了 JS 代码,如 stripe 网站中所述。我得到了卡验证的真实响应,但付款方式在控制台上给了我这个错误“Uncaught TypeError:无法读取未定义的属性‘create’”,
以下是我的 HTML 代码:
<div class="checkout_popup" id="checkout_popup">
<div class="col-md-12"><h3>Form</h3></div>
<form id="payment-form" method="post">
<div class="col-md-12"><input type="email" id="email" placeholder="Email" /></div>
<div class="col-md-12"><input type="text" id="card-number" data-stripe="number" value="4242424242424242" placeholder="Card Number (16 Digit)"/></div>
<div class="col-md-12"><input type="text" id="card-cvc" placeholder="cvc" data-stripe="cvc" value="123" /></div>
<div class="col-md-12"><input type="text" id="card-expiry-month" data-stripe="exp_month" value="12" placeholder="Month Expire" /></div>
<div class="col-md-12"><input type="text" id="card-expiry-year" data-stripe="exp_year" value="2017" placeholder="Year Expire" /></div>
<div class="col-md-12"><input type="button" id="pay-now" value="Pay Now" ng-click="submitstripe()" /></div>
</form>
</div>
这是 JS 代码:
.controller('UserAccountController', function($scope, $http, $state, $stateParams, $filter) {
$scope.submitstripe = function(){
console.log('ready stripe');
Stripe.card.createToken({
number: document.getElementById('card-number').value,
cvc: document.getElementById('card-cvc').value,
exp_month: document.getElementById('card-expiry-month').value,
exp_year: document.getElementById('card-expiry-year').value
}, stripeResponseHandler);
return false;
};
})
function stripeResponseHandler(status, response) {
if (response.error) { // Problem!
console.log(response.error.message);
} else { // Token was created!
// Get the token ID:
var token = response.id;
// Insert the token into the form so it gets submitted to the server:
console.log('Credit card verified, your token is : '+token);
var email = document.getElementById('email').value;
var charge = Stripe.charges.create({
amount: 10, // Amount in cents
currency: "usd",
source: token,
description: "test charges"
}, function(err, charge) {
if (err && err.type === 'StripeCardError') {
// The card has been declined
alert('Your card is not valid');
}
document.getElementById('card-number').value = '';
document.getElementById('card-cvc').value = '';
document.getElementById('card-expiry-month').value = '';
document.getElementById('card-expiry-year').value = '';
document.getElementById('checkout_popup').style.display = 'none';
alert('payment successfull');
});
}
}
最佳答案
您似乎正在尝试在客户端处理费用。但文档指出:
Use Stripe's API and your server-side code to process charges.
客户端库与 NodeJS 库不同。您尝试在不存在的客户端库上调用 stripe.charges
。该逻辑应该在服务器端创建。
如果您检查客户端库的源代码 here您将看到 charges
对象不存在。
相反,它可用 here在 NodeJS 库中
关于javascript - "Uncaught TypeError: Cannot read property ' 在 angularJS/NodeJS 中使用 strip 支付方法 Stripe.charges.create({}) 时创建 ' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803341/