Skip to content

Instantly share code, notes, and snippets.

@jshuadvd
Created June 8, 2017 01:05
Show Gist options
  • Save jshuadvd/700b503c72440a0478329fb1ceb3254f to your computer and use it in GitHub Desktop.
Save jshuadvd/700b503c72440a0478329fb1ceb3254f to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/bitoqapine
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<form action="/" id="my-sample-form" method="post">
<label for="card-number">Card Number</label>
<div id="card-number"></div>
<label for="cvv">CVV</label>
<div id="cvv"></div>
<label for="expiration-date">Expiration Date</label>
<div id="expiration-date"></div>
<input type="submit" value="Pay" />
</form>
<script src="https://js.braintreegateway.com/web/3.17.0/js/client.min.js"></script>
<script src="https://js.braintreegateway.com/web/3.17.0/js/hosted-fields.min.js"></script>
<script>
var form = document.querySelector('#my-sample-form');
var submit = document.querySelector('input[type="submit"]');
var clientToken = "eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiIwZDJjZTZhOWJjMjE2MWMzMGVlNjAyYzU4MTMwOGQ1Y2NjMWE2NDY1N2QzOWNhZWIzZTVmMjg5YTBhZWRjYTE4fGNyZWF0ZWRfYXQ9MjAxNy0wNi0wN1QyMToyOTowMC4xOTM4NDc3NzUrMDAwMFx1MDAyNm1lcmNoYW50X2lkPTM0OHBrOWNnZjNiZ3l3MmJcdTAwMjZwdWJsaWNfa2V5PTJuMjQ3ZHY4OWJxOXZtcHIiLCJjb25maWdVcmwiOiJodHRwczovL2FwaS5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvMzQ4cGs5Y2dmM2JneXcyYi9jbGllbnRfYXBpL3YxL2NvbmZpZ3VyYXRpb24iLCJjaGFsbGVuZ2VzIjpbXSwiZW52aXJvbm1lbnQiOiJzYW5kYm94IiwiY2xpZW50QXBpVXJsIjoiaHR0cHM6Ly9hcGku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TRCJ9LCJjb2luYmFzZUVuYWJsZWQiOmZhbHNlLCJtZXJjaGFudElkIjoiMzQ4cGs5Y2dmM2JneXcyYiIsInZlbm1vIjoib2ZmIn0=";
braintree.client.create({
authorization: clientToken
}, function (clientErr, clientInstance) {
if (clientErr) {
console.error(clientErr);
return;
}
// This example shows Hosted Fields, but you can also use this
// client instance to create additional components here, such as
// PayPal or Data Collector.
braintree.hostedFields.create({
client: clientInstance,
styles: {
'input': {
'font-size': '14px'
},
'input.invalid': {
'color': 'red'
},
'input.valid': {
'color': 'green'
}
},
fields: {
number: {
selector: '#card-number',
placeholder: 'Enter your card.'
},
cvv: {
selector: '#cvv',
placeholder: 'CVV'
},
expirationDate: {
selector: '#expiration-date',
placeholder: '00/00'
}
}
}, function (hostedFieldsErr, hostedFieldsInstance) {
if (hostedFieldsErr) {
console.error(hostedFieldsErr);
return;
}
submit.removeAttribute('disabled');
form.addEventListener('submit', function (event) {
event.preventDefault();
hostedFieldsInstance.tokenize(function (tokenizeErr, payload) {
console.log("PAYLOAD:", payload);
if (tokenizeErr) {
console.error(tokenizeErr);
return;
}
// If this was a real integration, this is where you would
// send the nonce to your server.
console.log('Got a nonce: ' + payload.nonce);
});
}, false);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment