Skip to content

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

Allow staff user to update transaction assigned to another staff (#12836)

Maciej Korycinski16bc2476ad01
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 75957d292c..eee319e23a 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -1153,12 +1153,14 @@ class TransactionUpdate(TransactionCreate):
class Meta:
auto_permission_message = False
description = (
- "Create transaction for checkout or order."
+ "Update transaction."
+ ADDED_IN_34
+ PREVIEW_FEATURE
+ "\n\nRequires the following permissions: "
+ f"{AuthorizationFilters.OWNER.name} "
- + f"and {PaymentPermissions.HANDLE_PAYMENTS.name}."
+ + f"and {PaymentPermissions.HANDLE_PAYMENTS.name} for apps, "
+ f"{PaymentPermissions.HANDLE_PAYMENTS.name} for staff users. "
+ f"Staff user cannot update a transaction that is owned by the app."
)
doc_category = DOC_CATEGORY_PAYMENTS
error_type_class = common_types.TransactionUpdateError
@@ -1505,7 +1507,9 @@ class TransactionEventReport(ModelMutation):
+ PREVIEW_FEATURE
+ "\n\nRequires the following permissions: "
+ f"{AuthorizationFilters.OWNER.name} "
- + f"and {PaymentPermissions.HANDLE_PAYMENTS.name}."
+ + f"and {PaymentPermissions.HANDLE_PAYMENTS.name} for apps, "
+ f"{PaymentPermissions.HANDLE_PAYMENTS.name} for staff users. "
+ f"Staff user cannot update a transaction that is owned by the app."
)
error_type_class = common_types.TransactionEventReportError
permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
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 5ea3ab227b..62539e3572 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
@@ -209,6 +209,79 @@ def test_transaction_event_report_by_user(
assert transaction.available_actions == [TransactionActionEnum.CANCEL.value]
+def test_transaction_event_report_by_another_user(
+ staff_api_client, permission_manage_payments, admin_user, transaction_item_generator
+):
+ # given
+ transaction = transaction_item_generator(user=admin_user)
+ 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.CANCEL.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 = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_payments]
+ )
+
+ # then
+ get_graphql_content(response)
+ event = TransactionEvent.objects.get()
+ assert event.psp_reference == psp_reference
+ assert event.type == TransactionEventTypeEnum.CHARGE_SUCCESS.value
+ assert event.amount_value == amount
+ assert event.currency == transaction.currency
+ assert event.created_at == event_time
+ assert event.external_url == external_url
+ assert event.transaction == transaction
+ assert event.app_identifier is None
+ assert event.app is None
+ assert transaction.user != staff_api_client.user
+ assert event.user == staff_api_client.user
+
+ transaction.refresh_from_db()
+ assert transaction.available_actions == [TransactionActionEnum.CANCEL.value]
+
+
def test_transaction_event_report_no_permission(
transaction_item_created_by_app,
app_api_client,
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_update.py b/saleor/graphql/payment/tests/mutations/test_transaction_update.py
index 3d35008172..13cd79d285 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_update.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_update.py
@@ -1043,7 +1043,7 @@ def test_creates_transaction_event_by_reinstalled_app(
assert event.user is None
-def test_only_owner_can_update_its_transaction_by_staff(
+def test_only_app_owner_can_update_its_transaction_by_staff(
transaction_item_created_by_app,
permission_manage_payments,
staff_api_client,
@@ -1095,6 +1095,43 @@ def test_transaction_update_status_by_staff(
assert transaction_item_created_by_user.status == status
+def test_transaction_update_by_another_staff(
+ transaction_item_created_by_user,
+ permission_manage_payments,
+ staff_api_client,
+ admin_user,
+):
+ # given
+ transaction = transaction_item_created_by_user
+ transaction.user = admin_user
+ transaction.save()
+
+ status = "Captured for 10$"
+
+ variables = {
+ "id": graphene.Node.to_global_id("TransactionItem", transaction.token),
+ "transaction": {
+ "status": status,
+ },
+ }
+
+ # when
+ response = staff_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
+ assert transaction_item_created_by_user.user != staff_api_client.user
+ assert len(data["events"]) == 1
+ assert data["events"][0]["createdBy"]["id"] == graphene.Node.to_global_id(
+ "User", staff_api_client.user.pk
+ )
+
+
def test_transaction_update_metadata_by_staff(
transaction_item_created_by_user, permission_manage_payments, staff_api_client
):
diff --git a/saleor/graphql/payment/utils.py b/saleor/graphql/payment/utils.py
index 995d83b0fd..90f1f5876d 100644
--- a/saleor/graphql/payment/utils.py
+++ b/saleor/graphql/payment/utils.py
@@ -26,7 +26,7 @@ def check_if_requestor_has_access(
):
return True
- if user and transaction.user_id == user.id:
+ if user and transaction.user_id:
return True
if app:
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 59b972d746..c22599a31b 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -14234,13 +14234,13 @@ type Mutation {
): TransactionCreate @doc(category: "Payments")
"""
- Create transaction for checkout or order.
+ Update transaction.
Added in Saleor 3.4.
Note: this API is currently in Feature Preview and can be subject to changes at later point.
- Requires the following permissions: OWNER and HANDLE_PAYMENTS.
+ Requires the following permissions: OWNER and HANDLE_PAYMENTS for apps, HANDLE_PAYMENTS for staff users. Staff user cannot update a transaction that is owned by the app.
"""
transactionUpdate(
"""The ID of the transaction."""
@@ -14282,7 +14282,7 @@ type Mutation {
Note: this API is currently in Feature Preview and can be subject to changes at later point.
- Requires the following permissions: OWNER and HANDLE_PAYMENTS.
+ Requires the following permissions: OWNER and HANDLE_PAYMENTS for apps, HANDLE_PAYMENTS for staff users. Staff user cannot update a transaction that is owned by the app.
"""
transactionEventReport(
"""The amount of the event to report."""
@@ -20915,13 +20915,13 @@ input TransactionEventInput @doc(category: "Payments") {
}
"""
-Create transaction for checkout or order.
+Update transaction.
Added in Saleor 3.4.
Note: this API is currently in Feature Preview and can be subject to changes at later point.
-Requires the following permissions: OWNER and HANDLE_PAYMENTS.
+Requires the following permissions: OWNER and HANDLE_PAYMENTS for apps, HANDLE_PAYMENTS for staff users. Staff user cannot update a transaction that is owned by the app.
"""
type TransactionUpdate @doc(category: "Payments") {
transaction: TransactionItem
@@ -21076,7 +21076,7 @@ Added in Saleor 3.13.
Note: this API is currently in Feature Preview and can be subject to changes at later point.
-Requires the following permissions: OWNER and HANDLE_PAYMENTS.
+Requires the following permissions: OWNER and HANDLE_PAYMENTS for apps, HANDLE_PAYMENTS for staff users. Staff user cannot update a transaction that is owned by the app.
"""
type TransactionEventReport @doc(category: "Payments") {
"""Defines if the reported event hasn't been processed earlier."""