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

function Payment() {

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

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

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

        console.log(fullAddress);
    }

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

    function handleClick() {
        if (paymentWay.length !== 0) {
            setPaymentWay([])
        }
        else {
            const a = (
65
66
                <Row className="justify-content-md-center">
                    <Col md={6} className="border m-5 p-5">
Jiwon Yoon's avatar
Jiwon Yoon committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
                        <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>
85
                    </Col>
Jiwon Yoon's avatar
Jiwon Yoon committed
86

87
                </Row>)
88
89
90
91
            setPaymentWay(a)
        }
    }

92
    function handleClick2() {
93
94
95
96
        if (paymentWay.length !== 0) {
            setPaymentWay([])
        }
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

    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,
113
114
115
                approval_url: 'http://localhost:3000/account',
                fail_url: 'http://localhost:3000/shoppingcart',
                cancel_url: 'http://localhost:3000/kakaopay/payment',
Jiwon Yoon's avatar
Jiwon Yoon committed
116
117
118
119
120
121
122
            })
        })
        const data = await response.json()
        console.log(data)
        window.location.href = data.redirect_url
        // setRedirect(data.redirect_url)
    }
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    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
140
141
142
143
144
145

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

Kim, Subin's avatar
Kim, Subin committed
146
147
    return (
        <div>
148
            <Container>
Jiwon Yoon's avatar
Jiwon Yoon committed
149
                <h3 className="my-5 font-weight-bold text-center">주문/결제</h3>
150
                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
151
                    <h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>주문자 정보</h5>
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
                    <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>
170
171
172
                </div>

                <div>
Jiwon Yoon's avatar
Jiwon Yoon committed
173
174
175
176
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
                    <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
205
206
                        </Col>
                    </Row>
207
208
209
                </div>

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

Jiwon Yoon's avatar
Jiwon Yoon committed
234
                <div className="p-5 m-5" style={{ background: '#F7F3F3' }}>
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
                    <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
251
                    <h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>결제수단</h5>
252
                    <div className="text-center mt-5">
Jiwon Yoon's avatar
Jiwon Yoon committed
253
                        <Button variant="success" className="align-top" onClick={handleClick} >무통장입금</Button>
254
                        <input type="image" alt="카카오페이결제" src="icon/payment_icon_yellow_small.png" onClick={kakaopay} />
255
256
                    </div>
                    {paymentWay}
Jiwon Yoon's avatar
Jiwon Yoon committed
257
258
259
                </div>
                <div className="text-center">
                    <Button className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} href="/account" block>결제완료</Button>
260
261
                </div>
            </Container>
Kim, Subin's avatar
Kim, Subin committed
262
263
264
265
266
        </div>
    )
}

export default Payment