File size: 4,030 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 (
    <Form onSubmit={handleSubmit(onSubmit)}>
      <Row>
        <Col md={6} xs={12}>
          <Form.Group>
            <Form.Label style={{ fontWeight: "bold" }}>Your Name</Form.Label>
            <Form.Control
              type="text"
              defaultValue={user.name}
              {...register("name", { required: true })}
              placeholder="Your Name" />
          </Form.Group>

          <Form.Group>
            <Form.Label style={{ fontWeight: "bold" }}>Email</Form.Label>
            <Form.Control
              type="text"
              defaultValue={user.email}
              {...register("email", { required: true })}
              placeholder="Email Address" />
          </Form.Group>

          <Form.Group>
            <Form.Label style={{ fontWeight: "bold" }}>Address</Form.Label>
            <Form.Control
              type="text"
              {...register("address", { required: true })}
              placeholder="Address" />
          </Form.Group>
        </Col>
        <Col md={6} xs={12}>
          <div>
            <Form.Label style={{ fontWeight: "bold" }}>Card Number</Form.Label>
            <CardNumberElement className="form-control" options={options} />
          </div>
          <div className="mt-3">
            <Form.Label style={{ fontWeight: "bold" }}>Expiration Date</Form.Label>
            <CardExpiryElement className="form-control" options={options} />
          </div>
          <div className="mt-3">
            <Form.Label style={{ fontWeight: "bold" }}>CVC</Form.Label>
            <CardCvcElement className="form-control" options={options} />
          </div>
        </Col>
      </Row>
      <div className="text-center">
        <button className="mainBtn mt-4" type="submit" disabled={!stripe}>Pay Now</button>
      </div>
    </Form>
  );
};

export default Checkout;