doc: add axios, useEffect, useState, Link, and useHistory to payment component
This commit is contained in:
1 parent
1173614e22
commit
1eac6e687c
1 file changed
+36
-4
@@ -1,23 +1,55 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { useStateValue } from "../../redux/StateProvider";
|
import { useStateValue } from "../../redux/StateProvider";
|
||||||
import { Link } from "react-router-dom";
|
import { Link, useHistory } from "react-router-dom";
|
||||||
import CheckoutProduct from "../CheckoutProduct";
|
import CheckoutProduct from "../CheckoutProduct";
|
||||||
import { CardElement, useElements, useStripe } from "@stripe/react-stripe-js";
|
import { CardElement, useElements, useStripe } from "@stripe/react-stripe-js";
|
||||||
import CurrencyFormat from "react-currency-format";
|
import CurrencyFormat from "react-currency-format";
|
||||||
import { getBasketTotal } from "../../redux/reducer";
|
import { getBasketTotal } from "../../redux/reducer";
|
||||||
|
|
||||||
|
import axios from "../../Auth/axios";
|
||||||
import "./Payment.css";
|
import "./Payment.css";
|
||||||
|
|
||||||
function Payment() {
|
function Payment() {
|
||||||
const [{ basket, user }, dispatch] = useStateValue();
|
const [{ basket, user }, dispatch] = useStateValue();
|
||||||
const stripe = useStripe();
|
const stripe = useStripe();
|
||||||
const elements = useElements();
|
const elements = useElements();
|
||||||
|
const history = useHistory();
|
||||||
const [succeeded, setSucceed] = useState(false);
|
const [succeeded, setSucceed] = useState(false);
|
||||||
const [processing, setProcessing] = useState("");
|
const [processing, setProcessing] = useState("");
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
const [disabled, setDisabled] = useState(true);
|
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) => {
|
const handleChange = (e) => {
|
||||||
setDisabled(e.empty);
|
setDisabled(e.empty);
|
||||||
setError(e.error ? e.error.message : "");
|
setError(e.error ? e.error.message : "");
|
||||||
|
|||||||
Reference in new issue
Block a user