2.14.124 • Published 10 months ago

@diotoborg/esse-distinctio-repellat v2.14.124

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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 @diotoborg/esse-distinctio-repellat @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 '@diotoborg/esse-distinctio-repellat';

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 '@diotoborg/esse-distinctio-repellat';

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.

2.14.124

10 months ago

1.11.96

11 months ago

1.11.94

11 months ago

1.11.95

11 months ago

1.11.92

11 months ago

1.11.93

11 months ago

1.11.90

11 months ago

1.11.91

11 months ago

2.14.115

10 months ago

2.14.116

10 months ago

2.14.113

10 months ago

2.14.114

10 months ago

2.14.111

10 months ago

2.14.112

10 months ago

2.14.110

10 months ago

1.4.24

1 year ago

1.4.26

1 year ago

1.4.25

1 year ago

2.14.119

10 months ago

1.4.28

1 year ago

1.4.27

1 year ago

2.14.117

10 months ago

2.14.118

10 months ago

1.4.29

1 year ago

1.11.85

11 months ago

1.11.86

11 months ago

1.11.83

11 months ago

1.11.84

11 months ago

1.11.81

11 months ago

1.11.82

11 months ago

2.11.96

11 months ago

1.11.80

11 months ago

2.14.122

10 months ago

1.11.89

11 months ago

1.4.31

1 year ago

2.14.123

10 months ago

1.4.30

1 year ago

2.14.120

10 months ago

1.11.87

11 months ago

1.4.33

1 year ago

2.14.121

10 months ago

1.11.88

11 months ago

1.4.32

1 year ago

1.4.35

1 year ago

1.2.12

1 year ago

1.4.34

1 year ago

1.2.13

1 year ago

1.4.36

1 year ago

1.2.14

1 year ago

1.2.15

1 year ago

2.13.103

11 months ago

2.13.104

10 months ago

2.13.105

10 months ago

2.13.106

10 months ago

2.13.100

11 months ago

2.13.101

11 months ago

2.13.102

11 months ago

1.9.49

1 year ago

2.13.107

10 months ago

2.13.108

10 months ago

1.8.45

1 year ago

1.8.46

1 year ago

1.8.47

1 year ago

1.8.48

1 year ago

1.8.49

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.6.37

1 year ago

1.6.36

1 year ago

1.9.69

12 months ago

1.9.68

12 months ago

1.9.67

12 months ago

1.9.66

12 months ago

1.9.65

12 months ago

1.9.64

12 months ago

1.9.63

12 months ago

1.9.62

12 months ago

1.9.61

12 months ago

1.9.60

12 months ago

1.5.36

1 year ago

1.3.17

1 year ago

1.3.18

1 year ago

1.3.15

1 year ago

1.3.16

1 year ago

1.3.19

1 year ago

1.9.59

12 months ago

1.9.58

1 year ago

1.9.57

1 year ago

1.9.56

1 year ago

1.9.55

1 year ago

1.9.54

1 year ago

1.9.53

1 year ago

1.9.52

1 year ago

1.9.51

1 year ago

1.10.79

11 months ago

1.9.50

1 year ago

1.10.77

12 months ago

1.3.20

1 year ago

1.10.78

11 months ago

1.3.21

1 year ago

1.3.24

1 year ago

1.3.22

1 year ago

1.3.23

1 year ago

2.12.100

11 months ago

1.11.79

11 months ago

1.7.37

1 year ago

1.7.38

1 year ago

1.7.39

1 year ago

1.9.77

12 months ago

2.12.99

11 months ago

1.9.76

12 months ago

2.12.98

11 months ago

1.9.75

12 months ago

2.12.97

11 months ago

1.9.74

12 months ago

1.7.40

1 year ago

2.12.96

11 months ago

1.9.73

12 months ago

1.7.41

1 year ago

1.9.72

12 months ago

1.7.42

1 year ago

1.9.71

12 months ago

1.7.43

1 year ago

1.9.70

12 months ago

1.7.44

1 year ago

1.7.45

1 year ago

2.14.108

10 months ago

2.14.109

10 months ago

1.1.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.10

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago