Skip to content

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

Apply changes after review

Maciej Korycinskiaf8b744e95af
diff --git a/saleor/checkout/calculations.py b/saleor/checkout/calculations.py
index 66eec0e5a4..edc6e1602e 100644
--- a/saleor/checkout/calculations.py
+++ b/saleor/checkout/calculations.py
@@ -42,7 +42,7 @@ def checkout_subtotal(
return quantize_price(calculated_checkout_subtotal, checkout.currency)
-def calculate_checkout_total(
+def calculate_checkout_total_with_gift_cards(
checkout: "Checkout", discounts: Optional[Iterable[DiscountInfo]] = None
) -> "TaxedMoney":
total = (
diff --git a/saleor/core/tests/test_url.py b/saleor/core/tests/test_url.py
index fa2bb62100..8d16e1ca47 100644
--- a/saleor/core/tests/test_url.py
+++ b/saleor/core/tests/test_url.py
@@ -7,4 +7,4 @@ def test_prepare_url():
redirect_url = "https://www.example.com"
params = urlencode({"param1": "abc", "param2": "xyz"})
result = prepare_url(params, redirect_url)
- assert result == "https://www.example.com?param1=abc&param2=xyz"
+ assert result == f"{redirect_url}?param1=abc&param2=xyz"
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 9c5a587ee9..50150ddc8b 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -8,7 +8,7 @@ from django.db.models import Prefetch
from ...account.error_codes import AccountErrorCode
from ...checkout import models
-from ...checkout.calculations import calculate_checkout_total
+from ...checkout.calculations import calculate_checkout_total_with_gift_cards
from ...checkout.error_codes import CheckoutErrorCode
from ...checkout.utils import (
abort_order_data,
@@ -776,7 +776,12 @@ class CheckoutComplete(BaseMutation):
@classmethod
def validate_payment_amount(cls, discounts, payment, checkout):
- if payment.total != calculate_checkout_total(checkout, discounts).gross.amount:
+ if (
+ payment.total
+ != calculate_checkout_total_with_gift_cards(
+ checkout, discounts
+ ).gross.amount
+ ):
gateway.payment_refund_or_void(payment)
raise ValidationError(
"Payment does not cover all checkout value.",
@@ -875,8 +880,6 @@ class CheckoutComplete(BaseMutation):
if txn.customer_id and user.is_authenticated:
store_customer_id(user, payment.gateway, txn.customer_id)
- confirmation_needed = False
- confirmation_data = {}
order = None
if not txn.action_required:
@@ -890,16 +893,12 @@ class CheckoutComplete(BaseMutation):
# remove checkout after order is successfully paid
checkout.delete()
- else:
- # If gateway returns information that additional steps are required we need
- # to inform the frontend and pass all required data
- confirmation_needed = True
- confirmation_data = txn.action_required_data
-
+ # If gateway returns information that additional steps are required we need
+ # to inform the frontend and pass all required data
return CheckoutComplete(
order=order,
- confirmation_needed=confirmation_needed,
- confirmation_data=confirmation_data,
+ confirmation_needed=txn.action_required,
+ confirmation_data=txn.action_required_data if txn.action_required else {},
)
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index b8e4733fdb..0b1a759ea3 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -1708,7 +1708,7 @@ def test_checkout_complete(
checkout_line_quantity = checkout_line.quantity
checkout_line_variant = checkout_line.variant
- total = calculations.calculate_checkout_total(checkout=checkout)
+ total = calculations.calculate_checkout_total_with_gift_cards(checkout=checkout)
payment = payment_dummy
payment.is_active = True
payment.order = None
@@ -1911,7 +1911,7 @@ def test_checkout_complete_error_in_gateway_response_for_dummy_credit_card(
checkout.store_value_in_private_metadata(items={"accepted": "false"})
checkout.save()
- total = calculations.calculate_checkout_total(checkout=checkout)
+ total = calculations.calculate_checkout_total_with_gift_cards(checkout=checkout)
payment = payment_dummy_credit_card
payment.is_active = True
payment.order = None
@@ -2331,7 +2331,7 @@ def test_checkout_complete_without_redirect_url(
checkout_line_quantity = checkout_line.quantity
checkout_line_variant = checkout_line.variant
- total = calculations.calculate_checkout_total(checkout=checkout)
+ total = calculations.calculate_checkout_total_with_gift_cards(checkout=checkout)
payment = payment_dummy
payment.is_active = True
payment.order = None
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 91b30cd818..df06670695 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -2,7 +2,7 @@ import graphene
from django.conf import settings
from django.core.exceptions import ValidationError
-from ...checkout.calculations import calculate_checkout_total
+from ...checkout.calculations import calculate_checkout_total_with_gift_cards
from ...checkout.utils import cancel_active_payments
from ...core.permissions import OrderPermissions
from ...core.utils import get_client_ip
@@ -155,7 +155,9 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
cls.validate_token(info.context.plugins, gateway, data)
cls.validate_return_url(data)
- checkout_total = calculate_checkout_total(checkout, info.context.discounts)
+ checkout_total = calculate_checkout_total_with_gift_cards(
+ checkout, info.context.discounts
+ )
amount = data.get("amount", checkout_total.gross.amount)
clean_checkout_shipping(
checkout, list(checkout), info.context.discounts, PaymentErrorCode
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.py
index e631ba4daf..a6188e0f56 100644
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@ -143,7 +143,7 @@ class AdyenGatewayPlugin(BasePlugin):
"Notification user": {
"type": ConfigurationTypeField.STRING,
"help_text": (
- "Base User provided on the Adyen side for authenticate incoming "
+ "Base User provided on the Adyen side to authenticate incoming "
"notifications. https://docs.adyen.com/development-resources/webhooks#"
"set-up-notifications-in-your-customer-area "
"The Saleor webhook url is "
diff --git a/saleor/payment/gateways/adyen/tests/test_utils.py b/saleor/payment/gateways/adyen/tests/test_utils.py
index 7b5480bc58..a6e214d8b9 100644
--- a/saleor/payment/gateways/adyen/tests/test_utils.py
+++ b/saleor/payment/gateways/adyen/tests/test_utils.py
@@ -9,11 +9,11 @@ from .....core.prices import quantize_price
from .... import PaymentError
from ..utils import (
append_klarna_data,
- convert_adyen_price_format,
- get_price_amount,
+ from_adyen_price,
get_shopper_locale_value,
request_data_for_gateway_config,
request_data_for_payment,
+ to_adyen_price,
update_payment_with_action_required_data,
)
@@ -41,7 +41,7 @@ def test_append_klarna_data(dummy_payment_data, payment_dummy, checkout_with_ite
result = append_klarna_data(dummy_payment_data, payment_data)
# then
- total = get_price_amount(
+ total = to_adyen_price(
line.variant.price_amount * line.quantity, line.variant.currency
)
assert result == {
@@ -97,10 +97,10 @@ def test_append_klarna_data_tax_included(
"description": line.variant.product.description,
"quantity": line.quantity,
"id": line.variant.sku,
- "taxAmount": get_price_amount((gross - net).amount, "USD"),
+ "taxAmount": to_adyen_price((gross - net).amount, "USD"),
"taxPercentage": tax_percent,
- "amountExcludingTax": get_price_amount(net.amount, "USD"),
- "amountIncludingTax": get_price_amount(gross.amount, "USD"),
+ "amountExcludingTax": to_adyen_price(net.amount, "USD"),
+ "amountIncludingTax": to_adyen_price(gross.amount, "USD"),
}
],
}
@@ -147,7 +147,7 @@ def test_request_data_for_payment(dummy_payment_data):
# then
assert result == {
"amount": {
- "value": get_price_amount(
+ "value": to_adyen_price(
dummy_payment_data.amount, dummy_payment_data.currency
),
"currency": dummy_payment_data.currency,
@@ -181,7 +181,7 @@ def test_request_data_for_payment_append_klarna_data(
dummy_payment_data.data = data
klarna_result = {
"amount": {
- "value": get_price_amount(
+ "value": to_adyen_price(
dummy_payment_data.amount, dummy_payment_data.currency
),
"currency": dummy_payment_data.currency,
@@ -216,9 +216,9 @@ def test_request_data_for_payment_append_klarna_data(
(Decimal(51), "US", Decimal("0.51")),
],
)
-def test_convert_adyen_price_format(value, currency, expected_result):
+def test_from_adyen_price(value, currency, expected_result):
# when
- result = convert_adyen_price_format(value, currency)
+ result = from_adyen_price(value, currency)
# then
assert result == expected_result
@@ -232,9 +232,9 @@ def test_convert_adyen_price_format(value, currency, expected_result):
(Decimal(100), "US", "10000"),
],
)
-def test_get_price_amount(value, currency, expected_result):
+def test_to_adyen_price(value, currency, expected_result):
# when
- result = get_price_amount(value, currency)
+ result = to_adyen_price(value, currency)
# then
assert result == expected_result
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 b34dbda60e..c4b45846e6 100644
--- a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
+++ b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
@@ -6,7 +6,7 @@ import pytest
from ......order import OrderStatus
from ..... import ChargeStatus, TransactionKind
-from ...utils import get_price_amount
+from ...utils import to_adyen_price
from ...webhooks import (
create_new_transaction,
handle_authorization,
@@ -27,7 +27,7 @@ def test_handle_authorization(notification, adyen_plugin, payment_adyen_for_orde
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
handle_authorization(notification, config)
@@ -45,7 +45,7 @@ def test_handle_authorization_with_adyen_auto_capture(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
config.connection_params["adyen_auto_capture"] = True
@@ -64,7 +64,7 @@ def test_handle_authorization_with_auto_capture(
notification = notification(
psp_reference="853596537720508F",
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
config.auto_capture = True
@@ -88,7 +88,7 @@ def test_handle_authorization_with_adyen_auto_capture_and_payment_charged(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
config.connection_params["adyen_auto_capture"] = True
@@ -104,7 +104,7 @@ def test_handle_cancel(notification, adyen_plugin, payment_adyen_for_order):
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -127,7 +127,7 @@ def test_handle_cancel_already_canceleld(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
create_new_transaction(notification, payment, TransactionKind.CANCEL)
@@ -145,7 +145,7 @@ def test_handle_capture(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -172,7 +172,7 @@ def test_handle_capture_with_payment_already_charged(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -195,7 +195,7 @@ def test_handle_failed_capture(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -219,7 +219,7 @@ def test_handle_failed_capture_partial_charge(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -238,7 +238,7 @@ def test_handle_pending(notification, adyen_plugin, payment_adyen_for_order):
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -259,7 +259,7 @@ def test_handle_pending_with_adyen_auto_capture(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
config.connection_params["adyen_auto_capture"] = True
@@ -282,7 +282,7 @@ def test_handle_pending_already_pending(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
create_new_transaction(notification, payment, TransactionKind.PENDING)
@@ -303,7 +303,7 @@ def test_handle_refund(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -333,7 +333,7 @@ def test_handle_refund_already_refunded(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
create_new_transaction(notification, payment, TransactionKind.REFUND)
config = adyen_plugin().config
@@ -354,7 +354,7 @@ def test_handle_failed_refund_missing_transaction(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -373,7 +373,7 @@ def test_handle_failed_refund_with_transaction_refund_ongoing(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
create_new_transaction(notification, payment, TransactionKind.REFUND_ONGOING)
@@ -396,7 +396,7 @@ def test_handle_failed_refund_with_transaction_refund(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
create_new_transaction(notification, payment, TransactionKind.REFUND)
@@ -416,7 +416,7 @@ def test_handle_reversed_refund(notification, adyen_plugin, payment_adyen_for_or
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
handle_reversed_refund(notification, config)
@@ -437,7 +437,7 @@ def test_handle_reversed_refund_already_processed(
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
create_new_transaction(notification, payment, TransactionKind.REFUND_REVERSED)
@@ -455,7 +455,7 @@ def test_webhook_not_implemented(notification, adyen_plugin, payment_adyen_for_o
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
config = adyen_plugin().config
@@ -474,7 +474,7 @@ def test_handle_cancel_or_refund_action_refund(
config = adyen_plugin().config
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
notification["additionalData"]["modification.action"] = "refund"
@@ -492,7 +492,7 @@ def test_handle_cancel_or_refund_action_cancel(
config = adyen_plugin().config
notification = notification(
merchant_reference=payment_id,
- value=get_price_amount(payment.total, payment.currency),
+ value=to_adyen_price(payment.total, payment.currency),
)
notification["additionalData"]["modification.action"] = "cancel"
diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.py
index fb7f177484..b71e898005 100644
--- a/saleor/payment/gateways/adyen/utils.py
+++ b/saleor/payment/gateways/adyen/utils.py
@@ -25,14 +25,14 @@ PENDING_STATUSES = ["pending", "received"]
AUTH_STATUS = "authorised"
-def convert_adyen_price_format(value: str, currency: str):
+def from_adyen_price(value: str, currency: str):
value = Decimal(value)
precision = get_currency_precision(currency)
number_places = Decimal(10) ** -precision
return value * number_places
-def get_price_amount(value: Decimal, currency: str):
+def to_adyen_price(value: Decimal, currency: str):
"""Adyen doesn't use values with comma.
Take the value, discover the precision of currency and multiply value by
@@ -84,7 +84,7 @@ def request_data_for_payment(
request_data = {
"amount": {
- "value": get_price_amount(
+ "value": to_adyen_price(
payment_information.amount, payment_information.currency
),
"currency": payment_information.currency,
@@ -127,12 +127,12 @@ def append_klarna_data(payment_information: "PaymentData", payment_data: dict):
tax_amount = total.tax.amount
line_data = {
"quantity": line.quantity,
- "amountExcludingTax": get_price_amount(total_net, currency),
+ "amountExcludingTax": to_adyen_price(total_net, currency),
"taxPercentage": round(tax_amount / total_gross * 100),
"description": line.variant.product.description,
"id": line.variant.sku,
- "taxAmount": get_price_amount(tax_amount, currency),
- "amountIncludingTax": get_price_amount(total_gross, currency),
+ "taxAmount": to_adyen_price(tax_amount, currency),
+ "amountIncludingTax": to_adyen_price(total_gross, currency),
}
line_items.append(line_data)
payment_data["lineItems"] = line_items
@@ -183,7 +183,7 @@ def request_data_for_gateway_config(
"countryCode": country_code,
"channel": channel,
"amount": {
- "value": get_price_amount(total.gross.amount, checkout.currency),
+ "value": to_adyen_price(total.gross.amount, checkout.currency),
"currency": checkout.currency,
},
}
@@ -195,7 +195,7 @@ def request_for_payment_refund(
return {
"merchantAccount": merchant_account,
"modificationAmount": {
- "value": get_price_amount(
+ "value": to_adyen_price(
payment_information.amount, payment_information.currency
),
"currency": payment_information.currency,
@@ -211,7 +211,7 @@ def request_for_payment_capture(
return {
"merchantAccount": merchant_account,
"modificationAmount": {
- "value": get_price_amount(
+ "value": to_adyen_price(
payment_information.amount, payment_information.currency
),
"currency": payment_information.currency,
@@ -248,6 +248,8 @@ def call_capture(
token: str,
adyen_client: Adyen.Adyen,
):
+ # https://docs.adyen.com/checkout/capture#make-an-api-call-to-capture-a-payment
+
request = request_for_payment_capture(
payment_information=payment_information,
merchant_account=merchant_account,
diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.py
index 6d5eace3a4..690aeaa161 100644
--- a/saleor/payment/gateways/adyen/webhooks.py
+++ b/saleor/payment/gateways/adyen/webhooks.py
@@ -34,7 +34,7 @@ from ... import ChargeStatus, PaymentError, TransactionKind
from ...gateway import capture
from ...interface import GatewayConfig, GatewayResponse
from ...utils import create_payment_information, create_transaction, gateway_postprocess
-from .utils import FAILED_STATUSES, api_call, convert_adyen_price_format
+from .utils import FAILED_STATUSES, api_call, from_adyen_price
def get_payment(payment_id: Optional[str]) -> Optional[Payment]:
@@ -55,9 +55,7 @@ def get_transaction(
def create_new_transaction(notification, payment, kind):
transaction_id = notification.get("pspReference")
currency = notification.get("amount", {}).get("currency")
- amount = convert_adyen_price_format(
- notification.get("amount", {}).get("value"), currency
- )
+ amount = from_adyen_price(notification.get("amount", {}).get("value"), currency)
is_success = True if notification.get("success") == "true" else False
gateway_response = GatewayResponse(