Skip to content

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

Handle confirm action (#5959)

Maciej Korycinski1f6fbe2ebf9b
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.py
index 77d18984b0..bff8dab193 100644
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@ -11,7 +11,7 @@ from ....checkout.models import Checkout
from ....plugins.base_plugin import BasePlugin, ConfigurationTypeField
from ... import PaymentError, TransactionKind
from ...interface import GatewayConfig, GatewayResponse, PaymentData, PaymentGateway
-from ...models import Transaction
+from ...models import Payment, Transaction
from ..utils import get_supported_currencies
from .utils import (
api_call,
@@ -293,19 +293,38 @@ class AdyenGatewayPlugin(BasePlugin):
def confirm_payment(
self, payment_information: "PaymentData", previous_value
) -> "GatewayResponse":
- additional_data = payment_information.data
- result = api_call(additional_data, self.adyen.checkout.payments)
- is_success = result.message["resultCode"].strip().lower() not in FAILED_STATUSES
+ _type, payment_id = from_global_id(payment_information.payment_id)
+
+ # The additional checks are proceed asynchronously so we try to confirm that
+ # the payment is already processed
+ payment = Payment.objects.filter(id=payment_id).first()
+ if not payment:
+ raise PaymentError("Unable to find the payment.")
+
+ transaction = (
+ payment.transactions.filter(
+ payment__id=payment_id,
+ kind__in=[TransactionKind.AUTH, TransactionKind.CAPTURE],
+ is_success=True,
+ )
+ .exclude(token__isnull=True, token__exact="")
+ .last()
+ )
+ if not transaction:
+ raise PaymentError(
+ "Unable to finish the payment. Payment needs to be confirm by external "
+ "source."
+ )
return GatewayResponse(
- is_success=is_success,
- action_required="action" in result.message,
+ is_success=transaction.is_success,
+ action_required=False,
kind=TransactionKind.CONFIRM,
- amount=payment_information.amount,
- currency=payment_information.currency,
- transaction_id=result.get("pspReference", ""),
- error=result.message.get("refusalReason"),
- raw_response=result.message,
+ amount=transaction.amount,
+ currency=transaction.currency,
+ transaction_id=transaction.token,
+ error=None,
+ raw_response={},
)
@require_active_plugin
@@ -317,7 +336,7 @@ class AdyenGatewayPlugin(BasePlugin):
# we take Auth kind because it contains the transaction id that we need
transaction = (
Transaction.objects.filter(
- payment__id=payment_id, kind=TransactionKind.AUTH
+ payment__id=payment_id, kind=TransactionKind.AUTH, is_success=True
)
.exclude(token__isnull=True, token__exact="")
.last()
@@ -351,7 +370,7 @@ class AdyenGatewayPlugin(BasePlugin):
_type, payment_id = from_global_id(payment_information.payment_id)
transaction = (
Transaction.objects.filter(
- payment__id=payment_id, kind=TransactionKind.AUTH
+ payment__id=payment_id, kind=TransactionKind.AUTH, is_success=True
)
.exclude(token__isnull=True, token__exact="")
.last()
diff --git a/saleor/payment/gateways/adyen/tests/test_plugin.py b/saleor/payment/gateways/adyen/tests/test_plugin.py
index eec849bd0f..7f9a59d3ab 100644
--- a/saleor/payment/gateways/adyen/tests/test_plugin.py
+++ b/saleor/payment/gateways/adyen/tests/test_plugin.py
@@ -86,17 +86,33 @@ def test_process_payment_with_auto_capture(
assert response.error is None
[email protected](reason="To finish when additional auth data schema will be known")
def test_confirm_payment(payment_adyen_for_order, adyen_plugin):
- return # test it when we will have additional auth data
- payment_info = create_payment_information(
- payment_adyen_for_order,
- # additional_data=...
+ payment_info = create_payment_information(payment_adyen_for_order,)
+ gateway_response = GatewayResponse(
+ kind=TransactionKind.AUTH,
+ action_required=False,
+ transaction_id="882595494831959A",
+ is_success=True,
+ amount=payment_info.amount,
+ currency=payment_info.currency,
+ error="",
+ raw_response={},
+ )
+
+ auth_transaction = create_transaction(
+ payment=payment_adyen_for_order,
+ payment_information=payment_info,
+ kind=TransactionKind.AUTH,
+ gateway_response=gateway_response,
)
adyen_plugin = adyen_plugin()
response = adyen_plugin.confirm_payment(payment_info, None)
- assert response
+
+ assert response is not None
+ assert response.is_success is True
+ assert response.kind == TransactionKind.CONFIRM
+ assert response.amount == auth_transaction.amount
+ assert response.currency == auth_transaction.currency
@pytest.mark.vcr
@@ -109,7 +125,7 @@ def test_refund_payment(payment_adyen_for_order, order_with_lines, adyen_plugin)
kind=TransactionKind.AUTH,
action_required=False,
transaction_id="882595494831959A",
- is_success=False,
+ is_success=True,
amount=payment_info.amount,
currency=payment_info.currency,
error="",
@@ -141,7 +157,7 @@ def test_capture_payment(payment_adyen_for_order, order_with_lines, adyen_plugin
kind=TransactionKind.AUTH,
action_required=False,
transaction_id="882595494831959A",
- is_success=False,
+ is_success=True,
amount=payment_info.amount,
currency=payment_info.currency,
error="",
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index c563a7c580..5c6dafec1e 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -215,6 +215,13 @@ def gateway_postprocess(transaction, payment):
payment.save(update_fields=["to_confirm"])
return
+ changed_fields = []
+ # to_confirm is defined by the transaction.action_required. Payment doesn't
+ # require confirmation when we got action_required == False
+ if payment.to_confirm:
+ payment.to_confirm = False
+ changed_fields.append("to_confirm")
+
transaction_kind = transaction.kind
if transaction_kind in {
@@ -229,28 +236,26 @@ def gateway_postprocess(transaction, payment):
payment.charge_status = ChargeStatus.PARTIALLY_CHARGED
if payment.get_charge_amount() <= 0:
payment.charge_status = ChargeStatus.FULLY_CHARGED
-
- payment.save(update_fields=["charge_status", "captured_amount", "modified"])
+ changed_fields += ["charge_status", "captured_amount", "modified"]
elif transaction_kind == TransactionKind.VOID:
payment.is_active = False
- payment.save(update_fields=["is_active", "modified"])
+ changed_fields += ["is_active", "modified"]
elif transaction_kind == TransactionKind.REFUND:
- changed_fields = ["captured_amount", "modified"]
+ changed_fields += ["captured_amount", "modified"]
payment.captured_amount -= transaction.amount
payment.charge_status = ChargeStatus.PARTIALLY_REFUNDED
if payment.captured_amount <= 0:
payment.charge_status = ChargeStatus.FULLY_REFUNDED
payment.is_active = False
changed_fields += ["charge_status", "is_active"]
- payment.save(update_fields=changed_fields)
elif transaction_kind == TransactionKind.PENDING:
payment.charge_status = ChargeStatus.PENDING
- payment.save(update_fields=["charge_status"])
+ changed_fields += ["charge_status"]
elif transaction_kind == TransactionKind.CANCEL:
payment.charge_status = ChargeStatus.CANCELLED
- payment.save(update_fields=["charge_status"])
+ changed_fields += ["charge_status"]
elif transaction_kind == TransactionKind.CAPTURE_FAILED:
if payment.charge_status in {
ChargeStatus.PARTIALLY_CHARGED,
@@ -260,8 +265,9 @@ def gateway_postprocess(transaction, payment):
payment.charge_status = ChargeStatus.PARTIALLY_CHARGED
if payment.captured_amount <= 0:
payment.charge_status = ChargeStatus.NOT_CHARGED
-
- payment.save(update_fields=["charge_status", "captured_amount", "modified"])
+ changed_fields += ["charge_status", "captured_amount", "modified"]
+ if changed_fields:
+ payment.save(update_fields=changed_fields)
def fetch_customer_id(user: User, gateway: str):