1.2.4 • Published 7 years ago

react-square-hosted-fields v1.2.4

Weekly downloads
119
License
Apache-2.0
Repository
bitbucket
Last release
7 years ago

React Square Hosted Fields

A react component for Square's hosted fields

What this Is

A quick way of plugging in your App ID to Square's hosted payment fields in a react environment.

Getting Started

You can get started with the React payment form in a few easy steps:

npm install --save react-square-hosted-fields

Simple Usage

Import the payment form:

import SquarePaymentForm from 'react-square-hosted-fields';

Put the payment form anywhere:

<SquarePaymentForm appId="YOUR-SQUARE-APPLICATION-ID" onNonceGenerated={this.handleNonce} />

Using a GET Resource

You can either plug in your application id directly via the appId prop as it's done above, or use the get prop to specify a get resource.

<SquarePaymentForm get="/some/api/path" onNonceGenerated={this.handleNonce} onGetAppIdError={this.handleGetError} />

Don't forget to handle an error with your resource! The component expects the resource to return a json object like this:

{ appId: 'YOUR-SQUARE-APPLICATION-ID' }

Square Options

A majority of the options match what goes into Square's SqPaymentForm. See the docs for more clarification there.

PropTypeDefaultDescription
appIdString'' (Required ifn get)Your Square Application ID
getString'' (Required ifn appId)URL or path to a GET resouce, handled with promises
submitTextStringSubmit PaymentThe text that shows up on the submit button to generate a nonce
inputClassStringsq-inputThe class name(s) that will be put into iframe elements for styling the inputs
inputStylesArray[]Array of input style objects as per SqPaymentForm docs
cardNumber.elementIdStringsq-card-numberThe element id that SqPaymentForm will replace with the card number field
cardNumber.placeholderStringE.g. XXXX-XXXX-XXXX-XXXXThe placeholder text for the card number field once replaced by SqPaymentForm
cvv.elementIdStringsq-cvvThe element id that SqPaymentForm will replace with the CVV field
cvv.placeholderStringE.g. XXXThe placeholder text for the CVV field once replaced by SqPaymentForm
expirationDate.elementIdStringsq-expiration-dateThe element id that SqPaymentForm will replace with the expiration date field
expirationDate.placeholderStringMM/YYThe placeholder text for the expiration date field once replaced by SqPaymentForm
postalCode.elementIdStringsq-postal-codeThe element id that SqPaymentForm will replace with the postal code field
postalCode.placeholderStringE.g. XXXXXThe placeholder text for the postal code field once replaced by SqPaymentFor

Callbacks

The component comes with a few callbacks that you can use to loading or user events with the form. See Square's SqPaymentForm for more details on SqPaymentForm

PropParametersFires
onGetAppIdErrorObject axiosErrorIf the get prop is specified and the API resource returns an error code
onScriptCreatedAs soon as the SqPaymentForm client library script tags are created and put into the DOM
onScriptErrorWhen the SqPaymentForm client library encounters an error while loading
onScriptLoadedWhen the SqPaymentForm client library is finished loading and ready for use
onPaymentFormLoadedAfter a new instance of SqPaymentForm is created
onInputEventReceivedString inputEventWhenever a user does something to the payment form, Square fires their own inputEvent((https://docs.connect.squareup.com/articles/adding-payment-form) object
onUnsupportedBrowserDetectedIf SqPaymentForm has determined the user's browser is not supported
onNonceRequestedAs soon as the user clicks the submit button for the form (throws an error if it happens before the payment form is generated)
onNonceGeneratedString nonce, Object cardDataWhen a payment nonce has been generated successfully
onNonceErrorArray errorsWhen Square reports an error while generating a payment nonce

Styling Options

There are also a few options specific to this package that can help style the form. Note that no styling is done on the form other than what Square does by default.

PropTypeDefaultDescription
componentWrapperClassStringsq-component-wrapperClass name(s) for the div that wraps the entire component
formWrapperClassStringsq-form-wrapperClass name(s) for the div that wraps the entire form
fieldsWrapperClassStringsq-fields-wrapperClass name(s) for the div that wraps only the input fields (not the submit button)
buttonWrapperClassStringsq-button-wrapperClass name(s) for the div that wraps only the submit button
inputWrapperClassStringsq-input-wrapperClass name(s) for the div that wraps each input field (iframe)
cardWrapperClassStringsq-card-wrapperClass name(s) for the div that wraps the card input field (would override inputWrapperClass)
cvvWrapperClassStringsq-cvv-wrapperClass name(s) for the div that wraps the cvv input field (would override inputWrapperClass)
expirationWrapperClassStringsq-expiration-wrapperClass name(s) for the div that wraps the expiration date input field (would override inputWrapperClass)
postalWrapperClassStringsq-postal-wrapperClass name(s) for the div that wraps the postal code input field (would override inputWrapperClass)

The submit button can be styled using .buttonWrapperClass button, as it's simply a button element.

Using Custom Elements

The component also supports the ability to use children within the component itself if you already have a payment form styled.

<SquarePaymentForm get="/some/api/resource" onNonceGenerated={this.handleNonce}>
  <div className="some-custom-wrapper">
    <CustomWrapper>
      <CustomComponent>Card Number</CustomComponent>
      <div className="my-own-class" id="sq-card-number"></div>
    </CustomWrapper>

    <CustomWrapper>
      <CustomComponent>Card Number</CustomComponent>
      <div className="my-own-class" id="sq-cvv"></div>
    </CustomWrapper>
    
    <CustomWrapper>
      <CustomComponent>Card Number</CustomComponent>
      <div className="my-own-class" id="sq-expiration-date"></div>
    </CustomWrapper>
    
    <CustomWrapper>
      <CustomComponent>Card Number</CustomComponent>
      <div className="my-own-class" id="sq-postal-code"></div>
    </CustomWrapper>
  </div>
</SquarePaymentForm>

Do not forget to include the id props for your divs and make sure they match with the props elementId props you send into the component (the defaults are used in the example). If you don't, SqPaymentForm will not be able to find the divs to replace with iframes.

Note that the submit payment button will be included as part of the component. If you have a way around this, please suggest it.

More Information

If you see something you think is missing or would like to contribute, please check out the Bitbucket repo

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago