Skip to content

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

Order is no longer created when the payment was unsuccessful in the API

NyanKiyoshi2981de629def
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8254519d9a..b46261b9de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix searches and pickers - #4487 by @dominik-zeglen
- Fix dashboard menu styles - #4491 by @benekex2
- Do not allow random ids to appear in snapshots - #4495 by @dominik-zeglen
+- Order is no longer created when the payment was unsuccessful in the API - #4500 by @NyanKiyoshi
## 2.8.0
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index f7bd905723..611a507ca0 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -548,6 +548,9 @@ class CheckoutComplete(BaseMutation):
if txn.is_success and txn.customer_id and user.is_authenticated:
store_customer_id(user, payment.gateway, txn.customer_id)
+ if not txn.is_success:
+ raise PaymentError(txn.error)
+
except PaymentError as e:
abort_order_data(order_data)
raise ValidationError(str(e))
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index 84b0f63822..0c961b465e 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -17,6 +17,7 @@ from saleor.graphql.checkout.mutations import (
from saleor.graphql.core.utils import str_to_enum
from saleor.order.models import Order
from saleor.payment import PaymentError
+from saleor.payment.models import Transaction
from saleor.shipping import ShippingMethodType
from saleor.shipping.models import ShippingMethod
from tests.api.utils import get_graphql_content
@@ -1048,6 +1049,18 @@ def test_checkout_complete(
).exists(), "Checkout should have been deleted"
+def _process_payment_raise_error(*args, **kwargs):
+ raise PaymentError("Oops! Something went wrong.")
+
+
+def _process_payment_transaction_returns_error(*args, **kwargs):
+ return Transaction(error="Oops! Something went wrong.", is_success=False)
+
+
+ "side_effect",
+ (_process_payment_raise_error, _process_payment_transaction_returns_error),
+)
@patch("saleor.graphql.checkout.mutations.gateway_process_payment")
def test_checkout_complete_does_not_delete_checkout_after_unsuccessful_payment(
mocked_process_payment,
@@ -1057,12 +1070,10 @@ def test_checkout_complete_does_not_delete_checkout_after_unsuccessful_payment(
payment_dummy,
address,
shipping_method,
+ side_effect,
):
- def _process_payment(*args, **kwargs):
- raise PaymentError("Oops! Something went wrong.")
-
expected_voucher_usage_count = voucher.used
- mocked_process_payment.side_effect = _process_payment
+ mocked_process_payment.side_effect = side_effect
checkout = checkout_with_voucher
checkout.shipping_address = address