From 2732e9b914f5ee7650d541ad02e7410b8a60819f Mon Sep 17 00:00:00 2001 From: rezwan-hossen Date: Tue, 11 Jun 2024 01:53:24 +0600 Subject: [PATCH] payment success --- src/Dashbord/CheckOutFrom.jsx | 105 ++++++++++++++++++++++++++++++++++ src/Dashbord/Payment.jsx | 28 +++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/Dashbord/CheckOutFrom.jsx diff --git a/src/Dashbord/CheckOutFrom.jsx b/src/Dashbord/CheckOutFrom.jsx new file mode 100644 index 0000000..62286ec --- /dev/null +++ b/src/Dashbord/CheckOutFrom.jsx @@ -0,0 +1,105 @@ +import { CardElement, useElements, useStripe } from "@stripe/react-stripe-js"; +import { useState } from "react"; +import { useEffect } from "react"; +import toast from "react-hot-toast"; +import useAuth from "../Hooks/useAuth"; +import useAxiosSecqur from "../Hooks/useAxiosSecqur"; + +const CheckOutFrom = ({ badge }) => { + const { user } = useAuth(); + const stripe = useStripe(); + const elements = useElements(); + const axiosSec = useAxiosSecqur(); + const [clientSecret, setclientSecret] = useState(); + const [pro, setpro] = useState(false); + const price = badge?.price; + // console.log(price); + + useEffect(() => { + axiosSec.post("/create-payment-intent", { price: price }).then((res) => { + // console.log(res.data.clientSecret); + setclientSecret(res.data.clientSecret); + }); + }, [axiosSec, price]); + + const handelSubmit = async (e) => { + e.preventDefault(); + setpro(true); + if (!stripe || !elements) { + return; + } + const card = elements.getElement(CardElement); + + if (card == null) { + return; + } + const { error, paymentMethod } = await stripe.createPaymentMethod({ + type: "card", + card, + }); + if (error) { + // console.log("[error]", error); + toast.error(error.message); + setpro(false); + return; + } else { + console.log("[PaymentMethod]", paymentMethod); + toast.success("Pay successfully"); + } + // payment con + const { error: confError, paymentIntent } = await stripe.confirmCardPayment( + clientSecret, + { + payment_method: { + card: card, + billing_details: { + email: user?.email, + name: user?.displayName, + }, + }, + } + ); + if (confError) { + console.log(confError); + toast.error(confError.message); + setpro(false); + return; + } + if (paymentIntent.status === "succeeded") { + const paymentinfo = { + ...badge, + transactionId: paymentIntent.id, + data: new Date(), + }; + } + }; + return ( +
+ + +
+ ); +}; + +export default CheckOutFrom; diff --git a/src/Dashbord/Payment.jsx b/src/Dashbord/Payment.jsx index feab822..a303cfc 100644 --- a/src/Dashbord/Payment.jsx +++ b/src/Dashbord/Payment.jsx @@ -1,7 +1,35 @@ +import { Elements } from "@stripe/react-stripe-js"; +import { loadStripe } from "@stripe/stripe-js"; +import { useQuery } from "@tanstack/react-query"; +import { useParams } from "react-router-dom"; +import useAxiosCommon from "../Hooks/useAxiosCommon"; +import LogingSpiner from "../Sheare/LogingSpiner"; +// import useAxiosSecqur from "../Hooks/useAxiosSecqur"; +// import { Elements } from "@stripe/react-stripe-js"; +import CheckOutFrom from "./CheckOutFrom"; +const stripePromise = loadStripe(import.meta.env.VITE_pymeny); + const Payment = () => { + const { id } = useParams(); + const axioscommon = useAxiosCommon(); + const { data: badge = {}, isLoading } = useQuery({ + queryKey: ["badge", id], + queryFn: async () => { + const { data } = await axioscommon.get(`/badge/${id}`); + return data; + }, + }); + if (isLoading) return ; + //console.log(badge); + return (

Payment

+
+ + + +
); };