Skip to content

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

Expose idempotencyKey on transaction action requested webhooks (#19811)

Lukasz Ostrowski361982487bea
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index e8d7a39922..709452da2f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -37884,6 +37884,13 @@ type TransactionCancelationRequested implements Event @doc(category: "Payments")
"""Requested action data."""
action: TransactionAction!
+
+ """
+ Idempotency key assigned to this requested action. Saleor's delivery retries of this request carry the same key, so an app can use it to avoid performing the action twice. Requesting the action again is a new request and gets a new key.
+
+ Added in Saleor 3.23.
+ """
+ idempotencyKey: String!
}
type TransactionAction @doc(category: "Payments") {
@@ -37916,6 +37923,13 @@ type TransactionChargeRequested implements Event @doc(category: "Payments") {
"""Requested action data."""
action: TransactionAction!
+
+ """
+ Idempotency key assigned to this requested action. Saleor's delivery retries of this request carry the same key, so an app can use it to avoid performing the action twice. Requesting the action again is a new request and gets a new key.
+
+ Added in Saleor 3.23.
+ """
+ idempotencyKey: String!
}
"""Event sent when transaction refund is requested."""
@@ -37938,6 +37952,13 @@ type TransactionRefundRequested implements Event @doc(category: "Payments") {
"""Requested action data."""
action: TransactionAction!
+ """
+ Idempotency key assigned to this requested action. Saleor's delivery retries of this request carry the same key, so an app can use it to avoid performing the action twice. Requesting the action again is a new request and gets a new key.
+
+ Added in Saleor 3.23.
+ """
+ idempotencyKey: String!
+
"""
Granted refund related to refund request.
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index d6cdcda57b..35fee562aa 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -2060,6 +2060,15 @@ class TransactionActionBase(AbstractType):
required=True,
description="Requested action data.",
)
+ idempotency_key = graphene.String(
+ required=True,
+ description=(
+ "Idempotency key assigned to this requested action. Saleor's delivery "
+ "retries of this request carry the same key, so an app can use it to "
+ "avoid performing the action twice. Requesting the action again is a new "
+ "request and gets a new key." + ADDED_IN_323
+ ),
+ )
@staticmethod
def resolve_transaction(root, _info: ResolveInfo):
@@ -2073,6 +2082,12 @@ class TransactionActionBase(AbstractType):
transaction_action_data: TransactionActionData
return transaction_action_data
+ @staticmethod
+ def resolve_idempotency_key(root, _info: ResolveInfo):
+ _, transaction_action_data = root
+ transaction_action_data: TransactionActionData
+ return transaction_action_data.event.idempotency_key
+
class TransactionChargeRequested(TransactionActionBase, SubscriptionObjectType):
class Meta:
diff --git a/saleor/payment/tests/test_tasks.py b/saleor/payment/tests/test_tasks.py
index c5350a991a..5ffd569b85 100644
--- a/saleor/payment/tests/test_tasks.py
+++ b/saleor/payment/tests/test_tasks.py
@@ -353,10 +353,10 @@ def test_transaction_release_funds_for_checkout_task_transaction_with_authorizat
transaction_release_funds_for_checkout_task()
# then
- request_event = transaction_item.events.filter(
+ request_event = transaction_item.events.get(
type=TransactionEventType.CANCEL_REQUEST
- ).first()
- assert request_event
+ )
+ assert request_event.idempotency_key
assert not mocked_refund_action.called
mocked_cancel_action.assert_called_once_with(
channel_slug=checkout.channel.slug,
@@ -401,10 +401,10 @@ def test_transaction_release_funds_for_checkout_task_transaction_with_charge(
transaction_release_funds_for_checkout_task()
# then
- request_event = transaction_item.events.filter(
+ request_event = transaction_item.events.get(
type=TransactionEventType.REFUND_REQUEST
- ).first()
- assert request_event
+ )
+ assert request_event.idempotency_key
assert not mocked_cancel_action.called
mocked_refund_action.assert_called_once_with(
channel_slug=checkout.channel.slug,
diff --git a/saleor/webhook/tests/subscription_webhooks/test_transaction_requested_idempotency_key.py b/saleor/webhook/tests/subscription_webhooks/test_transaction_requested_idempotency_key.py
new file mode 100644
index 0000000000..7a43fd42d1
--- /dev/null
+++ b/saleor/webhook/tests/subscription_webhooks/test_transaction_requested_idempotency_key.py
@@ -0,0 +1,133 @@
+import json
+from decimal import Decimal
+
+import pytest
+
+from ....graphql.payment.mutations.transaction.utils import (
+ create_transaction_event_requested,
+)
+from ....payment import TransactionAction, TransactionEventType
+from ....payment.interface import TransactionActionData
+from ....payment.models import TransactionItem
+from ...event_types import WebhookEventSyncType
+from ...models import Webhook
+from ...transport.asynchronous import create_deliveries_for_subscriptions
+
+IDEMPOTENCY_KEY_SUBSCRIPTION = """
+subscription {
+ event {
+ ... on TransactionChargeRequested {
+ idempotencyKey
+ }
+ ... on TransactionRefundRequested {
+ idempotencyKey
+ }
+ ... on TransactionCancelationRequested {
+ idempotencyKey
+ }
+ }
+}
+"""
+
+
+ ("_case", "event_type", "action_type", "request_event_type"),
+ [
+ (
+ "charge",
+ WebhookEventSyncType.TRANSACTION_CHARGE_REQUESTED,
+ TransactionAction.CHARGE,
+ TransactionEventType.CHARGE_REQUEST,
+ ),
+ (
+ "refund",
+ WebhookEventSyncType.TRANSACTION_REFUND_REQUESTED,
+ TransactionAction.REFUND,
+ TransactionEventType.REFUND_REQUEST,
+ ),
+ (
+ "cancelation",
+ WebhookEventSyncType.TRANSACTION_CANCELATION_REQUESTED,
+ TransactionAction.CANCEL,
+ TransactionEventType.CANCEL_REQUEST,
+ ),
+ ],
+)
+def test_payload_exposes_request_event_idempotency_key(
+ _case,
+ event_type,
+ action_type,
+ request_event_type,
+ order,
+ webhook_app,
+ permission_manage_payments,
+):
+ # given
+ webhook_app.permissions.add(permission_manage_payments)
+ transaction = TransactionItem.objects.create(
+ name="Credit card",
+ psp_reference="PSP ref",
+ available_actions=["charge", "refund", "cancel"],
+ currency="USD",
+ order_id=order.pk,
+ authorized_value=Decimal(10),
+ charged_value=Decimal(10),
+ )
+ action_value = Decimal("5.00")
+ idempotency_key = "5b9a6d2e-2a0f-4f4f-9a3c-2f6d9e1b7c4a"
+ request_event = transaction.events.create(
+ amount_value=action_value,
+ currency=transaction.currency,
+ type=request_event_type,
+ idempotency_key=idempotency_key,
+ )
+
+ webhook = Webhook.objects.create(
+ name="Webhook",
+ app=webhook_app,
+ target_url="http://www.example.com/any",
+ subscription_query=IDEMPOTENCY_KEY_SUBSCRIPTION,
+ )
+ webhook.events.create(event_type=event_type)
+
+ transaction_data = TransactionActionData(
+ transaction=transaction,
+ action_type=action_type,
+ action_value=action_value,
+ event=request_event,
+ transaction_app_owner=None,
+ )
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(
+ event_type, transaction_data, [webhook]
+ )
+
+ # then
+ assert len(deliveries) == 1
+ assert json.loads(deliveries[0].payload.get_payload()) == {
+ "idempotencyKey": request_event.idempotency_key
+ }
+
+
+ "action",
+ [TransactionAction.CHARGE, TransactionAction.REFUND, TransactionAction.CANCEL],
+)
+def test_requested_event_is_always_created_with_an_idempotency_key(action, order):
+ """Back the `idempotencyKey: String!` non-null guarantee at the mint site."""
+ # given
+ transaction = TransactionItem.objects.create(
+ currency="USD",
+ order_id=order.pk,
+ authorized_value=Decimal(10),
+ charged_value=Decimal(10),
+ )
+
+ # when
+ request_event = create_transaction_event_requested(
+ transaction, Decimal("5.00"), action
+ )
+
+ # then
+ assert request_event.idempotency_key