fake charge
This commit is contained in:
parent
1c437c49c6
commit
e54e4ae829
2 changed files with 20 additions and 11 deletions
|
|
@ -12,6 +12,7 @@ from rest_framework.views import APIView
|
|||
from apps.core.forms import ChargeForm
|
||||
from apps.core.models import Charge
|
||||
from apps.core.serializers import ChargeSerializer
|
||||
from apps.wallet.constans import StateChoices
|
||||
from apps.wallet.models import Transaction
|
||||
|
||||
|
||||
|
|
@ -38,29 +39,32 @@ class ChargeView(UpdateView):
|
|||
|
||||
def get_queryset(self):
|
||||
return Charge.objects.all()
|
||||
# return Charge.objects.filter(state__in=[StateChoices.CREATED.value, StateChoices.SUCCESS.value])
|
||||
|
||||
def form_valid(self, form):
|
||||
self.object = form.save()
|
||||
amount = form.cleaned_data['amount']
|
||||
payer_pk = 'a95557d6-4f21-4717-99c2-354db85f940e'
|
||||
charge = self.get_object()
|
||||
transaction = Transaction.objects.create(
|
||||
uuid= charge.uuid,
|
||||
payee_id= charge.user.pk,
|
||||
payee_type=1,
|
||||
payer_id=payer_pk,
|
||||
payer_type=2,
|
||||
amount=amount,
|
||||
details = {'description': 'شارژ مجازی کیف پول'}
|
||||
)
|
||||
|
||||
charge: Charge = self.get_object()
|
||||
try:
|
||||
charge.change_state(from_state=StateChoices.CREATED.value, to_state=StateChoices.PENDING.value)
|
||||
transaction = Transaction.objects.create(
|
||||
uuid= charge.uuid,
|
||||
payee_id= charge.user.pk,
|
||||
payee_type=1,
|
||||
payer_id=payer_pk,
|
||||
payer_type=2,
|
||||
amount=amount,
|
||||
details = {'description': 'شارژ مجازی کیف پول'}
|
||||
)
|
||||
transaction.submit()
|
||||
transaction.verify()
|
||||
charge.change_state(from_state=StateChoices.PENDING.value, to_state=StateChoices.SUCCESS.value)
|
||||
messages.success(self.request, 'شارژ انجام شد')
|
||||
|
||||
except:
|
||||
messages.error(self.request, 'خطایی رخ داده است')
|
||||
charge.change_state(from_state=StateChoices.PENDING.value, to_state=StateChoices.EXPECTED_FAILURE.value)
|
||||
|
||||
return HttpResponseRedirect(self.get_success_url())
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
|
||||
{% block content %}
|
||||
{% if object.state in '1' %}
|
||||
<form action="{{ request.get_full_path }}" method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
<div class="row g-3">
|
||||
|
|
@ -23,5 +24,9 @@
|
|||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% else %}
|
||||
{{ object.amount }} in state {{ object.get_state_display }}<br>
|
||||
back to application
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
Loading…
Add table
Reference in a new issue