transaction detail view
This commit is contained in:
parent
6256c340c9
commit
ca78b94653
8 changed files with 89 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from model_utils.choices import Choices
|
||||
|
||||
# TODO: add a state for invoice created
|
||||
STATE_CHOICES = Choices(
|
||||
(1, 'created', 'created'),
|
||||
(2, 'delayed', 'delayed'), # delayed as user wish
|
||||
|
|
|
|||
8
apps/wallet/forms.py
Normal file
8
apps/wallet/forms.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
from django import forms
|
||||
from .models import Transaction
|
||||
|
||||
|
||||
class TransactionActionForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Transaction
|
||||
fields = []
|
||||
|
|
@ -50,6 +50,7 @@ class TransactionManager(models.Manager):
|
|||
|
||||
|
||||
class Transaction(models.Model):
|
||||
state_choices = STATE_CHOICES
|
||||
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4, unique=True,
|
||||
db_index=True) # reserve_code
|
||||
amount = models.IntegerField(_('amount'), blank=False)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
from django.urls import path
|
||||
from .views import TransactionList, TransactionDetail, TransactionPay, TransactionReceipt, \
|
||||
WithdrawVerify, WithdrawSubmit, DepositSubmit, DepositVerify, TransactionListView
|
||||
WithdrawVerify, WithdrawSubmit, DepositSubmit, DepositVerify, TransactionListView, TransactionDetailView
|
||||
|
||||
app_name = "wallet"
|
||||
|
||||
urlpatterns = [
|
||||
path('transactions/', TransactionListView.as_view(), name='transaction_list'),
|
||||
path('transactions/<pk>', TransactionDetailView.as_view(), name='transaction_detail'),
|
||||
|
||||
path('api/transactions/', TransactionList.as_view(), name='transaction_list_api'),
|
||||
path('api/transactions/<uuid>', TransactionDetail.as_view(), name='transaction_detail_api'),
|
||||
path('api/transactions/<uuid>/pay', TransactionPay.as_view(), name='transaction_pay_api'),
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Q
|
||||
from django.urls import reverse
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.generic import DetailView, ListView
|
||||
from django.views.generic import DetailView, ListView, UpdateView
|
||||
|
||||
from oauth2_provider.contrib.rest_framework import TokenHasScope, IsAuthenticatedOrTokenHasScope
|
||||
from rest_framework import generics, permissions
|
||||
|
|
@ -11,6 +13,10 @@ from apps.gooyal_oauth2.rest_framework import IsAuthenticatedOrTokenMatchesOASRe
|
|||
from apps.wallet.models import Transaction
|
||||
from apps.wallet.serializers import TransactionSerializer, DepositSerializer, WithdrawSerializer
|
||||
from .constans import STATE_CHOICES
|
||||
from .forms import TransactionActionForm
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
|
||||
def get_application_client_id(request):
|
||||
|
|
@ -180,3 +186,28 @@ class TransactionListView(ListView):
|
|||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
return Transaction.objects.filter(Q(payee=user) | Q(payer=user)).all()
|
||||
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
class TransactionDetailView(UpdateView):
|
||||
model = Transaction
|
||||
form_class = TransactionActionForm
|
||||
template_name = 'wallet/transaction_detail.html'
|
||||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
return Transaction.objects.filter(Q(payee=user) | Q(payer=user) | Q(payer__isnull=True)).all()
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse('wallet:transaction_detail', kwargs={'pk': self.get_object().pk})
|
||||
|
||||
def form_valid(self, form):
|
||||
transaction: Transaction = form.instance
|
||||
if 'pay' in self.request.POST:
|
||||
result = transaction.pay(payer=self.request.user)
|
||||
messages.error(self.request, _('success.'))
|
||||
if 'receipt' in self.request.POST:
|
||||
result = transaction.receipt()
|
||||
messages.error(self.request, _('success.'))
|
||||
return super().form_valid(form)
|
||||
|
||||
|
|
|
|||
14
client.py
14
client.py
|
|
@ -144,16 +144,16 @@ class ApiClient():
|
|||
# return response and 'code' in response, response
|
||||
|
||||
# create application invoice
|
||||
def create_invoice(self, amount, delay):
|
||||
def create_invoice(self, amount, delay=0):
|
||||
data = {
|
||||
'delay': delay,
|
||||
'amount': amount
|
||||
}
|
||||
response = self._request('transactions/', method='post', data=data)
|
||||
response = self._request('wallet/api/transactions/', method='post', data=data)
|
||||
return response and 'code' in response, response
|
||||
|
||||
def pay_transaction(self, code):
|
||||
path = f'transactions/{code}/pay'
|
||||
def pay_invoice(self, code):
|
||||
path = f'wallet/api/transactions/{code}/pay'
|
||||
return self._request(path=path)
|
||||
|
||||
def receipt_transaction(self, code):
|
||||
|
|
@ -188,3 +188,9 @@ class ApiClient():
|
|||
except:
|
||||
return response.text
|
||||
|
||||
|
||||
client = ApiClient('+989106853582')
|
||||
client.request_otp()
|
||||
client.login('12345')
|
||||
print(client.create_invoice(2000, 10))
|
||||
# print(client.pay_invoice('7967a76a-5de1-48b2-92ac-1b0f39f7223b'))
|
||||
|
|
|
|||
25
templates/wallet/transaction_detail.html
Normal file
25
templates/wallet/transaction_detail.html
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{% extends "base_generic.html" %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
{% if object.payee == request.user %}
|
||||
دریافت مبلغ {{ object.amount }}از: {{ object.payer }} در وضعیت {{ object.get_state_display }}
|
||||
{% else %}
|
||||
پرداخت مبلغ {{ object.amount }} به: {{ object.payee }}
|
||||
{% endif %}
|
||||
|
||||
<form action="{{ request.get_full_path }}" method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
{% if object.state == object.state_choices.created %}
|
||||
<div class="col-6 mb-3">
|
||||
<input class="w-100 btn btn-primary btn" type="submit" value="پرداخت" name="pay"/>
|
||||
</div>
|
||||
{% elif object.state == object.state_choices.delayed %}
|
||||
<div class="col-6 mb-3">
|
||||
<input class="w-100 btn btn-primary btn" type="submit" value="تایید پرداخت" name="receipt"/>
|
||||
</div>
|
||||
{% endif %}
|
||||
</form>
|
||||
|
||||
|
||||
{% endblock %}
|
||||
|
|
@ -3,10 +3,17 @@
|
|||
{% block content %}
|
||||
{% for transaction in object_list %}
|
||||
{% if transaction.payee == request.user %}
|
||||
دریافت مبلغ {{ transaction.amount }}از: {{ transaction.payer }} در وضعیت {{ transaction.get_state_display }}
|
||||
<a href="{% url 'wallet:transaction_detail' transaction.pk %}">
|
||||
{{ transaction.amount }}
|
||||
</a>
|
||||
از: {{ transaction.payer }} در وضعیت {{ transaction.get_state_display }}
|
||||
{% else %}
|
||||
پرداخت مبلغ {{ transaction.amount }} به: {{ transaction.payee }}
|
||||
<a href="{% url 'wallet:transaction_detail' transaction.pk %}">
|
||||
{{ transaction.amount }}
|
||||
</a>
|
||||
به: {{ transaction.payee }} در وضعیت {{ transaction.get_state_display }}
|
||||
{% endif %}
|
||||
<br>
|
||||
{% endfor %}
|
||||
|
||||
{% endblock %}
|
||||
Loading…
Add table
Reference in a new issue