Skip to content

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

Cancel active checkout payments before creating new payment

IKarbowiak4c95b48bfc5a
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 189fcc81e9..82a0bc743f 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -3,6 +3,7 @@ from django.conf import settings
from django.core.exceptions import ValidationError
from ...checkout import calculations
+from ...checkout.utils import cancel_active_payments
from ...core.permissions import OrderPermissions
from ...core.taxes import zero_taxed_money
from ...core.utils import get_client_ip
@@ -140,6 +141,8 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
cls.clean_payment_amount(info, checkout_total, amount)
extra_data = {"customer_user_agent": info.context.META.get("HTTP_USER_AGENT")}
+ cancel_active_payments(checkout)
+
payment = create_payment(
gateway=gateway,
payment_token=data["token"],
@@ -150,7 +153,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
customer_ip_address=get_client_ip(info.context),
checkout=checkout,
)
- return CheckoutPaymentCreate(payment=payment)
+ return CheckoutPaymentCreate(payment=payment, checkout=checkout)
class PaymentCapture(BaseMutation):
diff --git a/saleor/graphql/payment/tests/test_payment.py b/saleor/graphql/payment/tests/test_payment.py
index 0ac78af507..8054b70b7f 100644
--- a/saleor/graphql/payment/tests/test_payment.py
+++ b/saleor/graphql/payment/tests/test_payment.py
@@ -73,7 +73,7 @@ def test_payment_void_gateway_error(
assert not txn.is_success
-CREATE_QUERY = """
+CREATE_PAYMENT_MUTATION = """
mutation CheckoutPaymentCreate($checkoutId: ID!, $input: PaymentInput!) {
checkoutPaymentCreate(checkoutId: $checkoutId, input: $input) {
payment {
@@ -109,7 +109,7 @@ def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(
"amount": total.gross.amount,
},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["paymentErrors"]
@@ -145,7 +145,7 @@ def test_checkout_add_payment_without_shipping_method_with_shipping_required(
"amount": total.gross.amount,
},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -172,7 +172,7 @@ def test_checkout_add_payment_with_shipping_method_and_shipping_required(
"amount": total.gross.amount,
},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -208,7 +208,7 @@ def test_checkout_add_payment(
"amount": total.gross.amount,
},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -241,7 +241,7 @@ def test_checkout_add_payment_default_amount(
"checkoutId": checkout_id,
"input": {"gateway": DUMMY_GATEWAY, "token": "sample-token"},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["paymentErrors"]
@@ -277,7 +277,7 @@ def test_checkout_add_payment_bad_amount(
),
},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert (
@@ -299,7 +299,7 @@ def test_checkout_add_payment_not_supported_gateways(
"checkoutId": checkout_id,
"input": {"gateway": DUMMY_GATEWAY, "token": "sample-token", "amount": "10.0"},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert (
@@ -322,7 +322,7 @@ def test_use_checkout_billing_address_as_payment_billing(
"amount": total.gross.amount,
},
}
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -338,7 +338,7 @@ def test_use_checkout_billing_address_as_payment_billing(
address.save()
checkout.billing_address = address
checkout.save()
- response = user_api_client.post_graphql(CREATE_QUERY, variables)
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
get_graphql_content(response)
checkout.refresh_from_db()
@@ -347,6 +347,49 @@ def test_use_checkout_billing_address_as_payment_billing(
assert payment.billing_address_1 == address.street_address_1
+def test_create_payment_for_checkout_with_active_payments(
+ checkout_with_payments, user_api_client, address
+):
+ # given
+ checkout = checkout_with_payments
+ address.street_address_1 = "spanish-inqusition"
+ address.save()
+ checkout.billing_address = address
+ checkout.save()
+
+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ variables = {
+ "checkoutId": checkout_id,
+ "input": {
+ "gateway": DUMMY_GATEWAY,
+ "token": "sample-token",
+ "amount": total.gross.amount,
+ },
+ }
+
+ payments_count = checkout.payments.count()
+ previous_active_payments = checkout.payments.filter(is_active=True)
+ previous_active_payments_ids = list(
+ previous_active_payments.values_list("pk", flat=True)
+ )
+ assert len(previous_active_payments_ids) > 0
+
+ # when
+ response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["checkoutPaymentCreate"]
+
+ assert not data["paymentErrors"]
+ checkout.refresh_from_db()
+ assert checkout.payments.all().count() == payments_count + 1
+ active_payments = checkout.payments.all().filter(is_active=True)
+ assert active_payments.count() == 1
+ assert active_payments.first().pk not in previous_active_payments_ids
+
+
CAPTURE_QUERY = """
mutation PaymentCapture($paymentId: ID!, $amount: Decimal!) {
paymentCapture(paymentId: $paymentId, amount: $amount) {