Skip to content

Commit a49c6e3

Browse files
committed
FE에서 API로 전송할 결제 성공 API 작성
1 parent ef4eb97 commit a49c6e3

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

payment/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ class PaymentHistory(models.Model):
3333
(5, "환불 완료"),
3434
)
3535
)
36+
is_webhook = models.BooleanField(default=False)
3637
create_at = models.DateTimeField(auto_now_add=True)
3738
update_at = models.DateTimeField(auto_now=True)

payment/views.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
from django.db import transaction
12
from rest_framework.decorators import api_view
23
from rest_framework.response import Response
34
from rest_framework.views import APIView
45

6+
from payment import enum
57
from ticket.models import TicketType
68
from payment.logic import generate_payment_key
7-
from payment.models import Payment
9+
from payment.models import Payment, PaymentHistory
810

911
from django.conf import settings
1012

1113

1214
class PortoneWebhookApi(APIView):
15+
@transaction.atomic
1316
def post(self, request):
1417
portone_ips = [
1518
"52.78.100.19",
@@ -20,9 +23,20 @@ def post(self, request):
2023
if settings.DEBUG is False and request.META.get("REMOTE_ADDR") not in portone_ips:
2124
raise ValueError("Not Allowed IP")
2225

23-
target_payment = Payment.objects.get(payment_key=request.data["merchant_uid"])
26+
payment_key = request.data["merchant_uid"]
2427

25-
if request.data["status "] != "paid":
28+
target_payment = Payment.objects.get(payment_key=payment_key)
29+
target_payment.status = enum.PaymentStatus.PAYMENT_SUCCESS.value
30+
target_payment.save()
31+
32+
payment_history = PaymentHistory(
33+
payment_key=payment_key,
34+
status=enum.PaymentStatus.PAYMENT_SUCCESS.value,
35+
is_webhook=True
36+
)
37+
payment_history.save()
38+
39+
if request.data["status"] != "paid":
2640
raise ValueError("결제 승인건 이외의 요청")
2741

2842
dto = {
@@ -33,6 +47,28 @@ def post(self, request):
3347
return Response(dto)
3448

3549

50+
class PaymentSuccessApi(APIView):
51+
def post(self, request):
52+
if not request.is_authenticated:
53+
return Response({"msg": "not logged in user"}, status=400)
54+
55+
payment_key = request.data["merchant_uid"]
56+
57+
payment_history = PaymentHistory(
58+
payment_key=payment_key,
59+
status=enum.PaymentStatus.PAYMENT_SUCCESS.value,
60+
is_webhook=False
61+
)
62+
payment_history.save()
63+
64+
dto = {
65+
"msg": "ok",
66+
"merchant_uid": request.data["merchant_uid"]
67+
}
68+
69+
return Response(dto)
70+
71+
3672
@api_view(["GET"])
3773
def get__generate_payment_key(request):
3874

0 commit comments

Comments
 (0)