Skip to content

Commit 1a256f6

Browse files
committed
callback, payment_key 생성 추가
1 parent 42083e8 commit 1a256f6

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

payment/logic.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from django.contrib.auth import get_user_model
2+
from django.db import transaction
3+
4+
from payment.enum import PaymentStatus
5+
from payment.models import Payment, PaymentHistory
6+
from ticket.models import TicketType, Ticket
7+
8+
import shortuuid
9+
10+
User = get_user_model()
11+
12+
13+
@transaction.atomic
14+
def generate_payment_key(user: User, ticket_type: TicketType):
15+
new_payment = Payment(
16+
payment_key=shortuuid.uuid(),
17+
user_id=user,
18+
# ticket_type=ticket_type, # TODO
19+
money=ticket_type.price,
20+
status=PaymentStatus.BEFORE_PAYMENT.value
21+
)
22+
23+
new_payment.save()
24+
25+
_save_history(new_payment.payment_key, PaymentStatus.BEFORE_PAYMENT.value)
26+
return new_payment.payment_key
27+
28+
29+
@transaction.atomic
30+
def proceed_payment(payment_key: str, is_succeed: bool):
31+
status_value = PaymentStatus.PAYMENT_SUCCESS.value if is_succeed else PaymentStatus.PAYMENT_FAILED.value
32+
33+
target_payment = Payment.objects.get(payment_key=payment_key)
34+
target_payment.status = status_value
35+
target_payment.save()
36+
37+
_save_history(payment_key, status_value)
38+
39+
40+
def _save_history(payment_key: str, status: int):
41+
new_payment_history = PaymentHistory(
42+
payment_key=payment_key,
43+
status=status
44+
)
45+
46+
new_payment_history.save()

payment/urls.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
from django.contrib import admin
22
from django.urls import include, path
33

4-
urlpatterns = []
4+
from payment.views import PortoneWebhookApi
5+
6+
7+
urlpatterns = [
8+
path("portone/webhook", PortoneWebhookApi.as_view(), name="portone-webhook"),
9+
]

payment/views.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
1+
from rest_framework.decorators import api_view
2+
from rest_framework.response import Response
13
from rest_framework.views import APIView
24

5+
from ticket.models import TicketType
6+
from payment.logic import generate_payment_key
7+
from payment.models import Payment
38

4-
class PortoneWebhookAPI(APIView):
9+
10+
class PortoneWebhookApi(APIView):
511
def post(self, request):
612
# TODO: IP Filtering
713
# 52.78.100.19
814
# 52.78.48.223
915
# 52.78.5.241 (Webhook Test Only)
1016

11-
pass
17+
target_payment = Payment.objects.get(payment_key=request.data["merchant_uid"])
18+
19+
if request.data["status "] != "paid":
20+
raise ValueError("결제 승인건 이외의 요청")
21+
22+
dto = {
23+
"msg": "ok",
24+
"merchant_uid": request.data["merchant_uid"]
25+
}
26+
27+
return Response(dto)
28+
29+
30+
@api_view(["GET"])
31+
def get__generate_payment_key(request):
32+
33+
request_ticket_type = TicketType.objects.get(id=request.data["ticket_type"])
34+
35+
payment_key = generate_payment_key(
36+
user=request.user,
37+
ticket_type=request_ticket_type
38+
)
39+
40+
response_data = {
41+
"msg": "ok",
42+
"payment_key": payment_key
43+
}
1244

45+
return Response()

0 commit comments

Comments
 (0)