Skip to content

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

Add redirect url to order (#6500)

Damian Wysocki85d416c024e1
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index f2d7809dc3..e1a00ab5cb 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -276,6 +276,7 @@ def _create_order(*, checkout: Checkout, order_data: dict, user: User) -> Order:
# copy metadata from the checkout into the new order
order.metadata = checkout.metadata
+ order.redirect_url = checkout.redirect_url
order.private_metadata = checkout.private_metadata
order.save()
diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.py
index 3b4d15fcc7..2ff9d21eae 100644
--- a/saleor/graphql/checkout/tests/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py
@@ -178,7 +178,8 @@ def test_checkout_complete(
orders_count = Order.objects.count()
checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
- variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}
+ redirect_url = "https://www.example.com"
+ variables = {"checkoutId": checkout_id, "redirectUrl": redirect_url}
response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
content = get_graphql_content(response)
@@ -189,6 +190,7 @@ def test_checkout_complete(
assert Order.objects.count() == orders_count + 1
order = Order.objects.first()
assert order.token == order_token
+ assert order.redirect_url == redirect_url
assert order.total.gross == total.gross
assert order.metadata == checkout.metadata
assert order.private_metadata == checkout.private_metadata
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index 3bd013ff4c..0fd1da9120 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -7,6 +7,7 @@ from ....account.models import User
from ....core.exceptions import InsufficientStock
from ....core.permissions import OrderPermissions
from ....core.taxes import TaxError, zero_taxed_money
+from ....core.utils.url import validate_storefront_url
from ....order import OrderStatus, events, models
from ....order.actions import order_created
from ....order.error_codes import OrderErrorCode
@@ -66,6 +67,13 @@ class DraftOrderInput(InputObjectType):
channel = graphene.ID(
description="ID of the channel associated with the order.", name="channel"
)
+ redirect_url = graphene.String(
+ required=False,
+ description=(
+ "URL of a view where users should be redirected to "
+ "see the order details. URL in RFC 1808 format."
+ ),
+ )
class DraftOrderCreateInput(DraftOrderInput):
@@ -114,9 +122,18 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
}
)
+ @classmethod
+ def clean_redirect_url(cls, redirect_url):
+ try:
+ validate_storefront_url(redirect_url)
+ except ValidationError as error:
+ error.code = OrderErrorCode.INVALID.value
+ raise ValidationError({"redirect_url": error})
+
@classmethod
def clean_input(cls, info, instance, data):
shipping_address = data.pop("shipping_address", None)
+ redirect_url = data.pop("redirect_url", None)
billing_address = data.pop("billing_address", None)
cleaned_input = super().clean_input(info, instance, data)
lines = data.pop("lines", None)
@@ -177,6 +194,10 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
billing_address, "billing", user=instance
)
cleaned_input["billing_address"] = billing_address
+ if redirect_url:
+ cls.clean_redirect_url(redirect_url)
+ cleaned_input["redirect_url"] = redirect_url
+
return cleaned_input
@staticmethod
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 3061bd27cb..0f606d78ae 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -785,13 +785,14 @@ DRAFT_ORDER_CREATE_MUTATION = """
mutation draftCreate(
$user: ID, $discount: PositiveDecimal, $lines: [OrderLineCreateInput],
$shippingAddress: AddressInput, $shippingMethod: ID, $voucher: ID,
- $customerNote: String, $channel: ID
+ $customerNote: String, $channel: ID, $redirectUrl: String
) {
draftOrderCreate(
input: {user: $user, discount: $discount,
lines: $lines, shippingAddress: $shippingAddress,
shippingMethod: $shippingMethod, voucher: $voucher,
channel: $channel,
+ redirectUrl: $redirectUrl,
customerNote: $customerNote}) {
orderErrors {
field
@@ -804,6 +805,7 @@ DRAFT_ORDER_CREATE_MUTATION = """
amount
}
discountName
+ redirectUrl
lines {
productName
productSku
@@ -854,6 +856,8 @@ def test_draft_order_create(
shipping_id = graphene.Node.to_global_id("ShippingMethod", shipping_method.id)
voucher_id = graphene.Node.to_global_id("Voucher", voucher.id)
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ redirect_url = "https://www.example.com"
+
variables = {
"user": user_id,
"discount": discount,
@@ -863,6 +867,7 @@ def test_draft_order_create(
"voucher": voucher_id,
"customerNote": customer_note,
"channel": channel_id,
+ "redirectUrl": redirect_url,
}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders]
@@ -873,6 +878,7 @@ def test_draft_order_create(
assert data["status"] == OrderStatus.DRAFT.upper()
assert data["voucher"]["code"] == voucher.code
assert data["customerNote"] == customer_note
+ assert data["redirectUrl"] == redirect_url
order = Order.objects.first()
assert order.user == customer_user
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index fe1ee77790..0108c92300 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -401,6 +401,7 @@ class Order(CountableDjangoObjectType):
"user",
"voucher",
"weight",
+ "redirect_url",
]
@staticmethod
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 659616c045..6d2b7f1d3f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1798,6 +1798,7 @@ input DraftOrderCreateInput {
voucher: ID
customerNote: String
channel: ID
+ redirectUrl: String
lines: [OrderLineCreateInput]
}
@@ -1817,6 +1818,7 @@ input DraftOrderInput {
voucher: ID
customerNote: String
channel: ID
+ redirectUrl: String
}
type DraftOrderLineDelete {
@@ -2819,6 +2821,7 @@ type Order implements Node & ObjectWithMetadata {
displayGrossPrices: Boolean!
customerNote: String!
weight: Weight
+ redirectUrl: String
privateMetadata: [MetadataItem]!
metadata: [MetadataItem]!
fulfillments: [Fulfillment]!
diff --git a/saleor/order/emails.py b/saleor/order/emails.py
index dd30ff64a7..d0c3e66f4f 100644
--- a/saleor/order/emails.py
+++ b/saleor/order/emails.py
@@ -121,7 +121,7 @@ def send_fulfillment_confirmation(order_pk, fulfillment_pk):
def send_fulfillment_confirmation_to_customer(order, fulfillment, user):
- send_fulfillment_confirmation.delay(order.pk, fulfillment.pk)
+ send_fulfillment_confirmation.delay(order.pk, fulfillment.pk, order.redirect_url)
events.email_sent_event(
order=order, user=user, email_type=events.OrderEventsEmails.FULFILLMENT
diff --git a/saleor/order/migrations/0091_order_redirect_url.py b/saleor/order/migrations/0091_order_redirect_url.py
new file mode 100644
index 0000000000..49fde2e98f
--- /dev/null
+++ b/saleor/order/migrations/0091_order_redirect_url.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.1.2 on 2020-11-18 09:33
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("order", "0090_order_channel_listing"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="order",
+ name="redirect_url",
+ field=models.URLField(blank=True, null=True),
+ ),
+ ]
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 664c795c05..5dd734ab80 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -173,6 +173,7 @@ class Order(ModelWithMetadata):
weight = MeasurementField(
measurement=Weight, unit_choices=WeightUnits.CHOICES, default=zero_weight
)
+ redirect_url = models.URLField(blank=True, null=True)
objects = OrderQueryset.as_manager()
class Meta:
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index 48efca8077..fbd43f7a09 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -752,8 +752,9 @@ def test_send_fulfillment_order_lines_mails(
has_standard,
has_digital,
):
-
+ redirect_url = "http://localhost.pl"
order = fulfilled_order
+ order.redirect_url = redirect_url
assert order.lines.count() == 2
if not has_standard:
@@ -769,12 +770,12 @@ def test_send_fulfillment_order_lines_mails(
line.save()
send_fulfillment_confirmation_to_customer(
- order=order, fulfillment=fulfillment, user=staff_user
+ order=order, fulfillment=fulfillment, user=staff_user,
)
events = OrderEvent.objects.all()
mocked_send_fulfillment_confirmation.delay.assert_called_once_with(
- order.pk, fulfillment.pk
+ order.pk, fulfillment.pk, redirect_url
)
# Ensure the standard fulfillment event was triggered
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index 2b57aab2b1..276ff4a0e8 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -63,8 +63,10 @@ def test_handle_fully_paid_order_digital_lines(
mock_send_fulfillment_confirmation,
order_with_digital_line,
):
-
+ redirect_url = "http://localhost.pl"
order = order_with_digital_line
+ order.redirect_url = redirect_url
+ order.save()
handle_fully_paid_order(order)
fulfillment = order.fulfillments.first()
@@ -89,7 +91,9 @@ def test_handle_fully_paid_order_digital_lines(
)
mock_send_payment_confirmation.assert_called_once_with(order.pk)
- mock_send_fulfillment_confirmation.assert_called_once_with(order.pk, fulfillment.pk)
+ mock_send_fulfillment_confirmation.assert_called_once_with(
+ order.pk, fulfillment.pk, redirect_url
+ )
order.refresh_from_db()
assert order.status == OrderStatus.FULFILLED