### Installing react-payment-inputs with npm
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Installs the react-payment-inputs library and its dependencies using the npm package manager.
```Shell
npm install react-payment-inputs --save
```
--------------------------------
### Installing react-payment-inputs with Yarn
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Installs the react-payment-inputs library and its dependencies using the Yarn package manager.
```Shell
yarn add react-payment-inputs
```
--------------------------------
### Integrating react-payment-inputs with React Final Form (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
This snippet demonstrates integrating `react-payment-inputs` with React Final Form. Similar to the Formik example, it uses `usePaymentInputs` to get input props and validation meta. These are then connected to React Final Form's `Field` components, using the `input` prop provided by React Final Form's render prop pattern. The validation logic mirrors the Formik example, translating `react-payment-inputs` errors into the format expected by React Final Form.
```jsx
import { Form, Field } from 'react-final-form';
import { PaymentInputsWrapper, usePaymentInputs } from 'react-payment-inputs';
function PaymentForm() {
const {
meta,
getCardImageProps,
getCardNumberProps,
getExpiryDateProps,
getCVCProps,
wrapperProps
} = usePaymentInputs();
return (
)}
);
}
```
--------------------------------
### Apply Props to ZIP Input (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Provides an example of applying props from `getZIPProps` to the ZIP code input, ensuring custom event handlers are correctly integrated by passing them within the function call.
```jsx
```
--------------------------------
### Apply Props to Card Image SVG (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Shows how to use `getCardImageProps` to apply necessary props to an SVG element for displaying the card type image. This example includes importing the default images provided by the library.
```jsx
import images from 'react-payment-inputs/images';
```
--------------------------------
### Using usePaymentInputs Hook in React
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Demonstrates how to use the usePaymentInputs hook in a React functional component to manage payment input fields. It shows how to get input props and display validation errors.
```JSX
import React from 'react';
import { usePaymentInputs } from 'react-payment-inputs';
export default function PaymentInputs() {
const { meta, getCardNumberProps, getExpiryDateProps, getCVCProps } = usePaymentInputs();
return (
);
}
```
--------------------------------
### Integrating react-payment-inputs with Formik (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
This snippet shows how to integrate `react-payment-inputs` with Formik. It uses the `usePaymentInputs` hook to get input props and validation meta, and then wires these into Formik's `Field` components. Formik handles the form state and submission, while `react-payment-inputs` provides the input logic and validation details via the `meta` object. The validation function maps `react-payment-inputs` errors to Formik's error structure.
```jsx
import { Formik, Field } from 'formik';
import { PaymentInputsWrapper, usePaymentInputs } from 'react-payment-inputs';
function PaymentForm() {
const {
meta,
getCardImageProps,
getCardNumberProps,
getExpiryDateProps,
getCVCProps,
wrapperProps
} = usePaymentInputs();
return (
console.log(data)}
validate={() => {
let errors = {};
if (meta.erroredInputs.cardNumber) {
errors.cardNumber = meta.erroredInputs.cardNumber;
}
if (meta.erroredInputs.expiryDate) {
errors.expiryDate = meta.erroredInputs.expiryDate;
}
if (meta.erroredInputs.cvc) {
errors.cvc = meta.erroredInputs.cvc;
}
return errors;
}}
>
{({ handleSubmit }) => (
)}
);
}
```
--------------------------------
### Access Input-Specific Errors (JavaScript)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Illustrates how to access the `meta.erroredInputs` object to get individual validation error messages for each payment input field. The object keys correspond to input names.
```javascript
const { meta } = usePaymentInputs();
console.log(meta.erroredInputs);
/*
{
cardNumber: undefined,
expiryDate: 'Enter an expiry date',
cvc: 'Enter a CVC'
}
*/
```
--------------------------------
### Custom Card Number Validator Function (JS)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Provides an example of a custom validator function for the card number input. This function checks if the card type is either Visa or Mastercard and returns an error message if it's not. It demonstrates how to pass this custom validator to the `usePaymentInputs` hook options.
```JS
const cardNumberValidator = ({ cardNumber, cardType, errorMessages }) => {
if (cardType.displayName === 'Visa' || cardType.displayName === 'Mastercard') {
return;
}
return 'Card must be Visa or Mastercard';
}
export default function MyComponent() {
const { ... } = usePaymentInputs({
cardNumberValidator
});
}
```
--------------------------------
### Apply Props to CVC Input (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Illustrates applying props from `getCVCProps` to the CVC input, highlighting the correct way to pass custom event handlers like `onBlur` and `onChange` within the function call.
```jsx
```
--------------------------------
### Integrating React Payment Inputs with Fannypack (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
This snippet demonstrates how to integrate the `usePaymentInputs` hook with Fannypack UI components to create payment input fields. It shows how to spread the hook's props onto the input elements and display validation errors based on the `meta` object.
```jsx
import React from 'react';
import { FieldSet, InputField } from 'fannypack';
import { usePaymentInputs } from 'react-payment-inputs';
import images from 'react-payment-inputs/images';
export default function PaymentInputs() {
const {
meta,
getCardNumberProps,
getExpiryDateProps,
getCVCProps
} = usePaymentInputs();
const { erroredInputs, touchedInputs } = meta;
return (
);
}
```
--------------------------------
### Using PaymentInputsWrapper with usePaymentInputs Hook (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Shows how to use the `PaymentInputsWrapper` component along with the `usePaymentInputs` hook to create styled payment input fields. It utilizes prop getters from the hook (`wrapperProps`, `getCardImageProps`, etc.) to apply necessary props to the wrapper, image, and input elements. Requires `styled-components` as a dependency.
```JSX
import React from 'react';
import { PaymentInputsWrapper, usePaymentInputs } from 'react-payment-inputs';
import images from 'react-payment-inputs/images';
export default function PaymentInputs() {
const {
wrapperProps,
getCardImageProps,
getCardNumberProps,
getExpiryDateProps,
getCVCProps
} = usePaymentInputs();
return (
);
}
```
--------------------------------
### Custom Error Messages Configuration (JS)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Illustrates how to define and use custom error messages for the payment inputs. An object containing specific error keys and their desired message strings is created and then passed to the `errorMessages` option of the `usePaymentInputs` hook.
```JS
const ERROR_MESSAGES = {
emptyCardNumber: 'El número de la tarjeta es inválido',
invalidCardNumber: 'El número de la tarjeta es inválido',
emptyExpiryDate: 'La fecha de expiración es inválida',
monthOutOfRange: 'El mes de expiración debe estar entre 01 y 12',
yearOutOfRange: 'El año de expiración no puede estar en el pasado',
dateOutOfRange: 'La fecha de expiración no puede estar en el pasado',
invalidExpiryDate: 'La fecha de expiración es inválida',
emptyCVC: 'El código de seguridad es inválido',
invalidCVC: 'El código de seguridad es inválido'
}
export default function MyComponent() {
const { ... } = usePaymentInputs({
errorMessages: ERROR_MESSAGES
});
}
```
--------------------------------
### Apply Props to Expiry Date Input (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Shows how to apply props from `getExpiryDateProps` to the expiry date input, emphasizing the need to include custom event handlers within the function call to avoid overriding default handlers.
```jsx
```
--------------------------------
### Apply Props to Card Number Input (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Demonstrates how to apply props returned by `getCardNumberProps` to an input element, including custom event handlers like `onBlur` and `onChange`. Custom handlers must be passed within the function call.
```jsx
```
--------------------------------
### Using PaymentInputsContainer with Render Props (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Demonstrates how to use the `PaymentInputsContainer` component with the render props pattern. It shows how to access prop getter functions (`getCardNumberProps`, `getExpiryDateProps`, `getCVCProps`) and meta data from the render props to build custom payment input fields. Event handlers like `onChange` must be passed within the prop getter functions.
```JSX
import React from 'react';
import { PaymentInputsContainer } from 'react-payment-inputs';
export default function PaymentInputs() {
return (
{({ meta, getCardNumberProps, getExpiryDateProps, getCVCProps }) => (
)}
);
}
```
--------------------------------
### Access Input-Specific Touched State (JavaScript)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Demonstrates how to access the `meta.touchedInputs` object to check the touched state (whether the user has interacted with) each payment input field. The object keys correspond to input names.
```javascript
const { meta } = usePaymentInputs();
console.log(meta.touchedInputs);
/*
{
cardNumber: true,
expiryDate: true,
cvc: false
}
*/
```
--------------------------------
### Access Current Card Type Metadata (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Demonstrates how to access the `meta.cardType` object from the `usePaymentInputs` hook to retrieve and display information about the currently detected card type, such as its display name.
```jsx
const { meta } = usePaymentInputs();
Current card: {meta.cardType.displayName}
```
--------------------------------
### Access Global Error Message (JavaScript)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Shows how to retrieve the overall validation error message across all inputs using `meta.error` from the `usePaymentInputs` hook. This property holds a single string representing the highest priority error.
```javascript
const { meta } = usePaymentInputs();
console.log(meta.error); // "Card number is invalid"
```
--------------------------------
### Integrating React Payment Inputs with Bootstrap (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
This snippet illustrates how to use the `usePaymentInputs` hook with React Bootstrap form components. It applies the hook's props to `Form.Control` elements and uses Bootstrap's `isInvalid` prop and `Form.Control.Feedback` for displaying validation errors.
```jsx
import React from 'react';
import { FieldSet, InputField } from 'fannypack';
import { usePaymentInputs } from 'react-payment-inputs';
import images from 'react-payment-inputs/images';
export default function PaymentInputs() {
const {
meta,
getCardNumberProps,
getExpiryDateProps,
getCVCProps
} = usePaymentInputs();
const { erroredInputs, touchedInputs } = meta;
return (
Card number{erroredInputs.cardNumber}Expiry date{erroredInputs.expiryDate}CVC{erroredInputs.cvc}
);
}
```
--------------------------------
### Access Currently Focused Input (JavaScript)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
Shows how to access the `meta.focused` string to determine which payment input field currently has focus. The value is the name of the focused input ('cardNumber', 'expiryDate', 'cvc', or 'zip').
```javascript
const { meta } = usePaymentInputs();
console.log(meta.focused); // "cardNumber"
```
--------------------------------
### Customizing React Payment Inputs Wrapper Styles (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
This snippet shows how to apply custom styles to the PaymentInputsWrapper and its internal elements (field wrapper, input wrapper, input, error text) using the styles prop. It utilizes styled-components' css helper for defining styles based on different states (base, errored, focused) and input types (cardNumber, expiryDate, cvc).
```JSX
import { css } from 'styled-components';
import { usePaymentInputs, PaymentInputsWrapper } from 'react-payment-inputs';
function PaymentForm() {
const {
getCardNumberProps,
getExpiryDateProps,
getCVCProps,
wrapperProps
} = usePaymentInputs();
return (
);
}
```
--------------------------------
### Providing Custom Card Images for React Payment Inputs (JSX)
Source: https://github.com/medipass/react-payment-inputs/blob/master/README.md
This snippet illustrates how to replace the default card images by passing a custom images object to the getCardImageProps function. The images object should contain SVG paths or elements keyed by card type names (e.g., 'mastercard'). The custom image is rendered within an