npm install @stripe/react-stripe-js

React components for Stripe.js and Stripe Elements

About @stripe/react-stripe-js

The npm package "@stripe/react-stripe-js" is a powerful tool designed specifically for integrating Stripe's payment processing capabilities into React applications. This package provides a set of React components optimized for the use of Stripe.js and Elements, making it easier for developers to embed highly customizable payment forms directly into their web apps. By using "@stripe/react-stripe-js", developers can leverage Stripe’s robust infrastructure to handle complex payment functionalities securely and efficiently. The package ensures that payment information is handled safely, as it facilitates direct communication from the client's browser to Stripe's servers, bypassing the need to send sensitive data through your own backend.

To begin using the "@stripe/react-stripe-js" in your project, you can simply run the command `npm install @stripe/react-stripe-js`. This command installs the package into your project, allowing you to utilize the pre-built React components that streamline the integration of Stripe’s payment services. The installation is straightforward and once completed, developers can quickly implement features such as card payments, subscriptions, and one-time payments directly in their applications. The components provided by "@stripe/react-stripe-js" are designed to be both flexible and easy to customize, fitting seamlessly into the aesthetic and functional requirements of any React-based project.

The "@stripe/react-stripe-js" library is continuously updated to support the latest versions of React and to comply with the evolving security standards required by payment processors. This ensures that applications using this package remain compliant with global payment regulations, including PCI DSS requirements, thus safeguarding user data against potential security threats. The integration of "@stripe/react-stripe-js" not only enhances the security aspect of payment processing but also significantly improves the user experience by providing smooth and fast checkout flows.

More from stripe

stripe npm packages

Find the best node modules for your project.

Search npm

@stripe/react-stripe-js

React components for Stripe...

Read more
,

@stripe/stripe-react-native

Stripe SDK for React...

Read more

Dependencies

Core dependencies of this npm package and its dev dependencies.

prop-types, @arethetypeswrong/cli, @babel/cli, @babel/core, @babel/preset-env, @babel/preset-react, @rollup/plugin-babel, @rollup/plugin-commonjs, @rollup/plugin-node-resolve, @rollup/plugin-replace, @rollup/plugin-terser, @storybook/react, @stripe/stripe-js, @testing-library/jest-dom, @testing-library/react, @testing-library/react-hooks, @types/jest, @types/react, @types/react-dom, @typescript-eslint/eslint-plugin, @typescript-eslint/parser, babel-eslint, babel-jest, babel-loader, eslint, eslint-config-airbnb, eslint-config-prettier, eslint-plugin-import, eslint-plugin-jest, eslint-plugin-jsx-a11y, eslint-plugin-prettier, eslint-plugin-react, eslint-plugin-react-hooks, fork-ts-checker-webpack-plugin, jest, prettier, react, react-docgen-typescript-loader, react-dom, react-test-renderer, rimraf, rollup, rollup-plugin-ts, ts-jest, ts-loader, typescript

Documentation

A README file for the @stripe/react-stripe-js code repository. View Code

React Stripe.js

React components for Stripe.js and Elements.

build status npm version

Requirements

The minimum supported version of React is v16.8. If you use an older version, upgrade React to use this library. If you prefer not to upgrade your React version, we recommend using legacy react-stripe-elements.

Getting started

Documentation

Minimal example

First, install React Stripe.js and Stripe.js.

npm install @stripe/react-stripe-js @stripe/stripe-js

Using hooks

import React, {useState} from 'react';
import ReactDOM from 'react-dom';
import {loadStripe} from '@stripe/stripe-js';
import {
  PaymentElement,
  Elements,
  useStripe,
  useElements,
} from '@stripe/react-stripe-js';

