Skip to content

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

Fix inccorect handling of last response object

Maciej Korycinskie87435f3e0ba
diff --git a/saleor/payment/gateways/stripe/plugin.py b/saleor/payment/gateways/stripe/plugin.py
index 7538b185f0..c0d292beff 100644
--- a/saleor/payment/gateways/stripe/plugin.py
+++ b/saleor/payment/gateways/stripe/plugin.py
@@ -211,14 +211,17 @@ class StripeGatewayPlugin(BasePlugin):
customer_email=payment_information.customer_email,
)
- if error and payment_method_id and not intent:
- # we can receive an error which is caused by a required authentication
- # but stripe already created payment_intent.
- stripe_error = error.error
- intent = getattr(stripe_error, "payment_intent", None)
- error = None if intent else error
-
raw_response = None
+ if error:
+ raw_response = getattr(error, "json_body", None)
+ if payment_method_id and not intent:
+ # we can receive an error which is caused by a required authentication
+ # but stripe already created payment_intent.
+ if error.code == "authentication_required":
+ stripe_error = error.error
+ intent = getattr(stripe_error, "payment_intent", None)
+ error = None if intent else error
+
client_secret = None
intent_id = None
kind = TransactionKind.ACTION_TO_CONFIRM
@@ -228,7 +231,8 @@ class StripeGatewayPlugin(BasePlugin):
intent.status
)
client_secret = intent.client_secret
- raw_response = intent.last_response.data
+ last_response = intent.last_response
+ raw_response = last_response.data if last_response else None
intent_id = intent.id
return GatewayResponse(
diff --git a/saleor/payment/gateways/stripe/tests/test_plugin.py b/saleor/payment/gateways/stripe/tests/test_plugin.py
index 2229e3c6fb..0ad6810eb2 100644
--- a/saleor/payment/gateways/stripe/tests/test_plugin.py
+++ b/saleor/payment/gateways/stripe/tests/test_plugin.py
@@ -595,7 +595,7 @@ def test_process_payment_offline(
@patch("saleor.payment.gateways.stripe.stripe_api.stripe.Customer.create")
@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.create")
-def test_process_payment_with_customer_and_payment_method_raises_card_error(
+def test_process_payment_with_customer_and_payment_method_raises_authentication_error(
mocked_payment_intent,
mocked_customer_create,
stripe_plugin,
@@ -609,6 +609,7 @@ def test_process_payment_with_customer_and_payment_method_raises_card_error(
payment_intent = Mock()
stripe_error_object = StripeError()
stripe_error_object.error = StripeError()
+ stripe_error_object.code = "authentication_required"
stripe_error_object.error.payment_intent = payment_intent
mocked_payment_intent.side_effect = stripe_error_object
@@ -674,6 +675,86 @@ def test_process_payment_with_customer_and_payment_method_raises_card_error(
)
+@patch("saleor.payment.gateways.stripe.stripe_api.stripe.Customer.create")
+@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.create")
+def test_process_payment_with_customer_and_payment_method_raises_error(
+ mocked_payment_intent,
+ mocked_customer_create,
+ stripe_plugin,
+ payment_stripe_for_checkout,
+ channel_USD,
+ customer_user,
+):
+ customer = Mock()
+ mocked_customer_create.return_value = customer
+
+ payment_intent = Mock()
+ stripe_error_object = StripeError(
+ message="Card declined", json_body={"error": "body"}
+ )
+ stripe_error_object.error = StripeError()
+ stripe_error_object.code = "card_declined"
+ stripe_error_object.error.payment_intent = payment_intent
+ mocked_payment_intent.side_effect = stripe_error_object
+
+ client_secret = "client-secret"
+ dummy_response = {
+ "id": "evt_1Ip9ANH1Vac4G4dbE9ch7zGS",
+ }
+ payment_intent_id = "payment-intent-id"
+ payment_intent.id = payment_intent_id
+ payment_intent.client_secret = client_secret
+ payment_intent.last_response.data = dummy_response
+
+ plugin = stripe_plugin(auto_capture=True)
+
+ payment_stripe_for_checkout.checkout.user = customer_user
+ payment_stripe_for_checkout.checkout.email = customer_user.email
+
+ payment_info = create_payment_information(
+ payment_stripe_for_checkout,
+ customer_id=None,
+ store_source=True,
+ additional_data={"payment_method_id": "pm_ID", "off_session": True},
+ )
+
+ response = plugin.process_payment(payment_info, None)
+
+ assert response.is_success is False
+ assert response.action_required is True
+ assert response.kind == TransactionKind.ACTION_TO_CONFIRM
+ assert response.amount == payment_info.amount
+ assert response.currency == payment_info.currency
+ assert not response.transaction_id
+ assert response.error == "Card declined"
+ assert response.raw_response == {"error": "body"}
+ assert response.action_required_data == {"client_secret": None, "id": None}
+
+ api_key = plugin.config.connection_params["secret_api_key"]
+ mocked_payment_intent.assert_called_once_with(
+ api_key=api_key,
+ amount=price_to_minor_unit(payment_info.amount, payment_info.currency),
+ currency=payment_info.currency,
+ capture_method=AUTOMATIC_CAPTURE_METHOD,
+ customer=customer,
+ payment_method="pm_ID",
+ confirm=True,
+ off_session=True,
+ metadata={
+ "channel": channel_USD.slug,
+ "payment_id": payment_info.graphql_payment_id,
+ },
+ receipt_email=payment_stripe_for_checkout.checkout.email,
+ stripe_version=STRIPE_API_VERSION,
+ )
+
+ mocked_customer_create.assert_called_once_with(
+ api_key="secret_key",
+ email=customer_user.email,
+ stripe_version=STRIPE_API_VERSION,
+ )
+
+
@patch("saleor.payment.gateways.stripe.stripe_api.stripe.PaymentIntent.create")
def test_process_payment_with_disabled_order_auto_confirmation(
mocked_payment_intent,
@@ -773,7 +854,9 @@ def test_process_payment_with_manual_capture(
def test_process_payment_with_error(
mocked_payment_intent, stripe_plugin, payment_stripe_for_checkout, channel_USD
):
- mocked_payment_intent.side_effect = StripeError(message="stripe-error")
+ mocked_payment_intent.side_effect = StripeError(
+ message="stripe-error", json_body={"error": "body"}
+ )
plugin = stripe_plugin()
@@ -790,7 +873,7 @@ def test_process_payment_with_error(
assert response.currency == payment_info.currency
assert response.transaction_id == ""
assert response.error == "stripe-error"
- assert response.raw_response is None
+ assert response.raw_response == {"error": "body"}
assert response.action_required_data == {"client_secret": None, "id": None}
api_key = plugin.config.connection_params["secret_api_key"]