Skip to content

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

Fix Braintree merchant accounts mismatch error (#9778)

ex59c3a7392828e
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.py
index 375a5d1b4e..3accb786bf 100644
--- a/saleor/payment/gateways/braintree/__init__.py
+++ b/saleor/payment/gateways/braintree/__init__.py
@@ -118,17 +118,23 @@ def get_client_token(
span = scope.span
span.set_tag(opentracing.tags.COMPONENT, "payment")
span.set_tag("service.name", "braintree")
- if not token_config:
- return gateway.client_token.generate()
parameters = create_token_params(config, token_config)
return gateway.client_token.generate(parameters)
-def create_token_params(config: GatewayConfig, token_config: TokenConfig) -> dict:
+def create_token_params(
+ config: GatewayConfig, token_config: Optional[TokenConfig] = None
+) -> dict:
params = {}
- customer_id = token_config.customer_id
- if customer_id and config.store_customer:
- params["customer_id"] = customer_id
+ if "merchant_account_id" in config.connection_params:
+ merchant_account_id = config.connection_params["merchant_account_id"]
+ if merchant_account_id:
+ params["merchant_account_id"] = merchant_account_id
+
+ if token_config:
+ customer_id = token_config.customer_id
+ if customer_id and config.store_customer:
+ params["customer_id"] = customer_id
return params
diff --git a/saleor/payment/gateways/braintree/tests/test_braintree.py b/saleor/payment/gateways/braintree/tests/test_braintree.py
index bf88138f83..f53b827ca3 100644
--- a/saleor/payment/gateways/braintree/tests/test_braintree.py
+++ b/saleor/payment/gateways/braintree/tests/test_braintree.py
@@ -190,7 +190,28 @@ def test_get_client_token(mock_gateway, gateway_config):
mock_gateway.return_value = Mock(client_token=Mock(generate=mock_generate))
token = get_client_token(gateway_config)
mock_gateway.assert_called_once_with(**gateway_config.connection_params)
- mock_generate.assert_called_once_with()
+ mock_generate.assert_called_once_with({})
+ assert token == expected_token
+
+
+def gateway_config_with_merchant_account_id(gateway_config):
+ gateway_config.connection_params["merchant_account_id"] = "foobar"
+ return gateway_config
+
+
+@patch("saleor.payment.gateways.braintree.get_braintree_gateway")
+def test_get_client_token_with_merchant_account_id(
+ mock_gateway, gateway_config_with_merchant_account_id
+):
+ expected_token = "client-token"
+ mock_generate = Mock(return_value=expected_token)
+ mock_gateway.return_value = Mock(client_token=Mock(generate=mock_generate))
+ token = get_client_token(gateway_config_with_merchant_account_id)
+ mock_gateway.assert_called_once_with(
+ **gateway_config_with_merchant_account_id.connection_params
+ )
+ mock_generate.assert_called_once_with({"merchant_account_id": "foobar"})
assert token == expected_token
@@ -217,6 +238,37 @@ def test_get_client_token_with_customer_id(
assert token == expected_token
+def gateway_config_with_merchant_account_id_and_store_enabled(
+ gateway_config_with_merchant_account_id,
+):
+ gateway_config_with_merchant_account_id.store_customer = True
+ return gateway_config_with_merchant_account_id
+
+
+@patch("saleor.payment.gateways.braintree.get_braintree_gateway")
+def test_get_client_token_with_customer_id_and_merchant_account_id(
+ mock_gateway, gateway_config_with_merchant_account_id_and_store_enabled
+):
+ expected_token = "client-token"
+ mock_generate = Mock(return_value=expected_token)
+ mock_gateway.return_value = Mock(client_token=Mock(generate=mock_generate))
+ token = get_client_token(
+ gateway_config_with_merchant_account_id_and_store_enabled,
+ TokenConfig(customer_id="1234"),
+ )
+ mock_gateway.assert_called_once_with(
+ **gateway_config_with_merchant_account_id_and_store_enabled.connection_params
+ )
+ mock_generate.assert_called_once_with(
+ {
+ "customer_id": "1234",
+ "merchant_account_id": "foobar",
+ }
+ )
+ assert token == expected_token
+
+
@patch("saleor.payment.gateways.braintree.get_braintree_gateway")
def test_get_client_token_with_no_customer_id_when_disabled(
mock_gateway, gateway_config