React components for payments:
<CardForm>
: credit card entry (with validation)<BankForm>
: bank account entry (with validation)<PaymentMethods>
: list of payment methods (with add / remove buttons)
You can configure/modify some things with props and CSS, and if you need to do any further customization, they're small filesβsend me a quick PR!
Demo
CardForm
BankForm
PaymentMethods
Usage
yarn add react-payment
Since this library uses Material-UI components, you need to have a Material-UI theme. To get the default style, just wrap this module's components in a <MuiThemeProvider>
tag (see the full example).
The alternate syntax for partial imports is react-payment/dist/ComponentName
:
; OR ;
CardForm usage
<CardForm>
is a credit card form. By default it only has inputs for number, expiration, and CVC.
Props:
onSubmit(card => {})
getName
: show the name input, defaultfalse
getZip
: show the zip code input, defaultfalse
styles
: override styles on the elementsdefaultValues
: initial input values. Object of the form{ inputName: defaultString }
, and the input names are:name, number, expiration, cvc, zip
. Expiration is of the format"01/44"
for January 2044.
import CardForm from 'react-payment'; { const number exp_month exp_year cvc name zip = card; Stripecard;} <CardForm = = =/>
BankForm usage
<BankForm>
is a form for entering US bank account information.
If you would like BankForm
to intelligently validate the account & routing number, make sure that Stripe.js is loaded (see the full example below).
Props:
onSubmit(account => {})
defaultValues
: initial input values. Object of the form{ inputName: defaultString }
, and the input names arename, accountNumber, routingNumber
.
; { const name accountNumber routingNumber accountType = account; const account_holder_type = accountType === 'personal' ? 'individual' : 'company'; StripebankAccount;} <BankForm onSubmit=thisonSubmit/>
PaymentMethods usage
<PaymentMethods>
is a list of your credit cards and/or bank accounts.
Props:
showCards
: whether to show the card list & add buttonshowBanks
: whether to show the bank list & add buttoncards
: array of cards, in the format{ id: '1', last4: '1234', brand: 'visa' }
banks
: array of banks, in the format{ id: '1', last4: '1234' }
onAddCard
onAddBank
onRemoveCard(id => {})
onRemoveBank(id => {})
import PaymentMethods from 'react-payment'; <PaymentMethods = = = = = />
Full example
import CardForm BankForm PaymentMethods from 'react-payment';import React Component from 'react'import Dialog from 'material-ui/Dialog';import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; import server from './server'; let loadedStripe = false; state = dialogOpen: false cardDialog: true ; { if loadedStripe return; const script = document; scriptsrc = "https://js.stripe.com/v2/"; scripttype = "text/javascript"; scriptasync = true; script { Stripe; }; documentbody; loadedStripe = true; } { this; }; { this; }; { server; }; { server; }; { const number exp_month exp_year cvc name zip = card; Stripecard; }; { const name accountNumber routingNumber accountType = account; const account_holder_type = accountType === 'personal' ? 'individual' : 'company'; StripebankAccount }; { const title = thisstatecardDialog ? 'Add credit card' : 'Add bank account'; return <MuiThemeProvider> <PaymentMethods = = = = = = = = /> <Dialog = = = = > thisstatecardDialog ? <CardForm = = = /> : <BankForm = /> </Dialog> </MuiThemeProvider> ; }
Development
git clone git@github.com:lorensr/react-payment.gitnpm installnpm run storybook
Deployment
npm version patchnpm publish
npm run deploy-storybook
Credits
- Contributions by these fine folks
- Segmented control component from
segmented-control