Payment.js 12.3 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import React, { useState, useEffect, useRef } from 'react';
Kim, Subin's avatar
Kim, Subin committed
2
3
import MainNav from '../Components/MainNav';
import SubNav from '../Components/SubNav';
Jiwon Yoon's avatar
Jiwon Yoon committed
4
import DaumPostcode from "react-daum-postcode";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
6
import { Container, Card, Row, Col, Button, Form, FormGroup } from 'react-bootstrap';
import { Redirect } from 'react-router-dom';
Kim, Subin's avatar
Kim, Subin committed
7
8
9

function Payment() {

10
    const [paymentWay, setPaymentWay] = useState([])
Jiwon Yoon's avatar
Jiwon Yoon committed
11
12
13
14
    const [isAddress, setIsAddress] = useState("");
    const [isZoneCode, setIsZoneCode] = useState();
    const [isPostOpen, setIsPostOpen] = useState();
    const [post, setPost] = useState([])
Jiwon Yoon's avatar
Jiwon Yoon committed
15
16
17
    const [redirect, setRedirect] = useState(null)
    const [address, setAddress] = useState("")
    const [num, setNum] = useState(0)
Jiwon Yoon's avatar
Jiwon Yoon committed
18
19
20
21
22
23
24
25

    function postClick() {
        if (post.length !== 0) {
            setPost([])
        }
        else {
            setPost(
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
26
                    <DaumPostcode style={postCodeStyle} onComplete={handleComplete} autoClose={true} />
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
30
31
32
33
34
                </div>
            )
        }

    }
    const handleComplete = (data) => {
        let fullAddress = data.address;
        let extraAddress = "";
Jiwon Yoon's avatar
Jiwon Yoon committed
35
        console.log(data)
Jiwon Yoon's avatar
Jiwon Yoon committed
36
37
38
        if (data.addressType === "R") {
            if (data.bname !== "") {
                extraAddress += data.bname;
Jiwon Yoon's avatar
Jiwon Yoon committed
39
                console.log(extraAddress)
Jiwon Yoon's avatar
Jiwon Yoon committed
40
41
42
43
44
45
46
            }
            if (data.buildingName !== "") {
                extraAddress +=
                    extraAddress !== "" ? `, ${data.buildingName}` : data.buildingName;
            }
            fullAddress += extraAddress !== "" ? ` (${extraAddress})` : "";
        }
Jiwon Yoon's avatar
Jiwon Yoon committed
47
48
49
50
51
        setAddress({ full: fullAddress, zone: data.zonecode });

        console.log(fullAddress);
    }

Jiwon Yoon's avatar
Jiwon Yoon committed
52
    const postCodeStyle = {
Jiwon Yoon's avatar
Jiwon Yoon committed
53
        // display: "block",
Jiwon Yoon's avatar
Jiwon Yoon committed
54
55
56
57
        position: "absolute",
        width: "400px",
        height: "500px",
        padding: "7px",
Jiwon Yoon's avatar
Jiwon Yoon committed
58
        zIndex: "1000"
Jiwon Yoon's avatar
Jiwon Yoon committed
59
    };
60
61
62
63
64
65
66

    function handleClick() {
        if (paymentWay.length !== 0) {
            setPaymentWay([])
        }
        else {
            const a = (
67
68
                <Row className="justify-content-md-center">
                    <Col md={6} className="border m-5 p-5">
Jiwon Yoon's avatar
Jiwon Yoon committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
                        <Form>
                            <Form.Group controlId="exampleForm.ControlSelect1">
                                <Form.Label>입금은행</Form.Label>
                                <Form.Control as="select" placeholder="입금은행을 선택하세요.">
                                    <option>농협 / 352-0559-2528-83 / 김수빈</option>
                                    <option>우리은행 / 0000-000-000000 / 이재연</option>
                                    <option>국민은행 / 111111-11-111111 / 윤대기</option>
                                </Form.Control>
                            </Form.Group>
                            <Form.Group controlId="formName">
                                <Form.Label>입금자</Form.Label>
                                <Form.Control type="email" placeholder="윤지원" />
                            </Form.Group>
                            <Form.Group controlId="formDay">
                                <Form.Label>입금예정일</Form.Label>
                                <Form.Control type="date" />
                            </Form.Group>
                        </Form>
87
                    </Col>
Jiwon Yoon's avatar
Jiwon Yoon committed
88

89
                </Row>)
90
91
92
93
            setPaymentWay(a)
        }
    }

94
    function handleClick2() {
95
96
97
98
        if (paymentWay.length !== 0) {
            setPaymentWay([])
        }
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
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

    async function kakaopay() {
        const response = await fetch('/api/kakaopay/test/single', {
            method: "POST",
            headers: {
                'Content-type': 'application/json'
            },
            body: JSON.stringify({
                cid: 'TC0ONETIME',
                partner_order_id: 'partner_order_id',
                partner_user_id: 'partner_user_id',
                item_name: '앙고라 반목 폴라 베이직 모헤어 니트 (T)',
                quantity: 1,
                total_amount: 22000,
                vat_amount: 200,
                tax_free_amount: 0,
                approval_url: 'http://localhost:3000/kakaopay/success',
                fail_url: 'http://localhost:3000/kakaopay/fail',
                cancel_url: 'http://localhost:3000/kakaopay/cancel',
            })
        })
        const data = await response.json()
        console.log(data)
        window.location.href = data.redirect_url
        // setRedirect(data.redirect_url)
    }
125

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    function plusNum() {
        setNum(num + 1)
    }
    function minusNum() {
        if (num === 0) {
            setNum(0)
        }
        else {
            setNum(num - 1)

        }
    }
    function deleteCart() {
        //장바구니 DB에서 해당 항목 삭제 
        console.log('카트에 담긴 항목을 삭제했습니다.')
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
142
143
144
145
146
147

    if (redirect) {
        console.log(redirect)
        return <Redirect to={'/kakao'} />
    }

Kim, Subin's avatar
Kim, Subin committed
148
149
    return (
        <div>
Kim, Subin's avatar
Kim, Subin committed
150
151
            <MainNav />
            <SubNav />
152
            <Container>
Jiwon Yoon's avatar
Jiwon Yoon committed
153
                <h3 className="my-5 font-weight-bold text-center">주문/결제</h3>
154
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
155
                    <h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문자 정보</h5>
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
                    <Row className="justify-content-md-center">
                        <Col md={4}>
                            <Form>
                                <Form.Group controlId="formBasicName">
                                    <Form.Label>이름</Form.Label>
                                    <Form.Control type="text" placeholder="윤지원" />
                                </Form.Group>
                                <Form.Group controlId="formBasicEmail">
                                    <Form.Label>이메일</Form.Label>
                                    <Form.Control type="email" placeholder="jiwon5393@naver.com" />
                                </Form.Group>
                                <Form.Group controlId="formBasicTel">
                                    <Form.Label>휴대전화</Form.Label>
                                    <Form.Control type="tel" placeholder="010-0000-0000" />
                                </Form.Group>
                            </Form>
                        </Col>
                    </Row>
174
175
176
                </div>

                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                    <h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>받는사람 정보</h5>
                    <Row className="justify-content-center">
                        <Col md={8}>
                            <Form>
                                <Form.Group>
                                    <Form.Label>이름</Form.Label>
                                    <Form.Control></Form.Control>
                                </Form.Group>
                                <Form.Group controlId="formBasicAdd">
                                    <Form.Label>주소</Form.Label>
                                    <Form.Row>
                                        <Col xs={4} sm={4}>
                                            <Form.Control type="text" id="add" value={address.zone} disabled={(address.zone == null) ? false : true} placeholder="우편번호" required ></Form.Control>
                                        </Col>
                                        <Col >
                                            <Button style={{ background: '#91877F', borderColor: '#91877F' }} className="mx-1" onClick={postClick}>우편번호</Button>
                                            {post}
                                        </Col>
                                    </Form.Row>
                                    <Form.Row>
                                        <Col>
                                            <Form.Control type="text" id="add1" value={address.full} disabled={(address.zone == null) ? false : true} placeholder="주소" required></Form.Control>
                                            <Form.Control type="text" id="add2" placeholder="상세주소" required></Form.Control>
                                            <Form.Control.Feedback type="invalid" > 상세 주소를 입력하세요. </Form.Control.Feedback>
                                        </Col>
                                    </Form.Row>
                                </Form.Group>
                                <Form.Group>
                                    <Form.Label>휴대전화</Form.Label>
                                    <Form.Control></Form.Control>
                                </Form.Group>
                            </Form>
Jiwon Yoon's avatar
Jiwon Yoon committed
209
210
                        </Col>
                    </Row>
211
212
213
                </div>

                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
214
                    <h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문상품정보</h5>
215
                    <Card >
216
217
218
                        <Row className="mx-1">
                            <Col xs={2} sm={2} className="text-center my-auto">
                                <input className="" type="checkbox" id="exampleCheck1" />
219
                            </Col>
220
                            <Col className="text-center">
221
222
                                <Card.Img className="img-fluid" variant="top" src="img/asd.jpg" style={{ width: '20rem' }} />
                            </Col>
223
                            <Col md={6} className="p-2">
224
                                <Card.Body>
225
                                    <input type="image" src="https://img.icons8.com/fluent-systems-regular/24/000000/close-window.png" className="float-right" onClick={deleteCart} />
226
227
228
                                    <Card.Title className="font-weight-bold mt-3">제품명</Card.Title>
                                    <Card.Text>가격</Card.Text>
                                    <Card.Text>옵션</Card.Text>
229
230
231
232
233
                                    <Card.Text>수량</Card.Text>
                                    <div>
                                        <input type="image" src="https://img.icons8.com/ios-glyphs/20/000000/minus-math.png" className="align-middle" onClick={minusNum} />
                                        <input type="text" style={{ width: '30px' }} className="text-center align-middle mx-1" placeholder="1" value={num} readOnly></input>
                                        <input type="image" src="https://img.icons8.com/ios-glyphs/20/000000/plus-math.png" className="align-middle" onClick={plusNum} />
234
235
236
237
238
239
240
                                    </div>
                                </Card.Body>
                            </Col>
                        </Row>
                    </Card>
                </div>

Jiwon Yoon's avatar
Jiwon Yoon committed
241
                <div className="p-5 m-5" style={{ background: '#F7F3F3' }}>
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                    <ul className="pl-0" style={{ listStyle: 'none' }}>
                        <li>
                            <span className="text-secondary"> 상품금액</span>
                            <span className="text-secondary float-right">12,000</span>
                        </li>
                        <li>
                            <span className="text-secondary">배송비</span>
                            <span className="text-secondary float-right">2,500</span>
                        </li>
                    </ul>
                    <div className="my-1 pt-2 border-top font-weight-bold">
                        결제금액<span className="float-right">14,500</span>
                    </div>
                </div>

                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
258
                    <h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>결제수단</h5>
259
                    <div className="text-center mt-5">
Jiwon Yoon's avatar
Jiwon Yoon committed
260
261
                        <Button variant="success" className="align-top" onClick={handleClick} >무통장입금</Button>
                        <input type="image" src="img/payment_icon_yellow_small.png" onClick={kakaopay} />
262
263
                    </div>
                    {paymentWay}
Jiwon Yoon's avatar
Jiwon Yoon committed
264
265
266
                </div>
                <div className="text-center">
                    <Button className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} href="/account" block>결제완료</Button>
267
268
                </div>
            </Container>
Kim, Subin's avatar
Kim, Subin committed
269
270
271
272
273
        </div>
    )
}

export default Payment