Skip to content

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

Assign app as a transaction owner if missing (#13085)

Maciej Korycinskibd64d6fdbe4b
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index af8755f799..f071ead360 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -1250,6 +1250,23 @@ class TransactionUpdate(TransactionCreate):
)
recalculate_transaction_amounts(instance)
+ @classmethod
+ def assign_app_to_transaction_data_if_missing(
+ cls,
+ transaction: payment_models.TransactionItem,
+ transaction_data: dict,
+ app: Optional["App"],
+ ):
+ """Assign app to transaction if missing.
+
+ TransactionItem created before 3.13, doesn't have a relation to the owner app.
+ When app updates a transaction, we need to assign the app to the transaction.
+ """
+ transaction_has_assigned_app = transaction.app_id or transaction.app_identifier
+ if app and not transaction.user_id and not transaction_has_assigned_app:
+ transaction_data["app"] = app
+ transaction_data["app_identifier"] = app.identifier
+
@classmethod
def perform_mutation( # type: ignore[override]
cls,
@@ -1279,6 +1296,7 @@ class TransactionUpdate(TransactionCreate):
if transaction:
cls.validate_transaction_input(instance, transaction)
+ cls.assign_app_to_transaction_data_if_missing(instance, transaction, app)
cls.cleanup_metadata_data(transaction)
money_data = cls.get_money_data_from_input(transaction)
cls.update_transaction(instance, transaction, money_data, user, app)
@@ -1539,6 +1557,36 @@ class TransactionEventReport(ModelMutation):
def _update_mutation_arguments_and_fields(cls, arguments, fields):
cls._meta.arguments.update(arguments)
+ @classmethod
+ def update_transaction(
+ cls,
+ transaction: payment_models.TransactionItem,
+ available_actions: Optional[list[str]] = None,
+ app: Optional["App"] = None,
+ ):
+ fields_to_update = [
+ "authorized_value",
+ "charged_value",
+ "refunded_value",
+ "canceled_value",
+ "authorize_pending_value",
+ "charge_pending_value",
+ "refund_pending_value",
+ "cancel_pending_value",
+ ]
+ if available_actions is not None:
+ transaction.available_actions = available_actions
+ fields_to_update.append("available_actions")
+
+ recalculate_transaction_amounts(transaction, save=False)
+ transaction_has_assigned_app = transaction.app_id or transaction.app_identifier
+ if app and not transaction.user_id and not transaction_has_assigned_app:
+ transaction.app_id = app.pk
+ transaction.app_identifier = app.identifier
+ fields_to_update.append("app")
+ fields_to_update.append("app_identifier")
+ transaction.save(update_fields=fields_to_update)
+
@classmethod
def perform_mutation( # type: ignore[override]
cls,
@@ -1637,10 +1685,11 @@ class TransactionEventReport(ModelMutation):
previous_authorized_value = transaction.authorized_value
previous_charged_value = transaction.charged_value
previous_refunded_value = transaction.refunded_value
- if available_actions is not None:
- transaction.available_actions = available_actions
- transaction.save(update_fields=["available_actions"])
- recalculate_transaction_amounts(transaction)
+ cls.update_transaction(
+ transaction,
+ available_actions=available_actions,
+ app=app,
+ )
if transaction.order_id:
order = cast(order_models.Order, transaction.order)
update_order_search_vector(order, save=False)
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 62539e3572..6ba1bd46e8 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
@@ -28,6 +28,14 @@ fragment TransactionEventData on TransactionEventReport {
events {
id
}
+ createdBy {
+ ... on User {
+ id
+ }
+ ... on App {
+ id
+ }
+ }
}
transactionEvent {
id
@@ -640,7 +648,7 @@ def test_transaction_event_report_calls_amount_recalculations(
)
# then
- mocked_recalculation.assert_called_once_with(transaction)
+ mocked_recalculation.assert_called_once_with(transaction, save=False)
transaction.refresh_from_db()
assert transaction.charged_value == amount
@@ -1638,3 +1646,72 @@ def test_transaction_event_report_for_order_triggers_webhooks_when_partially_ref
assert not mock_order_fully_refunded.called
mock_order_refunded.assert_called_once_with(order)
mock_order_updated.assert_called_once_with(order)
+
+
+def test_transaction_event_report_by_app_assign_app_owner(
+ transaction_item_generator,
+ app_api_client,
+ permission_manage_payments,
+):
+ # given
+ transaction = transaction_item_generator(authorized_value=Decimal("10"))
+ event_time = timezone.now()
+ external_url = f"http://{TEST_SERVER_DOMAIN}/external-url"
+ message = "Sucesfull charge"
+ psp_reference = "111-abc"
+ amount = Decimal("11.00")
+ transaction_id = graphene.Node.to_global_id("TransactionItem", transaction.token)
+ variables = {
+ "id": transaction_id,
+ "type": TransactionEventTypeEnum.CHARGE_SUCCESS.name,
+ "amount": amount,
+ "pspReference": psp_reference,
+ "time": event_time.isoformat(),
+ "externalUrl": external_url,
+ "message": message,
+ "availableActions": [TransactionActionEnum.REFUND.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
+ response = get_graphql_content(response)
+ transaction_report_data = response["data"]["transactionEventReport"]
+ transaction.refresh_from_db()
+
+ assert transaction_report_data["transaction"]["createdBy"][
+ "id"
+ ] == to_global_id_or_none(app_api_client.app)
+ assert transaction.app_identifier == app_api_client.app.identifier
+ assert transaction.app == app_api_client.app
+ assert transaction.user is None
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_update.py b/saleor/graphql/payment/tests/mutations/test_transaction_update.py
index 13cd79d285..99b543ec67 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_update.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_update.py
@@ -68,6 +68,14 @@ mutation TransactionUpdate(
key
value
}
+ createdBy{
+ ... on User {
+ id
+ }
+ ... on App {
+ id
+ }
+ }
events{
status
pspReference
@@ -2902,3 +2910,32 @@ def test_transaction_update_for_order_triggers_webhook_when_partially_refunded(
assert not mock_order_fully_refunded.called
mock_order_updated.assert_called_once_with(order_with_lines)
mock_order_refunded.assert_called_once_with(order_with_lines)
+
+
+def test_transaction_update_by_app_assign_app_owner(
+ transaction_item_generator, permission_manage_payments, app_api_client
+):
+ # given
+ transaction = transaction_item_generator()
+ status = "Captured for 10$"
+
+ variables = {
+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),
+ "transaction": {
+ "status": status,
+ },
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ MUTATION_TRANSACTION_UPDATE, variables, permissions=[permission_manage_payments]
+ )
+
+ # then
+ transaction.refresh_from_db()
+ content = get_graphql_content(response)
+ data = content["data"]["transactionUpdate"]["transaction"]
+ assert data["createdBy"]["id"] == to_global_id_or_none(app_api_client.app)
+ assert transaction.app_identifier == app_api_client.app.identifier
+ assert transaction.app == app_api_client.app
+ assert transaction.user is None