Gathering detailed insights and metrics for @stripe/react-stripe-js
Gathering detailed insights and metrics for @stripe/react-stripe-js
Gathering detailed insights and metrics for @stripe/react-stripe-js
Gathering detailed insights and metrics for @stripe/react-stripe-js
React components for Stripe.js and Stripe Elements
npm install @stripe/react-stripe-js
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,778 Stars
311 Commits
272 Forks
44 Watching
56 Branches
2,956 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (94.66%)
Shell (2.79%)
JavaScript (2.53%)
HTML (0.02%)
Cumulative downloads
Total Downloads
Last day
-11.2%
244,044
Compared to previous day
Last week
1.2%
1,384,333
Compared to previous week
Last month
7.5%
5,884,707
Compared to previous month
Last year
33.7%
63,038,338
Compared to previous year
1
3
45
React components for Stripe.js and Elements.
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
.
react-stripe-elements
react-stripe-elements
docsFirst, install React Stripe.js and Stripe.js.
1npm install @stripe/react-stripe-js @stripe/stripe-js
1import React, {useState} from 'react'; 2import ReactDOM from 'react-dom'; 3import {loadStripe} from '@stripe/stripe-js'; 4import { 5 PaymentElement, 6 Elements, 7 useStripe, 8 useElements, 9} from '@stripe/react-stripe-js'; 10 11const CheckoutForm = () => { 12 const stripe = useStripe(); 13 const elements = useElements(); 14 15 const [errorMessage, setErrorMessage] = useState(null); 16 17 const handleSubmit = async (event) => { 18 event.preventDefault(); 19 20 if (elements == null) { 21 return; 22 } 23 24 // Trigger form validation and wallet collection 25 const {error: submitError} = await elements.submit(); 26 if (submitError) { 27 // Show error to your customer 28 setErrorMessage(submitError.message); 29 return; 30 } 31 32 // Create the PaymentIntent and obtain clientSecret from your server endpoint 33 const res = await fetch('/create-intent', { 34 method: 'POST', 35 }); 36 37 const {client_secret: clientSecret} = await res.json(); 38 39 const {error} = await stripe.confirmPayment({ 40 //`Elements` instance that was used to create the Payment Element 41 elements, 42 clientSecret, 43 confirmParams: { 44 return_url: 'https://example.com/order/123/complete', 45 }, 46 }); 47 48 if (error) { 49 // This point will only be reached if there is an immediate error when 50 // confirming the payment. Show error to your customer (for example, payment 51 // details incomplete) 52 setErrorMessage(error.message); 53 } else { 54 // Your customer will be redirected to your `return_url`. For some payment 55 // methods like iDEAL, your customer will be redirected to an intermediate 56 // site first to authorize the payment, then redirected to the `return_url`. 57 } 58 }; 59 60 return ( 61 <form onSubmit={handleSubmit}> 62 <PaymentElement /> 63 <button type="submit" disabled={!stripe || !elements}> 64 Pay 65 </button> 66 {/* Show error message to your customers */} 67 {errorMessage && <div>{errorMessage}</div>} 68 </form> 69 ); 70}; 71 72const stripePromise = loadStripe('pk_test_6pRNASCoBOKtIshFeQd4XMUh'); 73 74const options = { 75 mode: 'payment', 76 amount: 1099, 77 currency: 'usd', 78 // Fully customizable with appearance API. 79 appearance: { 80 /*...*/ 81 }, 82}; 83 84const App = () => ( 85 <Elements stripe={stripePromise} options={options}> 86 <CheckoutForm /> 87 </Elements> 88); 89 90ReactDOM.render(<App />, document.body);
1import React from 'react'; 2import ReactDOM from 'react-dom'; 3import {loadStripe} from '@stripe/stripe-js'; 4import { 5 PaymentElement, 6 Elements, 7 ElementsConsumer, 8} from '@stripe/react-stripe-js'; 9 10class CheckoutForm extends React.Component { 11 handleSubmit = async (event) => { 12 event.preventDefault(); 13 const {stripe, elements} = this.props; 14 15 if (elements == null) { 16 return; 17 } 18 19 // Trigger form validation and wallet collection 20 const {error: submitError} = await elements.submit(); 21 if (submitError) { 22 // Show error to your customer 23 return; 24 } 25 26 // Create the PaymentIntent and obtain clientSecret 27 const res = await fetch('/create-intent', { 28 method: 'POST', 29 }); 30 31 const {client_secret: clientSecret} = await res.json(); 32 33 const {error} = await stripe.confirmPayment({ 34 //`Elements` instance that was used to create the Payment Element 35 elements, 36 clientSecret, 37 confirmParams: { 38 return_url: 'https://example.com/order/123/complete', 39 }, 40 }); 41 42 if (error) { 43 // This point will only be reached if there is an immediate error when 44 // confirming the payment. Show error to your customer (for example, payment 45 // details incomplete) 46 } else { 47 // Your customer will be redirected to your `return_url`. For some payment 48 // methods like iDEAL, your customer will be redirected to an intermediate 49 // site first to authorize the payment, then redirected to the `return_url`. 50 } 51 }; 52 53 render() { 54 const {stripe} = this.props; 55 return ( 56 <form onSubmit={this.handleSubmit}> 57 <PaymentElement /> 58 <button type="submit" disabled={!stripe}> 59 Pay 60 </button> 61 </form> 62 ); 63 } 64} 65 66const InjectedCheckoutForm = () => ( 67 <ElementsConsumer> 68 {({stripe, elements}) => ( 69 <CheckoutForm stripe={stripe} elements={elements} /> 70 )} 71 </ElementsConsumer> 72); 73 74const stripePromise = loadStripe('pk_test_6pRNASCoBOKtIshFeQd4XMUh'); 75 76const options = { 77 mode: 'payment', 78 amount: 1099, 79 currency: 'usd', 80 // Fully customizable with appearance API. 81 appearance: { 82 /*...*/ 83 }, 84}; 85 86const App = () => ( 87 <Elements stripe={stripePromise} options={options}> 88 <InjectedCheckoutForm /> 89 </Elements> 90); 91 92ReactDOM.render(<App />, document.body);
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
.
If you would like to contribute to React Stripe.js, please make sure to read our contributor guidelines.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
8 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 6
Reason
Found 12/24 approved changesets -- score normalized to 5
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
35 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More