Skip to content

Commit

Permalink
feat: add payment status
Browse files Browse the repository at this point in the history
  • Loading branch information
Adibov committed Nov 23, 2023
1 parent d56e3f3 commit 77997b5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.4 on 2023-11-23 21:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('backend_api', '0046_teacher_workplace'),
]

operations = [
migrations.RemoveField(
model_name='payment',
name='is_done',
),
migrations.AddField(
model_name='payment',
name='status',
field=models.IntegerField(choices=[(0, 'Awaiting payment'), (1, 'Payment confirmed'), (2, 'Payment rejected')], default=0),
),
]
43 changes: 24 additions & 19 deletions backend/backend_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,17 @@ def __str__(self):


class Payment(models.Model):
class PaymentStatus(models.IntegerChoices):
AWAITING_PAYMENT = 0, _('Awaiting payment')
PAYMENT_CONFIRMED = 1, _('Payment confirmed')
PAYMENT_REJECTED = 2, _('Payment rejected')

id = models.BigAutoField(primary_key=True)
amount = models.PositiveIntegerField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
workshops = models.ManyToManyField(Workshop, blank=True)
presentations = models.ManyToManyField(Presentation, blank=True)
is_done = models.BooleanField(default=False)
status = models.IntegerField(default=PaymentStatus.AWAITING_PAYMENT, choices=PaymentStatus.choices)
year = models.IntegerField(blank=False, default=2020)
date = models.DateField(blank=False,
default=datetime.datetime(year=2020, month=7, day=1, hour=0, minute=0, second=0,
Expand All @@ -304,22 +309,23 @@ class Payment(models.Model):
def __str__(self):
return f"Payment for {self.user.account} ({self.amount}) in {str(self.date)}"

def verify_payment(self):
self.is_done = True
for workshop in self.workshops.all():
try:
workshop = WorkshopRegistration.objects.get(user=self.user, workshop=workshop)
workshop.status = WorkshopRegistration.StatusChoices.PURCHASED
workshop.save()
except ObjectDoesNotExist:
pass
for presentation in self.presentations.all():
try:
presentation = PresentationParticipation.objects.get(user=self.user, presentation=presentation)
presentation.status = PresentationParticipation.StatusChoices.PURCHASED
presentation.save()
except ObjectDoesNotExist:
pass
def update_payment_status(self, payment_status: PaymentStatus):
self.status = payment_status
if payment_status == self.PaymentStatus.PAYMENT_CONFIRMED:
for workshop in self.workshops.all():
try:
workshop = WorkshopRegistration.objects.get(user=self.user, workshop=workshop)
workshop.status = WorkshopRegistration.StatusChoices.PURCHASED
workshop.save()
except ObjectDoesNotExist:
pass
for presentation in self.presentations.all():
try:
presentation = PresentationParticipation.objects.get(user=self.user, presentation=presentation)
presentation.status = PresentationParticipation.StatusChoices.PURCHASED
presentation.save()
except ObjectDoesNotExist:
pass
self.save()

@staticmethod
Expand All @@ -346,8 +352,7 @@ def create_payment_for_user(user: User):
except ObjectDoesNotExist:
raise ValueError(f"User {user} is registered for presentation {presentation} but has no registration")
if len(workshops) == 0 and len(presentations) == 0:
return Response(new_detailed_response(
status.HTTP_400_BAD_REQUEST, "User has no unpaid item"))
return None
payment = Payment.objects.create(user=user, amount=total_cost, year=datetime.date.today().year,
date=datetime.datetime.now())
payment.workshops.set(workshops)
Expand Down
14 changes: 6 additions & 8 deletions backend/backend_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,10 @@ def payment(self, request):
return Response(new_detailed_response(
status.HTTP_400_BAD_REQUEST, "User not found"))

try:
payment = Payment.objects.get(user=user, is_done=False)
payment.delete()
except ObjectDoesNotExist:
pass
finally:
payment = Payment.create_payment_for_user(user)
payment = Payment.create_payment_for_user(user)
if payment is None:
return Response(new_detailed_response(
status.HTTP_400_BAD_REQUEST, "User has no unpaid item"))

response = ZIFYRequest().create_payment(payment.pk, payment.amount, user.name, user.phone_number,
user.account.email)
Expand All @@ -261,10 +258,11 @@ def verify(self, request):
status.HTTP_400_BAD_REQUEST, "Payment not found"))
response = ZIFYRequest().verify_payment(payment.track_id)
if response['status'] == ZIFY_STATUS_OK:
payment.verify_payment()
payment.update_payment_status(Payment.PaymentStatus.PAYMENT_CONFIRMED)
# FIXME: redirect to payment success page
return Response(new_detailed_response(status.HTTP_200_OK, "Payment verified successfully"))
else:
payment.update_payment_status(Payment.PaymentStatus.PAYMENT_REJECTED)
return Response(
new_detailed_response(response['status'], response["message"]))

Expand Down

0 comments on commit 77997b5

Please sign in to comment.