const CheckoutForm = () => {
  const stripe = useStripe();
  const elements = useElements();

  const [errorMessage, setErrorMessage] = useState(null);

  const handleSubmit = async (event) => {
    event.preventDefault();

    if (elements == null) {
      return;
    }

    // Trigger form validation and wallet collection
    const {error: submitError} = await elements.submit();
    if (submitError) {
      // Show error to your customer
      setErrorMessage(submitError.message);
      return;
    }

    // Create the PaymentIntent and obtain clientSecret from your server endpoint
    const res = await fetch('/create-intent', {
      method: 'POST',
    });

    const {client_secret: clientSecret} = await res.json();

    const {error} = await stripe.confirmPayment({
      //`Elements` instance that was used to create the Payment Element
      elements,
      clientSecret,
      confirmParams: {
        return_url: 'https://example.com/order/123/complete',
      },
    });

    if (error) {
      // This point will only be reached if there is an immediate error when
      // confirming the payment. Show error to your customer (for example, payment
      // details incomplete)
      setErrorMessage(error.message);
    } else {
      // Your customer will be redirected to your `return_url`. For some payment
      // methods like iDEAL, your customer will be redirected to an intermediate
      // site first to authorize the payment, then redirected to the `return_url`.
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <PaymentElement />
      <button type="submit" disabled={!stripe || !elements}>
        Pay
      </button>
      {/* Show error message to your customers */}
      {errorMessage && <div>{errorMessage}</div>}
    </form>
  );
};

const stripePromise = loadStripe('pk_test_6pRNASCoBOKtIshFeQd4XMUh');

const options = {
  mode: 'payment',
  amount: 1099,
  currency: 'usd',
  // Fully customizable with appearance API.
  appearance: {
    /*...*/
  },
};

const App = () => (
  <Elements stripe={stripePromise} options={options}>
    <CheckoutForm />
  </Elements>
);

ReactDOM.render(<App />, document.body);

Using class components

import React from 'react';
import ReactDOM from 'react-dom';
import {loadStripe} from '@stripe/stripe-js';
import {
  PaymentElement,
  Elements,
  ElementsConsumer,
} from '@stripe/react-stripe-js';

class CheckoutForm extends React.Component {
  handleSubmit = async (event) => {
    event.preventDefault();
    const {stripe, elements} = this.props;

    if (elements == null) {
      return;
    }

    // Trigger form validation and wallet collection
    const {error: submitError} = await elements.submit();
    if (submitError) {
      // Show error to your customer
      return;
    }

    // Create the PaymentIntent and obtain clientSecret
    const res = await fetch('/create-intent', {
      method: 'POST',
    });

    const {client_secret: clientSecret} = await res.json();

    const {error} = await stripe.confirmPayment({
      //`Elements` instance that was used to create the Payment Element
      elements,
      clientSecret,
      confirmParams: {
        return_url: 'https://example.com/order/123/complete',
      },
    });

    if (error) {
      // This point will only be reached if there is an immediate error when
      // confirming the payment. Show error to your customer (for example, payment
      // details incomplete)
    } else {
      // Your customer will be redirected to your `return_url`. For some payment
      // methods like iDEAL, your customer will be redirected to an intermediate
      // site first to authorize the payment, then redirected to the `return_url`.
    }
  };

  render() {
    const {stripe} = this.props;
    return (
      <form onSubmit={this.handleSubmit}>
        <PaymentElement />
        <button type="submit" disabled={!stripe}>
          Pay
        </button>
      </form>
    );
  }
}

const InjectedCheckoutForm = () => (
  <ElementsConsumer>
    {({stripe, elements}) => (
      <CheckoutForm stripe={stripe} elements={elements} />
    )}
  </ElementsConsumer>
);

const stripePromise = loadStripe('pk_test_6pRNASCoBOKtIshFeQd4XMUh');

const options = {
  mode: 'payment',
  amount: 1099,
  currency: 'usd',
  // Fully customizable with appearance API.
  appearance: {
    /*...*/
  },
};

const App = () => (
  <Elements stripe={stripePromise} options={options}>
    <InjectedCheckoutForm />
  </Elements>
);

ReactDOM.render(<App />, document.body);

TypeScript support

React Stripe.js is packaged with TypeScript declarations. Some types are pulled from @stripe/stripe-js—be sure to add @stripe/stripe-js as a dependency to your project for full TypeScript support.

Typings in React Stripe.js follow the same versioning policy as @stripe/stripe-js.

Contributing

If you would like to contribute to React Stripe.js, please make sure to read our contributor guidelines.