React components for Stripe.js and Stripe Elements
Installations
npm install @stripe/react-stripe-js
Developer
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
Typescript Support
Yes
Node Version
NPM Version
Statistics
1,778 Stars
311 Commits
272 Forks
44 Watching
56 Branches
2,956 Contributors
Updated on 28 Nov 2024
Bundle Size
15.88 kB
Minified
4.95 kB
Minified + Gzipped
Languages
TypeScript (94.66%)
Shell (2.79%)
JavaScript (2.53%)
HTML (0.02%)
Total Downloads
Cumulative downloads
Total Downloads
165,921,235
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
3
Dev Dependencies
45
React Stripe.js
React components for Stripe.js and Elements.
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
- React Stripe.js reference
- Migrate from
react-stripe-elements
- Legacy
react-stripe-elements
docs - Examples
Minimal example
First, install React Stripe.js and Stripe.js.
1npm install @stripe/react-stripe-js @stripe/stripe-js
Using hooks
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);
Using class components
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);
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/stripe/.github/SECURITY.md:1
- Info: Found linked content: github.com/stripe/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/stripe/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/stripe/.github/SECURITY.md:1
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
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/stripe/react-stripe-js/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/stripe/react-stripe-js/build.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Reason
35 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-w5p7-h5w8-2hfq
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
4.6
/10
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