Skip to content

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

Allow creating order without payment (#13339)

Krzysztof Waliczek7d12bbd0d90c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 01d8b2ec45..d69a15ef08 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -71,6 +71,8 @@ Shipping methods can be removed by the user after it has been assigned to a chec
- Add metadata support for channels. #13230 by @Smit-Parmar
- Remove `Preview feature` label from `metafield`, `metafields`, `metadata`,
`privateMetafield`, `privateMetafields` and `privateMetadata` fields - #13245 by @korycins
+- [Preview] Add possibility to completeCheckout without payment in transaction flow - #13339 by @kadewu:
+ - New field `allowUnpaidOrders` in `OrderSettings` for `Channel`
- Add `search` to `giftCards` query - #13173 by @zedzior
- Add `ProductBulkTranslate` mutation - #13329 by @SzymJ
- Add `ProductVariantBulkTranslate` mutation - #13329 by @SzymJ
diff --git a/saleor/channel/migrations/0014_channel_allow_to_create_order_without_payment.py b/saleor/channel/migrations/0014_channel_allow_to_create_order_without_payment.py
new file mode 100644
index 0000000000..d5844060df
--- /dev/null
+++ b/saleor/channel/migrations/0014_channel_allow_to_create_order_without_payment.py
@@ -0,0 +1,23 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("channel", "0013_auto_20230630_1039"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="channel",
+ name="allow_unpaid_orders",
+ field=models.BooleanField(default=False),
+ ),
+ migrations.RunSQL(
+ sql="""
+ ALTER TABLE channel_channel
+ ALTER COLUMN allow_unpaid_orders
+ SET DEFAULT false;
+ """,
+ reverse_sql=migrations.RunSQL.noop,
+ ),
+ ]
diff --git a/saleor/channel/models.py b/saleor/channel/models.py
index 86c3201356..dbb6d717b8 100644
--- a/saleor/channel/models.py
+++ b/saleor/channel/models.py
@@ -33,6 +33,7 @@ class Channel(ModelWithMetadata):
)
automatically_confirm_all_new_orders = models.BooleanField(default=True, null=True)
+ allow_unpaid_orders = models.BooleanField(default=False)
automatically_fulfill_non_shippable_gift_card = models.BooleanField(
default=True,
null=True,
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 8c55c03096..f3e6e2662f 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -665,7 +665,10 @@ def _prepare_checkout_with_transactions(
):
"""Prepare checkout object with transactions to complete the checkout process."""
clean_billing_address(checkout_info, CheckoutErrorCode)
- if checkout_info.checkout.authorize_status != CheckoutAuthorizeStatus.FULL:
+ if (
+ checkout_info.checkout.authorize_status != CheckoutAuthorizeStatus.FULL
+ and not checkout_info.channel.allow_unpaid_orders
+ ):
raise ValidationError(
{
"id": ValidationError(
@@ -1299,10 +1302,15 @@ def complete_checkout(
# paid is used. In case when we have TRANSACTION_FLOW we use transaction flow to
# finalize the checkout.
checkout_is_zero = checkout_info.checkout.total.gross.amount == Decimal(0)
- if transactions or (
- checkout_is_zero
- and checkout_info.channel.order_mark_as_paid_strategy
+ is_transaction_flow = (
+ checkout_info.channel.order_mark_as_paid_strategy
== MarkAsPaidStrategy.TRANSACTION_FLOW
+ )
+ if (
+ transactions
+ or checkout_info.channel.allow_unpaid_orders
+ or checkout_is_zero
+ and is_transaction_flow
):
order = complete_checkout_with_transaction(
manager=manager,
diff --git a/saleor/graphql/channel/mutations/channel_create.py b/saleor/graphql/channel/mutations/channel_create.py
index 52e6b6248b..d28a10c01a 100644
--- a/saleor/graphql/channel/mutations/channel_create.py
+++ b/saleor/graphql/channel/mutations/channel_create.py
@@ -99,6 +99,13 @@ class OrderSettingsInput(BaseInputObjectType):
"requested action for the transaction." + ADDED_IN_313 + PREVIEW_FEATURE
),
)
+ allow_unpaid_orders = graphene.Boolean(
+ required=False,
+ description=(
+ "Determine if it is possible to place unpdaid order by calling "
+ "`checkoutComplete` mutation." + ADDED_IN_315 + PREVIEW_FEATURE
+ ),
+ )
class Meta:
doc_category = DOC_CATEGORY_ORDERS
@@ -236,6 +243,10 @@ class ChannelCreate(ModelMutation):
"default_transaction_flow_strategy"
] = default_transaction_strategy
+ allow_unpaid_orders = order_settings.get("allow_unpaid_orders")
+ if allow_unpaid_orders is not None:
+ cleaned_input["allow_unpaid_orders"] = allow_unpaid_orders
+
return cleaned_input
@classmethod
diff --git a/saleor/graphql/channel/mutations/channel_update.py b/saleor/graphql/channel/mutations/channel_update.py
index ead90427f6..7e657a1b32 100644
--- a/saleor/graphql/channel/mutations/channel_update.py
+++ b/saleor/graphql/channel/mutations/channel_update.py
@@ -142,6 +142,11 @@ class ChannelUpdate(ModelMutation):
cleaned_input[
"default_transaction_flow_strategy"
] = default_transaction_strategy
+
+ allow_unpaid_orders = order_settings.get("allow_unpaid_orders")
+ if allow_unpaid_orders is not None:
+ cleaned_input["allow_unpaid_orders"] = allow_unpaid_orders
+
return cleaned_input
@classmethod
diff --git a/saleor/graphql/channel/tests/mutations/test_channel_create.py b/saleor/graphql/channel/tests/mutations/test_channel_create.py
index 30e897c7a5..3d11c69176 100644
--- a/saleor/graphql/channel/tests/mutations/test_channel_create.py
+++ b/saleor/graphql/channel/tests/mutations/test_channel_create.py
@@ -46,6 +46,7 @@ CHANNEL_CREATE_MUTATION = """
markAsPaidStrategy
defaultTransactionFlowStrategy
deleteExpiredOrdersAfter
+ allowUnpaidOrders
}
}
errors{
@@ -711,3 +712,41 @@ def test_channel_create_mutation_set_incorrect_delete_expired_orders_after(
error = content["data"]["channelCreate"]["errors"][0]
assert error["field"] == "deleteExpiredOrdersAfter"
assert error["code"] == ChannelErrorCode.INVALID.name
+
+
[email protected]("allowUnpaid", [True, False])
+def test_channel_create_set_allow_unpaid_orders(
+ allowUnpaid,
+ permission_manage_channels,
+ staff_api_client,
+):
+ # given
+ name = "testName"
+ slug = "test_slug"
+ currency_code = "USD"
+ default_country = "US"
+ variables = {
+ "input": {
+ "name": name,
+ "slug": slug,
+ "currencyCode": currency_code,
+ "defaultCountry": default_country,
+ "orderSettings": {"allowUnpaidOrders": allowUnpaid},
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ CHANNEL_CREATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_channels,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["channelCreate"]
+ assert not data["errors"]
+ channel_data = data["channel"]
+ channel = Channel.objects.get()
+ assert channel_data["orderSettings"]["allowUnpaidOrders"] == allowUnpaid
+ assert channel.allow_unpaid_orders == allowUnpaid
diff --git a/saleor/graphql/channel/tests/mutations/test_channel_update.py b/saleor/graphql/channel/tests/mutations/test_channel_update.py
index f6102b50c3..b853711dc2 100644
--- a/saleor/graphql/channel/tests/mutations/test_channel_update.py
+++ b/saleor/graphql/channel/tests/mutations/test_channel_update.py
@@ -44,6 +44,7 @@ CHANNEL_UPDATE_MUTATION = """
markAsPaidStrategy
defaultTransactionFlowStrategy
deleteExpiredOrdersAfter
+ allowUnpaidOrders
}
}
errors{
@@ -78,6 +79,7 @@ def test_channel_update_mutation_as_staff_user(
"automaticallyConfirmAllNewOrders": False,
"automaticallyFulfillNonShippableGiftCard": False,
"expireOrdersAfter": 10,
+ "allowUnpaidOrders": True,
},
},
}
@@ -110,6 +112,7 @@ def test_channel_update_mutation_as_staff_user(
is False
)
assert channel_data["orderSettings"]["expireOrdersAfter"] == 10
+ assert channel_data["orderSettings"]["allowUnpaidOrders"] is True
def test_channel_update_mutation_as_app(
@@ -799,12 +802,15 @@ def test_channel_update_order_settings_manage_orders(
):
# given
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ channel_USD.allow_unpaid_orders = True
+ channel_USD.save()
variables = {
"id": channel_id,
"input": {
"orderSettings": {
"automaticallyConfirmAllNewOrders": False,
"automaticallyFulfillNonShippableGiftCard": False,
+ "allowUnpaidOrders": False,
},
},
}
@@ -826,6 +832,7 @@ def test_channel_update_order_settings_manage_orders(
channel_data["orderSettings"]["automaticallyFulfillNonShippableGiftCard"]
is False
)
+ assert channel_data["orderSettings"]["allowUnpaidOrders"] is False
def test_channel_update_order_settings_empty_order_settings(
diff --git a/saleor/graphql/channel/tests/queries/test_channel.py b/saleor/graphql/channel/tests/queries/test_channel.py
index afff0f3d2e..07a6b98d72 100644
--- a/saleor/graphql/channel/tests/queries/test_channel.py
+++ b/saleor/graphql/channel/tests/queries/test_channel.py
@@ -292,6 +292,7 @@ QUERY_CHANNEL_ORDER_SETTINGS = """
markAsPaidStrategy
defaultTransactionFlowStrategy
deleteExpiredOrdersAfter
+ allowUnpaidOrders
}
}
}
@@ -344,6 +345,10 @@ def test_query_channel_order_settings_as_staff_user(
channel_data["orderSettings"]["deleteExpiredOrdersAfter"]
== channel_USD.delete_expired_orders_after.days
)
+ assert (
+ channel_data["orderSettings"]["allowUnpaidOrders"]
+ == channel_USD.allow_unpaid_orders
+ )
def test_query_channel_order_settings_as_app(
diff --git a/saleor/graphql/channel/types.py b/saleor/graphql/channel/types.py
index cd6accf291..f1789caa4d 100644
--- a/saleor/graphql/channel/types.py
+++ b/saleor/graphql/channel/types.py
@@ -222,6 +222,13 @@ class OrderSettings(ObjectType):
+ PREVIEW_FEATURE
),
)
+ allow_unpaid_orders = graphene.Boolean(
+ required=True,
+ description=(
+ "Determine if it is possible to place unpdaid order by calling "
+ "`checkoutComplete` mutation." + ADDED_IN_315 + PREVIEW_FEATURE
+ ),
+ )
class Meta:
description = "Represents the channel-specific order settings."
@@ -469,4 +476,5 @@ class Channel(ModelObjectType):
mark_as_paid_strategy=root.order_mark_as_paid_strategy,
default_transaction_flow_strategy=root.default_transaction_flow_strategy,
delete_expired_orders_after=root.delete_expired_orders_after.days,
+ allow_unpaid_orders=(root.allow_unpaid_orders),
)
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
index 9bb3a3540b..285d41652f 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
@@ -120,6 +120,60 @@ def test_checkout_without_any_transaction(
assert data["errors"][0]["code"] == CheckoutErrorCode.CHECKOUT_NOT_FULLY_PAID.name
+def test_checkout_without_any_transaction_allow_to_create_order(
+ user_api_client,
+ checkout_with_gift_card,
+ gift_card,
+ transaction_item_generator,
+ address,
+ shipping_method,
+):
+ # given
+ checkout = checkout_with_gift_card
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.tax_exemption = True
+ checkout.save()
+
+ channel = checkout.channel
+ channel.automatically_confirm_all_new_orders = True
+ channel.allow_unpaid_orders = True
+ channel.order_mark_as_paid_strategy = MarkAsPaidStrategy.TRANSACTION_FLOW
+ channel.save()
+
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ total = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, address
+ )
+
+ update_checkout_payment_statuses(
+ checkout=checkout_info.checkout,
+ checkout_total_gross=total.gross,
+ )
+
+ redirect_url = "https://www.example.com"
+ variables = {"id": to_global_id_or_none(checkout), "redirectUrl": redirect_url}
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ content = get_graphql_content(response)
+
+ order = Order.objects.get()
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+ assert to_global_id_or_none(order) == data["order"]["id"]
+
+ assert order.total_charged == zero_money(order.currency)
+ assert order.total_authorized == zero_money(order.currency)
+ assert order.charge_status == OrderChargeStatus.NONE
+ assert order.authorize_status == OrderAuthorizeStatus.NONE
+
+
def test_checkout_with_total_0(
checkout_with_item_total_0,
user_api_client,
@@ -477,6 +531,62 @@ def test_checkout_partially_paid(
assert error["code"] == CheckoutErrorCode.CHECKOUT_NOT_FULLY_PAID.name
+def test_checkout_partially_paid_allow_unpaid_order(
+ user_api_client,
+ checkout_with_gift_card,
+ gift_card,
+ transaction_item_generator,
+ address,
+ shipping_method,
+):
+ # given
+ checkout = checkout_with_gift_card
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.tax_exemption = True
+ checkout.save()
+
+ channel = checkout.channel
+ channel.automatically_confirm_all_new_orders = True
+ channel.allow_unpaid_orders = True
+ channel.save()
+
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ total = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, address
+ )
+
+ transaction = transaction_item_generator(
+ checkout_id=checkout.pk, charged_value=total.gross.amount - Decimal("10")
+ )
+
+ update_checkout_payment_statuses(
+ checkout=checkout_info.checkout,
+ checkout_total_gross=total.gross,
+ )
+
+ redirect_url = "https://www.example.com"
+ variables = {"id": to_global_id_or_none(checkout), "redirectUrl": redirect_url}
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ order = Order.objects.get()
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+ assert to_global_id_or_none(order) == data["order"]["id"]
+
+ assert order.total_charged_amount == transaction.charged_value
+ assert order.total_authorized == zero_money(order.currency)
+ assert order.charge_status == OrderChargeStatus.PARTIAL
+ assert order.authorize_status == OrderAuthorizeStatus.PARTIAL
+
+
def test_checkout_with_pending_charged(
user_api_client,
checkout_with_gift_card,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index f0fce88c2f..c8a41e2aa6 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -5355,6 +5355,15 @@ type OrderSettings {
Note: this API is currently in Feature Preview and can be subject to changes at later point.
"""
deleteExpiredOrdersAfter: Day!
+
+ """
+ Determine if it is possible to place unpdaid order by calling `checkoutComplete` mutation.
+
+ Added in Saleor 3.15.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ allowUnpaidOrders: Boolean!
}
"""
@@ -26617,6 +26626,15 @@ input OrderSettingsInput @doc(category: "Orders") {
Note: this API is currently in Feature Preview and can be subject to changes at later point.
"""
defaultTransactionFlowStrategy: TransactionFlowStrategyEnum
+
+ """
+ Determine if it is possible to place unpdaid order by calling `checkoutComplete` mutation.
+
+ Added in Saleor 3.15.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ allowUnpaidOrders: Boolean
}
"""