Skip to content

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

Add tests for handle_capture

Maciej Korycinskia8d4ba0a7a24
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 19139aa235..7c07ae0fea 100644
--- a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
+++ b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
@@ -195,10 +195,7 @@ def test_handle_cancel_already_canceleld(
assert payment.transactions.count() == 2
[email protected]("saleor.payment.gateways.adyen.webhooks.order_captured")
-def test_handle_capture(
- mocked_captured, notification, adyen_plugin, payment_adyen_for_order
-):
+def test_handle_capture_for_order(notification, adyen_plugin, payment_adyen_for_order):
payment = payment_adyen_for_order
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
@@ -206,17 +203,51 @@ def test_handle_capture(
value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
+ handle_capture(notification, config)
+
+ assert payment.transactions.count() == 1
+ transaction = payment.transactions.get()
+ assert transaction.is_success is True
+ assert transaction.kind == TransactionKind.AUTH
+
+def test_handle_capture_for_checkout(
+ notification, adyen_plugin, payment_adyen_for_checkout, address, shipping_method,
+):
+ checkout = payment_adyen_for_checkout.checkout
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.save()
+ checkout_token = str(checkout.token)
+
+ payment = payment_adyen_for_checkout
+ total = calculations.calculate_checkout_total_with_gift_cards(checkout=checkout)
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.to_confirm = True
+ payment.save()
+
+ payment_id = graphene.Node.to_global_id("Payment", payment.pk)
+ notification = notification(
+ merchant_reference=payment_id,
+ value=to_adyen_price(payment.total, payment.currency),
+ )
+ config = adyen_plugin().config
handle_capture(notification, config)
+ payment.refresh_from_db()
assert payment.transactions.count() == 2
- transaction = payment.transactions.filter(kind=TransactionKind.CAPTURE).get()
+ transaction = payment.transactions.exclude(
+ kind=TransactionKind.ACTION_TO_CONFIRM
+ ).get()
assert transaction.is_success is True
- payment.refresh_from_db()
- assert payment.charge_status == ChargeStatus.FULLY_CHARGED
- mocked_captured.assert_called_once_with(
- payment.order, None, transaction.amount, payment
- )
+ assert transaction.kind == TransactionKind.AUTH
+ assert payment.checkout is None
+ assert payment.order
+ assert payment.order.checkout_token == checkout_token
def test_handle_capture_with_payment_already_charged(
diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.py
index 84327a108c..78058d2ef8 100644
--- a/saleor/payment/gateways/adyen/webhooks.py
+++ b/saleor/payment/gateways/adyen/webhooks.py
@@ -27,19 +27,14 @@ 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_captured, order_refunded
+from ....order.actions import cancel_order, order_refunded
from ....order.events import external_notification_event
from ....payment.models import Payment, Transaction
from ... import ChargeStatus, PaymentError, TransactionKind
from ...gateway import payment_refund_or_void
from ...interface import GatewayConfig, GatewayResponse
from ...utils import create_payment_information, create_transaction, gateway_postprocess
-from .utils import (
- FAILED_STATUSES,
- api_call,
- create_order_from_checkout,
- from_adyen_price,
-)
+from .utils import FAILED_STATUSES, api_call, from_adyen_price
def get_payment(payment_id: Optional[str]) -> Optional[Payment]:
@@ -228,35 +223,55 @@ def handle_capture(notification: Dict[str, Any], _gateway_config: GatewayConfig)
return
checkout = get_checkout(payment)
- transaction_id = notification.get("pspReference")
if payment.charge_status == ChargeStatus.FULLY_CHARGED:
# the payment has already status captured.
return
- new_transaction = create_new_transaction(
- notification, payment, TransactionKind.CAPTURE
- )
+ transaction_id = notification.get("pspReference")
+ transaction = payment.transactions.filter(
+ token=transaction_id,
+ action_required=False,
+ is_success=True,
+ kind=TransactionKind.CAPTURE,
+ ).last()
- gateway_postprocess(new_transaction, payment)
+ if transaction:
+ # We already have this transaction
+ return
- reason = notification.get("reason", "-")
+ action_transaction = payment.transactions.filter(
+ token=transaction_id,
+ action_required=False,
+ is_success=True,
+ kind=TransactionKind.ACTION_TO_CONFIRM,
+ ).last()
+ if not payment.order:
+ # 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:
+ action_transaction = create_new_transaction(
+ notification, payment, TransactionKind.ACTION_TO_CONFIRM
+ )
- if not payment.order and new_transaction.is_success and checkout:
- try:
- order = create_order_from_checkout(checkout)
- except Exception:
- payment_refund_or_void(payment)
- return
- checkout.delete()
- payment.order = order
+ if action_transaction.is_success and checkout: # type: ignore
+ try:
+ discounts = fetch_active_discounts()
+ order, _, _ = complete_checkout(
+ checkout=checkout,
+ payment_data={},
+ store_source=False,
+ discounts=discounts,
+ user=checkout.user or AnonymousUser(),
+ )
+ except ValidationError:
+ payment_refund_or_void(payment)
+ return
+ reason = notification.get("reason", "-")
+ is_success = True if notification.get("success") == "true" else False
success_msg = f"Adyen: The capture {transaction_id} request was successful."
failed_msg = f"Adyen: The capture {transaction_id} request failed. Reason: {reason}"
- create_payment_notification_for_order(
- payment, success_msg, failed_msg, new_transaction.is_success
- )
- if payment.order:
- order_captured(payment.order, None, new_transaction.amount, payment)
+ create_payment_notification_for_order(payment, success_msg, failed_msg, is_success)
@django_transaction.atomic