Constructor
new HostedFields(options)
Do not use this constructor directly. Use braintree-web.hosted-fields.create instead.
Parameters:
| Name | Type | Description |
|---|---|---|
options |
object | Hosted Fields create options |
Methods
on(event, handler) → {void}
Subscribes a handler function to a named event, such as fieldEvent
Parameters:
| Name | Type | Description |
|---|---|---|
event |
string | The name of the event to subscribe to |
handler |
function | A callback to handle the event |
Example
Listening to a fieldEvent
var hostedFields = require('braintree-web/hosted-fields');
hostedFields.create({ ... }, function (err, instance) {
instance.on('fieldEvent', function (event) {
if (event.card != null) {
console.log(event.card.type);
} else {
console.log('Type of card not yet known');
}
});
});
setPlaceholder(field, placeholder, callbackopt) → {void}
Sets the placeholder of a field.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
field |
string | The field whose placeholder you wish to change. Must be a valid fieldOption. |
|
placeholder |
string | Will be used as the |
|
callback |
errback |
<optional> |
Callback executed on completion, containing an error if one occurred. No data is returned if the placeholder updated successfully. |
Examples
hostedFieldsInstance.setPlaceholder('number', '4111 1111 1111 1111', function (err) {
if (err) {
console.error(err);
}
});
Update CVV field on card type change
var cvvPlaceholder = 'CVV'; // Create a default value
hostedFieldsInstance.on('fieldEvent', function (event) {
if (event.target.fieldKey !== 'number') { return; } // Ignore all non-number field events
// Update the placeholder value if the card code name has changed
if (event.card && event.card.code.name !== cvvPlaceholder) {
cvvPlaceholder = event.card.code.name;
hostedFields.setPlaceholder('cvv', cvvPlaceholder, function (err) {
if (err) {
console.error(err);
}
});
}
});
teardown(callbackopt) → {void}
Cleanly tear down anything set up by create
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
callback |
errback |
<optional> |
Callback executed on completion, containing an error if one occurred. No data is returned if teardown completes successfully. |
Example
hostedFieldsInstance.teardown(function (err) {
if (err) {
console.error('Could not tear down Hosted Fields!');
} else {
console.info('Hosted Fields has been torn down!');
}
});
tokenize(callback) → {void}
Attempts to tokenize fields, returning a nonce payload
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
errback | The second argument, |
Example
hostedFieldsInstance.tokenize(function (err, payload) {
if (err) {
console.error(err);
} else {
console.log('Got nonce:', payload.nonce);
}
});
Type Definitions
tokenizePayload :object
Properties:
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
nonce |
string | The payment method nonce |
|||||||||
details |
object | Additional account details Properties
|
|||||||||
description |
string | A human-readable description |
Events
fieldEvent :object
This event is emitted when activity within one or more inputs has resulted in a change of state, such as a field attaining focus, an input becoming empty, or the user entering enough information for us to guess the type of card.
Properties:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
type |
string |
|
|||||||||||||||||||||||||
isEmpty |
boolean | Whether or not the user has entered a value in the input |
|||||||||||||||||||||||||
isFocused |
boolean | Whether or not the input is currently focused |
|||||||||||||||||||||||||
isPotentiallyValid |
boolean | A determination based on the future validity of the input value.
This is helpful when a user is entering a card number and types |
|||||||||||||||||||||||||
isValid |
boolean | Whether or not the value of the associated input is fully qualified for submission |
|||||||||||||||||||||||||
target |
object |
Properties
|
|||||||||||||||||||||||||
card |
object |
<nullable> |
If not enough information is available, or if there is invalid data, this value will be Properties
|
Example
Listening to a fieldEvent
var hostedFields = require('braintree-web/hosted-fields');
hostedFields.create({ ... }, function (err, instance) {
instance.on('fieldEvent', function (event) {
console.log('fieldEvent', event.type', triggered on "', event.target.fieldKey, '" field');
if (event.card != null) {
console.log(event.card.type);
} else {
console.log('Type of card not yet known');
}
});
});