import React, { useMemo } from 'react' import { useStripe, useElements, CardNumberElement, CardCvcElement, CardExpiryElement } from '@stripe/react-stripe-js'; import swal from 'sweetalert'; import { Form, Col, Row } from 'react-bootstrap'; import { useForm } from 'react-hook-form'; import toast from 'react-hot-toast'; import axios from 'axios'; import { useAppContext } from '../../../../context'; const useOptions = () => { const options = useMemo(() => ({ style: { base: { fontSize: "1.2rem", lineHeight: "2", color: "#495057", letterSpacing: "0.025em", "::placeholder": { color: "#aab7c4" } }, invalid: { color: "#9e2146" } } }), []); return options; }; const Checkout = () => { const { state: { user, selectedService: { name, img, _id, description, price } } } = useAppContext(); const stripe = useStripe(); const elements = useElements(); const options = useOptions(); const { register, handleSubmit, reset } = useForm(); const onSubmit = async data => { if (!stripe || !elements) { return; } const loading = toast.loading('Please wait...!'); const { error, paymentMethod } = await stripe.createPaymentMethod({ type: "card", card: elements.getElement(CardNumberElement) }); if (error) { toast.dismiss(loading); return swal("Failed!", error.message, "error", { dangerMode: true }); } else { const orderData = { ...data, paymentMethod: "card", paymentId: paymentMethod.id, status: "Pending", serviceId: _id, serviceName: name, description: description, img: img, price: price } axios.post('https://immense-river-40491.herokuapp.com/addOrder', orderData) .then(res => { toast.dismiss(loading); swal("Congratulation!", "Your order has been placed successfully", "success"); reset(); }) .catch(err => { toast.dismiss(loading); swal("Failed!", "Something went wrong! please try again", "error") }) } }; return (
Your Name Email Address
Card Number
Expiration Date
CVC
); }; export default Checkout;