Skip to content

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

Handle all Braintree errors

L3str4nge20f21c34aa9c
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.py
index 4817108385..375a5d1b4e 100644
--- a/saleor/payment/gateways/braintree/__init__.py
+++ b/saleor/payment/gateways/braintree/__init__.py
@@ -3,6 +3,7 @@ from typing import Dict, List, Optional
import braintree as braintree_sdk
import opentracing
import opentracing.tags
+from braintree.exceptions.braintree_error import BraintreeError
from django.core.exceptions import ImproperlyConfigured
from ... import TransactionKind
@@ -14,7 +15,7 @@ from ...interface import (
PaymentMethodInfo,
TokenConfig,
)
-from .errors import DEFAULT_ERROR_MESSAGE, BraintreeException
+from .errors import DEFAULT_ERROR_MESSAGE, BraintreeException, handle_braintree_error
# Error codes whitelist should be a dict of code: error_msg_override
# if no error_msg_override is provided,
@@ -139,8 +140,8 @@ def authorize(
result = transaction_for_new_customer(payment_information, config)
else:
result = transaction_for_existing_customer(payment_information, config)
- except braintree_sdk.exceptions.NotFoundError:
- raise BraintreeException(DEFAULT_ERROR_MESSAGE)
+ except BraintreeError as exc:
+ handle_braintree_error(exc)
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response["errors"])
@@ -187,18 +188,22 @@ def transaction_for_new_customer(
merchant_account_id = config.connection_params["merchant_account_id"]
if merchant_account_id:
params["merchant_account_id"] = merchant_account_id
- return gateway.transaction.sale(
- {
- "amount": str(payment_information.amount),
- "payment_method_nonce": payment_information.token,
- "options": {
- "submit_for_settlement": config.auto_capture,
- "store_in_vault_on_success": payment_information.reuse_source,
- "three_d_secure": {"required": config.require_3d_secure},
- },
- **params,
- }
- )
+
+ try:
+ return gateway.transaction.sale(
+ {
+ "amount": str(payment_information.amount),
+ "payment_method_nonce": payment_information.token,
+ "options": {
+ "submit_for_settlement": config.auto_capture,
+ "store_in_vault_on_success": payment_information.reuse_source,
+ "three_d_secure": {"required": config.require_3d_secure},
+ },
+ **params,
+ }
+ )
+ except BraintreeError as exc:
+ handle_braintree_error(exc)
def transaction_for_existing_customer(
@@ -215,14 +220,18 @@ def transaction_for_existing_customer(
merchant_account_id = config.connection_params["merchant_account_id"]
if merchant_account_id:
params["merchant_account_id"] = merchant_account_id
- return gateway.transaction.sale(
- {
- "amount": str(payment_information.amount),
- "customer_id": payment_information.customer_id,
- "options": {"submit_for_settlement": config.auto_capture},
- **params,
- }
- )
+
+ try:
+ return gateway.transaction.sale(
+ {
+ "amount": str(payment_information.amount),
+ "customer_id": payment_information.customer_id,
+ "options": {"submit_for_settlement": config.auto_capture},
+ **params,
+ }
+ )
+ except BraintreeError as exc:
+ handle_braintree_error(exc)
def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayResponse:
@@ -239,8 +248,8 @@ def capture(payment_information: PaymentData, config: GatewayConfig) -> GatewayR
transaction_id=payment_information.token,
amount=str(payment_information.amount),
)
- except braintree_sdk.exceptions.NotFoundError:
- raise BraintreeException(DEFAULT_ERROR_MESSAGE)
+ except BraintreeError as exc:
+ handle_braintree_error(exc)
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response["errors"])
@@ -270,8 +279,8 @@ def void(payment_information: PaymentData, config: GatewayConfig) -> GatewayResp
span.set_tag(opentracing.tags.COMPONENT, "payment")
span.set_tag("service.name", "braintree")
result = gateway.transaction.void(transaction_id=payment_information.token)
- except braintree_sdk.exceptions.NotFoundError:
- raise BraintreeException(DEFAULT_ERROR_MESSAGE)
+ except BraintreeError as exc:
+ handle_braintree_error(exc)
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response["errors"])
diff --git a/saleor/payment/gateways/braintree/errors.py b/saleor/payment/gateways/braintree/errors.py
index 753f7d37ee..e427d714d4 100644
--- a/saleor/payment/gateways/braintree/errors.py
+++ b/saleor/payment/gateways/braintree/errors.py
@@ -12,6 +12,16 @@
#
# For the full list of errors please refer to:
# https://developers.braintreepayments.com/reference/general/validation-errors/all/python#transaction
+from braintree.exceptions.authentication_error import AuthenticationError
+from braintree.exceptions.authorization_error import AuthorizationError
+from braintree.exceptions.gateway_timeout_error import GatewayTimeoutError
+from braintree.exceptions.not_found_error import NotFoundError
+from braintree.exceptions.request_timeout_error import RequestTimeoutError
+from braintree.exceptions.server_error import ServerError
+from braintree.exceptions.service_unavailable_error import ServiceUnavailableError
+from braintree.exceptions.too_many_requests_error import TooManyRequestsError
+from braintree.exceptions.upgrade_required_error import UpgradeRequiredError
+
from ... import TransactionError
INVALID_EXPIRY_DATE_ERRORS = [
@@ -117,3 +127,23 @@ DEFAULT_ERROR_MESSAGE = (
class BraintreeException(Exception):
pass
+
+
+BRAINTREE_ERROR_MESSAGES = {
+ AuthenticationError: "Authentication error",
+ AuthorizationError: "Authorization error",
+ NotFoundError: DEFAULT_ERROR_MESSAGE,
+ RequestTimeoutError: "Request timeout error",
+ UpgradeRequiredError: "Upgrade required error",
+ TooManyRequestsError: "Too many requests error",
+ ServerError: "Server error",
+ ServiceUnavailableError: "Server unavailable error",
+ GatewayTimeoutError: "Gateway timeout error",
+}
+
+
+def handle_braintree_error(exc):
+ msg = BRAINTREE_ERROR_MESSAGES.get(
+ type(exc), "Unexpected http response " + str(exc)
+ )
+ raise BraintreeException(msg)
diff --git a/saleor/payment/gateways/braintree/tests/test_errors.py b/saleor/payment/gateways/braintree/tests/test_errors.py
new file mode 100644
index 0000000000..3badea5ae5
--- /dev/null
+++ b/saleor/payment/gateways/braintree/tests/test_errors.py
@@ -0,0 +1,33 @@
+import pytest
+from braintree.exceptions.authentication_error import AuthenticationError
+from braintree.exceptions.authorization_error import AuthorizationError
+from braintree.exceptions.gateway_timeout_error import GatewayTimeoutError
+from braintree.exceptions.not_found_error import NotFoundError
+from braintree.exceptions.request_timeout_error import RequestTimeoutError
+from braintree.exceptions.server_error import ServerError
+from braintree.exceptions.service_unavailable_error import ServiceUnavailableError
+from braintree.exceptions.too_many_requests_error import TooManyRequestsError
+from braintree.exceptions.upgrade_required_error import UpgradeRequiredError
+from django.core.exceptions import ValidationError
+
+from saleor.payment.gateways.braintree import BraintreeException, handle_braintree_error
+
+
+ "braintree_error",
+ [
+ AuthenticationError,
+ AuthorizationError,
+ NotFoundError,
+ RequestTimeoutError,
+ UpgradeRequiredError,
+ TooManyRequestsError,
+ ServerError,
+ ServiceUnavailableError,
+ GatewayTimeoutError,
+ ValidationError, # ensure that we can handle all type of errors
+ ],
+)
+def test_handle_braintree_error(braintree_error):
+ with pytest.raises(BraintreeException):
+ handle_braintree_error(braintree_error("Message"))