My e-commerce website application having issues with Stripe
P粉593649715
P粉593649715 2023-09-05 09:15:39
0
1
499
<p>I need to create a custom component just to get the card information and store it on Stripe so I can charge it later. </p> <pre class="brush:php;toolbar:false;">v3:1 Uncaught (in promise) IntegrationError: We cannot retrieve data from the specified element. Make sure the element you want to use is still mounted. at ni (v3:1:319630) at e._handleMessage (v3:1:324970) at e._handleMessage (v3:1:146061) at v3:1:322539</pre> <p>I got this error, I've checked to see if the component is mounted, but it is mounted all the time because this is how Stripe works, all Stripe components are in different settings for our application Brings extra load, but that's another topic. </p> <p>I need to get rid of this error. </p> <p>I have a route using stripe.checkout.sessions.create but it redirects the user to another tab which is not the behavior I want, I prefer the popup in the same app And get the card data there and store it. </p> <p>Pass stripe reference in index (for global access)</p> <pre class="brush:php;toolbar:false;">import {Elements} from "@stripe/react-stripe-js"; import {loadStripe} from "@stripe/stripe-js"; const stripePromise = loadStripe(process.env.REACT_APP_STRIPE_PUBLISHABLE_KEY); ...others const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <React.StrictMode> <BrowserRouter> <Provider store={store}> <Elements stripe={stripePromise}> <ThemeProvider> <App/> </ThemeProvider> </Elements> </Provider> </BrowserRouter> </React.StrictMode> );</pre> <p>Stripe CardInput Wrapper (just like in the documentation) Note: I tried this code in an empty project before nesting the components and it worked perfectly.</p> <pre class="brush:php;toolbar:false;">import React, {useState} from 'react'; import {CardElement} from '@stripe/react-stripe-js'; import {styled} from "@mui/material/styles"; import {Box, Stack, Typography} from "@mui/material"; import EzLoadingBtn from "../../ezComponents/EzLoadingBtn/EzLoadingBtn"; const CARD_ELEMENT_OPTIONS = { style: { base: { 'color': '#32325d', 'fontFamily': '"Helvetica Neue", Helvetica, sans-serif', 'fontSmoothing': 'antialiased', 'fontSize': '16px', '::placeholder': { color: '#aab7c4', }, }, invalid: { color: '#fa755a', iconColor: '#fa755a', }, }, }; const RootStyle = styled(Stack)(({theme}) => ({ width: '400px', padding: '50px 30px 30px 30px' })) export default function CardInput({onSubmit}) { const [loading, setLoading] = useState(false); return ( <RootStyle> <Box component='form' onSubmit={onSubmit}> <Stack flexDirection='row' justifyContent='space-between' sx={{marginBottom: '25px'}}> <Typography variant='span'>Card</Typography> </Stack> <CardElement options={CARD_ELEMENT_OPTIONS}/> <EzLoadingBtn sx={{marginTop: '25px'}} color="inherit" size='large' type='submit' variant='outlined' loading={loading} > Save Card </EzLoadingBtn> </Box> </RootStyle> ); }</pre></p>
P粉593649715
P粉593649715

reply all(1)
P粉738346380

DONE - The problem is, I used to think of "wrapping" the entire application

<Elements stripe={stripePromise}>
   <ThemeProvider>
      <App/>
   </ThemeProvider>
</Elements>

That's enough, but it's not. The key is that you don't have to wrap your entire application in Elements, just the subcomponents that use certain stripe components (CardElement, PaymentElement, etc.).

/*父组件*/
<Elements stripe={stripePromise} options={options}>
    <CardInput/>
</Elements>

/*子组件*/
export default function CardInput() {
    return (
        <div>
            <PaymentElement/>
        </div>
    );
}

So you are ready.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template