Last active
June 5, 2019 10:30
-
-
Save Debojyoti-Chakraborty/5c2305c34ac504756dcf7946bb37bb8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import * as braintree from 'braintree-web'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.scss'] | |
}) | |
export class AppComponent implements OnInit { | |
hostedFieldsInstance: braintree.HostedFields; | |
cardholdersName: string; | |
ngOnInit() { | |
this.createBraintreeUI(); | |
} | |
createBraintreeUI() { | |
braintree.client.create({ | |
authorization: 'CLIENT_AUTHORIZATION' | |
}).then((clientInstance) => { | |
braintree.hostedFields.create({ | |
client: clientInstance, | |
styles: { | |
// Override styles for the hosted fields | |
}, | |
// The hosted fields that we will be using | |
// NOTE : cardholder's name field is not available in the field options | |
// and a separate input field has to be used incase you need it | |
fields: { | |
number: { | |
selector: '#card-number', | |
placeholder: '1111 1111 1111 1111' | |
}, | |
cvv: { | |
selector: '#cvv', | |
placeholder: '111' | |
}, | |
expirationDate: { | |
selector: '#expiration-date', | |
placeholder: 'MM/YY' | |
} | |
} | |
}).then((hostedFieldsInstance) => { | |
this.hostedFieldsInstance = hostedFieldsInstance; | |
hostedFieldsInstance.on('focus', (event) => { | |
const field = event.fields[event.emittedBy]; | |
const label = this.findLabel(field); | |
label.classList.remove('filled'); // added and removed css classes | |
// can add custom code for custom validations here | |
}); | |
hostedFieldsInstance.on('blur', (event) => { | |
const field = event.fields[event.emittedBy]; | |
const label = this.findLabel(field); // fetched label to apply custom validations | |
// can add custom code for custom validations here | |
}); | |
hostedFieldsInstance.on('empty', (event) => { | |
const field = event.fields[event.emittedBy]; | |
// can add custom code for custom validations here | |
}); | |
hostedFieldsInstance.on('validityChange', (event) => { | |
const field = event.fields[event.emittedBy]; | |
const label = this.findLabel(field); | |
if (field.isPotentiallyValid) { // applying custom css and validations | |
label.classList.remove('invalid'); | |
} else { | |
label.classList.add('invalid'); | |
} | |
// can add custom code for custom validations here | |
}); | |
}); | |
}); | |
} | |
// Tokenize the collected details so that they can be sent to your server | |
// called from the html when the 'Pay' button is clicked | |
tokenizeUserDetails() { | |
this.hostedFieldsInstance.tokenize({cardholderName: this.cardholdersName}).then((payload) => { | |
console.log(payload); | |
// submit payload.nonce to the server from here | |
}).catch((error) => { | |
console.log(error); | |
// perform custom validation here or log errors | |
}); | |
} | |
// Fetches the label element for the corresponding field | |
findLabel(field: braintree.HostedFieldsHostedFieldsFieldData) { | |
return document.querySelector('.hosted-field--label[for="' + field.container.id + '"]'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment