Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add zero-downtime support for support-refund (#12237)
Krzysztof Waliczekb5ebb916d904
diff --git a/saleor/celeryconf.py b/saleor/celeryconf.pyindex 796bb47112..2ab37df664 100644--- a/saleor/celeryconf.py+++ b/saleor/celeryconf.py@@ -27,5 +27,11 @@ app = Celery("saleor")app.config_from_object("django.conf:settings", namespace="CELERY")app.autodiscover_tasks()+app.autodiscover_tasks(+ packages=[+ "saleor.payment.migrations.tasks",+ ],+ related_name="saleor3_12",+)app.autodiscover_tasks(lambda: discover_plugins_modules(settings.PLUGINS)) # type: ignore[misc] # circular import # noqa: E501app.autodiscover_tasks(related_name="search_tasks")diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.pyindex 1b32cc5509..6b004239d2 100644--- a/saleor/payment/__init__.py+++ b/saleor/payment/__init__.py@@ -169,3 +169,63 @@ class TransactionStatus:FAILURE = "failure"CHOICES = [(PENDING, "Pending"), (SUCCESS, "Success"), (FAILURE, "Failure")]+++class TransactionEventType:+ """Represents possible event types.++ Added in Saleor 3.12.+ The following types are possible:+ AUTHORIZATION_SUCCESS - represents success authorization.+ AUTHORIZATION_FAILURE - represents failure authorization.+ AUTHORIZATION_ADJUSTMENT - represents authorization adjustment.+ AUTHORIZATION_REQUEST - represents authorization request.+ CHARGE_SUCCESS - represents success charge.+ CHARGE_FAILURE - represents failure charge.+ CHARGE_BACK - represents chargeback.+ CHARGE_REQUEST - represents charge request.+ REFUND_SUCCESS - represents success refund.+ REFUND_FAILURE - represents failure refund.+ REFUND_REVERSE - represents reverse refund.+ REFUND_REQUEST - represents refund request.+ CANCEL_SUCCESS - represents success cancel.+ CANCEL_FAILURE - represents failure cancel.+ CANCEL_REQUEST - represents cancel request.+ INFO - represents info event.+ """++ AUTHORIZATION_SUCCESS = "authorization_success"+ AUTHORIZATION_FAILURE = "authorization_failure"+ AUTHORIZATION_ADJUSTMENT = "authorization_adjustment"+ AUTHORIZATION_REQUEST = "authorization_request"+ CHARGE_SUCCESS = "charge_success"+ CHARGE_FAILURE = "charge_failure"+ CHARGE_BACK = "charge_back"+ CHARGE_REQUEST = "charge_request"+ REFUND_SUCCESS = "refund_success"+ REFUND_FAILURE = "refund_failure"+ REFUND_REVERSE = "refund_reverse"+ REFUND_REQUEST = "refund_request"+ CANCEL_SUCCESS = "cancel_success"+ CANCEL_FAILURE = "cancel_failure"+ CANCEL_REQUEST = "cancel_request"+ INFO = "info"++ CHOICES = [+ (AUTHORIZATION_SUCCESS, "Represents success authorization"),+ (AUTHORIZATION_FAILURE, "Represents failure authorization"),+ (AUTHORIZATION_ADJUSTMENT, "Represents authorization adjustment"),+ (AUTHORIZATION_REQUEST, "Represents authorization request"),+ (CHARGE_SUCCESS, "Represents success charge"),+ (CHARGE_FAILURE, "Represents failure charge"),+ (CHARGE_BACK, "Represents chargeback."),+ (CHARGE_REQUEST, "Represents charge request"),+ (REFUND_SUCCESS, "Represents success refund"),+ (REFUND_FAILURE, "Represents failure refund"),+ (REFUND_REVERSE, "Represents reverse refund"),+ (REFUND_REQUEST, "Represents refund request"),+ (CANCEL_SUCCESS, "Represents success cancel"),+ (CANCEL_FAILURE, "Represents failure cancel"),+ (CANCEL_REQUEST, "Represents cancel request"),+ (INFO, "Represents an info event"),+ ]diff --git a/saleor/payment/migrations/0038_auto_20230223_0926.py b/saleor/payment/migrations/0038_auto_20230223_0926.pynew file mode 100644index 0000000000..c09fda6d72--- /dev/null+++ b/saleor/payment/migrations/0038_auto_20230223_0926.py@@ -0,0 +1,271 @@+from decimal import Decimal+from django.db import migrations, models+from django.utils import timezone+import django.db.models.deletion+++class Migration(migrations.Migration):+ dependencies = [+ ("account", "0075_add_address_metadata"),+ ("app", "0019_fix_constraint_names_in_app_app_permisons"),+ ("payment", "0037_alter_transaction_error"),+ ]++ operations = [+ migrations.AddField(+ model_name="transactionevent",+ name="amount_value",+ field=models.DecimalField(+ decimal_places=3, default=Decimal("0"), max_digits=12+ ),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionevent+ ALTER COLUMN amount_value+ SET DEFAULT 0;+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="app",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to="app.app",+ ),+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="app_identifier",+ field=models.CharField(blank=True, max_length=256, null=True),+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="currency",+ field=models.CharField(blank=True, max_length=3, null=True),+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="external_url",+ field=models.URLField(blank=True, null=True),+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="include_in_calculations",+ field=models.BooleanField(default=False),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionevent+ ALTER COLUMN include_in_calculations+ SET DEFAULT false;+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="message",+ field=models.CharField(blank=True, default="", max_length=512, null=True),+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="psp_reference",+ field=models.CharField(blank=True, max_length=512, null=True),+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="type",+ field=models.CharField(+ choices=[+ ("authorization_success", "Represents success authorization"),+ ("authorization_failure", "Represents failure authorization"),+ ("authorization_adjustment", "Represents authorization adjustment"),+ ("authorization_request", "Represents authorization request"),+ ("charge_success", "Represents success charge"),+ ("charge_failure", "Represents failure charge"),+ ("charge_back", "Represents chargeback."),+ ("charge_request", "Represents charge request"),+ ("refund_success", "Represents success refund"),+ ("refund_failure", "Represents failure refund"),+ ("refund_reverse", "Represents reverse refund"),+ ("refund_request", "Represents refund request"),+ ("cancel_success", "Represents success cancel"),+ ("cancel_failure", "Represents failure cancel"),+ ("cancel_request", "Represents cancel request"),+ ("info", "Represents an info event"),+ ],+ max_length=128,+ default="info",+ ),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionevent+ ALTER COLUMN type+ SET DEFAULT 'info';+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionevent",+ name="user",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to="account.user",+ ),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="app",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to="app.app",+ ),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="app_identifier",+ field=models.CharField(blank=True, max_length=256, null=True),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="authorize_pending_value",+ field=models.DecimalField(+ decimal_places=3, default=Decimal("0"), max_digits=12+ ),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionitem+ ALTER COLUMN authorize_pending_value+ SET DEFAULT 0;+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="cancel_pending_value",+ field=models.DecimalField(+ decimal_places=3, default=Decimal("0"), max_digits=12+ ),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionitem+ ALTER COLUMN cancel_pending_value+ SET DEFAULT 0;+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="canceled_value",+ field=models.DecimalField(+ decimal_places=3, default=Decimal("0"), max_digits=12+ ),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionitem+ ALTER COLUMN canceled_value+ SET DEFAULT 0;+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="charge_pending_value",+ field=models.DecimalField(+ decimal_places=3, default=Decimal("0"), max_digits=12+ ),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionitem+ ALTER COLUMN charge_pending_value+ SET DEFAULT 0;+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="external_url",+ field=models.URLField(blank=True, null=True),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="message",+ field=models.CharField(blank=True, default="", max_length=512, null=True),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="name",+ field=models.CharField(blank=True, default="", max_length=512, null=True),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="psp_reference",+ field=models.CharField(blank=True, max_length=512, null=True),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="refund_pending_value",+ field=models.DecimalField(+ decimal_places=3, default=Decimal("0"), max_digits=12+ ),+ ),+ migrations.RunSQL(+ """+ ALTER TABLE payment_transactionitem+ ALTER COLUMN refund_pending_value+ SET DEFAULT 0;+ """,+ migrations.RunSQL.noop,+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="user",+ field=models.ForeignKey(+ blank=True,+ null=True,+ on_delete=django.db.models.deletion.SET_NULL,+ related_name="+",+ to="account.user",+ ),+ ),+ migrations.AlterField(+ model_name="transactionevent",+ name="status",+ field=models.CharField(+ blank=True,+ choices=[+ ("pending", "Pending"),+ ("success", "Success"),+ ("failure", "Failure"),+ ],+ default="success",+ max_length=128,+ null=True,+ ),+ ),+ migrations.AlterField(+ model_name="transactionitem",+ name="status",+ field=models.CharField(blank=True, default="", max_length=512, null=True),+ ),+ migrations.AlterField(+ model_name="transactionevent",+ name="created_at",+ field=models.DateTimeField(default=timezone.now),+ ),+ ]diff --git a/saleor/payment/migrations/0039_transactionevent_currency.py b/saleor/payment/migrations/0039_transactionevent_currency.pynew file mode 100644index 0000000000..78d34a124c--- /dev/null+++ b/saleor/payment/migrations/0039_transactionevent_currency.py@@ -0,0 +1,28 @@+from django.db import migrations+from django.apps import apps as registry+from django.db.models.signals import post_migrate++from .tasks.saleor3_12 import (+ set_default_currency_for_transaction_event_task,+)+++def set_default_currency_for_transaction_event_migration(apps, _schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ set_default_currency_for_transaction_event_task.delay()++ sender = registry.get_app_config("payment")+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++class Migration(migrations.Migration):+ dependencies = [+ ("payment", "0038_auto_20230223_0926"),+ ]++ operations = [+ migrations.RunPython(+ set_default_currency_for_transaction_event_migration,+ migrations.RunPython.noop,+ ),+ ]diff --git a/saleor/payment/migrations/0040_migrate_renamed_fields.py b/saleor/payment/migrations/0040_migrate_renamed_fields.pynew file mode 100644index 0000000000..6e4233dd10--- /dev/null+++ b/saleor/payment/migrations/0040_migrate_renamed_fields.py@@ -0,0 +1,33 @@+from django.apps import apps as registry+from django.db import migrations+from django.db.models.signals import post_migrate++from .tasks.saleor3_12 import (+ transaction_event_migrate_name_to_message_task,+ transaction_event_migrate_reference_to_psp_reference_task,+ transaction_item_migrate_reference_to_psp_reference_task,+ transaction_item_migrate_type_to_name_task,+ transaction_item_migrate_voided_to_canceled_task,+)+++def migrate_data_for_renamed_fields(apps, _schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ transaction_event_migrate_name_to_message_task.delay()+ transaction_event_migrate_reference_to_psp_reference_task.delay()+ transaction_item_migrate_reference_to_psp_reference_task.delay()+ transaction_item_migrate_type_to_name_task.delay()+ transaction_item_migrate_voided_to_canceled_task.delay()++ sender = registry.get_app_config("payment")+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++class Migration(migrations.Migration):+ dependencies = [+ ("payment", "0039_transactionevent_currency"),+ ]++ operations = [+ migrations.RunPython(migrate_data_for_renamed_fields, migrations.RunPython.noop)+ ]diff --git a/saleor/payment/migrations/0041_add_calculation_transaction_events.py b/saleor/payment/migrations/0041_add_calculation_transaction_events.pynew file mode 100644index 0000000000..c4cb6fc08e--- /dev/null+++ b/saleor/payment/migrations/0041_add_calculation_transaction_events.py@@ -0,0 +1,31 @@+from django.db import migrations+from django.apps import apps as registry+from django.db.models.signals import post_migrate++from .tasks.saleor3_12 import (+ create_event_for_authorized_task,+ create_event_for_canceled_task,+ create_event_for_charged_task,+ create_event_for_refunded_task,+)+++def create_transaction_events(apps, schema_editor):+ def on_migrations_complete(sender=None, **kwargs):+ create_event_for_authorized_task.delay()+ create_event_for_canceled_task.delay()+ create_event_for_charged_task.delay()+ create_event_for_refunded_task.delay()++ sender = registry.get_app_config("payment")+ post_migrate.connect(on_migrations_complete, weak=False, sender=sender)+++class Migration(migrations.Migration):+ dependencies = [+ ("payment", "0040_migrate_renamed_fields"),+ ]++ operations = [+ migrations.RunPython(create_transaction_events, migrations.RunPython.noop),+ ]diff --git a/saleor/payment/migrations/tasks/saleor3_12.py b/saleor/payment/migrations/tasks/saleor3_12.pynew file mode 100644index 0000000000..bb3e22c80e--- /dev/null+++ b/saleor/payment/migrations/tasks/saleor3_12.py@@ -0,0 +1,262 @@+from decimal import Decimal++from django.db.models import (+ Case,+ Exists,+ F,+ Func,+ OuterRef,+ Subquery,+ Q,+ QuerySet,+ When,+)++from ....celeryconf import app+from ...models import TransactionEvent, TransactionItem++# Batch size of 1000 is about ~20MB of memory usage in task+BATCH_SIZE = 1000+++def create_transaction_events(transactions_qs, type):+ transaction_events = []+ for pk, amount, amount_sum, currency in transactions_qs:+ amount_sum = 0 if not amount_sum else amount_sum+ transaction_events.append(+ TransactionEvent(+ transaction_id=pk,+ type=type,+ amount_value=amount - amount_sum,+ currency=currency,+ include_in_calculations=True,+ message="Manual adjustment of the transaction.",+ )+ )+ if transaction_events:+ TransactionEvent.objects.bulk_create(transaction_events)+++def _get_events(type):+ return (+ TransactionEvent.objects.filter(+ transaction=OuterRef("pk"), type=type, include_in_calculations=True+ )+ .annotate(amount_sum=Func(F("amount_value"), function="Sum"))+ .values("amount_sum")+ )+++def create_event_for_authorized(transaction_qs):+ transaction_events = []++ for pk, amount, currency in transaction_qs:+ transaction_events.append(+ TransactionEvent(+ transaction_id=pk,+ type="authorization_success",+ amount_value=amount,+ currency=currency,+ include_in_calculations=True,+ message="Manual adjustment of the transaction.",+ )+ )+ if transaction_events:+ TransactionEvent.objects.bulk_create(transaction_events)+++def create_event_for_authorized_task():+ authorize_events = TransactionEvent.objects.filter(+ Q(transaction=OuterRef("pk"))+ & (Q(type="authorization_success") | Q(type="authorization_adjustment"))+ & Q(include_in_calculations=True)+ )+ qs = TransactionItem.objects.filter(+ ~Q(authorized_value=Decimal(0)),+ ~Exists(authorize_events),+ ).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids).values_list("id", "authorized_value", "currency")++ if ids:+ create_event_for_authorized(qs)+ create_event_for_authorized_task.delay()+++def create_event_for_charged_task():+ type = "charge_success"+ events = _get_events(type)+ qs = (+ TransactionItem.objects.filter(~Q(charged_value=Decimal(0)))+ .annotate(+ amount_sum_temp=Subquery(events),+ amount_sum=Case(+ When(amount_sum_temp__isnull=True, then=Decimal(0)),+ default=F("amount_sum_temp"),+ ),+ )+ .filter(charged_value__gt=F("amount_sum"))+ ).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids).values_list(+ "id", "charged_value", "amount_sum", "currency"+ )++ if ids:+ create_transaction_events(qs, type)+ create_event_for_charged_task.delay()+++def create_event_for_refunded_task():+ type = "refund_success"+ events = _get_events(type)+ qs = (+ TransactionItem.objects.filter(~Q(refunded_value=Decimal(0)))+ .annotate(+ amount_sum_temp=Subquery(events),+ amount_sum=Case(+ When(amount_sum_temp__isnull=True, then=Decimal(0)),+ default=F("amount_sum_temp"),+ ),+ )+ .filter(refunded_value__gt=F("amount_sum"))+ ).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids).values_list(+ "id", "refunded_value", "amount_sum", "currency"+ )++ if ids:+ create_transaction_events(qs, type)+ create_event_for_refunded_task.delay()+++def create_event_for_canceled_task():+ type = "cancel_success"+ events = _get_events(type)+ # voided_value instead of canceled_value cause some data may not be migrated yet+ qs = (+ TransactionItem.objects.filter(~Q(voided_value=Decimal(0)))+ .annotate(+ amount_sum_temp=Subquery(events),+ amount_sum=Case(+ When(amount_sum_temp__isnull=True, then=Decimal(0)),+ default=F("amount_sum_temp"),+ ),+ )+ .filter(voided_value__gt=F("amount_sum"))+ ).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids).values_list(+ "id", "voided_value", "amount_sum", "currency"+ )++ if ids:+ create_transaction_events(qs, type)+ create_event_for_canceled_task.delay()+++def transaction_item_migrate_type_to_name(qs: QuerySet):+ qs.update(name=F("type"))+++def transaction_item_migrate_type_to_name_task():+ qs = TransactionItem.objects.filter(+ (Q(name__isnull=True) & Q(type__isnull=False)) | (Q(name="") & ~Q(type=""))+ ).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids)++ if ids:+ transaction_item_migrate_type_to_name(qs)+ transaction_item_migrate_type_to_name_task.delay()+++def transaction_item_migrate_reference_to_psp_reference(qs: QuerySet):+ qs.update(psp_reference=F("reference"))+++def transaction_item_migrate_reference_to_psp_reference_task():+ qs = TransactionItem.objects.filter(psp_reference__isnull=True).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids)++ if ids:+ transaction_item_migrate_reference_to_psp_reference(qs)+ transaction_item_migrate_reference_to_psp_reference_task.delay()+++def transaction_item_migrate_voided_to_canceled(qs: QuerySet):+ qs.update(canceled_value=F("voided_value"))+++def transaction_item_migrate_voided_to_canceled_task():+ qs = TransactionItem.objects.filter(~Q(canceled_value=F("voided_value"))).order_by(+ "-pk"+ )+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids)++ if ids:+ transaction_item_migrate_voided_to_canceled(qs)+ transaction_item_migrate_voided_to_canceled_task.delay()+++def transaction_event_migrate_name_to_message(qs: QuerySet):+ qs.update(message=F("name"))+++def transaction_event_migrate_name_to_message_task():+ qs = TransactionEvent.objects.filter(+ (Q(message__isnull=True) & Q(name__isnull=False))+ | (Q(message="") & ~Q(name=""))+ ).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids)++ if ids:+ transaction_event_migrate_name_to_message(qs)+ transaction_event_migrate_name_to_message_task.delay()+++def transaction_event_migrate_reference_to_psp_reference(qs: QuerySet):+ qs.update(psp_reference=F("reference"))+++def transaction_event_migrate_reference_to_psp_reference_task():+ qs = TransactionEvent.objects.filter(~Q(psp_reference=F("reference"))).order_by(+ "-pk"+ )+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids)++ if ids:+ transaction_event_migrate_reference_to_psp_reference(qs)+ transaction_event_migrate_reference_to_psp_reference_task.delay()+++def set_default_currency_for_transaction_event(qs: QuerySet):+ qs.update(currency=F("transaction_currency"))+++def set_default_currency_for_transaction_event_task():+ transaction_item = TransactionItem.objects.filter(+ pk=OuterRef("transaction")+ ).values("currency")+ qs = TransactionEvent.objects.filter(currency__isnull=True).order_by("-pk")+ ids = qs.values_list("pk", flat=True)[:BATCH_SIZE]+ qs = qs.filter(pk__in=ids).annotate(transaction_currency=Subquery(transaction_item))++ if ids:+ set_default_currency_for_transaction_event(qs)+ set_default_currency_for_transaction_event_task.delay()diff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 464eed4d34..8d2467c0ee 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -8,6 +8,7 @@ from django.core.serializers.json import DjangoJSONEncoderfrom django.core.validators import MaxValueValidator, MinValueValidatorfrom django.db import modelsfrom django.db.models import JSONField+from django.utils import timezonefrom django_prices.models import MoneyFieldfrom prices import Money@@ -20,6 +21,7 @@ from . import (CustomPaymentChoices,StorePaymentMethod,TransactionAction,+ TransactionEventType,TransactionKind,TransactionStatus,)@@ -28,9 +30,12 @@ from . import (class TransactionItem(ModelWithMetadata):created_at = models.DateTimeField(auto_now_add=True)modified_at = models.DateTimeField(auto_now=True)- status = models.CharField(max_length=512, blank=True, default="")+ status = models.CharField(max_length=512, blank=True, null=True, default="")type = models.CharField(max_length=512, blank=True, default="")reference = models.CharField(max_length=512, blank=True, default="")+ name = models.CharField(max_length=512, blank=True, null=True, default="")+ message = models.CharField(max_length=512, blank=True, null=True, default="")+ psp_reference = models.CharField(max_length=512, blank=True, null=True)available_actions = ArrayField(models.CharField(max_length=128, choices=TransactionAction.CHOICES),default=list,@@ -66,6 +71,51 @@ class TransactionItem(ModelWithMetadata):decimal_places=settings.DEFAULT_DECIMAL_PLACES,default=Decimal("0"),)+ amount_canceled = MoneyField(+ amount_field="canceled_value", currency_field="currency"+ )+ canceled_value = models.DecimalField(+ max_digits=settings.DEFAULT_MAX_DIGITS,+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,+ default=Decimal("0"),+ )+ amount_refund_pending = MoneyField(+ amount_field="refund_pending_value", currency_field="currency"+ )+ refund_pending_value = models.DecimalField(+ max_digits=settings.DEFAULT_MAX_DIGITS,+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,+ default=Decimal("0"),+ )++ amount_charge_pending = MoneyField(+ amount_field="charge_pending_value", currency_field="currency"+ )+ charge_pending_value = models.DecimalField(+ max_digits=settings.DEFAULT_MAX_DIGITS,+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,+ default=Decimal("0"),+ )++ amount_authorize_pending = MoneyField(+ amount_field="authorize_pending_value", currency_field="currency"+ )+ authorize_pending_value = models.DecimalField(+ max_digits=settings.DEFAULT_MAX_DIGITS,+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,+ default=Decimal("0"),+ )++ cancel_pending_value = models.DecimalField(+ max_digits=settings.DEFAULT_MAX_DIGITS,+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,+ default=Decimal("0"),+ )+ amount_cancel_pending = MoneyField(+ amount_field="cancel_pending_value", currency_field="currency"+ )++ external_url = models.URLField(blank=True, null=True)checkout = models.ForeignKey(Checkout,@@ -79,6 +129,27 @@ class TransactionItem(ModelWithMetadata):null=True,on_delete=models.PROTECT,)+ user = models.ForeignKey(+ settings.AUTH_USER_MODEL,+ blank=True,+ null=True,+ on_delete=models.SET_NULL,+ related_name="+",+ )++ # We store app and app_identifier, as the app field stores apps of+ # all types (local, third-party), and the app_identifier field stores+ # only third-party apps.+ # In the case of re-installing the third-party app, we are able to match+ # existing transactions with the re-installed app by using `app_identifier`.+ app = models.ForeignKey(+ "app.App",+ related_name="+",+ null=True,+ blank=True,+ on_delete=models.SET_NULL,+ )+ app_identifier = models.CharField(blank=True, null=True, max_length=256)class Meta:ordering = ("pk",)@@ -90,18 +161,59 @@ class TransactionItem(ModelWithMetadata):class TransactionEvent(models.Model):- created_at = models.DateTimeField(auto_now_add=True)+ created_at = models.DateTimeField(default=timezone.now)status = models.CharField(max_length=128,choices=TransactionStatus.CHOICES,default=TransactionStatus.SUCCESS,+ blank=True,+ null=True,)reference = models.CharField(max_length=512, blank=True, default="")+ psp_reference = models.CharField(max_length=512, blank=True, null=True)+name = models.CharField(max_length=512, blank=True, default="")+ message = models.CharField(max_length=512, blank=True, null=True, default="")transaction = models.ForeignKey(TransactionItem, related_name="events", on_delete=models.CASCADE)+ external_url = models.URLField(blank=True, null=True)+ currency = models.CharField(+ max_length=settings.DEFAULT_CURRENCY_CODE_LENGTH,+ null=True,+ blank=True,+ )+ type = models.CharField(+ max_length=128,+ choices=TransactionEventType.CHOICES,+ default=TransactionEventType.INFO,+ )+ amount = MoneyField(amount_field="amount_value", currency_field="currency")+ amount_value = models.DecimalField(+ max_digits=settings.DEFAULT_MAX_DIGITS,+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,+ default=Decimal("0"),+ )+ user = models.ForeignKey(+ settings.AUTH_USER_MODEL,+ blank=True,+ null=True,+ on_delete=models.SET_NULL,+ related_name="+",+ )++ # We store app and app_identifier, as the app field stores apps of+ # all types (local, third-party), and the app_identifier field stores+ # only third-party apps.+ # In the case of re-installing the third-party app, we are able to match+ # existing transactions with the re-installed app by using `app_identifier`.+ app = models.ForeignKey(+ "app.App", related_name="+", null=True, blank=True, on_delete=models.SET_NULL+ )+ app_identifier = models.CharField(blank=True, null=True, max_length=256)++ include_in_calculations = models.BooleanField(default=False)class Meta:ordering = ("pk",)