Compare commits
No commits in common. "b8e7ebb2952a8df881f217cea9064eb61a353486" and "0083f1b09233aed6945f0bdb5a72f0a715484d8f" have entirely different histories.
b8e7ebb295
...
0083f1b092
1 changed files with 49 additions and 3 deletions
|
|
@ -11,7 +11,7 @@ from django.urls import reverse
|
||||||
from oauth2_provider.settings import oauth2_settings
|
from oauth2_provider.settings import oauth2_settings
|
||||||
|
|
||||||
from apps.core.models import BaseModel, BasePGModel
|
from apps.core.models import BaseModel, BasePGModel
|
||||||
from apps.pg.constans import StateChoices
|
from apps.pg.constans import StateChoices, TypeChoices
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from utils.clients.wallet_client import deposit_to_user_wallet_submit, deposit_to_user_wallet_verify
|
from utils.clients.wallet_client import deposit_to_user_wallet_submit, deposit_to_user_wallet_verify
|
||||||
|
|
@ -83,6 +83,10 @@ class PaymentRequest(models.Model):
|
||||||
|
|
||||||
return to_jalali(self.created_at)
|
return to_jalali(self.created_at)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def income_uuid(self):
|
||||||
|
return uuid.uuid5(self.uuid, 'income')
|
||||||
|
|
||||||
def save_commission(self):
|
def save_commission(self):
|
||||||
self.commission = calculate_pna_commission(self.amount)
|
self.commission = calculate_pna_commission(self.amount)
|
||||||
self.save(update_fields=['commission'])
|
self.save(update_fields=['commission'])
|
||||||
|
|
@ -153,7 +157,7 @@ class PaymentRequest(models.Model):
|
||||||
'application_details_url': ''
|
'application_details_url': ''
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
submit_response = deposit_to_user_wallet_submit(settings.WALLET_IPG_CREDIT, data)
|
submit_response = deposit_to_user_wallet_submit(settings.WALLET_RIAL_DEPOSIT, data)
|
||||||
|
|
||||||
if not submit_response or 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}")
|
logger.error(f"[Deposit Submit] Wallet submission failed | payment_id={self.pk}")
|
||||||
|
|
@ -171,7 +175,7 @@ class PaymentRequest(models.Model):
|
||||||
return to_success
|
return to_success
|
||||||
|
|
||||||
def deposit_verify(self):
|
def deposit_verify(self):
|
||||||
verify_response = deposit_to_user_wallet_verify(settings.WALLET_IPG_CREDIT, self.uuid)
|
verify_response = deposit_to_user_wallet_verify(settings.WALLET_RIAL_DEPOSIT, self.uuid)
|
||||||
|
|
||||||
if not verify_response:
|
if not verify_response:
|
||||||
logger.error(f"[Deposit Verify] Failed to get response from wallet service | payment_id={self.pk}")
|
logger.error(f"[Deposit Verify] Failed to get response from wallet service | payment_id={self.pk}")
|
||||||
|
|
@ -205,6 +209,47 @@ class PaymentRequest(models.Model):
|
||||||
|
|
||||||
return to_pay_success
|
return to_pay_success
|
||||||
|
|
||||||
|
def deposit_income_submit(self):
|
||||||
|
data = {
|
||||||
|
"uuid": str(self.income_uuid),
|
||||||
|
"payee": str(self.application_id),
|
||||||
|
"payee_type": TypeChoices.APPLICATION.value,
|
||||||
|
"payee_wallet": settings.WALLET_IPG_CREDIT,
|
||||||
|
"amount": self.commission,
|
||||||
|
"details": {
|
||||||
|
'description': str(_("ipg commission income")),
|
||||||
|
'reference_id': str(self.uuid),
|
||||||
|
'application_details_url': ''
|
||||||
|
},
|
||||||
|
}
|
||||||
|
submit_response = deposit_to_user_wallet_submit(settings.WALLET_IPG_CREDIT, data)
|
||||||
|
|
||||||
|
if not submit_response or not submit_response.uuid:
|
||||||
|
raise Exception('Failed to submit income in wallet')
|
||||||
|
|
||||||
|
return submit_response
|
||||||
|
|
||||||
|
def deposit_income_verify(self):
|
||||||
|
verify_response = deposit_to_user_wallet_verify(settings.WALLET_IPG_CREDIT, self.income_uuid)
|
||||||
|
|
||||||
|
if not verify_response or not verify_response.uuid:
|
||||||
|
raise Exception('Failed to verify income in wallet')
|
||||||
|
|
||||||
|
if verify_response.state != 5:
|
||||||
|
raise Exception('Failed to verify income in wallet')
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def charge_wallet_income(self):
|
||||||
|
if not self.application_id or not self.commission:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
self.deposit_income_submit()
|
||||||
|
self.deposit_income_verify()
|
||||||
|
logger.info(f"[Charge Wallet] Income charged successfully | payment_id={self.pk}")
|
||||||
|
except Exception:
|
||||||
|
logger.exception(f"[Charge Wallet] Failed to charge income | payment_id={self.pk}")
|
||||||
|
|
||||||
def charge_wallet_balance(self):
|
def charge_wallet_balance(self):
|
||||||
logger.info(f"[Charge Wallet] Starting wallet charge process | payment_id={self.pk}")
|
logger.info(f"[Charge Wallet] Starting wallet charge process | payment_id={self.pk}")
|
||||||
if self.state in [StateChoices.IPG_SUCCESS]:
|
if self.state in [StateChoices.IPG_SUCCESS]:
|
||||||
|
|
@ -221,6 +266,7 @@ class PaymentRequest(models.Model):
|
||||||
to_pay_success = self.deposit_verify()
|
to_pay_success = self.deposit_verify()
|
||||||
if to_pay_success:
|
if to_pay_success:
|
||||||
logger.info(f"[Charge Wallet] Wallet charged successfully | payment_id={self.pk}")
|
logger.info(f"[Charge Wallet] Wallet charged successfully | payment_id={self.pk}")
|
||||||
|
self.charge_wallet_income()
|
||||||
else:
|
else:
|
||||||
logger.warning(f"[Charge Wallet] Wallet charge verification failed | payment_id={self.pk}")
|
logger.warning(f"[Charge Wallet] Wallet charge verification failed | payment_id={self.pk}")
|
||||||
return bool(to_pay_success)
|
return bool(to_pay_success)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue