![](/img/trans.png)
[英]Uncaught (in promise) Error: Invalid prop `stripe` supplied to `Elements`. We recommend using the `loadStripe` utility from `@stripe/stripe-js`
[英]Stripe Problem, please call Stripe ( ) error Uncaught (in promise)
尝试在我的电子商务中实施 Stripe 时遇到了这个问题
当我从 Stripe 测试网站输入信用卡号时,网站刷新,没有任何反应,甚至没有重定向到 /success,只有这个错误出现在控制台中:
Uncaught (in promise) IntegrationError: Please call Stripe() with your publishable key.
You used an empty string.
这是我的 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>
);
}
这是我的支付控制器,我认为问题可能出在 app.js 中,就像我调用 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>}/>}
在 app.js 中,您已经声明了支付路径路径,只需添加和条件&& stripeApiKey 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.