Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Clean up code. Add amount to paymentMethod request. reverse pending condition (#5982)
Maciej Korycinskid13d08ca654d
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.pyindex 838147d964..e631ba4daf 100644--- a/saleor/payment/gateways/adyen/plugin.py+++ b/saleor/payment/gateways/adyen/plugin.py@@ -17,6 +17,9 @@ from ...interface import GatewayConfig, GatewayResponse, PaymentData, PaymentGatfrom ...models import Payment, Transactionfrom ..utils import get_supported_currenciesfrom .utils import (+ AUTH_STATUS,+ FAILED_STATUSES,+ PENDING_STATUSES,api_call,call_capture,request_data_for_gateway_config,@@ -41,12 +44,6 @@ def require_active_plugin(fn):return wrapped-# https://docs.adyen.com/checkout/payment-result-codes-FAILED_STATUSES = ["refused", "error", "cancelled"]-PENDING_STATUSES = ["pending", "received"]-AUTH_STATUS = "authorised"--class AdyenGatewayPlugin(BasePlugin):PLUGIN_ID = "mirumee.payments.adyen"PLUGIN_NAME = GATEWAY_NAME@@ -274,10 +271,10 @@ class AdyenGatewayPlugin(BasePlugin):is_success = result_code not in FAILED_STATUSESadyen_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:+ if result_code in PENDING_STATUSES:kind = TransactionKind.PENDING+ elif adyen_auto_capture:+ kind = TransactionKind.CAPTURE# If auto capture is enabled, let's make a capture the auth paymentif self.config.auto_capture and result_code == AUTH_STATUS:diff --git a/saleor/payment/gateways/adyen/tests/test_utils.py b/saleor/payment/gateways/adyen/tests/test_utils.pyindex 8827af78ec..2c22d4b1da 100644--- a/saleor/payment/gateways/adyen/tests/test_utils.py+++ b/saleor/payment/gateways/adyen/tests/test_utils.py@@ -240,19 +240,22 @@ def test_get_price_amount(value, currency, expected_result):assert result == expected_result-def test_request_data_for_gateway_config(checkout, address):+def test_request_data_for_gateway_config(checkout_with_item, address):# given- checkout.billing_address = address+ checkout_with_item.billing_address = addressmerchant_account = "test_account"# when- response_config = request_data_for_gateway_config(checkout, merchant_account)+ response_config = request_data_for_gateway_config(+ checkout_with_item, merchant_account+ )# thenassert response_config == {"merchantAccount": merchant_account,- "countryCode": checkout.billing_address.country,+ "countryCode": checkout_with_item.billing_address.country,"channel": "web",+ "amount": {"currency": "USD", "value": "3000"},}@@ -268,6 +271,7 @@ def test_request_data_for_gateway_config_no_country(checkout, address, settings)"merchantAccount": merchant_account,"countryCode": settings.DEFAULT_COUNTRY,"channel": "web",+ "amount": {"currency": "USD", "value": "0"},}diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.pyindex b1463c6580..683319914f 100644--- a/saleor/payment/gateways/adyen/utils.py+++ b/saleor/payment/gateways/adyen/utils.py@@ -8,7 +8,7 @@ from babel.numbers import get_currency_precisionfrom django.conf import settingsfrom django_countries.fields import Country-from ....checkout.calculations import checkout_line_total+from ....checkout.calculations import checkout_line_total, checkout_totalfrom ....checkout.models import Checkoutfrom ....core.prices import quantize_pricefrom ....discount.utils import fetch_active_discounts@@ -19,6 +19,12 @@ from ...interface import PaymentDatalogger = logging.getLogger(__name__)+# https://docs.adyen.com/checkout/payment-result-codes+FAILED_STATUSES = ["refused", "error", "cancelled"]+PENDING_STATUSES = ["pending", "received"]+AUTH_STATUS = "authorised"++def convert_adyen_price_format(value: str, currency: str):value = Decimal(value)precision = get_currency_precision(currency)@@ -158,8 +164,11 @@ def get_shopper_locale_value(country_code: str):def request_data_for_gateway_config(checkout: "Checkout", merchant_account-) -> Dict[str, str]:+) -> Dict[str, Any]:address = checkout.billing_address or checkout.shipping_address+ discounts = fetch_active_discounts()+ lines = checkout.lines.prefetch_related("variant").all()+ total = checkout_total(checkout=checkout, lines=lines, discounts=discounts)country = address.country if address else Noneif country:@@ -171,6 +180,10 @@ def request_data_for_gateway_config("merchantAccount": merchant_account,"countryCode": country_code,"channel": channel,+ "amount": {+ "value": get_price_amount(total.gross.amount, checkout.currency),+ "currency": checkout.currency,+ },}diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.pyindex a98b5b0a40..6d5eace3a4 100644--- a/saleor/payment/gateways/adyen/webhooks.py+++ b/saleor/payment/gateways/adyen/webhooks.py@@ -34,7 +34,7 @@ from ... import ChargeStatus, PaymentError, TransactionKindfrom ...gateway import capturefrom ...interface import GatewayConfig, GatewayResponsefrom ...utils import create_payment_information, create_transaction, gateway_postprocess-from .utils import api_call, convert_adyen_price_format+from .utils import FAILED_STATUSES, api_call, convert_adyen_price_formatdef get_payment(payment_id: Optional[str]) -> Optional[Payment]:@@ -549,7 +549,6 @@ def handle_additional_actions(request_data = prepare_api_request_data(request, data)except KeyError as e:return HttpResponseBadRequest(e.args[0])-try:result = api_call(request_data, payment_details)except PaymentError as e:@@ -558,7 +557,6 @@ def handle_additional_actions(handle_api_response(payment, result, adyen_auto_capture, auto_capture)redirect_url = prepare_redirect_url(payment_id, checkout_pk, result, return_url)-return redirect(redirect_url)@@ -622,14 +620,17 @@ def handle_api_response(payment=payment, payment_token=payment.token,)- action_required = Falseerror_message = response.message.get("refusalReason")- if "action" in response.message:- action_required = True++ result_code = response.message["resultCode"].strip().lower()+ is_success = result_code not in FAILED_STATUSES++ # action_required is True as we want to call gateway.confirm from the+ # checkoutComplete mutationgateway_response = GatewayResponse(- is_success=True if error_message else False,- action_required=action_required,+ is_success=is_success,+ action_required=True,kind=kind,amount=payment_data.amount,currency=payment_data.currency,@@ -642,7 +643,7 @@ def handle_api_response(transaction = create_transaction(payment=payment,kind=kind,- action_required=action_required,+ action_required=True,payment_information=payment_data,gateway_response=gateway_response,)