npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-braintree-fields

v2.0.0

Published

React component for braintree hosted fields

Downloads

19,308

Readme

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/demo-class.jsx There is also a functional version available that illustrates how to work around the issue of storing a function reference using setState that was discovered in issue #20

Braintree Component

Props:

  • authorization: Required, either a tokenization key or a client token
  • onAuthorizationSuccess: Function that is called after Braintree successfully initializes the form
  • styles: Object containing valid field styles
  • onError: Function that is 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. If rejected it will return the error notice 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
  • options - an object containing any other valid Braintree options such as maskInput
  • 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.