Skip to content

Commit

Permalink
Merge pull request #91 from golony6449/feature/golony/payment
Browse files Browse the repository at this point in the history
FE에서 API로 전송할 결제 성공 API 작성
  • Loading branch information
golony6449 authored May 25, 2023
2 parents 205798c + af212d7 commit 7cec4af
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
18 changes: 18 additions & 0 deletions payment/migrations/0003_paymenthistory_is_webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-05-25 13:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("payment", "0002_rename_user_id_payment_user_payment_status_and_more"),
]

operations = [
migrations.AddField(
model_name="paymenthistory",
name="is_webhook",
field=models.BooleanField(default=False),
),
]
1 change: 1 addition & 0 deletions payment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ class PaymentHistory(models.Model):
(5, "환불 완료"),
)
)
is_webhook = models.BooleanField(default=False)
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
46 changes: 41 additions & 5 deletions payment/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from django.db import transaction
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.views import APIView

from payment import enum
from ticket.models import TicketType
from payment.logic import generate_payment_key
from payment.models import Payment
from payment.models import Payment, PaymentHistory

from django.conf import settings


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

target_payment = Payment.objects.get(payment_key=request.data["merchant_uid"])
payment_key = request.data["merchant_uid"]

if request.data["status "] != "paid":
target_payment = Payment.objects.get(payment_key=payment_key)
target_payment.status = enum.PaymentStatus.PAYMENT_SUCCESS.value
target_payment.save()

payment_history = PaymentHistory(
payment_key=payment_key,
status=enum.PaymentStatus.PAYMENT_SUCCESS.value,
is_webhook=True
)
payment_history.save()

if request.data["status"] != "paid":
raise ValueError("결제 승인건 이외의 요청")

dto = {
Expand All @@ -33,8 +47,30 @@ def post(self, request):
return Response(dto)


@api_view(["GET"])
def get__generate_payment_key(request):
class PaymentSuccessApi(APIView):
def post(self, request):
if not request.is_authenticated:
return Response({"msg": "not logged in user"}, status=400)

payment_key = request.data["merchant_uid"]

payment_history = PaymentHistory(
payment_key=payment_key,
status=enum.PaymentStatus.PAYMENT_SUCCESS.value,
is_webhook=False
)
payment_history.save()

dto = {
"msg": "ok",
"merchant_uid": request.data["merchant_uid"]
}

return Response(dto)


@api_view(["POST"])
def post__generate_payment_key(request):

request_ticket_type = TicketType.objects.get(id=request.data["ticket_type"])

Expand Down

0 comments on commit 7cec4af

Please sign in to comment.