Skip to content

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

Merge branch 'adyen_integration' into add-adyen-support-for-3d-secure

IKarbowiak786aaa3c48df
diff --cc saleor/payment/gateways/adyen/plugin.py
index 4f6d13a79d,77d18984b0..dc0ae7d860
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@@ -20,17 -15,14 +20,17 @@@ from ...models import Transactio
from ..utils import get_supported_currencies
from .utils import (
api_call,
+ call_capture,
request_data_for_gateway_config,
request_data_for_payment,
- request_for_payment_capture,
request_for_payment_refund,
+ update_payment_with_action_required_data,
)
-from .webhooks import handle_webhook
+from .webhooks import handle_additional_actions, handle_webhook
GATEWAY_NAME = "Adyen"
+WEBHOOK_PATH = "/webhooks"
+ADDITIONAL_ACTION_PATH = "/additional-actions"
def require_active_plugin(fn):
@@@ -263,21 -239,26 +267,33 @@@ class AdyenGatewayPlugin(BasePlugin)
merchant_account=self.config.connection_params["merchant_account"],
origin_url=self.config.connection_params["origin_url"],
)
-
result = api_call(request_data, self.adyen.checkout.payments)
- is_success = result.message["resultCode"].strip().lower() not in FAILED_STATUSES
- if self.config.auto_capture:
+ result_code = result.message["resultCode"].strip().lower()
+ is_success = result_code not in FAILED_STATUSES
+ adyen_auto_capture = self.config.connection_params["adyen_auto_capture"]
+ kind = TransactionKind.AUTH
+ if adyen_auto_capture:
+ kind = TransactionKind.CAPTURE
+ elif result_code in PENDING_STATUSES:
+ kind = TransactionKind.PENDING
+
+ # If auto capture is enabled, let's make a capture the auth payment
+ if self.config.auto_capture and result_code == AUTH_STATUS:
kind = TransactionKind.CAPTURE
- else:
- kind = TransactionKind.AUTH
+ result = call_capture(
+ payment_information=payment_information,
+ merchant_account=self.config.connection_params["merchant_account"],
+ token=result.message.get("pspReference"),
+ adyen_client=self.adyen,
+ )
+ action = result.message.get("action")
+ error_message = result.message.get("refusalReason")
+ if action:
+ update_payment_with_action_required_data(
+ payment, action, result.message.get("details", []),
+ )
+
return GatewayResponse(
is_success=is_success,
action_required="action" in result.message,
diff --cc saleor/payment/gateways/adyen/tests/test_plugin.py
index c96cb41c09,eec849bd0f..df16dfb016
--- a/saleor/payment/gateways/adyen/tests/test_plugin.py
+++ b/saleor/payment/gateways/adyen/tests/test_plugin.py
@@@ -48,9 -46,27 +48,28 @@@ def test_process_payment(payment_adyen_
assert response.currency == checkout_with_items.currency
assert response.transaction_id == "882595494831959A" # ID returned by Adyen
assert response.error is None
+ assert response.action_required_data is None
+ @pytest.mark.vcr
+ def test_process_payment_with_adyen_auto_capture(
+ payment_adyen_for_checkout, checkout_with_items, adyen_plugin
+ ):
+ payment_info = create_payment_information(
+ payment_adyen_for_checkout,
+ additional_data={"paymentMethod": {"paymentdata": ""}},
+ )
+ adyen_plugin = adyen_plugin(adyen_auto_capture=True)
+ response = adyen_plugin.process_payment(payment_info, None)
+ assert response.is_success is True
+ assert response.action_required is False
+ assert response.kind == TransactionKind.CAPTURE
+ assert response.amount == Decimal("1234")
+ assert response.currency == checkout_with_items.currency
+ assert response.transaction_id == "882595494831959A" # ID returned by Adyen
+ assert response.error is None
+
+
@pytest.mark.vcr
def test_process_payment_with_auto_capture(
payment_adyen_for_checkout, checkout_with_items, adyen_plugin
@@@ -66,125 -82,8 +85,125 @@@
assert response.kind == TransactionKind.CAPTURE
assert response.amount == Decimal("1234")
assert response.currency == checkout_with_items.currency
- assert response.transaction_id == "882595494831959A" # ID returned by Adyen
+ assert response.transaction_id == "853596624248395G" # ID returned by Adyen
assert response.error is None
+ assert response.action_required_data is None
+
+
[email protected]("saleor.payment.gateways.adyen.plugin.api_call")
+def test_process_payment_additional_action(
+ api_call_mock, payment_adyen_for_checkout, checkout_with_items, adyen_plugin
+):
+ payment_data = "Ab02b4c0!B"
+ action_data = {
+ "method": "GET",
+ "paymentData": payment_data,
+ "paymentMethodType": "ideal",
+ "type": "redirect",
+ "url": "https://test.adyen.com/hpp/redirectIdeal.shtml?brandCode=ideal",
+ }
+ message = {
+ "resultCode": "RedirectShopper",
+ "action": action_data,
+ "details": [{"key": "payload", "type": "text"}],
+ "pspReference": "882595494831959A",
+ }
+ api_call_mock.return_value.message = message
+
+ payment_info = create_payment_information(
+ payment_adyen_for_checkout,
+ additional_data={"paymentMethod": {"paymentdata": ""}},
+ )
+ adyen_plugin = adyen_plugin(auto_capture=True)
+ response = adyen_plugin.process_payment(payment_info, None)
+ assert response.is_success is True
+ assert response.action_required is True
- assert response.kind == TransactionKind.CAPTURE
++ assert response.kind == TransactionKind.AUTH
+ assert response.amount == Decimal("1234")
+ assert response.currency == checkout_with_items.currency
+ assert response.transaction_id == "882595494831959A"
+ assert response.error is None
+ assert response.action_required_data == action_data
+
+ payment_adyen_for_checkout.refresh_from_db()
+ assert payment_adyen_for_checkout.extra_data == json.dumps(
+ {"payment_data": payment_data, "parameters": ["payload"]}
+ )
+
+
[email protected]("saleor.payment.gateways.adyen.plugin.api_call")
+def test_process_payment_additional_action_payment_does_not_exists(
+ api_call_mock, payment_adyen_for_checkout, checkout_with_items, adyen_plugin
+):
+ action_data = {
+ "method": "GET",
+ "paymentData": "Ab02b4c0!B",
+ "paymentMethodType": "ideal",
+ "type": "redirect",
+ "url": "https://test.adyen.com/hpp/redirectIdeal.shtml?brandCode=ideal",
+ }
+ message = {
+ "resultCode": "RedirectShopper",
+ "action": action_data,
+ "details": [{"key": "payload", "type": "text"}],
+ "pspReference": "882595494831959A",
+ }
+ api_call_mock.return_value.message = message
+
+ payment_info = create_payment_information(
+ payment_adyen_for_checkout,
+ additional_data={"paymentMethod": {"paymentdata": ""}},
+ )
+
+ Payment.objects.all().delete()
+
+ adyen_plugin = adyen_plugin(auto_capture=True)
+
+ with pytest.raises(PaymentError) as e:
+ adyen_plugin.process_payment(payment_info, None)
+
+ assert str(e.value) == "Payment cannot be performed. Payment does not exists."
+
+
[email protected]("saleor.payment.gateways.adyen.plugin.api_call")
+def test_process_payment_additional_action_checkout_does_not_exists(
+ api_call_mock, payment_adyen_for_checkout, checkout_with_items, adyen_plugin
+):
+ action_data = {
+ "method": "GET",
+ "paymentData": "Ab02b4c0!B",
+ "paymentMethodType": "ideal",
+ "type": "redirect",
+ "url": "https://test.adyen.com/hpp/redirectIdeal.shtml?brandCode=ideal",
+ }
+ message = {
+ "resultCode": "RedirectShopper",
+ "action": action_data,
+ "details": [{"key": "payload", "type": "text"}],
+ "pspReference": "882595494831959A",
+ }
+ api_call_mock.return_value.message = message
+
+ payment_info = create_payment_information(
+ payment_adyen_for_checkout,
+ additional_data={"paymentMethod": {"paymentdata": ""}},
+ )
+
+ payment_adyen_for_checkout.checkout = None
+ payment_adyen_for_checkout.save()
+
+ adyen_plugin = adyen_plugin(auto_capture=True)
+
+ with pytest.raises(PaymentError) as e:
+ adyen_plugin.process_payment(payment_info, None)
+
+ assert (
+ str(e.value)
+ == "Payment cannot be performed. Checkout for this payment does not exist."
+ )
@pytest.mark.vcr
diff --cc saleor/payment/gateways/adyen/utils.py
index d4975f6f3d,fb77d8e18a..7aae247bb7
--- a/saleor/payment/gateways/adyen/utils.py
+++ b/saleor/payment/gateways/adyen/utils.py
@@@ -187,13 -185,15 +187,27 @@@ def request_for_payment_capture
}
+def update_payment_with_action_required_data(
+ payment: Payment, action: dict, details: list
+):
+ payment.extra_data = json.dumps(
+ {
+ "payment_data": action["paymentData"],
+ "parameters": [detail["key"] for detail in details],
+ }
+ )
+ payment.save(update_fields=["extra_data"])
++
++
+ def call_capture(
+ payment_information: "PaymentData",
+ merchant_account: str,
+ token: str,
+ adyen_client: Adyen.Adyen,
+ ):
+ request = request_for_payment_capture(
+ payment_information=payment_information,
+ merchant_account=merchant_account,
+ token=token,
+ )
+ return api_call(request, adyen_client.payment.capture)
diff --cc saleor/payment/gateways/adyen/webhooks.py
index 7d6ae948ad,4f6246b351..ee742bc97b
--- a/saleor/payment/gateways/adyen/webhooks.py
+++ b/saleor/payment/gateways/adyen/webhooks.py
@@@ -24,10 -19,11 +24,11 @@@ from ....order.actions import
)
from ....order.events import external_notification_event
from ....payment.models import Payment, Transaction
-from ... import ChargeStatus, TransactionKind
+from ... import ChargeStatus, PaymentError, TransactionKind
+ from ...gateway import capture
from ...interface import GatewayConfig, GatewayResponse
from ...utils import create_transaction, gateway_postprocess
-from .utils import convert_adyen_price_format
+from .utils import api_call, convert_adyen_price_format
def get_payment(payment_id: Optional[str]) -> Optional[Payment]: