Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:/sweeftdigital/accounts_G2 into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sandronadiradze committed Nov 5, 2024
2 parents 70358f9 + 1a0137c commit af06e06
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 5 deletions.
11 changes: 10 additions & 1 deletion locale/ka/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-11-05 11:14+0400\n"
"POT-Creation-Date: 2024-11-05 13:31+0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -433,6 +433,15 @@ msgstr ""
"თქვენ ხელთავიდან გამოგეგზავნათ ორ ფაქტორიანი ავთენტიფიკაციისთვის განკუთვნილი "
"ერთჯერადი კოდი."

#: payments/views.py:586 payments/views.py:601
msgid "No payments found for this user."
msgstr ""

#: templates/password_reset_with_email.html:28
msgid "Thanks for using our site!"
msgstr "მადლობას გიხდით ჩვენი ვებ-გვერდის გამოყენებისთვის!"

#, fuzzy
#~| msgid "You do not have permission to perform this action."
#~ msgid "You do not have permission to access this payment."
#~ msgstr "თქვენ არ გაქვთ ამ მოქმედების შესრულების უფლება."
22 changes: 22 additions & 0 deletions payments/migrations/0004_alter_balance_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.0.4 on 2024-11-05 07:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("payments", "0003_balance_currency"),
]

operations = [
migrations.AlterField(
model_name="balance",
name="currency",
field=models.CharField(
choices=[("GEL", "GEL"), ("USD", "USD"), ("EUR", "EUR")],
default="USD",
max_length=3,
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.4 on 2024-11-05 08:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("payments", "0004_alter_balance_currency"),
]

operations = [
migrations.AlterModelOptions(
name="payment",
options={"ordering": ["-created_at"]},
),
migrations.AlterField(
model_name="balance",
name="currency",
field=models.CharField(
choices=[("GEL", "GEL"), ("USD", "USD"), ("EUR", "EUR")],
default="GEL",
max_length=3,
),
),
]
5 changes: 5 additions & 0 deletions payments/models/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ class Status(models.TextChoices):
created_at = models.DateTimeField(auto_now_add=True, verbose_name="Created At")
updated_at = models.DateTimeField(auto_now=True, verbose_name="Updated At")

class Meta:
ordering = [
"-created_at",
]

def __str__(self):
return f"Payment {self.stripe_session_id} - {self.get_status_display()}"
16 changes: 16 additions & 0 deletions payments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,19 @@ def validate_amount(self, value):
if value <= Decimal("0.00"):
raise serializers.ValidationError("Amount must be greater than zero.")
return value


class PaymentSerializer(serializers.ModelSerializer):
class Meta:
model = Payment
fields = [
"id",
"customer",
"auction_id",
"stripe_session_id",
"amount",
"currency",
"status",
"created_at",
"updated_at",
]
4 changes: 4 additions & 0 deletions payments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
ConnectStripeAccountView,
CreateCheckoutSession,
DepositBalanceView,
PaymentListView,
SellerPayoutView,
TransferFundsView,
UserPaymentStatsView,
stripe_account_refresh,
stripe_account_return,
)
Expand Down Expand Up @@ -34,6 +36,8 @@
path("stripe-refresh/", stripe_account_refresh, name="stripe_account_refresh"),
path("transfer/", TransferFundsView.as_view(), name="transfer-funds"),
path("payout/", SellerPayoutView.as_view(), name="seller_payout"),
path("paymants/", PaymentListView.as_view(), name="payment_list"),
path("paymants/stats/", UserPaymentStatsView.as_view(), name="payment_stats"),
# Webhook Endpoints for handling stripe responses!
path("webhook/", stripe_webhook, name="deposit_webhook"),
path("connect/webhook/", stripe_connect_webhook, name="connect_webhook"),
Expand Down
69 changes: 65 additions & 4 deletions payments/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import stripe
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Count, Sum
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.exceptions import ValidationError
from rest_framework.exceptions import NotFound, ValidationError
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
Expand All @@ -24,6 +28,7 @@
from .serializers import (
CheckoutSessionSerializer,
DepositSerializer,
PaymentSerializer,
PayoutRequestSerializer,
TransferFundsSerializer,
)
Expand Down Expand Up @@ -182,8 +187,8 @@ def post(self, request):
}
],
mode="payment",
success_url="https://reverse-auction-front-g2-424868328181.europe-west3.run.app/balance-success",
cancel_url="https://reverse-auction-front-g2-424868328181.europe-west3.run.app/balance-cancel",
success_url="https://reverse-auction-front-g2-424868328181.europe-west3.run.app/dashboard",
cancel_url="https://reverse-auction-front-g2-424868328181.europe-west3.run.app/dashboard",
metadata={
"user_id": str(request.user.id),
"amount": str(amount),
Expand Down Expand Up @@ -555,7 +560,6 @@ def stripe_account_return(request):
return redirect(
"https://reverse-auction-front-g2-424868328181.europe-west3.run.app/dashboard"
)
# Adjust this to point to your front-end dashboard URL


def stripe_account_refresh(request):
Expand All @@ -564,3 +568,60 @@ def stripe_account_refresh(request):
Redirects the user back to the ConnectStripeAccountView to start a new session.
"""
return redirect(reverse("connect_stripe_account"))


@extend_schema(
tags=["Payment"],
)
class PaymentListView(ListAPIView):
permission_classes = [IsAuthenticated]
serializer_class = PaymentSerializer

def get_queryset(self):
queryset = Payment.objects.filter(customer=self.request.user).order_by(
"-created_at"
)

if not queryset.exists():
raise NotFound(detail=_("No payments found for this user."))

return queryset


@extend_schema(
tags=["Payment"],
)
class UserPaymentStatsView(APIView):
permission_classes = [IsAuthenticated]

def get(self, request):
user_payments = Payment.objects.filter(customer=request.user)

if not user_payments.exists():
raise NotFound(detail=_("No payments found for this user."))

all_time_stats = user_payments.aggregate(
total_amount=Sum("amount"), transaction_count=Count("id")
)

one_month_ago = timezone.now() - timezone.timedelta(days=30)

last_month_stats = user_payments.filter(created_at__gte=one_month_ago).aggregate(
total_amount=Sum("amount"), transaction_count=Count("id")
)

user_type = getattr(request.user, "user_type", None)

return Response(
{
"user_type": user_type,
"all_time": {
"total_amount": all_time_stats["total_amount"] or 0,
"transaction_count": all_time_stats["transaction_count"] or 0,
},
"last_month": {
"total_amount": last_month_stats["total_amount"] or 0,
"transaction_count": last_month_stats["transaction_count"] or 0,
},
}
)

0 comments on commit af06e06

Please sign in to comment.