Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add SaleorGiftcardPaymentMethodDetails to transaction (#18915)
Lukasz Ostrowski37fbfd1e24cd
diff --git a/saleor/giftcard/const.py b/saleor/giftcard/const.pyindex 7b2cecb94d..4099840532 100644--- a/saleor/giftcard/const.py+++ b/saleor/giftcard/const.py@@ -1,2 +1,4 @@GIFT_CARD_PAYMENT_GATEWAY_ID = "saleor.io.gift-card-payment-gateway"GIFT_CARD_PAYMENT_GATEWAY_NAME = "Gift Card Payment Gateway"+SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME = "Saleor Gift Card"+SALEOR_GIFT_CARD_BRAND = "Saleor"diff --git a/saleor/giftcard/gateway.py b/saleor/giftcard/gateway.pyindex 252be8d166..babe916fcd 100644--- a/saleor/giftcard/gateway.py+++ b/saleor/giftcard/gateway.py@@ -13,7 +13,7 @@ from ..graphql.payment.mutations.transaction.utils import (create_transaction_event_requested,)from ..order.models import Order-from ..payment import TransactionAction, TransactionEventType+from ..payment import PaymentMethodType, TransactionAction, TransactionEventTypefrom ..payment.interface import (TransactionSessionData,TransactionSessionResult,@@ -22,7 +22,11 @@ from ..payment.models import TransactionEvent, TransactionItemfrom ..payment.utils import (create_transaction_event_from_request_and_webhook_response,)-from .const import GIFT_CARD_PAYMENT_GATEWAY_ID+from .const import (+ GIFT_CARD_PAYMENT_GATEWAY_ID,+ SALEOR_GIFT_CARD_BRAND,+ SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME,+)from .models import GiftCard@@ -147,12 +151,25 @@ def attach_gift_card_to_transaction(transaction_session_data: "TransactionSessionData",gift_card: GiftCard | None,):- """Attach gift card to a transaction."""+ """Attach gift card to a transaction and set payment method details."""if not gift_card:return- transaction_session_data.transaction.gift_card = gift_card- transaction_session_data.transaction.save(update_fields=["gift_card"])+ transaction = transaction_session_data.transaction+ transaction.gift_card = gift_card+ transaction.payment_method_type = PaymentMethodType.GIFT_CARD+ transaction.payment_method_name = SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME+ transaction.gift_card_last_chars = gift_card.display_code+ transaction.gift_card_brand = SALEOR_GIFT_CARD_BRAND+ transaction.save(+ update_fields=[+ "gift_card",+ "payment_method_type",+ "payment_method_name",+ "gift_card_last_chars",+ "gift_card_brand",+ ]+ )def detach_gift_card_from_previous_checkout_transactions(diff --git a/saleor/graphql/payment/mutations/transaction/shared.py b/saleor/graphql/payment/mutations/transaction/shared.pyindex 3d1ba44d09..d5c8d09bd6 100644--- a/saleor/graphql/payment/mutations/transaction/shared.py+++ b/saleor/graphql/payment/mutations/transaction/shared.py@@ -1,5 +1,9 @@+from typing import Annotated+import graphenefrom django.core.exceptions import ValidationError+from pydantic import BaseModel, ConfigDict, StringConstraints+from pydantic import ValidationError as PydanticValidationErrorfrom .....payment import PaymentMethodTypefrom .....payment.error_codes import (@@ -8,9 +12,10 @@ from .....payment.error_codes import (TransactionUpdateErrorCode,)from .....payment.interface import PaymentMethodDetails-from ....core.descriptions import ADDED_IN_322+from ....core.descriptions import ADDED_IN_322, ADDED_IN_323from ....core.types.base import BaseInputObjectTypefrom ....core.validators import validate_one_of_args_is_in_mutation+from ....error import pydantic_to_validation_errorclass CardPaymentMethodDetailsInput(BaseInputObjectType):@@ -47,6 +52,30 @@ class OtherPaymentMethodDetailsInput(BaseInputObjectType):)+class GiftCardPaymentMethodDetailsInput(BaseInputObjectType):+ name = graphene.String(+ description=(+ "Name of the payment method used for the transaction. "+ "Max length is 256 characters." + ADDED_IN_323+ ),+ required=True,+ )+ brand = graphene.String(+ description=(+ "Brand of the gift card used for the transaction. "+ "Max length is 40 characters." + ADDED_IN_323+ ),+ required=False,+ )+ last_chars = graphene.String(+ description=(+ "Last characters of the gift card used for the transaction. "+ "Max length is 4 characters." + ADDED_IN_323+ ),+ required=False,+ )++class PaymentMethodDetailsInput(BaseInputObjectType):card = graphene.Field(CardPaymentMethodDetailsInput,@@ -58,11 +87,19 @@ class PaymentMethodDetailsInput(BaseInputObjectType):required=False,description="Details of the non-card payment method used for this transaction.",)+ gift_card = graphene.Field(+ GiftCardPaymentMethodDetailsInput,+ required=False,+ description=(+ "Details of the gift card payment method used for the transaction."+ + ADDED_IN_323+ ),+ )class Meta:description = ("Details of the payment method used for the transaction. "- "One of `card` or `other` is required." + ADDED_IN_322+ "One of `card`, `other`, or `giftCard` is required." + ADDED_IN_322)@@ -143,6 +180,34 @@ def validate_card_payment_method_details_input(return errors+class GiftCardPaymentMethodDetailsValidatedInput(BaseModel):+ model_config = ConfigDict(frozen=True)++ name: Annotated[str, StringConstraints(max_length=256)]+ brand: Annotated[str, StringConstraints(max_length=40)] | None = None+ last_chars: Annotated[str, StringConstraints(min_length=1, max_length=4)] | None = (+ None+ )+++def validate_gift_card_payment_method_details_input(+ gift_card_details_input: GiftCardPaymentMethodDetailsInput,+ error_code_class: type[TransactionEventReportErrorCode]+ | type[TransactionCreateErrorCode]+ | type[TransactionUpdateErrorCode],+):+ try:+ GiftCardPaymentMethodDetailsValidatedInput(+ name=gift_card_details_input.name,+ brand=gift_card_details_input.brand,+ last_chars=gift_card_details_input.last_chars,+ )+ except PydanticValidationError as exc:+ raise pydantic_to_validation_error(+ exc, default_error_code=error_code_class.INVALID.value+ ) from exc++def validate_payment_method_details_input(payment_method_details_input: PaymentMethodDetailsInput,error_code_class: type[TransactionEventReportErrorCode]@@ -152,11 +217,12 @@ def validate_payment_method_details_input(if (payment_method_details_input.card is Noneand payment_method_details_input.other is None+ and payment_method_details_input.gift_card is None):raise ValidationError({"payment_method_details": ValidationError(- "One of `card` or `other` is required.",+ "One of `card`, `other`, or `giftCard` is required.",code=error_code_class.INVALID.value,)}@@ -167,6 +233,8 @@ def validate_payment_method_details_input(payment_method_details_input.card,"other",payment_method_details_input.other,+ "gift_card",+ payment_method_details_input.gift_card,)except ValidationError as e:e.code = error_code_class.INVALID.value@@ -190,6 +258,15 @@ def validate_payment_method_details_input()})+ elif payment_method_details_input.gift_card:+ try:+ validate_gift_card_payment_method_details_input(+ payment_method_details_input.gift_card, error_code_class+ )+ except ValidationError as e:+ for field, field_errors in e.error_dict.items():+ for field_error in field_errors:+ errors.append({field: field_error})if errors:raise ValidationError({"payment_method_details": errors})@@ -224,5 +301,15 @@ def get_payment_method_details(type=PaymentMethodType.OTHER,name=other_details.name,)+ elif payment_method_details_input.gift_card:+ gc_details: GiftCardPaymentMethodDetailsInput = (+ payment_method_details_input.gift_card+ )+ payment_details_data = PaymentMethodDetails(+ type=PaymentMethodType.GIFT_CARD,+ name=gc_details.name,+ brand=gc_details.brand,+ last_digits=gc_details.last_chars,+ )return payment_details_datadiff --git a/saleor/graphql/payment/schema.py b/saleor/graphql/payment/schema.pyindex 4c5d1ca175..89ead8bec3 100644--- a/saleor/graphql/payment/schema.py+++ b/saleor/graphql/payment/schema.py@@ -36,6 +36,7 @@ from .resolvers import ()from .types import (CardPaymentMethodDetails,+ GiftCardPaymentMethodDetails,OtherPaymentMethodDetails,Payment,PaymentCountableConnection,@@ -45,6 +46,7 @@ from .types import (PAYMENT_ADDITIONAL_TYPES = [CardPaymentMethodDetails,+ GiftCardPaymentMethodDetails,OtherPaymentMethodDetails,]diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_create.py b/saleor/graphql/payment/tests/mutations/test_transaction_create.pyindex 0c17b898ec..b739ed733a 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_create.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_create.py@@ -2675,6 +2675,52 @@ def test_transaction_create_with_invalid_other_payment_method_details(assert error["field"] == "paymentMethodDetails"+def test_transaction_create_with_gift_card_empty_last_chars(+ order_with_lines,+ permission_manage_payments,+ app_api_client,+):+ # given+ name = "Credit Card"+ psp_reference = "PSP reference - 123"+ authorized_value = Decimal(10)++ variables = {+ "id": graphene.Node.to_global_id("Order", order_with_lines.pk),+ "transaction": {+ "name": name,+ "pspReference": psp_reference,+ "amountAuthorized": {+ "amount": authorized_value,+ "currency": "USD",+ },+ "paymentMethodDetails": {+ "giftCard": {+ "name": "Gift Card",+ "lastChars": "",+ }+ },+ },+ }++ # when+ response = app_api_client.post_graphql(+ MUTATION_TRANSACTION_CREATE,+ variables,+ permissions=[permission_manage_payments],+ )++ # then+ response = get_graphql_content(response)+ transaction_data = response["data"]["transactionCreate"]+ assert transaction_data["errors"]+ assert len(transaction_data["errors"]) == 1+ error = transaction_data["errors"][0]+ assert error["code"] == TransactionCreateErrorCode.INVALID.name+ assert error["field"] == "paymentMethodDetails"+ assert error["message"] == "String should have at least 1 character"++# Test wrapped by `transaction=True` to ensure that `selector_for_update` is called in a database transaction.@pytest.mark.django_db(transaction=True)@patch(diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py b/saleor/graphql/payment/tests/mutations/test_transaction_initialize.pyindex 1136bdd4be..ca8c0bf44d 100644--- a/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py+++ b/saleor/graphql/payment/tests/mutations/test_transaction_initialize.py@@ -17,10 +17,15 @@ from .....checkout.complete_checkout import create_order_from_checkoutfrom .....checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom .....checkout.models import Checkoutfrom .....core.prices import Money, quantize_price-from .....giftcard.const import GIFT_CARD_PAYMENT_GATEWAY_ID+from .....giftcard.const import (+ GIFT_CARD_PAYMENT_GATEWAY_ID,+ SALEOR_GIFT_CARD_BRAND,+ SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME,+)from .....order import OrderAuthorizeStatus, OrderChargeStatus, OrderStatusfrom .....order.models import Orderfrom .....payment import (+ PaymentMethodType,TransactionAction,TransactionEventType,TransactionItemIdempotencyUniqueError,@@ -3500,6 +3505,13 @@ def test_for_checkout_with_gift_card_payment_gateway(available_actions=[TransactionAction.CANCEL],)+ transaction = checkout.payment_transactions.last()+ assert transaction.payment_method_type == PaymentMethodType.GIFT_CARD+ assert transaction.payment_method_name == SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME+ assert transaction.gift_card_last_chars == gift_card_created_by_staff.display_code+ assert transaction.gift_card_brand == SALEOR_GIFT_CARD_BRAND+ assert transaction.gift_card == gift_card_created_by_staff+@mock.patch("saleor.giftcard.gateway.uuid4")def test_for_checkout_with_gift_card_payment_gateway_gift_card_does_not_exist(diff --git a/saleor/graphql/payment/tests/queries/test_transaction.py b/saleor/graphql/payment/tests/queries/test_transaction.pyindex 79ccb32346..c6e2ddf2d1 100644--- a/saleor/graphql/payment/tests/queries/test_transaction.py+++ b/saleor/graphql/payment/tests/queries/test_transaction.py@@ -5,6 +5,10 @@ import pytestfrom django.utils import timezonefrom freezegun import freeze_time+from .....giftcard.const import (+ SALEOR_GIFT_CARD_BRAND,+ SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME,+)from .....page.models import Pagefrom .....payment import PaymentMethodType, TransactionEventTypefrom .....payment.models import TransactionEvent@@ -110,6 +114,12 @@ TRANSACTION_QUERY = """...on OtherPaymentMethodDetails{name}+ ...on GiftCardPaymentMethodDetails{+ name+ brand+ lastChars+ isSaleorGiftcard+ }}}}@@ -985,6 +995,119 @@ def test_transaction_query_by_staff_with_payment_method_other(assert data["paymentMethodDetails"]["name"] == expected_payment_method_name+def test_transaction_query_by_app_with_payment_method_gift_card(+ app_api_client, transaction_item_created_by_app, permission_manage_payments, app+):+ # given+ expected_last_digits = "ABCD"+ expected_brand = "Saleor"+ expected_payment_method_name = "Saleor Gift Card"++ transaction_item_created_by_app.payment_method_type = PaymentMethodType.GIFT_CARD+ transaction_item_created_by_app.payment_method_name = expected_payment_method_name+ transaction_item_created_by_app.gift_card_last_chars = expected_last_digits+ transaction_item_created_by_app.gift_card_brand = expected_brand+ transaction_item_created_by_app.save()++ variables = {+ "id": graphene.Node.to_global_id(+ "TransactionItem", transaction_item_created_by_app.token+ )+ }++ # when+ response = app_api_client.post_graphql(+ TRANSACTION_QUERY, variables, permissions=[permission_manage_payments]+ )++ # then+ content = get_graphql_content(response)+ data = content["data"]["transaction"]+ assert data["paymentMethodDetails"]["__typename"] == "GiftCardPaymentMethodDetails"+ assert data["paymentMethodDetails"]["name"] == expected_payment_method_name+ assert data["paymentMethodDetails"]["brand"] == expected_brand+ assert data["paymentMethodDetails"]["lastChars"] == expected_last_digits+ assert data["paymentMethodDetails"]["isSaleorGiftcard"] is False+++def test_transaction_query_by_staff_with_payment_method_gift_card(+ staff_api_client, transaction_item_created_by_app, permission_manage_payments+):+ # given+ expected_last_digits = "WXYZ"+ expected_brand = "ExternalBrand"+ expected_payment_method_name = "External Gift Card"++ transaction_item_created_by_app.payment_method_type = PaymentMethodType.GIFT_CARD+ transaction_item_created_by_app.payment_method_name = expected_payment_method_name+ transaction_item_created_by_app.gift_card_last_chars = expected_last_digits+ transaction_item_created_by_app.gift_card_brand = expected_brand+ transaction_item_created_by_app.save()++ variables = {+ "id": graphene.Node.to_global_id(+ "TransactionItem", transaction_item_created_by_app.token+ )+ }++ # when+ response = staff_api_client.post_graphql(+ TRANSACTION_QUERY, variables, permissions=[permission_manage_payments]+ )++ # then+ content = get_graphql_content(response)+ data = content["data"]["transaction"]+ assert data["paymentMethodDetails"]["__typename"] == "GiftCardPaymentMethodDetails"+ assert data["paymentMethodDetails"]["name"] == expected_payment_method_name+ assert data["paymentMethodDetails"]["brand"] == expected_brand+ assert data["paymentMethodDetails"]["lastChars"] == expected_last_digits+ assert data["paymentMethodDetails"]["isSaleorGiftcard"] is False+++def test_transaction_query_with_saleor_gift_card_is_saleor_giftcard_true(+ app_api_client,+ transaction_item_created_by_app,+ permission_manage_payments,+ gift_card_created_by_staff,+ app,+):+ # given+ transaction_item_created_by_app.payment_method_type = PaymentMethodType.GIFT_CARD+ transaction_item_created_by_app.payment_method_name = (+ SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME+ )+ transaction_item_created_by_app.gift_card_last_chars = (+ gift_card_created_by_staff.display_code+ )+ transaction_item_created_by_app.gift_card_brand = SALEOR_GIFT_CARD_BRAND+ transaction_item_created_by_app.gift_card = gift_card_created_by_staff+ transaction_item_created_by_app.save()++ variables = {+ "id": graphene.Node.to_global_id(+ "TransactionItem", transaction_item_created_by_app.token+ )+ }++ # when+ response = app_api_client.post_graphql(+ TRANSACTION_QUERY, variables, permissions=[permission_manage_payments]+ )++ # then+ content = get_graphql_content(response)+ data = content["data"]["transaction"]+ assert data["paymentMethodDetails"]["__typename"] == "GiftCardPaymentMethodDetails"+ assert data["paymentMethodDetails"]["name"] == SALEOR_GIFT_CARD_PAYMENT_METHOD_NAME+ assert data["paymentMethodDetails"]["brand"] == SALEOR_GIFT_CARD_BRAND+ assert (+ data["paymentMethodDetails"]["lastChars"]+ == gift_card_created_by_staff.display_code+ )+ assert data["paymentMethodDetails"]["isSaleorGiftcard"] is True++def test_transaction_event_with_reason_reference(staff_api_client,transaction_item_created_by_user,diff --git a/saleor/graphql/payment/types.py b/saleor/graphql/payment/types.pyindex 3c06162594..aa3f8e1809 100644--- a/saleor/graphql/payment/types.py+++ b/saleor/graphql/payment/types.py@@ -7,7 +7,7 @@ from graphene import relayfrom promise import Promisefrom ...core.exceptions import PermissionDenied-from ...graphql.core.descriptions import ADDED_IN_322+from ...graphql.core.descriptions import ADDED_IN_322, ADDED_IN_323from ...payment import PaymentMethodType, modelsfrom ...payment.interface import PaymentMethodDatafrom ...permission.enums import OrderPermissions@@ -476,6 +476,8 @@ class PaymentMethodDetails(graphene.Interface):def resolve_type(cls, instance, info: graphene.ResolveInfo):if instance.payment_method_type == PaymentMethodType.CARD:return CardPaymentMethodDetails+ if instance.payment_method_type == PaymentMethodType.GIFT_CARD:+ return GiftCardPaymentMethodDetailsreturn OtherPaymentMethodDetails@staticmethod@@ -538,6 +540,45 @@ class OtherPaymentMethodDetails(BaseObjectType):interfaces = [PaymentMethodDetails]+class GiftCardPaymentMethodDetails(BaseObjectType):+ name = graphene.String(required=True, description="Name of the gift card.")+ brand = graphene.String(+ description="Brand of the gift card." + ADDED_IN_323,+ required=False,+ )+ last_chars = graphene.String(+ description="Last characters of the gift card code. Max 4 characters."+ + ADDED_IN_323,+ required=False,+ )+ is_saleor_giftcard = graphene.Boolean(+ required=True,+ description=(+ "Indicates whether the gift card is a built-in Saleor gift card."+ + ADDED_IN_323+ ),+ )++ class Meta:+ description = (+ "Represents a gift card payment method used for a transaction."+ + ADDED_IN_323+ )+ interfaces = [PaymentMethodDetails]++ @staticmethod+ def resolve_brand(root: models.TransactionItem, _info):+ return root.gift_card_brand++ @staticmethod+ def resolve_last_chars(root: models.TransactionItem, _info):+ return root.gift_card_last_chars++ @staticmethod+ def resolve_is_saleor_giftcard(root: models.TransactionItem, _info):+ return bool(root.gift_card_id)++class TransactionItem(ModelObjectType[models.TransactionItem]):token = graphene.Field(UUIDScalar, description="The transaction token.", required=Truediff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 9f8385b30e..7677fdb1e9 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -15955,10 +15955,12 @@ Represents possible payment method types.The following types are possible:CARD - represents a card payment method.OTHER - represents any payment method that is not a card payment.+ GIFT_CARD - represents a gift card payment method."""enum PaymentMethodTypeEnum @doc(category: "Payments") {CARDOTHER+ GIFT_CARD}input PaymentMethodDetailsCardFilterInput {@@ -26034,7 +26036,7 @@ input TransactionCreateInput @doc(category: "Payments") {}"""-Details of the payment method used for the transaction. One of `card` or `other` is required.+Details of the payment method used for the transaction. One of `card`, `other`, or `giftCard` is required.Added in Saleor 3.22."""@@ -26044,6 +26046,13 @@ input PaymentMethodDetailsInput {"""Details of the non-card payment method used for this transaction."""other: OtherPaymentMethodDetailsInput++ """+ Details of the gift card payment method used for the transaction.++ Added in Saleor 3.23.+ """+ giftCard: GiftCardPaymentMethodDetailsInput}input CardPaymentMethodDetailsInput {@@ -26083,6 +26092,29 @@ input OtherPaymentMethodDetailsInput {name: String!}+input GiftCardPaymentMethodDetailsInput {+ """+ Name of the payment method used for the transaction. Max length is 256 characters.++ Added in Saleor 3.23.+ """+ name: String!++ """+ Brand of the gift card used for the transaction. Max length is 40 characters.++ Added in Saleor 3.23.+ """+ brand: String++ """+ Last characters of the gift card used for the transaction. Max length is 4 characters.++ Added in Saleor 3.23.+ """+ lastChars: String+}+input TransactionEventInput @doc(category: "Payments") {"""PSP Reference related to this action."""pspReference: String@@ -37455,6 +37487,37 @@ type CardPaymentMethodDetails implements PaymentMethodDetails {expYear: Int}+"""+Represents a gift card payment method used for a transaction.++Added in Saleor 3.23.+"""+type GiftCardPaymentMethodDetails implements PaymentMethodDetails {+ """Name of the gift card."""+ name: String!++ """+ Brand of the gift card.++ Added in Saleor 3.23.+ """+ brand: String++ """+ Last characters of the gift card code. Max 4 characters.++ Added in Saleor 3.23.+ """+ lastChars: String++ """+ Indicates whether the gift card is a built-in Saleor gift card.++ Added in Saleor 3.23.+ """+ isSaleorGiftcard: Boolean!+}+"""Represents a payment method used for a transaction.diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.pyindex fbec69b69e..c6f90b9332 100644--- a/saleor/payment/__init__.py+++ b/saleor/payment/__init__.py@@ -292,13 +292,16 @@ class PaymentMethodType:The following types are possible:CARD - represents a card payment method.OTHER - represents any payment method that is not a card payment.+ GIFT_CARD - represents a gift card payment method."""CARD = "card"OTHER = "other"+ GIFT_CARD = "gift_card"CHOICES = [(CARD, "Card"),(OTHER, "Other"),+ (GIFT_CARD, "Gift card"),]diff --git a/saleor/payment/migrations/0069_transactionitem_gc_fields.py b/saleor/payment/migrations/0069_transactionitem_gc_fields.pynew file mode 100644index 0000000000..32981ca743--- /dev/null+++ b/saleor/payment/migrations/0069_transactionitem_gc_fields.py@@ -0,0 +1,36 @@+# Generated by Django 5.2.11 on 2026-03-06 09:04++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("payment", "0068_merge_20260206_1216"),+ ]++ operations = [+ migrations.AddField(+ model_name="transactionitem",+ name="gift_card_brand",+ field=models.CharField(blank=True, max_length=40, null=True),+ ),+ migrations.AddField(+ model_name="transactionitem",+ name="gift_card_last_chars",+ field=models.CharField(blank=True, max_length=4, null=True),+ ),+ migrations.AlterField(+ model_name="transactionitem",+ name="payment_method_type",+ field=models.CharField(+ blank=True,+ choices=[+ ("card", "Card"),+ ("other", "Other"),+ ("gift_card", "Gift card"),+ ],+ max_length=32,+ null=True,+ ),+ ),+ ]diff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 00cc82cd5b..b3f2e10569 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -175,6 +175,8 @@ class TransactionItem(ModelWithMetadata):choices=PaymentMethodType.CHOICES,)payment_method_name = models.CharField(max_length=256, blank=True, null=True)+ gift_card_last_chars = models.CharField(max_length=4, blank=True, null=True)+ gift_card_brand = models.CharField(max_length=40, blank=True, null=True)gift_card = models.ForeignKey("giftcard.GiftCard",diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex a97ade8f49..dbc0c66436 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -55,6 +55,7 @@ from . import (ChargeStatus,GatewayError,PaymentError,+ PaymentMethodType,StorePaymentMethod,TransactionAction,TransactionEventType,@@ -949,6 +950,14 @@ def parse_transaction_action_data_for_session_webhook()payment_method_details.exp_month = parsed_payment_method_details.exp_monthpayment_method_details.exp_year = parsed_payment_method_details.exp_year+ elif isinstance(+ parsed_payment_method_details,+ transaction_schemas.GiftCardPaymentMethodDetails,+ ):+ payment_method_details.brand = parsed_payment_method_details.brand+ payment_method_details.last_digits = (+ parsed_payment_method_details.last_chars+ )return (TransactionSessionResponse(@@ -1302,21 +1311,30 @@ def update_transaction_item_with_payment_method_details(if payment_method_details.name != transaction_item.payment_method_name:transaction_item.payment_method_name = payment_method_details.nameupdated_fields.append("payment_method_name")- if payment_method_details.brand != transaction_item.cc_brand:- transaction_item.cc_brand = payment_method_details.brand- updated_fields.append("cc_brand")- if payment_method_details.first_digits != transaction_item.cc_first_digits:- transaction_item.cc_first_digits = payment_method_details.first_digits- updated_fields.append("cc_first_digits")- if payment_method_details.last_digits != transaction_item.cc_last_digits:- transaction_item.cc_last_digits = payment_method_details.last_digits- updated_fields.append("cc_last_digits")- if payment_method_details.exp_month != transaction_item.cc_exp_month:- transaction_item.cc_exp_month = payment_method_details.exp_month- updated_fields.append("cc_exp_month")- if payment_method_details.exp_year != transaction_item.cc_exp_year:- transaction_item.cc_exp_year = payment_method_details.exp_year- updated_fields.append("cc_exp_year")++ if payment_method_details.type == PaymentMethodType.GIFT_CARD:+ if payment_method_details.brand != transaction_item.gift_card_brand:+ transaction_item.gift_card_brand = payment_method_details.brand+ updated_fields.append("gift_card_brand")+ if payment_method_details.last_digits != transaction_item.gift_card_last_chars:+ transaction_item.gift_card_last_chars = payment_method_details.last_digits+ updated_fields.append("gift_card_last_chars")+ else:+ if payment_method_details.brand != transaction_item.cc_brand:+ transaction_item.cc_brand = payment_method_details.brand+ updated_fields.append("cc_brand")+ if payment_method_details.first_digits != transaction_item.cc_first_digits:+ transaction_item.cc_first_digits = payment_method_details.first_digits+ updated_fields.append("cc_first_digits")+ if payment_method_details.last_digits != transaction_item.cc_last_digits:+ transaction_item.cc_last_digits = payment_method_details.last_digits+ updated_fields.append("cc_last_digits")+ if payment_method_details.exp_month != transaction_item.cc_exp_month:+ transaction_item.cc_exp_month = payment_method_details.exp_month+ updated_fields.append("cc_exp_month")+ if payment_method_details.exp_year != transaction_item.cc_exp_year:+ transaction_item.cc_exp_year = payment_method_details.exp_year+ updated_fields.append("cc_exp_year")return updated_fieldsdiff --git a/saleor/webhook/response_schemas/transaction.py b/saleor/webhook/response_schemas/transaction.pyindex 6ef3d29dfe..82eb620ab8 100644--- a/saleor/webhook/response_schemas/transaction.py+++ b/saleor/webhook/response_schemas/transaction.py@@ -112,6 +112,31 @@ class CardPaymentMethodDetails(PaymentMethodDetailsBase):] = None+class GiftCardPaymentMethodDetails(PaymentMethodDetailsBase):+ type: Annotated[ # type: ignore[name-defined]+ Literal[PaymentMethodTypeEnum.GIFT_CARD.name],+ Field(+ description="Type of the payment method used for the transaction.",+ max_length=32,+ ),+ ]+ brand: Annotated[+ str | None,+ Field(+ description="Brand of the gift card used for the transaction.",+ max_length=40,+ ),+ ] = None+ last_chars: Annotated[+ str | None,+ Field(+ description="Last characters of the gift card used for the transaction.",+ max_length=4,+ validation_alias="lastChars",+ ),+ ] = None++class TransactionBaseSchema(BaseModel):psp_reference: Annotated[DefaultIfNone[str],@@ -351,7 +376,10 @@ class TransactionSessionBaseSchema(TransactionBaseSchema):]payment_method_details: Annotated[- OtherPaymentMethodDetails | CardPaymentMethodDetails | None,+ OtherPaymentMethodDetails+ | CardPaymentMethodDetails+ | GiftCardPaymentMethodDetails+ | None,Field(validation_alias="paymentMethodDetails",default=None,