Skip to content

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

Fix stripe api breaking change in plugin. (#14951)

Krzysztof Waliczek692be3196ab4
diff --git a/saleor/payment/gateways/stripe/tests/test_webhooks.py b/saleor/payment/gateways/stripe/tests/test_webhooks.py
index 88cd1629bd..01cbb35d43 100644
--- a/saleor/payment/gateways/stripe/tests/test_webhooks.py
+++ b/saleor/payment/gateways/stripe/tests/test_webhooks.py
@@ -1170,6 +1170,50 @@ def test_handle_refund_already_processed(
assert payment.captured_amount == payment.total - Decimal("10")
+@patch("saleor.payment.gateways.stripe.webhooks.stripe.Charge.retrieve")
+def test_handle_refund_missing_refunds(
+ charge_retrieve, stripe_plugin, payment_stripe_for_order, channel_USD
+):
+ # given
+ payment = payment_stripe_for_order
+ payment.captured_amount = payment.total
+ payment.save()
+ payment.transactions.create(
+ is_success=True,
+ action_required=True,
+ kind=TransactionKind.CAPTURE,
+ amount=payment.total,
+ currency=payment.currency,
+ token="ABC",
+ gateway_response={},
+ )
+ plugin = stripe_plugin()
+
+ charge = StripeObject()
+ charge["payment_intent"] = "ABC"
+
+ refund = StripeObject(id="refund_id")
+ refund["amount"] = price_to_minor_unit(payment.total, payment.currency)
+ refund["currency"] = payment.currency
+ refund["last_response"] = None
+
+ charge_retrieve_obj = charge
+ charge_retrieve_obj["refunds"] = StripeObject()
+ charge_retrieve_obj["refunds"]["data"] = [refund]
+
+ charge_retrieve.return_value = charge_retrieve_obj
+
+ # when
+ handle_refund(charge, plugin.config, channel_USD.slug)
+
+ # then
+ payment.refresh_from_db()
+
+ assert payment.charge_status == ChargeStatus.FULLY_REFUNDED
+ assert payment.is_active is False
+ assert payment.captured_amount == Decimal("0")
+
+
@pytest.mark.parametrize("called", [True, False])
@patch(
"saleor.payment.gateways.stripe.webhooks._update_payment_with_new_transaction",
diff --git a/saleor/payment/gateways/stripe/webhooks.py b/saleor/payment/gateways/stripe/webhooks.py
index 8f181d0f8d..7f5acb6938 100644
--- a/saleor/payment/gateways/stripe/webhooks.py
+++ b/saleor/payment/gateways/stripe/webhooks.py
@@ -1,6 +1,7 @@
import logging
from typing import Optional, cast
+import stripe
from django.core.exceptions import ValidationError
from django.core.handlers.wsgi import WSGIRequest
from django.db.models import Prefetch
@@ -496,11 +497,21 @@ def handle_successful_payment_intent(
def handle_refund(
- charge: StripeObject, _gateway_config: "GatewayConfig", channel_slug: str
+ charge: StripeObject, gateway_config: "GatewayConfig", channel_slug: str
):
payment_intent_id = charge.payment_intent
payment = _get_payment(payment_intent_id)
+ # stripe introduced breaking change and in newer version of api
+ # charge object doesn't contain refunds by default
+ if not getattr(charge, "refunds", None):
+ api_key = gateway_config.connection_params["secret_api_key"]
+ charge_with_refunds = stripe.Charge.retrieve(
+ charge.stripe_id,
+ api_key=api_key,
+ expand=["refunds"],
+ )
+ charge.refunds = charge_with_refunds.refunds
refund = charge.refunds.data[0]
if not payment:
logger.warning(