Skip to content
React components for Braintree hosted fields
Branch: master
Clone or download
Latest commit 93c752a May 7, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs Update demo files for ghpages May 7, 2019
script Adding build script so we can get a dist folder Oct 18, 2017
specs update field id to have "braintree" prefix Aug 6, 2018
src expose client May 7, 2019
.eslintrc.js Revert "Didn’t run `ci` task after removing eslint argosity package" Oct 18, 2017
.gitignore Revert "Adding dist/ so we can refer directly to this branch via gith… Oct 18, 2017
.npmignore initial commit Apr 7, 2017
.travis.yml
README.md updated readme May 11, 2018
babel.config.js update dependencies May 7, 2019
demo.jsx use createRef, add ref to braintree May 7, 2019
jest.config.js update dependencies May 7, 2019
package-lock.json update dependencies May 7, 2019
package.json update dependencies May 7, 2019
rollup.config.js Update demo files for ghpages May 11, 2018
webpack.config.js update dependencies including rollup config May 11, 2018

README.md

React components to integrate Braintree hosted fields

A few small React components to make integrating Braintree's Hosted Fields easier.

Build Status

See also

I've also written a vendor agnostic library PaymentFields It's an extension of this library to also support Square and Stripe. You might check that out if you ever think you'll need to support additional processors.

Introduction

import { Braintree, HostedField } from 'react-braintree-fields';
class MySillyCheckoutForm extends React.PureComponent {

    function onSubmit() {
        // could also obtain a reference to the Braintree wrapper element and call `.tokenize()`
       this.getToken({ cardholderName: 'My Order Name' }).then((payload) => {
         console.log("nonce=" , payload.nonce)
         console.log("device_data", this.device_data)
       })
    }

    onCardTypeChange() {
        this.setState({ card: (1 === cards.length) ? cards[0].type : '' });
    }

    function onFocus(event) {
        console.log("number is focused", event);
    }

    onError(err) {
       console.warn(err);
       this.ccNum.focus(); // focus number field
    }

    onAuthorizationSuccess() {
      this.setState({ isBraintreeReady : true });
    }
    
    onDataCollectorInstanceReady(err, dataCollectorInstance) {
      if(!err) this.device_data = dataCollectorInstance.deviceData
    }

    render() {
        return (
            <Braintree
                className={ this.state.isBraintreeReady ? '' : 'disabled' }
                authorization='sandbox_g42y39zw_348pk9cgf3bgyw2b'
                onAuthorizationSuccess={this.onAuthorizationSuccess}
                onDataCollectorInstanceReady={this.onDataCollectorInstanceReady}
                onError={this.handleError}
                onCardTypeChange={this.onCardTypeChange}
                getTokenRef={ref => (this.getToken = ref)}
                styles={{
                    'input': {
                        'font-size': '14px',
                        'font-family': 'helvetica, tahoma, calibri, sans-serif',
                        'color': '#3a3a3a'
                    },
                    ':focus': {
                        'color': 'black'
                    }
                }}
            >
                <div className="fields">
                    <HostedField type="number" onFocus={onFocus} ref={ccNum => (this.ccNum = ccNum)} />
                    <HostedField type="expirationDate" />
                    <HostedField type="cvv" />
                </div>
                <button onClick={onSubmit}>Submit</button>
            </Braintree>
        );
    }
}

See demo site for a working example. It renders demo.jsx

Braintree Component

Props:

  • authorization: Required, either a tokenization key or a client token
  • onAuthorizationSuccess: Function that will be called after Braintree successfully initializes the form.
  • styles: Object containing valid field styles
  • onError: Function that will be called if an Braintree error is encountered.
  • getTokenRef: A function that will be called once Braintree the API is initialized. It will be called with a function that can be used to initiate tokenization.
    • The tokenization function will return a Promise which will be either resolved or rejected. If resolved, the promise payload will contain an object with the nonce and other data from Braintree.
  • onDataCollectorInstanceReady: A function that will be called with the results of Braintree.dataCollector.create. This can be used in conjunction with Braintree's Advanced Fraud Tools.

HostedField Component

Props:

  • type: Required, one of:
    • 'number', 'expirationDate', 'expirationMonth', 'expirationYear', 'cvv', 'postalCode'
  • onBlur
  • onFocus
  • onEmpty
  • onNotEmpty
  • onValidityChange
  • onCardTypeChange - accepted on any field, but will only be called by type="number"
  • placeholder - A string to that will be displayed in the input while it's empty
  • formatInput
  • maxlength,
  • minlength
  • select
  • prefill

See the Braintree api docs for more details

Fields also have "focus" and "clear" methods. These may be called by obtaining a reference to the field.

You can’t perform that action at this time.