Skip to content

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

Improve aproval rate for adyen requests (#6315)

Maciej Korycinski90e4703f50e7
diff --git a/saleor/payment/gateways/adyen/tests/test_plugin.py b/saleor/payment/gateways/adyen/tests/test_plugin.py
index 2fc4ec9582..6972902ac7 100644
--- a/saleor/payment/gateways/adyen/tests/test_plugin.py
+++ b/saleor/payment/gateways/adyen/tests/test_plugin.py
@@ -10,6 +10,44 @@ from ....models import Payment
from ....utils import create_payment_information, create_transaction
[email protected]("saleor.payment.gateways.adyen.plugin.api_call")
+def test_process_additional_action(
+ mocked_api_call,
+ dummy_payment_data,
+ payment_dummy,
+ checkout_ready_to_complete,
+ adyen_plugin,
+):
+ expected_message = {"resultCode": "authorised", "pspReference": "ref-id"}
+ mocked_app_response = mock.MagicMock(message=expected_message)
+
+ mocked_api_call.return_value = mocked_app_response
+ plugin = adyen_plugin(auto_capture=False)
+ dummy_payment_data.data = {
+ "additional-data": "payment-data",
+ }
+
+ kind = TransactionKind.AUTH
+ response = plugin._process_additional_action(dummy_payment_data, kind)
+
+ assert response == GatewayResponse(
+ is_success=True,
+ action_required=False,
+ action_required_data=None,
+ kind=kind,
+ amount=dummy_payment_data.amount,
+ currency=dummy_payment_data.currency,
+ transaction_id="ref-id",
+ error=None,
+ raw_response=expected_message,
+ searchable_key="ref-id",
+ payment_method_info=PaymentMethodInfo(),
+ )
+ mocked_api_call.assert_called_with(
+ dummy_payment_data.data, plugin.adyen.checkout.payments_details
+ )
+
+
@pytest.mark.vcr
def test_get_payment_gateway_for_checkout(
adyen_plugin, checkout_with_single_item, address
diff --git a/saleor/payment/gateways/adyen/tests/test_utils.py b/saleor/payment/gateways/adyen/tests/test_utils.py
index 3b095161cf..2766fb2ca9 100644
--- a/saleor/payment/gateways/adyen/tests/test_utils.py
+++ b/saleor/payment/gateways/adyen/tests/test_utils.py
@@ -31,13 +31,16 @@ def test_get_shopper_locale_value(country_code, shopper_locale, settings):
assert result == shopper_locale
-def test_append_klarna_data(dummy_payment_data, payment_dummy, checkout_with_item):
+def test_append_klarna_data(
+ dummy_payment_data, payment_dummy, checkout_ready_to_complete
+):
# given
- checkout_with_item.payments.add(payment_dummy)
- line = checkout_with_item.lines.first()
+ checkout_ready_to_complete.payments.add(payment_dummy)
+ line = checkout_ready_to_complete.lines.first()
payment_data = {
"reference": "test",
}
+ country_code = checkout_ready_to_complete.get_country()
# when
result = append_klarna_data(dummy_payment_data, payment_data)
@@ -48,10 +51,9 @@ def test_append_klarna_data(dummy_payment_data, payment_dummy, checkout_with_ite
)
assert result == {
"reference": "test",
- "shopperLocale": "en_US",
+ "shopperLocale": get_shopper_locale_value(country_code),
"shopperReference": dummy_payment_data.customer_email,
- "countryCode": str(checkout_with_item.country),
- "shopperEmail": dummy_payment_data.customer_email,
+ "countryCode": country_code,
"lineItems": [
{
"description": f"{line.variant.product.name}, {line.variant.name}",
@@ -61,14 +63,26 @@ def test_append_klarna_data(dummy_payment_data, payment_dummy, checkout_with_ite
"taxPercentage": 0,
"amountExcludingTax": total,
"amountIncludingTax": total,
- }
+ },
+ {
+ "amountExcludingTax": "1000",
+ "amountIncludingTax": "1000",
+ "description": "Shipping - DHL",
+ "id": f"Shipping:{checkout_ready_to_complete.shipping_method.id}",
+ "quantity": 1,
+ "taxAmount": "0",
+ "taxPercentage": 0,
+ },
],
}
@mock.patch("saleor.payment.gateways.adyen.utils.checkout_line_total")
def test_append_klarna_data_tax_included(
- mocked_checkout_line_total, dummy_payment_data, payment_dummy, checkout_with_item
+ mocked_checkout_line_total,
+ dummy_payment_data,
+ payment_dummy,
+ checkout_ready_to_complete,
):
# given
net = Money(100, "USD")
@@ -77,9 +91,10 @@ def test_append_klarna_data_tax_included(
mocked_checkout_line_total.return_value = quantize_price(
TaxedMoney(net=net, gross=gross), "USD"
)
+ country_code = checkout_ready_to_complete.get_country()
- checkout_with_item.payments.add(payment_dummy)
- line = checkout_with_item.lines.first()
+ checkout_ready_to_complete.payments.add(payment_dummy)
+ line = checkout_ready_to_complete.lines.first()
payment_data = {
"reference": "test",
}
@@ -88,12 +103,12 @@ def test_append_klarna_data_tax_included(
result = append_klarna_data(dummy_payment_data, payment_data)
# then
- assert result == {
+
+ expected_result = {
"reference": "test",
- "shopperLocale": "en_US",
+ "shopperLocale": get_shopper_locale_value(country_code),
"shopperReference": dummy_payment_data.customer_email,
- "countryCode": str(checkout_with_item.country),
- "shopperEmail": dummy_payment_data.customer_email,
+ "countryCode": country_code,
"lineItems": [
{
"description": f"{line.variant.product.name}, {line.variant.name}",
@@ -103,9 +118,19 @@ def test_append_klarna_data_tax_included(
"taxPercentage": 2300,
"amountExcludingTax": to_adyen_price(net.amount, "USD"),
"amountIncludingTax": to_adyen_price(gross.amount, "USD"),
- }
+ },
+ {
+ "amountExcludingTax": "1000",
+ "amountIncludingTax": "1000",
+ "description": "Shipping - DHL",
+ "id": f"Shipping:{checkout_ready_to_complete.shipping_method.id}",
+ "quantity": 1,
+ "taxAmount": "0",
+ "taxPercentage": 0,
+ },
],
}
+ assert result == expected_result
def test_request_data_for_payment_payment_not_valid(dummy_payment_data):
@@ -168,6 +193,7 @@ def test_request_data_for_payment(dummy_payment_data):
"billingAddress": data["billingAddress"],
"browserInfo": data["browserInfo"],
"channel": "web",
+ "shopperEmail": "[email protected]",
}
@@ -211,6 +237,7 @@ def test_request_data_for_payment_native_3d_secure(dummy_payment_data):
"browserInfo": data["browserInfo"],
"channel": "web",
"additionalData": {"allow3DS2": "true"},
+ "shopperEmail": "[email protected]",
}
@@ -241,6 +268,7 @@ def test_request_data_for_payment_channel_different_than_web(dummy_payment_data)
"merchantAccount": merchant_account,
"channel": "iOS",
"additionalData": {"allow3DS2": "true"},
+ "shopperEmail": "[email protected]",
}
@@ -278,7 +306,6 @@ def test_request_data_for_payment_append_klarna_data(
"billingAddress": data["billingAddress"],
"browserInfo": data["browserInfo"],
"shopperLocale": "test_shopper",
- "shopperEmail": "test_email",
}
append_klarna_data_mock.return_value = klarna_result
native_3d_secure = False
diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.py
index 86b0751ae6..599d6aebc1 100644
--- a/saleor/payment/gateways/adyen/utils.py
+++ b/saleor/payment/gateways/adyen/utils.py
@@ -8,7 +8,11 @@ from babel.numbers import get_currency_precision
from django.conf import settings
from django_countries.fields import Country
-from ....checkout.calculations import checkout_line_total, checkout_total
+from ....checkout.calculations import (
+ checkout_line_total,
+ checkout_shipping_price,
+ checkout_total,
+)
from ....checkout.models import Checkout
from ....core.prices import quantize_price
from ....discount.utils import fetch_active_discounts
@@ -45,6 +49,17 @@ def to_adyen_price(value: Decimal, currency: str):
return str(value_without_comma.quantize(Decimal("1")))
+def get_tax_percentage_in_adyen_format(total_gross, total_net):
+ tax_percentage_in_adyen_format = 0
+ if total_gross and total_net:
+ # get tax percent in adyen format
+ gross_percentage = total_gross / total_net
+ gross_percentage = gross_percentage.quantize(Decimal(".01")) # 1.23
+ tax_percentage = gross_percentage * 100 - 100 # 23.00
+ tax_percentage_in_adyen_format = int(tax_percentage * 100) # 2300
+ return tax_percentage_in_adyen_format
+
+
def api_call(request_data: Optional[Dict[str, Any]], method: Callable) -> Adyen.Adyen:
try:
return method(request_data)
@@ -71,22 +86,41 @@ def request_data_for_payment(
browser_info = payment_data.get("browserInfo")
if browser_info:
extra_request_params["browserInfo"] = browser_info
- if "billingAddress" in payment_data:
- extra_request_params["billingAddress"] = payment_data["billingAddress"]
- if "shopperIP" in payment_data:
- extra_request_params["shopperIP"] = payment_data["shopperIP"]
+
+ billing_address = payment_data.get("billingAddress")
+ if billing_address:
+ extra_request_params["billingAddress"] = billing_address
+
+ delivery_address = payment_data.get("deliveryAddress")
+ if delivery_address:
+ extra_request_params["deliveryAddress"] = delivery_address
+
+ shopper_ip = payment_data.get("shopperIP")
+ if shopper_ip:
+ extra_request_params["shopperIP"] = shopper_ip
+
+ device_fingerprint = payment_data.get("deviceFingerprint")
+ if device_fingerprint:
+ extra_request_params["deviceFingerprint"] = device_fingerprint
if channel.lower() == "web" and origin_url:
extra_request_params["origin"] = origin_url
+ shopper_name = payment_data.get("shopperName")
+ if shopper_name:
+ extra_request_params["shopperName"] = shopper_name
+
extra_request_params["channel"] = channel
- if native_3d_secure:
- extra_request_params["additionalData"] = {"allow3DS2": "true"}
payment_method = payment_data.get("paymentMethod")
if not payment_method:
raise PaymentError("Unable to find the paymentMethod section.")
+ method = payment_method.get("type", "")
+ if native_3d_secure and "scheme" == method:
+ extra_request_params["additionalData"] = {"allow3DS2": "true"}
+
+ extra_request_params["shopperEmail"] = payment_information.customer_email
request_data = {
"amount": {
"value": to_adyen_price(
@@ -101,21 +135,43 @@ def request_data_for_payment(
**extra_request_params,
}
- method = payment_method.get("type", [])
if "klarna" in method:
request_data = append_klarna_data(payment_information, request_data)
return request_data
+def get_shipping_data(checkout, lines, discounts):
+ shipping_total = checkout_shipping_price(
+ checkout=checkout, lines=lines, discounts=discounts
+ )
+ total_gross = shipping_total.gross.amount
+ total_net = shipping_total.net.amount
+ tax_amount = shipping_total.tax.amount
+ tax_percentage_in_adyen_format = get_tax_percentage_in_adyen_format(
+ total_gross, total_net
+ )
+ return {
+ "quantity": 1,
+ "amountExcludingTax": to_adyen_price(total_net, checkout.currency),
+ "taxPercentage": tax_percentage_in_adyen_format,
+ "description": f"Shipping - {checkout.shipping_method.name}",
+ "id": f"Shipping:{checkout.shipping_method.id}",
+ "taxAmount": to_adyen_price(tax_amount, checkout.currency),
+ "amountIncludingTax": to_adyen_price(total_gross, checkout.currency),
+ }
+
+
def append_klarna_data(payment_information: "PaymentData", payment_data: dict):
- checkout = Checkout.objects.filter(
- payments__id=payment_information.payment_id
- ).first()
+ checkout = (
+ Checkout.objects.prefetch_related("shipping_method",)
+ .filter(payments__id=payment_information.payment_id)
+ .first()
+ )
if not checkout:
raise PaymentError("Unable to calculate products for klarna.")
- lines = checkout.lines.prefetch_related("variant").all()
+ lines = checkout.lines.prefetch_related("variant__product").all()
discounts = fetch_active_discounts()
currency = payment_information.currency
country_code = checkout.get_country()
@@ -123,21 +179,16 @@ def append_klarna_data(payment_information: "PaymentData", payment_data: dict):
payment_data["shopperLocale"] = get_shopper_locale_value(country_code)
payment_data["shopperReference"] = payment_information.customer_email
payment_data["countryCode"] = country_code
- payment_data["shopperEmail"] = payment_information.customer_email
line_items = []
+
for line in lines:
total = checkout_line_total(line=line, discounts=discounts)
total_gross = total.gross.amount
total_net = total.net.amount
tax_amount = total.tax.amount
-
- tax_percentage_in_adyen_format = 0
- if total_gross:
- # get tax percent in adyen format
- gross_percentage = total_gross / total_net
- gross_percentage = gross_percentage.quantize(Decimal(".01")) # 1.23
- tax_percentage = gross_percentage * 100 - 100 # 23.00
- tax_percentage_in_adyen_format = int(tax_percentage * 100) # 2300
+ tax_percentage_in_adyen_format = get_tax_percentage_in_adyen_format(
+ total_gross, total_net
+ )
line_data = {
"quantity": line.quantity,
@@ -149,6 +200,10 @@ def append_klarna_data(payment_information: "PaymentData", payment_data: dict):
"amountIncludingTax": to_adyen_price(total_gross, currency),
}
line_items.append(line_data)
+
+ if checkout.shipping_method and checkout.is_shipping_required():
+ line_items.append(get_shipping_data(checkout, lines, discounts))
+
payment_data["lineItems"] = line_items
return payment_data