Skip to content

Commit f7f773c

Browse files
committed
티켓 구매 프로세스
1 parent b7387b2 commit f7f773c

File tree

12 files changed

+198
-21
lines changed

12 files changed

+198
-21
lines changed

payment/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from django.db import models
22
from django.contrib.auth import get_user_model
33

4+
from ticket.models import TicketType
5+
46
User = get_user_model()
57

8+
69
class Payment(models.Model):
710
payment_key = models.CharField(max_length=32) # TODO: uuid 처리
811
user_id = models.ForeignKey(User, on_delete=models.PROTECT)

payment/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.contrib.auth import get_user_model
2+
from django.db import transaction
3+
4+
from payment.models import Payment
5+
from ticket.models import TicketType
6+
7+
import shortuuid
8+
9+
User = get_user_model()
10+
11+
12+
@transaction.atomic
13+
def generate_payment_key(user: User, ticket_type: TicketType):
14+
new_payment = Payment(
15+
payment_key=shortuuid.uuid(),
16+
user_id=user,
17+
# ticket_type=ticket_type, # TODO
18+
money=ticket_type.price
19+
)
20+
21+
new_payment.save()
22+
23+
return new_payment.payment_key

ticket/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.contrib import admin
22

3-
from .models import ConferenceTicket, ConferenceTicketType
3+
from .models import ConferenceTicket, TicketType
44

55

66
class ConferenceTicketAdmin(admin.ModelAdmin):
@@ -25,4 +25,4 @@ class ConferenceTicketTypeAdmin(admin.ModelAdmin):
2525
)
2626

2727

28-
admin.site.register(ConferenceTicketType, ConferenceTicketTypeAdmin)
28+
admin.site.register(TicketType, ConferenceTicketTypeAdmin)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 4.1.5 on 2023-05-14 05:32
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("ticket", "0001_initial"),
10+
]
11+
12+
operations = [
13+
migrations.RenameModel(
14+
old_name="ConferenceTicketType",
15+
new_name="TicketType",
16+
),
17+
]

ticket/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
User = get_user_model()
99

1010

11-
class ConferenceTicketType(models.Model):
11+
class TicketType(models.Model):
1212
code = models.CharField(max_length=50)
1313
name = models.CharField(max_length=100)
1414
price = models.IntegerField()
@@ -48,7 +48,7 @@ def buyable(self) -> bool:
4848
else:
4949
raise ValueError(f"{self.day} is not valid day.")
5050

51-
def can_coexist(self, other: ConferenceTicketType) -> bool:
51+
def can_coexist(self, other: TicketType) -> bool:
5252
if self.day == "SAT" and other.day == "SUN":
5353
return True
5454
if self.day == "SUN" and other.day == "SAT":
@@ -64,7 +64,7 @@ def make_ticket_code() -> str:
6464
class ConferenceTicket(models.Model):
6565
# 구분
6666
ticket_type = models.ForeignKey(
67-
ConferenceTicketType, on_delete=models.RESTRICT, db_index=True
67+
TicketType, on_delete=models.RESTRICT, db_index=True
6868
)
6969
# 구매 일자
7070
bought_at = models.DateTimeField()

ticket/templates/ticket-detail.html

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!DOCTYPE html>
2+
<html lang="ko">
3+
<head>
4+
<!-- jQuery -->
5+
<script
6+
type="text/javascript"
7+
src="https://code.jquery.com/jquery-1.12.4.min.js"
8+
></script>
9+
10+
<!-- iamport.payment.js -->
11+
<script
12+
type="text/javascript"
13+
src="https://cdn.iamport.kr/js/iamport.payment-1.2.0.js"
14+
></script>
15+
16+
<script
17+
data-payment_key="{{ payment_key }}"
18+
data-ticket_name="{{ ticket_type.name }}"
19+
data-ticket_price="{{ ticket_type.price }}"
20+
data-user_name="{{ user_name }}"
21+
>
22+
const IMP = window.IMP;
23+
const data = document.currentScript.dataset;
24+
IMP.init("imp80859147");
25+
26+
function requestPay(payment_key, name, amount) {
27+
IMP.request_pay(
28+
{
29+
pg: "html5_inicis.INIpayTest",
30+
pay_method: "card",
31+
merchant_uid:data.payment_key,
32+
name: data.ticket_name,
33+
amount: data.ticket_price,
34+
buyer_name: data.user_name,
35+
buyer_email: "",
36+
buyer_tel: "01028458521"
37+
},
38+
function (rsp) {
39+
console.log(rsp)
40+
41+
if (rsp.success) {
42+
location.replace("{% url "page-ticket-success" %}")
43+
} else {
44+
location.replace("{% url "page-ticket-failed" %}")
45+
}
46+
//rsp.imp_uid 값으로 결제 단건조회 API를 호출하여 결제결과를 판단합니다.
47+
}
48+
);
49+
}
50+
</script>
51+
52+
<meta charset="UTF-8">
53+
<title>티켓 상세</title>
54+
</head>
55+
<body>
56+
<h1>{{ ticket_type.name }}</h1>
57+
<p>{{ ticket_type.desc }}</p>
58+
59+
<button onclick="requestPay()">
60+
구매하기
61+
</button>
62+
63+
</body>
64+
</html>

ticket/templates/ticket-failed.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="ko">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>티켓 구매 실패</title>
6+
</head>
7+
<body>
8+
<h1>티켓 구매 실패</h1>
9+
</body>
10+
</html>

ticket/templates/ticket-list.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="ko">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>티켓 종류</title>
6+
</head>
7+
<body>
8+
{% for item in ticket_items %}
9+
<p>{{ item.name }} : {{ item.price }} <a href="{% url "ticket-detail" item_id=item.id %}">구매하기</a></p>
10+
{% endfor %}
11+
12+
</body>
13+
</html>

ticket/templates/ticket-success.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="ko">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>티켓 구매 성공</title>
6+
</head>
7+
<body>
8+
<h1>티켓 구매 성공</h1>
9+
</body>
10+
</html>

ticket/urls.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
views.get__check_conference_ticket_type_buyable,
1010
),
1111
path("conference-tickets", views.post__add_conference_ticket),
12-
path("list", views.temp),
13-
path("<int:item_id>", views.temp),
14-
path("success", views.temp),
15-
path("failed", views.temp),
12+
path("list", views.get__ticket_list, name="ticket-list"),
13+
path("<int:item_id>", views.TicketDetailView.as_view(), name="ticket-detail"),
14+
path("success", views.ticket_success, name="page-ticket-success"),
15+
path("failed", views.ticket_failed, name="page-ticket-failed"),
1616
path("<int:ticket_id>/refund", views.temp_refund),
1717
]

0 commit comments

Comments
 (0)