I was coding a project using React. It s been 3 days I ve looking for a solution. I learned to code and tried to integrate the stripe payment option on my web app. However, unable to get the proper result. I searched a lot but found no solution. This is My App.js
import React, { useEffect } from "react";
import "./App.css";
import Header from "./Header";
import Home from "./Home";
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
import Checkout from "./Checkout";
import Login from "./Login";
import Payment from "./Payment";
import { getAuth, onAuthStateChanged } from "./firebase.js";
import { useStateValue } from "./StateProvider";
import { loadStripe } from "@stripe/stripe-js";
import { Elements } from "@stripe/react-stripe-js";
const stripePromise = loadStripe(
"pk_test_51Ku8iiSHAST6F5x2egU8YwHdYkqKAOcJIfrfXKa0b7kxeHm0ECkvFOLVW0KeaiudzfAfU31m954rOkIKGsoXhiAE00EQx04Did"
);
function App() {
const [{}, dispatch] = useStateValue();
const auth = getAuth();
useEffect(() => {
// will only run once when the app component loads...
onAuthStateChanged(auth, (user) => {
console.log("THE USER IS >>> ", user);
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
// ...
dispatch({
type: "SET_USER",
user: user,
});
} else {
// User is signed out
// ...
dispatch({
type: "SET_USER",
user: null,
});
}
});
}, []);
return (
// BEM
<Router>
<div className="app">
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/checkout" element={[<Header />, <Checkout />]} />
<Route
path="/payment"
element={[
<Header />,
<Elements stripe={stripePromise}>
<Payment />
</Elements>,
]}
/>
<Route path="/" element={[<Header />, <Home />]} />
</Routes>
</div>
</Router>
);
}
export default App;
//https://cloned-4270b.web.app
this is my Payment.js where CardElement is written.
import React, { useState, useEffect } from "react";
import "./Payment.css";
import { useStateValue } from "./StateProvider";
import { Link, useNavigate } from "react-router-dom";
import CheckoutProduct from "./CheckoutProduct";
import { CardElement, useStripe, useElements } from "@stripe/react-stripe-js";
function Payment() {
const [{ basket, user }, dispatch] = useStateValue();
const navigate = useNavigate();
const stripe = useStripe();
const elements = useElements();
return (
<div className="payment">
<div className="payment__container">
<h1>
Checkout (<Link to="/checkout">{basket?.length} items</Link>)
</h1>
{/* Payment section - delivery address */}
<div className="payment__section">
<div className="payment__title">
<h3>Delivery Address</h3>
</div>
<div className="payment__address">
<p>{user?.email}</p>
<p>123 React Lane</p>
<p>Los Angeles, CA</p>
</div>
</div>
{/* Payment section - Review Items */}
<div className="payment__section">
<div className="payment__title">
<h3>Review items and delivery</h3>
</div>
<div className="payment__items">
{basket.map((item) => (
<CheckoutProduct
id={item.id}
title={item.title}
image={item.image}
price={item.price}
rating={item.rating}
/>
))}
</div>
</div>
{/* Payment section - Payment method */}
<div className="payment__section">
<div className="payment__title">
<h3>Payment Method</h3>
</div>
<div className="payment__details">
{/* Stripe magic will go */}
<CardElement />
</div>
</div>
</div>
</div>
);
}
export default Payment;
version using stripe:
"dependencies": {
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@mui/icons-material": "^5.6.2",
"@mui/material": "^5.6.4",
"@stripe/react-stripe-js": "^1.7.2",
"@stripe/stripe-js": "^1.29.0",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"firebase": "^9.8.0",
"react": "^18.1.0",
"react-currency-format": "^1.1.0",
"react-dom": "^18.1.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
After every attempts, CardElement is not rendering in the image below