Skip to content

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

Add tests for payment_intent handlers

Mateusz Grzyb8cb3f8d81406
diff --git a/saleor/payment/gateways/stripe/stripe_api.py b/saleor/payment/gateways/stripe/stripe_api.py
index 0f2390446d..9adb65ebe9 100644
--- a/saleor/payment/gateways/stripe/stripe_api.py
+++ b/saleor/payment/gateways/stripe/stripe_api.py
@@ -178,19 +178,6 @@ def create_payment_intent(
return None, error
-def update_payment_method(api_key: str, payment_method_id: str, channel_slug: str):
- with stripe_opentracing_trace("stripe.PaymentMethod.modify"):
- try:
- stripe.PaymentMethod.modify(
- payment_method_id, api_key=api_key, metadata={"channel": channel_slug}
- )
- except StripeError as error:
- logger.warning(
- "Failed to assign channel slug to payment method",
- extra=_extra_log_data(error),
- )
-
-
def list_customer_payment_methods(
api_key: str, customer_id: str
) -> Tuple[Optional[StripeObject], Optional[StripeError]]:
@@ -319,5 +306,6 @@ def get_payment_method_details(
exp_month=exp_month,
brand=card_details.get("brand", ""),
type="card",
+ payment_metadata=payment_intent.get("metadata", {}),
)
return payment_method_info
diff --git a/saleor/payment/gateways/stripe/tests/test_stripe_api.py b/saleor/payment/gateways/stripe/tests/test_stripe_api.py
index d23f9a88fa..969773a1ea 100644
--- a/saleor/payment/gateways/stripe/tests/test_stripe_api.py
+++ b/saleor/payment/gateways/stripe/tests/test_stripe_api.py
@@ -491,11 +491,17 @@ def test_get_payment_method_details():
}
]
}
+ payment_intent.metadata = {"key": "value"}
payment_method_info = get_payment_method_details(payment_intent)
assert payment_method_info == PaymentMethodInfo(
- last_4="1234", exp_year=2222, exp_month=12, brand="visa", type="card"
+ last_4="1234",
+ exp_year=2222,
+ exp_month=12,
+ brand="visa",
+ type="card",
+ payment_metadata={"key": "value"},
)
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.py
index 3b50781299..6e9762a1c0 100644
--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py
+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py
@@ -115,13 +115,46 @@ def test_handle_successful_payment_intent_for_checkout(
# )
+ "metadata", [{f"key{i}": f"value{i}" for i in range(5)}, {}, None]
+)
+@patch(
+ "saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
+)
+def test_handle_successful_payment_intent_with_metadata(
+ _wrapped_checkout_complete,
+ payment_stripe_for_order,
+ stripe_plugin,
+ channel_USD,
+ metadata,
+):
+ # given
+ payment = payment_stripe_for_order
+ plugin = stripe_plugin()
+
+ payment_intent = StripeObject(id="token", last_response={})
+ payment_intent["amount_received"] = price_to_minor_unit(
+ payment.total, payment.currency
+ )
+ payment_intent["currency"] = payment.currency
+ if metadata is not None:
+ payment_intent["metadata"] = metadata
+ payment_intent["charges"] = {"data": [{"payment_method_details": {"type": "card"}}]}
+
+ # when
+ handle_successful_payment_intent(payment_intent, plugin.config, channel_USD.slug)
+
+ # then
+ payment.refresh_from_db()
+ assert payment.metadata == (metadata or {})
+
+
@patch(
"saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
)
def test_handle_successful_payment_intent_for_order(
wrapped_checkout_complete, payment_stripe_for_order, stripe_plugin, channel_USD
):
-
payment = payment_stripe_for_order
plugin = stripe_plugin()
payment_intent = StripeObject(id="ABC", last_response={})
@@ -243,7 +276,6 @@ def test_handle_authorized_payment_intent_for_order(
stripe_plugin,
channel_USD,
):
-
payment = payment_stripe_for_order
plugin = stripe_plugin()
payment_intent = StripeObject(id="ABC", last_response={})
@@ -278,6 +310,37 @@ def test_handle_authorized_payment_intent_for_processing_order_payment(
assert wrapped_checkout_complete.called is False
+ "metadata", [{f"key{i}": f"value{i}" for i in range(5)}, {}, None]
+)
+@patch(
+ "saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
+)
+def test_handle_authorized_payment_intent_with_metadata(
+ _wrapped_checkout_complete,
+ payment_stripe_for_order,
+ checkout_with_items,
+ stripe_plugin,
+ channel_USD,
+ metadata,
+):
+ # given
+ payment = payment_stripe_for_order
+ payment.charge_status = ChargeStatus.PENDING
+ plugin = stripe_plugin()
+ payment_intent = StripeObject(id="token", last_response={})
+ if metadata is not None:
+ payment_intent["metadata"] = metadata
+ payment_intent["charges"] = {"data": [{"payment_method_details": {"type": "card"}}]}
+
+ # when
+ handle_authorized_payment_intent(payment_intent, plugin.config, channel_USD.slug)
+
+ # then
+ payment.refresh_from_db()
+ assert payment.metadata == (metadata or {})
+
+
@patch(
"saleor.payment.gateways.stripe.webhooks.complete_checkout", wraps=complete_checkout
)
@@ -288,7 +351,6 @@ def test_handle_processing_payment_intent_for_order(
stripe_plugin,
channel_USD,
):
-
payment = payment_stripe_for_order
plugin = stripe_plugin()
payment_intent = StripeObject(id="ABC", last_response={})
diff --git a/saleor/payment/interface.py b/saleor/payment/interface.py
index d5a45ad4df..02f06c4043 100644
--- a/saleor/payment/interface.py
+++ b/saleor/payment/interface.py
@@ -18,7 +18,7 @@ class PaymentMethodInfo:
brand: Optional[str] = None
name: Optional[str] = None
type: Optional[str] = None
- payment_metadata: Optional[Dict[str, str]] = None
+ payment_metadata: Dict[str, str] = field(default_factory=dict)
@dataclass