Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add redirect url to order (#6500)
Damian Wysocki85d416c024e1
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex 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 orderorder.metadata = checkout.metadata+ order.redirect_url = checkout.redirect_urlorder.private_metadata = checkout.private_metadataorder.save()diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.pyindex 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 + 1order = Order.objects.first()assert order.token == order_token+ assert order.redirect_url == redirect_urlassert order.total.gross == total.grossassert order.metadata == checkout.metadataassert order.private_metadata == checkout.private_metadatadiff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.pyindex 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 Userfrom ....core.exceptions import InsufficientStockfrom ....core.permissions import OrderPermissionsfrom ....core.taxes import TaxError, zero_taxed_money+from ....core.utils.url import validate_storefront_urlfrom ....order import OrderStatus, events, modelsfrom ....order.actions import order_createdfrom ....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})+@classmethoddef 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@staticmethoddiff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex 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+ redirectUrllines {productNameproductSku@@ -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.codeassert data["customerNote"] == customer_note+ assert data["redirectUrl"] == redirect_urlorder = Order.objects.first()assert order.user == customer_userdiff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex 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",]@staticmethoddiff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 659616c045..6d2b7f1d3f 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1798,6 +1798,7 @@ input DraftOrderCreateInput {voucher: IDcustomerNote: Stringchannel: ID+ redirectUrl: Stringlines: [OrderLineCreateInput]}@@ -1817,6 +1818,7 @@ input DraftOrderInput {voucher: IDcustomerNote: Stringchannel: ID+ redirectUrl: String}type DraftOrderLineDelete {@@ -2819,6 +2821,7 @@ type Order implements Node & ObjectWithMetadata {displayGrossPrices: Boolean!customerNote: String!weight: Weight+ redirectUrl: StringprivateMetadata: [MetadataItem]!metadata: [MetadataItem]!fulfillments: [Fulfillment]!diff --git a/saleor/order/emails.py b/saleor/order/emails.pyindex 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.FULFILLMENTdiff --git a/saleor/order/migrations/0091_order_redirect_url.py b/saleor/order/migrations/0091_order_redirect_url.pynew file mode 100644index 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.pyindex 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.pyindex 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_urlassert order.lines.count() == 2if 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 triggereddiff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.pyindex 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