Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add metadata and store to Payment model

Mateusz Grzybb1e22393aa43
diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.py
index 88b0e329ec..713f2d1663 100644
--- a/saleor/payment/__init__.py
+++ b/saleor/payment/__init__.py
@@ -119,3 +119,25 @@ class ChargeStatus:
(REFUSED, "Refused"),
(CANCELLED, "Cancelled"),
]
+
+
+class Store:
+ """Represents if and how a payment should be stored in a payment gateway.
+
+ The following store types are possible:
+ - ON_SESSION - the payment is stored only to be reused when
+ the customer is present in the checkout flow
+ - OFF_SESSION - the payment is stored to be reused even if
+ the customer is absent
+ - NONE - the payment is not stored.
+ """
+
+ ON_SESSION = "on_session"
+ OFF_SESSION = "off_session"
+ NONE = "none"
+
+ CHOICES = [
+ (ON_SESSION, "On session"),
+ (OFF_SESSION, "Off session"),
+ (NONE, "None"),
+ ]
diff --git a/saleor/payment/migrations/0030_auto_20210823_1058.py b/saleor/payment/migrations/0030_auto_20210823_1058.py
new file mode 100644
index 0000000000..537e30f652
--- /dev/null
+++ b/saleor/payment/migrations/0030_auto_20210823_1058.py
@@ -0,0 +1,48 @@
+# Generated by Django 3.2.5 on 2021-08-23 10:58
+
+from django.db import migrations, models
+
+import saleor.core.utils.json_serializer
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("payment", "0029_alter_payment_options"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="payment",
+ name="metadata",
+ field=models.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ null=True,
+ ),
+ ),
+ migrations.AddField(
+ model_name="payment",
+ name="private_metadata",
+ field=models.JSONField(
+ blank=True,
+ default=dict,
+ encoder=saleor.core.utils.json_serializer.CustomJsonEncoder,
+ null=True,
+ ),
+ ),
+ migrations.AddField(
+ model_name="payment",
+ name="store",
+ field=models.CharField(
+ choices=[
+ ("on_session", "On session"),
+ ("off_session", "Off session"),
+ ("none", "None"),
+ ],
+ default="none",
+ max_length=11,
+ ),
+ ),
+ ]
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index 03b4bf47c6..c0abc57019 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -10,12 +10,13 @@ from django.db.models import JSONField # type: ignore
from prices import Money
from ..checkout.models import Checkout
+from ..core.models import ModelWithMetadata
from ..core.permissions import PaymentPermissions
from ..core.taxes import zero_money
-from . import ChargeStatus, CustomPaymentChoices, TransactionKind
+from . import ChargeStatus, CustomPaymentChoices, Store, TransactionKind
-class Payment(models.Model):
+class Payment(ModelWithMetadata):
"""A model that represents a single payment.
This might be a transactable payment information such as credit card
@@ -59,6 +60,7 @@ class Payment(models.Model):
order = models.ForeignKey(
"order.Order", null=True, related_name="payments", on_delete=models.PROTECT
)
+ store = models.CharField(max_length=11, choices=Store.CHOICES, default=Store.NONE)
billing_email = models.EmailField(blank=True)
billing_first_name = models.CharField(max_length=256, blank=True)
diff --git a/saleor/payment/tests/test_payment.py b/saleor/payment/tests/test_payment.py
index b88cd960cf..9e596ed17e 100644
--- a/saleor/payment/tests/test_payment.py
+++ b/saleor/payment/tests/test_payment.py
@@ -6,7 +6,7 @@ import pytest
from ...checkout.calculations import checkout_total
from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from ...plugins.manager import PluginsManager, get_plugins_manager
-from .. import ChargeStatus, GatewayError, PaymentError, TransactionKind, gateway
+from .. import ChargeStatus, GatewayError, PaymentError, Store, TransactionKind, gateway
from ..error_codes import PaymentErrorCode
from ..interface import GatewayResponse, PaymentMethodInfo
from ..models import Payment
@@ -111,6 +111,7 @@ def test_create_payment(checkout_with_item, address):
}
payment = create_payment(**data)
assert payment.gateway == "Dummy"
+ assert payment.store == Store.NONE
same_payment = create_payment(**data)
assert payment == same_payment