Features: 1) Add get_integration_class_object method to Transaction for dynamic gateway class instantiation;
Fixes: 1) Validate gateway presence in `process_transaction_changes` signal; 2) Add missing imports for `Type`, `create_object`, and `AbstractGateway`; Extra: 1) Cleanup unused diff lines in migrations files; 2) Improve JSONField, FloatField, and PositiveIntegerField declarations by consolidating into single lines; 3) Remove redundant match-case logic in `signals.py`.
This commit is contained in:
parent
a87cb31d7e
commit
5384fa494d
5 changed files with 22 additions and 23 deletions
|
|
@ -4,7 +4,6 @@ from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("blog", "0006_post_meta_description_post_meta_description_ar_ar_and_more"),
|
("blog", "0006_post_meta_description_post_meta_description_ar_ar_and_more"),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("core", "0047_alter_attribute_unique_together"),
|
("core", "0047_alter_attribute_unique_together"),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("payments", "0004_alter_transaction_payment_method"),
|
("payments", "0004_alter_transaction_payment_method"),
|
||||||
]
|
]
|
||||||
|
|
@ -98,15 +97,11 @@ class Migration(migrations.Migration):
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"minimum_transaction_amount",
|
"minimum_transaction_amount",
|
||||||
models.FloatField(
|
models.FloatField(default=0, verbose_name="minimum transaction amount"),
|
||||||
default=0, verbose_name="minimum transaction amount"
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"maximum_transaction_amount",
|
"maximum_transaction_amount",
|
||||||
models.FloatField(
|
models.FloatField(default=0, verbose_name="maximum transaction amount"),
|
||||||
default=0, verbose_name="maximum transaction amount"
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"daily_limit",
|
"daily_limit",
|
||||||
|
|
@ -126,15 +121,11 @@ class Migration(migrations.Migration):
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"priority",
|
"priority",
|
||||||
models.PositiveIntegerField(
|
models.PositiveIntegerField(default=10, unique=True, verbose_name="priority"),
|
||||||
default=10, unique=True, verbose_name="priority"
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"integration_variables",
|
"integration_variables",
|
||||||
models.JSONField(
|
models.JSONField(default=dict, verbose_name="integration variables"),
|
||||||
default=dict, verbose_name="integration variables"
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
from constance import config
|
from constance import config
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.postgres.indexes import GinIndex
|
from django.contrib.postgres.indexes import GinIndex
|
||||||
|
|
@ -16,6 +18,8 @@ from django.utils.timezone import now
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from core.abstract import NiceModel
|
from core.abstract import NiceModel
|
||||||
|
from evibes.utils.misc import create_object
|
||||||
|
from payments.gateways import AbstractGateway
|
||||||
|
|
||||||
|
|
||||||
class Transaction(NiceModel):
|
class Transaction(NiceModel):
|
||||||
|
|
@ -147,3 +151,14 @@ class Gateway(NiceModel):
|
||||||
monthly_ok = self.monthly_limit == 0 or monthly_sum < self.monthly_limit
|
monthly_ok = self.monthly_limit == 0 or monthly_sum < self.monthly_limit
|
||||||
|
|
||||||
return daily_ok and monthly_ok
|
return daily_ok and monthly_ok
|
||||||
|
|
||||||
|
def get_integration_class_object(self, raise_exc: bool = True) -> Type[AbstractGateway] | None:
|
||||||
|
if not self.integration_path:
|
||||||
|
if raise_exc:
|
||||||
|
raise ValueError(_("gateway integration path is not set"))
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
module_name, class_name = self.integration_path.rsplit(".", 1)
|
||||||
|
except ValueError as exc:
|
||||||
|
raise ValueError(_("invalid integration path: %(path)s") % {"path": self.integration_path}) from exc
|
||||||
|
return create_object(module_name, class_name)
|
||||||
|
|
|
||||||
|
|
@ -24,15 +24,10 @@ def create_balance_on_user_creation_signal(instance: User, created: bool, **kwar
|
||||||
@receiver(post_save, sender=Transaction)
|
@receiver(post_save, sender=Transaction)
|
||||||
def process_transaction_changes(instance: Transaction, created: bool, **kwargs: dict[Any, Any]) -> None:
|
def process_transaction_changes(instance: Transaction, created: bool, **kwargs: dict[Any, Any]) -> None:
|
||||||
if created:
|
if created:
|
||||||
|
if not instance.gateway:
|
||||||
|
raise ValueError("gateway is required to process a transaction")
|
||||||
try:
|
try:
|
||||||
gateway = None
|
gateway = instance.gateway.get_integration_class_object()
|
||||||
match instance.process.get("gateway", "default"):
|
|
||||||
case "gateway":
|
|
||||||
gateway = AbstractGateway()
|
|
||||||
case "default":
|
|
||||||
gateway = AbstractGateway()
|
|
||||||
case _:
|
|
||||||
gateway = AbstractGateway()
|
|
||||||
gateway.process_transaction(instance)
|
gateway.process_transaction(instance)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
instance.process = {"status": "ERRORED", "error": str(e)}
|
instance.process = {"status": "ERRORED", "error": str(e)}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue