-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
772f73f
commit 2732e9b
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<form onSubmit={handelSubmit}> | ||
<CardElement | ||
options={{ | ||
style: { | ||
base: { | ||
fontSize: "16px", | ||
color: "#424770", | ||
"::placeholder": { | ||
color: "#aab7c4", | ||
}, | ||
}, | ||
invalid: { | ||
color: "#9e2146", | ||
}, | ||
}, | ||
}} | ||
></CardElement> | ||
<button | ||
className="btn btn-outline btn-primary my-6" | ||
type="submit" | ||
disabled={!stripe || !clientSecret || pro} | ||
> | ||
Pay | ||
</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default CheckOutFrom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters