doc: add axios, useEffect, useState, Link, and useHistory to payment component
This commit is contained in:
@@ -1,23 +1,55 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { useStateValue } from "../../redux/StateProvider";
|
||||
import { Link } from "react-router-dom";
|
||||
import { Link, useHistory } from "react-router-dom";
|
||||
import CheckoutProduct from "../CheckoutProduct";
|
||||
import { CardElement, useElements, useStripe } from "@stripe/react-stripe-js";
|
||||
import CurrencyFormat from "react-currency-format";
|
||||
import { getBasketTotal } from "../../redux/reducer";
|
||||
|
||||
import axios from "../../Auth/axios";
|
||||
import "./Payment.css";
|
||||
|
||||
function Payment() {
|
||||
const [{ basket, user }, dispatch] = useStateValue();
|
||||
const stripe = useStripe();
|
||||
const elements = useElements();
|
||||
|
||||
const history = useHistory();
|
||||
const [succeeded, setSucceed] = useState(false);
|
||||
const [processing, setProcessing] = useState("");
|
||||
const [error, setError] = useState(null);
|
||||
const [disabled, setDisabled] = useState(true);
|
||||
const [clientSecret, setClientSecret] = useState(true);
|
||||
|
||||
const handleSubmit = (e) => {};
|
||||
useEffect(() => {
|
||||
const getClientSecret = async () => {
|
||||
const response = await axios({
|
||||
method: "POST",
|
||||
url: `/payments/create?total=${getBasketTotal(basket) * 100}`,
|
||||
});
|
||||
setClientSecret(response.data.clientSecret);
|
||||
};
|
||||
|
||||
getClientSecret();
|
||||
}, [basket]);
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
setProcessing(true);
|
||||
|
||||
const payload = await stripe
|
||||
.confirmCardPayment(clientSecret, {
|
||||
payment_method: {
|
||||
card: elements.getElement(CardElement),
|
||||
},
|
||||
})
|
||||
.then(({ paymentIntent }) => {
|
||||
setSucceed(true);
|
||||
setError(null);
|
||||
setProcessing(false);
|
||||
|
||||
history.replace("/orders");
|
||||
});
|
||||
};
|
||||
const handleChange = (e) => {
|
||||
setDisabled(e.empty);
|
||||
setError(e.error ? e.error.message : "");
|
||||
|
||||
Reference in New Issue
Block a user