简体   繁体   中英

Stripe Problem, please call Stripe ( ) error Uncaught (in promise)

I'm facing this problem when trying to implement Stripe in my ecommerce

When I put the credit card numbers from Stripe testing website, the website refresh, nothing happens, not even re direct to /success, only this error appears in console:

 Uncaught (in promise) IntegrationError: Please call Stripe() with your publishable key. 
 You used an empty string.

this is my app.js

 function App() {

const {isAuthenticated, user} = useSelector((state)=>state.user)

const [stripeApiKey, setStripeApiKey] = useState("");

 async function getStripeApiKey() {
 const {data} = await axios.get("/api/v1/stripeapikey");

  setStripeApiKey(data.stripeApiKey)
 }

 useEffect(() => { 

  WebFont.load({
   google:{
     families: [ "Droid Sans", "Chilanka"],
   },
  });

  store.dispatch(loadUser())

  getStripeApiKey()
 }, []);

return (
<Router>
  <Header/>
  {isAuthenticated && <UserOptions user={user} />}
  <Routes>
    <Route path="/" element={<Home/>}/>
    <Route path="/product/:id" element={<ProductDetails/>}/>
    <Route path="/products" element={<Products/>}/>
    <Route path="/products/:keyword" element={<Products/>}/>
    <Route path="/search" element={<Search/>}/>
    <Route element={<ProtectedRoute/>}/>
      <Route path="/account" element={<Profile/>}/>
    <Route element={<ProtectedRoute/>}/>
     <Route path="/me/update" element={<UpdateProfile/>}/>
    <Route element={<ProtectedRoute/>}/>
     <Route path="/password/update" element={<UpdatePassword/>}/>
    <Route path="/password/forgot" element={<ForgotPassword/>}/> 
    <Route path="/password/reset/:token" element={<ResetPassword/>}/> 
    <Route path="/login" element={<LoginSignUp/>}/>
    <Route path="/cart" element={<Cart/>}/>
    <Route element={<ProtectedRoute/>}/>
     <Route path="/shipping" element={<Shipping/>}/>
    <Route element={<ProtectedRoute/>}/>
     <Route path="/order/confirm" element={<ConfirmOrder/>}/>

     <Route element={<ProtectedRoute/>}/>
      <Route  path="/process/payment" element={ <Elements stripe={loadStripe(stripeApiKey)}><Payment/></Elements>}/>
  </Routes>
<Footer/>
</Router>
 );
}

and this is my paymentController, I think the problem may be in the app.js, as how I'm calling StripeP

const catchAsyncErrors = require("../middlewares/catchAsyncErrors");

  const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

exports.processPayment = catchAsyncErrors(async (req, res, next) => {
const myPayment = await stripe.paymentIntents.create({
amount: req.body.amount,
currency: "inr",
metadata: {
  company: "Ecommerce",
},
});

res
.status(200)
 .json({ success: true, client_secret: myPayment.client_secret });
 });

exports.sendStripeApiKey = catchAsyncErrors(async (req, res, next) => {
res.status(200).json({ stripeApiKey: process.env.STRIPE_API_KEY });
});  
{stripeApiKey && <Route exact path="/process/payment" element={<Elements stripe={loadStripe(stripeApiKey)}><Payment/></Elements>}/>}

In app.js where you have declared your Payment Route Path just add and condition && stripeApiKey .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM