Skip to content

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

Apply code review suggestions for adyen process_payment methos

IKarbowiakda1c5f56cf11
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.py
index ff16572701..39f2cc7184 100644
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@ -11,7 +11,6 @@ from graphql_relay import from_global_id
from ....checkout.models import Checkout
from ....core.emails import prepare_url
-from ....payment.models import Payment
from ....plugins.base_plugin import BasePlugin, ConfigurationTypeField
from ... import PaymentError, TransactionKind
from ...interface import GatewayConfig, GatewayResponse, PaymentData, PaymentGateway
@@ -23,6 +22,7 @@ from .utils import (
request_data_for_payment,
request_for_payment_capture,
request_for_payment_refund,
+ update_payment_with_action_required_data,
)
from .webhooks import handle_webhook
@@ -251,19 +251,12 @@ class AdyenGatewayPlugin(BasePlugin):
action = result.message.get("action")
error_message = result.message.get("refusalReason")
if action:
- _type, payment_id = from_global_id(payment_information.payment_id)
try:
- payment = Payment.objects.get(pk=payment_id)
- details = result.message.get("details")
- payment.extra_data = json.dumps(
- {
- "payment_data": action["paymentData"],
- "parameters": [detail["key"] for detail in details]
- if details
- else [],
- }
+ update_payment_with_action_required_data(
+ payment_information.payment_id,
+ action,
+ result.message.get("details", []),
)
- payment.save(update_fields=["extra_data"])
except ObjectDoesNotExist:
is_success = False
error_message = "Payment cannot be performed. Payment does not exists."
diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.py
index 4a54f45b37..bbdb86971a 100644
--- a/saleor/payment/gateways/adyen/utils.py
+++ b/saleor/payment/gateways/adyen/utils.py
@@ -1,3 +1,4 @@
+import json
import logging
from decimal import Decimal
from typing import Any, Callable, Dict, Optional
@@ -6,9 +7,11 @@ import Adyen
from babel.numbers import get_currency_precision
from django.conf import settings
from django_countries.fields import Country
+from graphql_relay import from_global_id
from ....checkout.models import Checkout
from ....core.prices import quantize_price
+from ....payment.models import Payment
from ... import PaymentError
from ...interface import PaymentData
@@ -183,3 +186,17 @@ def request_for_payment_capture(
"originalReference": token,
"reference": payment_information.payment_id,
}
+
+
+def update_payment_with_action_required_data(
+ payment_id: str, action: dict, details: list
+):
+ _type, payment_pk = from_global_id(payment_id)
+ payment = Payment.objects.get(pk=payment_pk)
+ payment.extra_data = json.dumps(
+ {
+ "payment_data": action["paymentData"],
+ "parameters": [detail["key"] for detail in details],
+ }
+ )
+ payment.save(update_fields=["extra_data"])