Skip to content

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

Fix capture notification

Maciej Korycinski225a61fd23c8
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 71608fce76..bde9d39ba6 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -166,7 +166,7 @@ def complete_checkout(
store_customer_id(user, payment.gateway, txn.customer_id) # type: ignore
action_required = txn.action_required
- action_data = txn.action_required_data if action_data else {}
+ action_data = txn.action_required_data if action_required else {}
if not action_required:
try:
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.py
index 1735b3f10d..8ffd4accff 100644
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@ -423,12 +423,23 @@ class AdyenGatewayPlugin(BasePlugin):
transaction = (
Transaction.objects.filter(
payment__id=payment_information.payment_id,
- kind__in=[TransactionKind.AUTH, TransactionKind.CAPTURE],
+ kind=TransactionKind.AUTH,
is_success=True,
)
.exclude(token__isnull=True, token__exact="")
.last()
)
+ if not transaction:
+ # If we don't find the Auth kind we will try to get Capture kind
+ transaction = (
+ Transaction.objects.filter(
+ payment__id=payment_information.payment_id,
+ kind=TransactionKind.CAPTURE,
+ is_success=True,
+ )
+ .exclude(token__isnull=True, token__exact="")
+ .last()
+ )
if not transaction:
raise PaymentError("Cannot find a payment reference to refund.")
diff --git a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
index 7c07ae0fea..2aaac52a6c 100644
--- a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
+++ b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
@@ -205,10 +205,11 @@ def test_handle_capture_for_order(notification, adyen_plugin, payment_adyen_for_
config = adyen_plugin().config
handle_capture(notification, config)
- assert payment.transactions.count() == 1
- transaction = payment.transactions.get()
+ payment.refresh_from_db()
+ assert payment.transactions.count() == 2
+ transaction = payment.transactions.filter(kind=TransactionKind.CAPTURE).get()
assert transaction.is_success is True
- assert transaction.kind == TransactionKind.AUTH
+ assert payment.charge_status == ChargeStatus.FULLY_CHARGED
def test_handle_capture_for_checkout(
diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.py
index 78058d2ef8..1d95a4716d 100644
--- a/saleor/payment/gateways/adyen/webhooks.py
+++ b/saleor/payment/gateways/adyen/webhooks.py
@@ -27,7 +27,7 @@ from ....checkout.complete_checkout import complete_checkout
from ....checkout.models import Checkout
from ....core.utils.url import prepare_url
from ....discount.utils import fetch_active_discounts
-from ....order.actions import cancel_order, order_refunded
+from ....order.actions import cancel_order, order_captured, order_refunded
from ....order.events import external_notification_event
from ....payment.models import Payment, Transaction
from ... import ChargeStatus, PaymentError, TransactionKind
@@ -228,24 +228,24 @@ def handle_capture(notification: Dict[str, Any], _gateway_config: GatewayConfig)
return
transaction_id = notification.get("pspReference")
- transaction = payment.transactions.filter(
+ capture_transaction = payment.transactions.filter(
token=transaction_id,
action_required=False,
is_success=True,
kind=TransactionKind.CAPTURE,
).last()
- if transaction:
+ if capture_transaction:
# We already have this transaction
return
- action_transaction = payment.transactions.filter(
- token=transaction_id,
- action_required=False,
- is_success=True,
- kind=TransactionKind.ACTION_TO_CONFIRM,
- ).last()
if not payment.order:
+ action_transaction = payment.transactions.filter(
+ token=transaction_id,
+ action_required=False,
+ is_success=True,
+ kind=TransactionKind.ACTION_TO_CONFIRM,
+ ).last()
# If the payment is not Auth/Capture, it means that user didn't return to the
# storefront and we need to finalize the checkout asynchronously.
if not action_transaction:
@@ -266,6 +266,12 @@ def handle_capture(notification: Dict[str, Any], _gateway_config: GatewayConfig)
except ValidationError:
payment_refund_or_void(payment)
return
+ else:
+ new_transaction = create_new_transaction(
+ notification, payment, TransactionKind.CAPTURE
+ )
+ gateway_postprocess(new_transaction, payment)
+ order_captured(payment.order, None, new_transaction.amount, payment)
reason = notification.get("reason", "-")
is_success = True if notification.get("success") == "true" else False
@@ -347,7 +353,7 @@ def handle_refund(notification: Dict[str, Any], _gateway_config: GatewayConfig):
create_payment_notification_for_order(
payment, success_msg, failed_msg, new_transaction.is_success
)
- if payment.order:
+ if payment.order and new_transaction.is_success:
order_refunded(payment.order, None, new_transaction.amount, payment)