FEATURE(pg): check payment request success and add logger
check payment request success and add logger
This commit is contained in:
parent
c8939de18f
commit
fe31f41362
4 changed files with 96 additions and 78 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
import io
|
import io
|
||||||
|
|
||||||
import jdatetime
|
import jdatetime
|
||||||
|
|
@ -7,13 +8,13 @@ from django.db.models import Sum
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect, get_object_or_404
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from utils.wallet_client import deposit_to_user_wallet_verify
|
|
||||||
|
|
||||||
from .constans import StateChoices
|
from .constans import StateChoices
|
||||||
from .models import PaymentRequest, PNA, Fake
|
from .models import PaymentRequest, PNA, Fake
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class PNAAdmin(admin.ModelAdmin):
|
class PNAAdmin(admin.ModelAdmin):
|
||||||
list_display = ['pna_status_code', 'pna_status_message', 'jajali_datetime', 'payment_request']
|
list_display = ['pna_status_code', 'pna_status_message', 'jajali_datetime', 'payment_request']
|
||||||
|
|
@ -83,7 +84,7 @@ class PaymentRequestAdmin(admin.ModelAdmin):
|
||||||
return super().changelist_view(request, extra_context=extra_context)
|
return super().changelist_view(request, extra_context=extra_context)
|
||||||
|
|
||||||
def check_success_again(self, obj):
|
def check_success_again(self, obj):
|
||||||
if obj.state == StateChoices.WALLET_SUBMIT_SUCCESS:
|
if obj.state in [StateChoices.IPG_SUCCESS, StateChoices.WALLET_SUBMIT_PENDING, StateChoices.WALLET_SUBMIT_SUCCESS]:
|
||||||
return format_html(
|
return format_html(
|
||||||
'<a class="button" href="{}">بررسی دوباره موفقیت</a>',
|
'<a class="button" href="{}">بررسی دوباره موفقیت</a>',
|
||||||
f'{obj.uuid}/check_success_again/'
|
f'{obj.uuid}/check_success_again/'
|
||||||
|
|
@ -96,26 +97,18 @@ class PaymentRequestAdmin(admin.ModelAdmin):
|
||||||
path('<uuid:payment_request_uuid>/check_success_again/', self.admin_site.admin_view(self.check_success_again_view))
|
path('<uuid:payment_request_uuid>/check_success_again/', self.admin_site.admin_view(self.check_success_again_view))
|
||||||
]
|
]
|
||||||
return custom_urls + urls
|
return custom_urls + urls
|
||||||
|
|
||||||
def check_success_again_view(self, request, payment_request_uuid):
|
def check_success_again_view(self, request, payment_request_uuid):
|
||||||
verify_response = deposit_to_user_wallet_verify(payment_request_uuid)
|
logger.info(f"[Admin panel] Check success again for payment_request_uuid={payment_request_uuid}")
|
||||||
if not verify_response:
|
payment_request: PaymentRequest = get_object_or_404(PaymentRequest, uuid=payment_request_uuid)
|
||||||
self.message_user(request, "خطای استعلام", level=messages.ERROR)
|
logger.info(f"[Admin panel] initial payment_request state={payment_request.state}")
|
||||||
else:
|
if payment_request.state == StateChoices.IPG_SUCCESS:
|
||||||
if not verify_response.uuid or verify_response.state != 5:
|
payment_request.charge_wallet_balance()
|
||||||
to_pay_failed = PaymentRequest.objects.filter(
|
elif payment_request.state == StateChoices.WALLET_SUBMIT_PENDING:
|
||||||
pk=payment_request_uuid,
|
payment_request.deposit_submit()
|
||||||
state=StateChoices.WALLET_SUBMIT_SUCCESS.value
|
elif payment_request.state == StateChoices.WALLET_SUBMIT_SUCCESS:
|
||||||
).update(
|
payment_request.deposit_verify()
|
||||||
state=StateChoices.FAILED.value,
|
payment_request.refresh_from_db()
|
||||||
)
|
logger.info(f"[Admin panel] final payment_request state={payment_request.state}")
|
||||||
else:
|
|
||||||
to_pay_success = PaymentRequest.objects.filter(
|
|
||||||
pk=payment_request_uuid,
|
|
||||||
state=StateChoices.WALLET_SUBMIT_SUCCESS.value
|
|
||||||
).update(
|
|
||||||
state=StateChoices.SUCCESS.value,
|
|
||||||
)
|
|
||||||
self.message_user(request, f"درخواست {payment_request_uuid} بررسی شد ✅")
|
self.message_user(request, f"درخواست {payment_request_uuid} بررسی شد ✅")
|
||||||
return redirect(request.META.get("HTTP_REFERER", ".."))
|
return redirect(request.META.get("HTTP_REFERER", ".."))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from mimetypes import guess_all_extensions
|
|
||||||
|
|
||||||
import jdatetime
|
import jdatetime
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
@ -18,7 +19,7 @@ from utils.wallet_client import deposit_to_user_wallet_submit, withdraw_from_use
|
||||||
|
|
||||||
from utils.pna import PNAClient
|
from utils.pna import PNAClient
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class GatewayChoise(TextChoices):
|
class GatewayChoise(TextChoices):
|
||||||
FAKE = "fake", _("fake")
|
FAKE = "fake", _("fake")
|
||||||
|
|
@ -114,16 +115,7 @@ class PaymentRequest(models.Model):
|
||||||
else:
|
else:
|
||||||
raise Exception(error_message)
|
raise Exception(error_message)
|
||||||
|
|
||||||
def charge_wallet_balance(self):
|
def deposit_submit(self):
|
||||||
if self.state in [StateChoices.IPG_SUCCESS]:
|
|
||||||
to_pending = PaymentRequest.objects.filter(
|
|
||||||
pk=self.pk,
|
|
||||||
state=StateChoices.IPG_SUCCESS.value,
|
|
||||||
).update(
|
|
||||||
state=StateChoices.WALLET_SUBMIT_PENDING.value)
|
|
||||||
if not to_pending:
|
|
||||||
raise Exception('Cannot charge balance because state')
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"uuid": str(self.uuid),
|
"uuid": str(self.uuid),
|
||||||
"payee_id": str(self.user.uuid),
|
"payee_id": str(self.user.uuid),
|
||||||
|
|
@ -135,9 +127,10 @@ class PaymentRequest(models.Model):
|
||||||
'application_details_url': ''
|
'application_details_url': ''
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
submit_response = deposit_to_user_wallet_submit(data)
|
submit_response = deposit_to_user_wallet_submit(settings.WALLET_CATEGORY_UUID, data)
|
||||||
|
|
||||||
if not submit_response.uuid:
|
if not submit_response or not submit_response.uuid:
|
||||||
|
logger.error(f"[Deposit Submit] Wallet submission failed | payment_id={self.pk}")
|
||||||
raise Exception('Failed to submit payment in wallet')
|
raise Exception('Failed to submit payment in wallet')
|
||||||
|
|
||||||
to_success = PaymentRequest.objects.filter(
|
to_success = PaymentRequest.objects.filter(
|
||||||
|
|
@ -147,11 +140,19 @@ class PaymentRequest(models.Model):
|
||||||
state=StateChoices.WALLET_SUBMIT_SUCCESS.value)
|
state=StateChoices.WALLET_SUBMIT_SUCCESS.value)
|
||||||
|
|
||||||
if not to_success:
|
if not to_success:
|
||||||
|
logger.error(f"[Deposit Submit] Failed to update state to WALLET_SUBMIT_SUCCESS | payment_id={self.pk}")
|
||||||
raise Exception('Cannot charge balance because state')
|
raise Exception('Cannot charge balance because state')
|
||||||
|
return to_success
|
||||||
|
|
||||||
verify_response = deposit_to_user_wallet_verify(self.uuid)
|
def deposit_verify(self):
|
||||||
|
verify_response = deposit_to_user_wallet_verify(settings.WALLET_CATEGORY_UUID, self.uuid)
|
||||||
|
|
||||||
|
if not verify_response:
|
||||||
|
logger.error(f"[Deposit Verify] Failed to get response from wallet service | payment_id={self.pk}")
|
||||||
|
raise Exception('Failed to get response from wallet service')
|
||||||
|
|
||||||
if not verify_response.uuid or verify_response.state != 5:
|
if not verify_response.uuid or verify_response.state != 5:
|
||||||
|
logger.error(f"[Deposit Verify] Wallet verification failed | payment_id={self.pk}, response={verify_response}")
|
||||||
to_pay_failed = PaymentRequest.objects.filter(
|
to_pay_failed = PaymentRequest.objects.filter(
|
||||||
pk=self.pk,
|
pk=self.pk,
|
||||||
state=StateChoices.WALLET_SUBMIT_SUCCESS.value
|
state=StateChoices.WALLET_SUBMIT_SUCCESS.value
|
||||||
|
|
@ -168,8 +169,29 @@ class PaymentRequest(models.Model):
|
||||||
)
|
)
|
||||||
|
|
||||||
if not to_pay_success:
|
if not to_pay_success:
|
||||||
|
logger.error(f"[Deposit Verify] Failed to update state to SUCCESS | payment_id={self.pk}")
|
||||||
raise Exception('Failed to update state payment')
|
raise Exception('Failed to update state payment')
|
||||||
|
|
||||||
|
return to_pay_success
|
||||||
|
|
||||||
|
def charge_wallet_balance(self):
|
||||||
|
logger.info(f"[Charge Wallet] Starting wallet charge process | payment_id={self.pk}")
|
||||||
|
if self.state in [StateChoices.IPG_SUCCESS]:
|
||||||
|
to_pending = PaymentRequest.objects.filter(
|
||||||
|
pk=self.pk,
|
||||||
|
state=StateChoices.IPG_SUCCESS.value,
|
||||||
|
).update(
|
||||||
|
state=StateChoices.WALLET_SUBMIT_PENDING.value)
|
||||||
|
if not to_pending:
|
||||||
|
logger.error(f"[Charge Wallet] Failed to update state to WALLET_SUBMIT_PENDING | payment_id={self.pk}")
|
||||||
|
raise Exception('Cannot charge balance because state')
|
||||||
|
logger.info(f"[Charge Wallet] State updated to WALLET_SUBMIT_PENDING | payment_id={self.pk}")
|
||||||
|
self.deposit_submit()
|
||||||
|
to_pay_success = self.deposit_verify()
|
||||||
|
if to_pay_success:
|
||||||
|
logger.info(f"[Charge Wallet] Wallet charged successfully | payment_id={self.pk}")
|
||||||
|
else:
|
||||||
|
logger.warning(f"[Charge Wallet] Wallet charge verification failed | payment_id={self.pk}")
|
||||||
return bool(to_pay_success)
|
return bool(to_pay_success)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -255,4 +255,5 @@ CACHES = {
|
||||||
|
|
||||||
PNA_CORPORATION_PIN = config('PNA_CORPORATION_PIN')
|
PNA_CORPORATION_PIN = config('PNA_CORPORATION_PIN')
|
||||||
WALLET_BASE_PUBLIC_URL = config('WALLET_BASE_PUBLIC_URL')
|
WALLET_BASE_PUBLIC_URL = config('WALLET_BASE_PUBLIC_URL')
|
||||||
|
WALLET_CATEGORY_UUID = config('WALLET_CATEGORY_UUID')
|
||||||
IPG_BASE_PUBLIC_URL = config('IPG_BASE_PUBLIC_URL')
|
IPG_BASE_PUBLIC_URL = config('IPG_BASE_PUBLIC_URL')
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ from httpx import Request
|
||||||
|
|
||||||
from gooyal_wallet_client import AuthenticatedClient
|
from gooyal_wallet_client import AuthenticatedClient
|
||||||
from gooyal_wallet_client.models import MyWallet, UserTransaction, ApplicationDeposit, ApplicationWithdraw
|
from gooyal_wallet_client.models import MyWallet, UserTransaction, ApplicationDeposit, ApplicationWithdraw
|
||||||
from gooyal_wallet_client.api.wallet import (wallet_api_application_deposit_create,
|
from gooyal_wallet_client.api.wallet import (wallet_api_application_deposit_create_2,
|
||||||
wallet_api_application_deposit_verify_retrieve,
|
wallet_api_application_deposit_verify_retrieve_2,
|
||||||
wallet_api_application_withdraw_create,
|
wallet_api_application_withdraw_create,
|
||||||
wallet_api_application_withdraw_verify_retrieve)
|
wallet_api_application_withdraw_verify_retrieve)
|
||||||
from gooyal_wallet_client.types import Response
|
from gooyal_wallet_client.types import Response
|
||||||
|
|
@ -73,22 +73,24 @@ def get_client():
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
def deposit_to_user_wallet_submit(data):
|
def deposit_to_user_wallet_submit(category_uuid, data):
|
||||||
class Tmp():
|
class Tmp():
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
deposit_data = Tmp()
|
deposit_data = Tmp()
|
||||||
client = get_client()
|
client = get_client()
|
||||||
response: ApplicationDeposit = wallet_api_application_deposit_create.sync(client=client, body=deposit_data)
|
response: ApplicationDeposit = wallet_api_application_deposit_create_2.sync(category_uuid= category_uuid,
|
||||||
|
client=client,
|
||||||
|
body=deposit_data)
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def deposit_to_user_wallet_verify(uuid):
|
def deposit_to_user_wallet_verify(category_uuid, uuid):
|
||||||
client = get_client()
|
client = get_client()
|
||||||
response: ApplicationDeposit = wallet_api_application_deposit_verify_retrieve.sync(uuid=uuid, client=client)
|
response: ApplicationDeposit = wallet_api_application_deposit_verify_retrieve_2.sync(category_uuid=category_uuid, uuid=uuid, client=client)
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue