Skip to content

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

Fix not saving availableActions in transactionEventReport (#13667)

Maciej Korycinskic17271564292
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_create.py b/saleor/graphql/payment/mutations/transaction/transaction_create.py
index c0b9051a62..c9de09d630 100644
--- a/saleor/graphql/payment/mutations/transaction/transaction_create.py
+++ b/saleor/graphql/payment/mutations/transaction/transaction_create.py
@@ -247,6 +247,9 @@ class TransactionCreate(BaseMutation):
app_identifier = None
if app and app.identifier:
app_identifier = app.identifier
+ transaction_input["available_actions"] = list(
+ set(transaction_input.get("available_actions", []))
+ )
transaction = payment_models.TransactionItem(
token=uuid.uuid4(),
use_old_id=True,
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_event_report.py b/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
index 3b5d3d0e59..4c445dce6d 100644
--- a/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
+++ b/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
@@ -211,6 +211,10 @@ class TransactionEventReport(ModelMutation):
)
cls.clean_instance(info, transaction_event)
+
+ if available_actions is not None:
+ available_actions = list(set(available_actions))
+
already_processed = False
error_code = None
error_msg = None
@@ -286,6 +290,11 @@ class TransactionEventReport(ModelMutation):
if transaction.checkout_id:
manager = get_plugin_manager_promise(info.context).get()
transaction_amounts_for_checkout_updated(transaction, manager)
+ elif available_actions is not None and set(
+ transaction.available_actions
+ ) != set(available_actions):
+ transaction.available_actions = available_actions
+ transaction.save(update_fields=["available_actions"])
return cls(
already_processed=already_processed,
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_update.py b/saleor/graphql/payment/mutations/transaction/transaction_update.py
index b34f98b6d8..60f88924b1 100644
--- a/saleor/graphql/payment/mutations/transaction/transaction_update.py
+++ b/saleor/graphql/payment/mutations/transaction/transaction_update.py
@@ -100,6 +100,11 @@ class TransactionUpdate(TransactionCreate):
cls, instance: payment_models.TransactionItem, transaction_data
):
currency = instance.currency
+ if transaction_data.get("available_actions") is not None:
+ transaction_data["available_actions"] = list(
+ set(transaction_data.get("available_actions", []))
+ )
+
cls.validate_money_input(
transaction_data,
currency,
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_create.py b/saleor/graphql/payment/tests/mutations/test_transaction_create.py
index 348c3d88e2..a57d28803d 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_create.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_create.py
@@ -141,6 +141,7 @@ def test_transaction_create_for_order_by_app(
psp_reference = "PSP reference - 123"
available_actions = [
TransactionActionEnum.CHARGE.name,
+ TransactionActionEnum.CHARGE.name,
]
authorized_value = Decimal("10")
metadata = {"key": "test-1", "value": "123"}
@@ -169,6 +170,8 @@ def test_transaction_create_for_order_by_app(
)
# then
+ available_actions = list(set(available_actions))
+
transaction = order_with_lines.payment_transactions.first()
content = get_graphql_content(response)
data = content["data"]["transactionCreate"]["transaction"]
@@ -290,6 +293,7 @@ def test_transaction_create_for_checkout_by_app(
psp_reference = "PSP reference - 123"
available_actions = [
TransactionActionEnum.CHARGE.name,
+ TransactionActionEnum.CHARGE.name,
]
authorized_value = Decimal("10")
metadata = {"key": "test-1", "value": "123"}
@@ -322,6 +326,8 @@ def test_transaction_create_for_checkout_by_app(
assert checkout_with_items.charge_status == CheckoutChargeStatus.NONE
assert checkout_with_items.authorize_status == CheckoutAuthorizeStatus.PARTIAL
+ available_actions = list(set(available_actions))
+
transaction = checkout_with_items.payment_transactions.first()
content = get_graphql_content(response)
data = content["data"]["transactionCreate"]["transaction"]
@@ -790,6 +796,7 @@ def test_transaction_create_for_order_by_staff(
psp_reference = "PSP reference - 123"
available_actions = [
TransactionActionEnum.CHARGE.name,
+ TransactionActionEnum.CHARGE.name,
]
authorized_value = Decimal("10")
metadata = {"key": "test-1", "value": "123"}
@@ -816,6 +823,8 @@ def test_transaction_create_for_order_by_staff(
)
# then
+ available_actions = list(set(available_actions))
+
transaction = order_with_lines.payment_transactions.first()
content = get_graphql_content(response)
data = content["data"]["transactionCreate"]["transaction"]
@@ -936,6 +945,7 @@ def test_transaction_create_for_checkout_by_staff(
psp_reference = "PSP reference - 123"
available_actions = [
TransactionActionEnum.CHARGE.name,
+ TransactionActionEnum.CHARGE.name,
]
authorized_value = Decimal("10")
metadata = {"key": "test-1", "value": "123"}
@@ -962,6 +972,8 @@ def test_transaction_create_for_checkout_by_staff(
)
# then
+ available_actions = list(set(available_actions))
+
checkout_with_items.refresh_from_db()
assert checkout_with_items.charge_status == CheckoutChargeStatus.NONE
assert checkout_with_items.authorize_status == CheckoutAuthorizeStatus.PARTIAL
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
index 5136649e0d..6a410753f2 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
@@ -165,7 +165,10 @@ def test_transaction_event_report_by_user(
"time": event_time.isoformat(),
"externalUrl": external_url,
"message": message,
- "availableActions": [TransactionActionEnum.CANCEL.name],
+ "availableActions": [
+ TransactionActionEnum.CANCEL.name,
+ TransactionActionEnum.CANCEL.name,
+ ],
}
query = (
MUTATION_DATA_FRAGMENT
@@ -507,6 +510,163 @@ def test_transaction_event_report_event_already_exists(
)
+def test_transaction_event_report_event_already_exists_updates_available_actions(
+ transaction_item_generator, app_api_client, permission_manage_payments, app
+):
+ # given
+ event_time = timezone.now()
+ external_url = f"http://{TEST_SERVER_DOMAIN}/external-url"
+ message = "Sucesfull charge"
+ psp_reference = "111-abc"
+ amount = Decimal("11.00")
+ event_type = TransactionEventTypeEnum.CHARGE_SUCCESS
+ transaction = transaction_item_generator(app=app, charged_value=amount)
+ transaction.events.update(
+ psp_reference=psp_reference,
+ )
+ transaction_id = graphene.Node.to_global_id("TransactionItem", transaction.token)
+ variables = {
+ "id": transaction_id,
+ "type": event_type.name,
+ "amount": amount,
+ "pspReference": psp_reference,
+ "time": event_time.isoformat(),
+ "externalUrl": external_url,
+ "message": message,
+ "availableActions": [
+ TransactionActionEnum.REFUND.name,
+ TransactionActionEnum.CHARGE.name,
+ ],
+ }
+
+ query = (
+ MUTATION_DATA_FRAGMENT
+ + """
+ mutation TransactionEventReport(
+ $id: ID!
+ $type: TransactionEventTypeEnum!
+ $amount: PositiveDecimal!
+ $pspReference: String!
+ $time: DateTime
+ $externalUrl: String
+ $message: String
+ $availableActions: [TransactionActionEnum!]!
+ ) {
+ transactionEventReport(
+ id: $id
+ type: $type
+ amount: $amount
+ pspReference: $pspReference
+ time: $time
+ externalUrl: $externalUrl
+ message: $message
+ availableActions: $availableActions
+ ) {
+ ...TransactionEventData
+ }
+ }
+ """
+ )
+ # when
+ response = app_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_payments]
+ )
+
+ # then
+ transaction.refresh_from_db()
+ response = get_graphql_content(response)
+ transaction_report_data = response["data"]["transactionEventReport"]
+ assert transaction_report_data["alreadyProcessed"] is True
+ assert set(transaction_report_data["transaction"]["actions"]) == set(
+ [
+ TransactionActionEnum.REFUND.name,
+ TransactionActionEnum.CHARGE.name,
+ ]
+ )
+ assert set(transaction.available_actions) == set(
+ [TransactionActionEnum.REFUND.value, TransactionActionEnum.CHARGE.value]
+ )
+
+
+def test_event_already_exists_do_not_overwrite_actions_when_not_provided_in_input(
+ transaction_item_generator, app_api_client, permission_manage_payments, app
+):
+ # given
+ event_time = timezone.now()
+ external_url = f"http://{TEST_SERVER_DOMAIN}/external-url"
+ message = "Sucesfull charge"
+ psp_reference = "111-abc"
+ amount = Decimal("11.00")
+ event_type = TransactionEventTypeEnum.CHARGE_SUCCESS
+ transaction = transaction_item_generator(
+ app=app,
+ charged_value=amount,
+ available_actions=[
+ TransactionActionEnum.REFUND.value,
+ TransactionActionEnum.CHARGE.value,
+ ],
+ )
+ transaction.events.update(
+ psp_reference=psp_reference,
+ )
+ transaction_id = graphene.Node.to_global_id("TransactionItem", transaction.token)
+ variables = {
+ "id": transaction_id,
+ "type": event_type.name,
+ "amount": amount,
+ "pspReference": psp_reference,
+ "time": event_time.isoformat(),
+ "externalUrl": external_url,
+ "message": message,
+ }
+
+ query = (
+ MUTATION_DATA_FRAGMENT
+ + """
+ mutation TransactionEventReport(
+ $id: ID!
+ $type: TransactionEventTypeEnum!
+ $amount: PositiveDecimal!
+ $pspReference: String!
+ $time: DateTime
+ $externalUrl: String
+ $message: String
+ ) {
+ transactionEventReport(
+ id: $id
+ type: $type
+ amount: $amount
+ pspReference: $pspReference
+ time: $time
+ externalUrl: $externalUrl
+ message: $message
+ ) {
+ ...TransactionEventData
+ }
+ }
+ """
+ )
+ # when
+ response = app_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_payments]
+ )
+
+ # then
+ transaction.refresh_from_db()
+ response = get_graphql_content(response)
+ transaction_report_data = response["data"]["transactionEventReport"]
+ assert transaction_report_data["alreadyProcessed"] is True
+ assert set(transaction_report_data["transaction"]["actions"]) == set(
+ [
+ TransactionActionEnum.REFUND.name,
+ TransactionActionEnum.CHARGE.name,
+ ]
+ )
+ assert set(transaction.available_actions) == set(
+ [TransactionActionEnum.REFUND.value, TransactionActionEnum.CHARGE.value]
+ )
+
+
def test_transaction_event_report_incorrect_amount_for_already_existing(
app_api_client, permission_manage_payments, transaction_item_generator, app
):
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_update.py b/saleor/graphql/payment/tests/mutations/test_transaction_update.py
index 8242dfcd85..68999cf572 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_update.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_update.py
@@ -335,7 +335,10 @@ def test_transaction_update_available_actions_by_app(
):
# given
transaction = transaction_item_created_by_app
- available_actions = [TransactionActionEnum.REFUND.name]
+ available_actions = [
+ TransactionActionEnum.REFUND.name,
+ TransactionActionEnum.REFUND.name,
+ ]
variables = {
"id": graphene.Node.to_global_id("TransactionItem", transaction.token),
@@ -353,8 +356,8 @@ def test_transaction_update_available_actions_by_app(
transaction.refresh_from_db()
content = get_graphql_content(response)
data = content["data"]["transactionUpdate"]["transaction"]
- assert data["actions"] == available_actions
- assert transaction.available_actions == ["refund"]
+ assert data["actions"] == list(set(available_actions))
+ assert transaction.available_actions == [TransactionActionEnum.REFUND.value]
@pytest.mark.parametrize(
@@ -1239,7 +1242,10 @@ def test_transaction_update_available_actions_by_staff(
):
# given
transaction = transaction_item_created_by_user
- available_actions = [TransactionActionEnum.REFUND.name]
+ available_actions = [
+ TransactionActionEnum.REFUND.name,
+ TransactionActionEnum.REFUND.name,
+ ]
variables = {
"id": graphene.Node.to_global_id("TransactionItem", transaction.token),
@@ -1257,8 +1263,8 @@ def test_transaction_update_available_actions_by_staff(
transaction.refresh_from_db()
content = get_graphql_content(response)
data = content["data"]["transactionUpdate"]["transaction"]
- assert data["actions"] == available_actions
- assert transaction.available_actions == ["refund"]
+ assert data["actions"] == list(set(available_actions))
+ assert transaction.available_actions == [TransactionActionEnum.REFUND.value]
@pytest.mark.parametrize(
diff --git a/saleor/payment/tests/test_utils.py b/saleor/payment/tests/test_utils.py
index e835b1796c..b54422b724 100644
--- a/saleor/payment/tests/test_utils.py
+++ b/saleor/payment/tests/test_utils.py
@@ -838,6 +838,7 @@ def test_create_transaction_event_from_request_and_webhook_response_full_event(
"time": event_time,
"externalUrl": event_url,
"message": event_cause,
+ "actions": ["CHARGE", "CHARGE", "CANCEL"],
}
# when
@@ -846,7 +847,10 @@ def test_create_transaction_event_from_request_and_webhook_response_full_event(
)
# then
- assert TransactionEvent.objects.count() == 2
+ transaction.refresh_from_db()
+ assert len(transaction.available_actions) == 2
+ assert set(transaction.available_actions) == set(["charge", "cancel"])
+ assert transaction.events.count() == 2
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert event
@@ -1511,7 +1515,7 @@ def test_create_transaction_event_for_transaction_session_success_sets_actions(
response = transaction_session_response.copy()
response["result"] = response_result.upper()
response["amount"] = expected_amount
- response["actions"] = ["CANCEL", "CHARGE", "REFUND"]
+ response["actions"] = ["CANCEL", "CANCEL", "CHARGE", "REFUND"]
transaction = transaction_item_generator()
request_event = TransactionEvent.objects.create(
@@ -1528,6 +1532,7 @@ def test_create_transaction_event_for_transaction_session_success_sets_actions(
# then
transaction.refresh_from_db()
+ assert len(transaction.available_actions) == 3
assert set(transaction.available_actions) == set(["refund", "charge", "cancel"])
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 666d86f69a..3f1f6f5157 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -1155,7 +1155,7 @@ def create_transaction_event_for_transaction_session(
transaction_item.psp_reference = event.psp_reference
available_actions = transaction_request_response.available_actions
if available_actions is not None:
- transaction_item.available_actions = available_actions
+ transaction_item.available_actions = list(set(available_actions))
recalculate_transaction_amounts(transaction_item, save=False)
transaction_item.save(
@@ -1227,7 +1227,7 @@ def create_transaction_event_from_request_and_webhook_response(
recalculate_transaction_amounts(transaction_item, save=False)
available_actions = transaction_request_response.available_actions
if available_actions is not None:
- transaction_item.available_actions = available_actions
+ transaction_item.available_actions = list(set(available_actions))
transaction_item.save(
update_fields=[