|
1 | 1 | import React, { useState, useEffect } from "react";
|
2 |
| -import {useNavigate} from 'react-router-dom'; |
3 |
| -import { useSelector } from "react-redux"; |
| 2 | +import { useNavigate } from "react-router-dom"; |
| 3 | +import { useSelector, useDis } from "react-redux"; |
4 | 4 | import { selectUser } from "../../redux/userSlice";
|
| 5 | +import { emptyCart } from "../../redux/cartSlice"; |
5 | 6 | import CheckoutCard from "../../components/card/CheckoutCard";
|
6 |
| -import { selectCartItems } from "../../redux/cartSelector"; |
7 |
| -import { selectCartItemsCount, selectSubtotal } from "../../redux/cartSelector"; |
| 7 | +import { |
| 8 | +selectCartItemsCount, |
| 9 | +selectSubtotal, |
| 10 | +selectCartItems, |
| 11 | +} from "../../redux/cartSelector"; |
8 | 12 | import { Link } from "react-router-dom";
|
9 | 13 | import { useStripe, useElements, CardElement } from "@stripe/react-stripe-js";
|
10 | 14 | import CurrencyFormat from "react-currency-format";
|
11 | 15 | import axios from "../../axios";
|
| 16 | +import { db } from "../../firebase.utils"; |
12 | 17 |
|
13 | 18 | const Payment = () => {
|
14 |
| - |
15 | 19 | const navigate = useNavigate();
|
| 20 | +const dis = useDis(); |
16 | 21 |
|
17 | 22 | const user = useSelector(selectUser);
|
18 | 23 | const cartItems = useSelector((state) => selectCartItems(state));
|
@@ -35,29 +40,39 @@ const Payment = () => {
|
35 | 40 | // Stripe expects the total in a currencies subunits
|
36 | 41 | url: `/payments/create?total=${subtotal * 100}`,
|
37 | 42 | });
|
38 |
| -
|
| 43 | + |
39 | 44 | setClientSecret(response.data.clientSecret);
|
40 | 45 | };
|
41 | 46 | getClientSecret();
|
42 | 47 | }, [subtotal]);
|
43 |
| - |
44 |
| - |
45 |
| - |
| 48 | +console.log(clientSecret) |
46 | 49 | const handleSubmit = async (event) => {
|
47 | 50 | event.preventDefault();
|
48 | 51 | setProcessing(true);
|
49 | 52 |
|
50 |
| -const payload = await stripe.confirmCardPayment(clientSecret, { |
51 |
| -payment_method: { |
52 |
| -card: elements.getElement(CardElement) |
53 |
| -} |
54 |
| -}).then(({ paymentIntent }) => { |
55 |
| -// paymentIntent = payment confirmation |
56 |
| -setSucceeded(true) |
57 |
| -setError(null) |
58 |
| -setProcessing(false) |
59 |
| -navigate('/orders') |
60 |
| -}) |
| 53 | +await stripe |
| 54 | +.confirmCardPayment(clientSecret, { |
| 55 | +payment_method: { |
| 56 | +card: elements.getElement(CardElement), |
| 57 | +}, |
| 58 | +}) |
| 59 | +.then(({ paymentIntent }) => { |
| 60 | +// paymentIntent = payment confirmation |
| 61 | +db.collection("users") |
| 62 | +.doc(user?.uid) |
| 63 | +.collection("orders") |
| 64 | +.doc(paymentIntent.id) |
| 65 | +.set({ |
| 66 | +cartItems, |
| 67 | +amount: paymentIntent.amount, |
| 68 | +created: paymentIntent.created, |
| 69 | +}); |
| 70 | +setSucceeded(true); |
| 71 | +setError(null); |
| 72 | +setProcessing(false); |
| 73 | +dis(emptyCart()); |
| 74 | +navigate("/orders"); |
| 75 | +}); |
61 | 76 | };
|
62 | 77 |
|
63 | 78 | const handleChange = (event) => {
|
@@ -92,7 +107,7 @@ const Payment = () => {
|
92 | 107 | {cartItems.length > 0 &&
|
93 | 108 | cartItems.map(({ productId, ...props }) => (
|
94 | 109 | <div key={productId}>
|
95 |
| -<CheckoutCard productId={productId} {...props} /> |
| 110 | +<CheckoutCard productId={productId} hiddenAction {...props} /> |
96 | 111 | <div className="checkout__cart__card" />
|
97 | 112 | </div>
|
98 | 113 | ))}
|
|
0 commit comments