How to Integrate Stripe Payment Gateway in React js



Here is a general outline of the steps you'll need to take to integrate Stripe into a React.js application:

  1. Sign up for a Stripe account and obtain your API keys (test and live).
  2. Install the Stripe library for React by running npm install react-stripe-elements in your project's root directory.
  3. Import the Stripe library and the necessary components into your React component that will handle the payment form.
  4. Create a form that will allow the user to enter their credit card information. This can be done using Stripe's pre-built elements such as CardNumber, CardExpiry, and CardCVCElement.
  5. When the form is submitted, use the stripe.createToken method to create a token from the user's card information.
  6. Send the token to your server where you can use the Stripe API to charge the user's card and complete the transaction.
  7. Handle any errors that may occur during the process and display them to the user.

Here is an example of a basic react component with Stripe form:

import React, { useState } from 'react';

import { loadStripe } from '@stripe/stripe-js';

import {

  CardNumberElement,

  CardExpiryElement,

  CardCVCElement,

  useStripe,

  useElements,

} from '@stripe/react-stripe-js';


const CheckoutForm = () => {

  const stripe = useStripe();

  const elements = useElements();

  const [error, setError] = useState(null);

  const [cardComplete, setCardComplete] = useState(false);


  const handleSubmit = async (event) => {

    event.preventDefault();


    if (!stripe || !elements) {

      // Stripe.js has not loaded yet. Make sure to disable

      // form submission until Stripe.js has loaded.

      return;

    }


    const cardElement = elements.getElement(CardNumberElement);


    if (error) {

      elements.getElement(CardNumberElement).focus();

      return;

    }


    if (!cardComplete) {

      cardElement.focus();

      return;

    }


    const { error, token } = await stripe.createToken(cardElement);


    if (error) {

      setError(error.message);

    } else {

      // Send the token to your server to charge the user

      console.log(token.id);

    }

  };


  const handleCardChange = (event) => {

    setError(event.error ? event.error.message : "");

    setCardComplete(event.complete);

  };


  return (

    <form onSubmit={handleSubmit}>

      <label>

        Card number

        <CardNumberElement

          onChange={handleCardChange}

        />

      </label>

      <label>

        Expiration date

        <CardExpiryElement />

      </label>

      <label>

        CVC

        <CardCVCElement />

      </label>

      {error && <div style={{ color: "red" }}>{error}</div>}

      <button type="submit" disabled={!stripe}>

      Pay </button> </form> ); };

      export default CheckoutForm;


It is important to note that the above example is just a basic example and should be modified to fit the specific needs of your application. Also, it's a good practice to handle security concerns, server-side validations, and testing the payment gateway with test cards before going live.

Also, make sure to replace the `console.log(token.id)` line with code that will send the token to your server for processing the payment.