admin管理员组文章数量:1393107
Braintree's customer creation SDK system generates a nonce input field in the form as soon as you hit submit.
<input name="payment_method_nonce" type="hidden" value="nonce-here">
However, with Angular's ng-model input identification system I can't detect the dynamically generated input in my controller. I'm executing a function in my controller as soon as the form is submitted.
<form id="checkout" id="checkout" ng-submit="processForm(formData)">
As you can see, there's no way to collect the value of the nonce and submit it to a brain tree API mand such as creating a new user's payment method.
From the controller the data would be submitted to the braintree api mand below using $http.
gateway.customer.create({
creditCard: {
token: "creditCard123",
},
paymentMethodNonce: "nonce-from-the-client"
}, function (err, result) {
});
Am I going about this the wrong way? Should I just jerry-rig a solution using pure node even though this application is in Angular/express? Or should I use jquery/angular to implant a ng-model in the said input field?
Braintree's customer creation SDK system generates a nonce input field in the form as soon as you hit submit.
<input name="payment_method_nonce" type="hidden" value="nonce-here">
However, with Angular's ng-model input identification system I can't detect the dynamically generated input in my controller. I'm executing a function in my controller as soon as the form is submitted.
<form id="checkout" id="checkout" ng-submit="processForm(formData)">
As you can see, there's no way to collect the value of the nonce and submit it to a brain tree API mand such as creating a new user's payment method.
From the controller the data would be submitted to the braintree api mand below using $http.
gateway.customer.create({
creditCard: {
token: "creditCard123",
},
paymentMethodNonce: "nonce-from-the-client"
}, function (err, result) {
});
Am I going about this the wrong way? Should I just jerry-rig a solution using pure node even though this application is in Angular/express? Or should I use jquery/angular to implant a ng-model in the said input field?
Share Improve this question edited Feb 4, 2015 at 20:06 hairboat 65019 silver badges30 bronze badges asked Jan 20, 2015 at 4:11 Elliot RobertElliot Robert 3551 gold badge4 silver badges13 bronze badges3 Answers
Reset to default 6I work at Braintree on the SDK team.
You can use a callback to listen for the nonce instead of having it automatically written to the DOM.
braintree.setup('CLIENT_TOKEN', 'dropin', {
container: 'container',
paymentMethodNonceReceived: function (event, nonce) {
// Do something with the nonce here
}
});
This will also prevent the form from being automatically submitted on your behalf. You can read some further documentation here. If you continue to encounter issues, feel free to reach out to [email protected].
I got it working by experimenting with putting the Braintree setup code in the angular controller and writing the post url call in node. I wanted to use Angular for as much as possible but writing the urls in Angular .Config routes didn't seem to be possible. Both answers I received helped me get it working. Thank you both.
In controller
$scope.$watch('cToken', function (newVal, oldVal) { //Watch for client token to be loaded then place in braintree.setup api call.
if (!newVal) return;
if (newVal){
console.log('set braintree');
braintree.setup($scope.cToken, 'custom', {
id: "checkout",
paymentMethodNonceReceived: function (event, nonce) {
// Do something with the nonce here
event.preventDefault();
}
});
}
});
In node.js app.js
app.post('/api/generateNonce', routes.generateNonce);
exports.generateNonce = function(req, res){
var nonce = req.body.payment_method_nonce;
console.log('nonce '+nonce);
gateway.customer.create({
id: 'clientId2222',
paymentMethodNonce: nonce
}, function (err, result) {
var result = JSON.stringify(result);
console.log('result '+ result);
console.log(result.success);
console.log(result.message);
// e.g f28wm
});
res.send(nonce);
}
And front end.
<form name="creatClientForm" class="css-form" id="checkout" action="/api/generateNonce" method="POST" enctype="multipart/form-data">
<input data-braintree-name="number" value="4111111111111111">
<input data-braintree-name="expiration_date" value="10/20">
<input type="submit" id="submit" value="submitform">
</form>
As @kdetella said, using paymentMethodNonceReceived
should allow you to intercept the nonce instead of picking it up from the DOM.
If it would make things easier, you can use braintree-angular for a slightly neater integration with Angular. Here's an example of using paymentMethodNonceReceived
.
版权声明:本文标题:javascript - Angular controller can't detect Braintree payment nonce dynamically inputed field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744704610a2620766.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论