| | import React, { useState, useEffect } from "react"; |
| | import "./Payment.css"; |
| | import CartProducts from "../CartProducts/CartProducts"; |
| | import { useStateValue } from "../../context/StateProvider"; |
| | import { Link, useHistory } from "react-router-dom"; |
| | import { CardElement, useStripe, useElements } from "@stripe/react-stripe-js"; |
| | import CurrencyFormat from "react-currency-format"; |
| | import { getCartTotal } from "../../context/reducer"; |
| | import axios from "../../utils/axios"; |
| | import { db } from "../../firebase/firebaseConfig"; |
| |
|
| | function Payment() { |
| | const [{ cart, user }, dispatch] = useStateValue(); |
| |
|
| | const history = useHistory(); |
| | const stripe = useStripe(); |
| | const elements = useElements(); |
| |
|
| | const [error, setError] = useState(null); |
| | const [disabled, setDisabled] = useState(true); |
| | const [processing, setProcessing] = useState(""); |
| | const [succeeded, setSucceeded] = useState(false); |
| | const [clientSecret, setClientSecret] = useState(true); |
| |
|
| | useEffect(() => { |
| | |
| | const getClientSecret = async () => { |
| | const response = await axios({ |
| | method: "post", |
| | |
| | url: `/payments/create?total=${getCartTotal(cart) * 100}`, |
| | }); |
| | setClientSecret(response.data.clientSecret); |
| | console.log("secret is -> ", clientSecret); |
| | }; |
| |
|
| | getClientSecret(); |
| | }, [cart]); |
| |
|
| | const handleSubmit = async (e) => { |
| | |
| | e.preventDefault(); |
| | setProcessing(true); |
| |
|
| | const payload = await stripe |
| | .confirmCardPayment(clientSecret, { |
| | payment_method: { |
| | card: elements.getElement(CardElement), |
| | }, |
| | }) |
| | .then(({ paymentIntent }) => { |
| | |
| |
|
| | db.collection("users") |
| | .doc(user?.uid) |
| | .collection("orders") |
| | .doc(paymentIntent.id) |
| | .set({ |
| | cart: cart, |
| | amount: paymentIntent.amount, |
| | created: paymentIntent.created, |
| | }); |
| |
|
| | setSucceeded(true); |
| | setError(null); |
| | setProcessing(false); |
| |
|
| | dispatch({ |
| | type: "EMPTY_BASKET", |
| | }); |
| |
|
| | history.replace("/orders"); |
| | }); |
| | }; |
| |
|
| | const handleChange = (e) => { |
| | |
| | |
| | setDisabled(false); |
| | }; |
| |
|
| | return ( |
| | <div className="payment"> |
| | <div className="payment__container"> |
| | <h1> |
| | Checkout(<Link to="">{cart?.length} items</Link>) |
| | </h1> |
| | |
| | {/* 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>Magarpatta, Pune</p> |
| | </div> |
| | </div> |
| | {/* review Items */} |
| | <div className="payment__section"> |
| | <div className="payment__title"> |
| | <h3>Review Items and Delivery</h3> |
| | </div> |
| | <div className="payment__items"> |
| | {cart.map((item, index) => ( |
| | <CartProducts |
| | key={index} |
| | id={item.id} |
| | title={item.title} |
| | price={item.price} |
| | rating={item.rating} |
| | image={item.image} |
| | /> |
| | ))} |
| | </div> |
| | </div> |
| | {/* payment method */} |
| | <div className="payment__section"> |
| | <div className="payment__title"> |
| | <h3>Payment Method</h3> |
| | </div> |
| | <div className="payment__details"> |
| | {/* Stripe Magic */} |
| | <form onSubmit={handleSubmit}> |
| | <CardElement onChange={handleChange} /> |
| | <div className="payment__priceContainer"> |
| | <CurrencyFormat |
| | renderText={(value) => <h3>Order Total : {value}</h3>} |
| | decimalScale={2} |
| | value={getCartTotal(cart)} |
| | displayType={"text"} |
| | thousandSeparator={true} |
| | prefix={"₹"} |
| | /> |
| | |
| | <button disabled={processing || disabled || succeeded}> |
| | <span>{processing ? <p>Processing</p> : <p>Buy Now</p>}</span> |
| | </button> |
| | </div> |
| | |
| | {error && <div>error</div>} |
| | </form> |
| | </div> |
| | </div> |
| | </div> |
| | </div> |
| | ); |
| | } |
| |
|
| | export default Payment; |
| |
|