Skip to content

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

Update code after code review

Mateusz Grzybb49fa2877e52
diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.py
index a797df576e..65f2658946 100644
--- a/saleor/payment/gateways/stripe/plugin.py
+++ b/saleor/payment/gateways/stripe/plugin.py
@@ -188,7 +188,7 @@ class StripeGatewayPlugin(BasePlugin):
payment_method_id = data.get("payment_method_id") if data else None
setup_future_usage = None
- # DEPRECATED
+ # DEPRECATED: reuse_source will be removed in Saleor 4.0
if payment_information.reuse_source:
setup_future_usage = data.get("setup_future_usage") if data else None
diff --git a/saleor/payment/gateways/stripe/stripe_api.py b/saleor/payment/gateways/stripe/stripe_api.py
index 1f42870491..bf4b638269 100644
--- a/saleor/payment/gateways/stripe/stripe_api.py
+++ b/saleor/payment/gateways/stripe/stripe_api.py
@@ -181,15 +181,14 @@ def create_payment_intent(
def update_payment_method(
api_key: str,
payment_method_id: str,
- channel_slug: str,
- metadata: Dict[str, str] = None,
+ metadata: Dict[str, str],
):
with stripe_opentracing_trace("stripe.PaymentMethod.modify"):
try:
stripe.PaymentMethod.modify(
payment_method_id,
api_key=api_key,
- metadata={**(metadata or {}), "channel": channel_slug},
+ metadata=metadata,
)
except StripeError as error:
logger.warning(
diff --git a/saleor/payment/gateways/stripe/tests/test_plugin_deprecated.py b/saleor/payment/gateways/stripe/tests/test_plugin_deprecated.py
index e04ad7bae5..6e6e59a491 100644
--- a/saleor/payment/gateways/stripe/tests/test_plugin_deprecated.py
+++ b/saleor/payment/gateways/stripe/tests/test_plugin_deprecated.py
@@ -1,12 +1,8 @@
from unittest.mock import Mock, patch
-from saleor.payment import TransactionKind
-from saleor.payment.gateways.stripe.consts import (
- AUTOMATIC_CAPTURE_METHOD,
- STRIPE_API_VERSION,
- SUCCESS_STATUS,
-)
-from saleor.payment.utils import create_payment_information, price_to_minor_unit
+from .... import TransactionKind
+from ....utils import create_payment_information, price_to_minor_unit
+from ..consts import AUTOMATIC_CAPTURE_METHOD, STRIPE_API_VERSION, SUCCESS_STATUS
@patch("saleor.payment.gateways.stripe.stripe_api.stripe.Customer.create")
diff --git a/saleor/payment/gateways/stripe/tests/test_stripe_api.py b/saleor/payment/gateways/stripe/tests/test_stripe_api.py
index 42cd3e9ad0..4f18ec8ad2 100644
--- a/saleor/payment/gateways/stripe/tests/test_stripe_api.py
+++ b/saleor/payment/gateways/stripe/tests/test_stripe_api.py
@@ -179,17 +179,16 @@ def test_update_payment_method(mocked_payment_method):
# given
api_key = "api_key"
payment_method_id = "1234"
- channel_slug = "channel_slug"
metadata = {"key": "value"}
# when
- update_payment_method(api_key, payment_method_id, channel_slug, metadata)
+ update_payment_method(api_key, payment_method_id, metadata)
# then
mocked_payment_method.modify.assert_called_once_with(
payment_method_id,
api_key=api_key,
- metadata={**metadata, "channel": channel_slug},
+ metadata=metadata,
)
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.py
index c1b4e0038d..f4901dc45c 100644
--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py
+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py
@@ -78,7 +78,8 @@ def test_handle_successful_payment_intent_for_checkout(
@pytest.mark.parametrize(
- "metadata", [{f"key{i}": f"value{i}" for i in range(5)}, {}, None]
+ ["metadata", "called"],
+ [({f"key{i}": f"value{i}" for i in range(5)}, True), ({}, False)],
)
@patch(
"saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
@@ -91,12 +92,12 @@ def test_handle_successful_payment_intent_with_metadata(
stripe_plugin,
channel_USD,
metadata,
+ called,
):
# given
payment = payment_stripe_for_order
current_metadata = {"currentkey": "currentvalue"}
- if metadata is not None:
- payment.metadata = metadata
+ payment.metadata = metadata
payment.charge_status = ChargeStatus.PENDING
payment.save()
plugin = stripe_plugin()
@@ -114,12 +115,14 @@ def test_handle_successful_payment_intent_with_metadata(
handle_successful_payment_intent(payment_intent, plugin.config, channel_USD.slug)
# then
- wrapped_update_payment_method.assert_called_once_with(
- plugin.config.connection_params["secret_api_key"],
- payment_intent.payment_method,
- channel_USD.slug,
- metadata or {},
- )
+ if not called:
+ assert wrapped_update_payment_method.call_count == 0
+ else:
+ wrapped_update_payment_method.assert_called_once_with(
+ plugin.config.connection_params["secret_api_key"],
+ payment_intent.payment_method,
+ metadata,
+ )
@patch(
@@ -385,7 +388,9 @@ def test_handle_authorized_payment_intent_for_processing_order_payment(
assert wrapped_checkout_complete.called is False
[email protected]("metadata", [{"key": "value"}, {}, None])
+ ["metadata", "called"], [({"key": "value"}, True), ({}, False)]
+)
@patch(
"saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
)
@@ -398,12 +403,12 @@ def test_handle_authorized_payment_intent_with_metadata(
stripe_plugin,
channel_USD,
metadata,
+ called,
):
# given
payment = payment_stripe_for_order
current_metadata = {"currentkey": "currentvalue"}
- if metadata is not None:
- payment.metadata = metadata
+ payment.metadata = metadata
payment.charge_status = ChargeStatus.PENDING
payment.save()
plugin = stripe_plugin()
@@ -418,12 +423,14 @@ def test_handle_authorized_payment_intent_with_metadata(
handle_authorized_payment_intent(payment_intent, plugin.config, channel_USD.slug)
# then
- wrapped_update_payment_method.assert_called_with(
- plugin.config.connection_params["secret_api_key"],
- payment_intent.payment_method,
- channel_USD.slug,
- metadata or {},
- )
+ if not called:
+ assert wrapped_update_payment_method.call_count == 0
+ else:
+ wrapped_update_payment_method.assert_called_with(
+ plugin.config.connection_params["secret_api_key"],
+ payment_intent.payment_method,
+ metadata,
+ )
@pytest.mark.parametrize("called", [True, False])
diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.py
index 3fb4aef6a1..08599d1acf 100644
--- a/saleor/payment/gateways/stripe/webhooks.py
+++ b/saleor/payment/gateways/stripe/webhooks.py
@@ -209,14 +209,12 @@ def _update_payment_method_metadata(
payment: Payment,
payment_intent: StripeObject,
gateway_config: "GatewayConfig",
- channel_slug: str,
) -> None:
api_key = gateway_config.connection_params["secret_api_key"]
metadata = payment.metadata
- update_payment_method(
- api_key, payment_intent.payment_method, channel_slug, metadata
- )
+ if metadata:
+ update_payment_method(api_key, payment_intent.payment_method, metadata)
def handle_authorized_payment_intent(
@@ -234,9 +232,7 @@ def handle_authorized_payment_intent(
if _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment):
return
- _update_payment_method_metadata(
- payment, payment_intent, gateway_config, channel_slug
- )
+ _update_payment_method_metadata(payment, payment_intent, gateway_config)
if payment.order_id:
if payment.charge_status == ChargeStatus.PENDING:
@@ -331,9 +327,7 @@ def handle_successful_payment_intent(
if _channel_slug_is_different_from_payment_channel_slug(channel_slug, payment):
return
- _update_payment_method_metadata(
- payment, payment_intent, gateway_config, channel_slug
- )
+ _update_payment_method_metadata(payment, payment_intent, gateway_config)
if payment.order_id:
if payment.charge_status in [ChargeStatus.PENDING, ChargeStatus.NOT_CHARGED]: