Use Promises with braintree-web
mainAll asynchronous methods in the Braintree SDK return a Promise if no callback is provided. This allows for cleaner async/await or .then() chains.
braintree.client
.create({ authorization: CLIENT_AUTHORIZATION })
.then(function (client) {
return braintree.hostedFields.create({
client: client,
styles: yourStylesConfig,
fields: yourFieldsConfig,
});
})
.then(function (hostedFields) {
return hostedFields.tokenize();
})
.then(function (payload) {
// send payload.nonce to your server
})
.catch(function (err) {
console.error(err);
});