import React from 'react'; import { Braintree, HostedField } from '../src/index'; class Demo extends React.PureComponent { constructor(props) { super(props); this.numberField = React.createRef(); this.braintree = React.createRef(); [ 'onError', 'getToken', 'onCardTypeChange', 'onAuthorizationSuccess', ].forEach(prop => (this[prop] = this[prop].bind(this))); } state = {} onError(error) { this.setState({ error: error.message || String(error) }); } getToken() { this.tokenize().then( token => this.setState({ token, error: null }), ).catch(error => this.onError(error)); } onCardTypeChange({ cards }) { if (1 === cards.length) { const [card] = cards; this.setState({ card: card.type }); if (card.code && card.code.name) { this.cvvField.setPlaceholder(card.code.name); } else { this.cvvField.setPlaceholder('CVV'); } } else { this.setState({ card: '' }); this.cvvField.setPlaceholder('CVV'); } } state = { numberFocused: false, } componentDidMount() { this.setState({ authorization: 'sandbox_g42y39zw_348pk9cgf3bgyw2b' }); } renderResult(title, obj) { if (!obj) { return null; } return (
{JSON.stringify(obj, null, 4)}
Card type: {this.state.card}
Name: