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

payment-fields

v0.7.1

Published

React component for Braintree/Stripe/Square payment fields

Downloads

181

Readme

Integrate Braintree/Stripe/Square payment fields

A React component to make integrating Braintree's Hosted Fields, Stripe's Elements and Square's Payment Form easier.

Care is taken so the API is (nearly) identical across the vendors.

This is intended for a Saas that allows customers to use their own payment processor, as long as it uses the newish "hosted iframe" approach.

further docs to be written

Build Status

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

Example

Note: methods are removed for brevity and this isn't fully copy & pastable. For a working example see demo.jsx


import React from 'react';
import PaymentFields from 'payment-fields';
import PropTypes from 'prop-types';

class PaymentForm extends React.PureComponent {

    static propTypes = {
        vendor: PropTypes.oneOf(['Square', 'Stripe', 'Braintree']).isRequired,
        authorization: PropTypes.String.isRequired,
    }

    render() {
      return (
        <PaymentFields
            vendor={this.props.vendor}
            authorization={this.props.authorization}
            onError={this.onError}
            onValidityChange={(ev) => this.setState({ valid: ev.isValid })}
            onCardTypeChange={(c)  => this.setState({ card: c.brand })}
            onReady={this.onFieldsReady}
            styles={{
                base: {
                    'font-size': '24px',
                    'font-family': 'helvetica, tahoma, calibri, sans-serif',
                    padding: '6px',
                    color: '#7d6b6b',
                },
                focus: { color: '#000000' },
                valid: { color: '#00bf00' },
                invalid: { color: '#a00000' },
            }}
        >
            <h4>Form is Valid: {this.state.isValid ? '👍' : '👎'}</h4>
            <p>Card number ({this.state.card}):</p>
            <PaymentFields.Field
                type="cardNumber"
                placeholder="•••• •••• •••• ••••"
                onBlur={this.logEvent}
                onFocus={this.logEvent}
                onValidityChange={this.onNumberValid}
                onChange={this.logEvent}
            />
            Date: <PaymentFields.Field type="expirationDate" />
            CVV: <PaymentFields.Field type="cvv" />
            Zip: <PaymentFields.Field type="postalCode" />
        </PaymentFields>
     );
  }

}

PaymentFields Component

Props:

  • vendor: Required, one of Braintree, Square, or Stripe
  • authorization: Required, the string key that corresponds to:
    • Braintree: calls it "authorization"
    • Square: "applicationId"
    • Stripe: the Api Key for Stripe Elements
  • onReady: function called once form fields are initialized and ready for input
  • onValidityChange: function that is called whenever the card validity changes. May be called repeatedly even if the validity is the same as the previous call. Will be passed a single object with a isValid property. The object may have other type specific properties as well.
  • onCardTypeChange: Called as soon as the card type is known and whenever it changes. Passed a single object with a brand property. The object may have other type specific properties as well.
  • onError: A function called whenever an error occurs, typically during tokenization but some vendors (Square at least) will also call it when the fields fail to initialize.
  • styles: A object that contains 'base', 'focus', 'valid', and 'invalid' properties. The PaymentFields component will convert the styles to match each vendor's method of specifying them and attempt to find the lowest common denominator. color and font-size are universally supported.
  • passThroughStyles: For when the styles property doesn't offer enough control. Anything specified here will be passed through to the vendor specific api in place of the styles.
  • tagName: which element to use as a wrapper element. Defaults to div
  • className: a className to set on the wrapper element, it's applied in addition to payment-fields-wrapper

PaymentFields.Field Component

Props:

  • type: Required, one of 'cardNumber', 'expirationDate', 'cvv', 'postalCode' Depending on fraud settings, some vendors do not require postalCode.
  • placeholder: What should be displayed in the field when it's empty and is not focused
  • className: a className to set on the placeholder element, some vendors will replace the placeholder with an iframe, while others will render the iframe inside the placeholder. All vendors retain the className property though so it's safe to use this for some styling.
  • onValidityChange: A function called when the field's validity changes. Like the onValidityChange on the main PaymentFields wrapper, may be called repeatedly with the same status
  • onFocus: A function called when the field is focused. Will be called with the vendor specific event
  • onBlur: A function called when the field loses focus. Will be called with the vendor specific event, as well as a isValid property that indicates if the field is valid, and isPotentiallyValid which is set if the input is possibily valid but still incomplete.