From c2c2a67753084215e8641fa3bc5b75ce093f09aa Mon Sep 17 00:00:00 2001
From: Matt Drayer <mattdrayer@edx.org>
Date: Thu, 2 Jun 2016 19:34:21 -0400
Subject: [PATCH] mattdrayer/WL-493: Enable bulk purchase via Otto for
 unauthenticated users

---
 common/djangoapps/course_modes/tests/test_views.py      | 6 ++++--
 common/djangoapps/course_modes/views.py                 | 9 ++++++---
 common/djangoapps/student/helpers.py                    | 2 +-
 lms/djangoapps/verify_student/tests/test_integration.py | 4 ++--
 lms/djangoapps/verify_student/views.py                  | 6 +++++-
 lms/static/js/student_account/views/FinishAuthView.js   | 4 +++-
 6 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/common/djangoapps/course_modes/tests/test_views.py b/common/djangoapps/course_modes/tests/test_views.py
index 0aa92002172..b8c5161ddf5 100644
--- a/common/djangoapps/course_modes/tests/test_views.py
+++ b/common/djangoapps/course_modes/tests/test_views.py
@@ -94,7 +94,8 @@ class CourseModeViewTest(UrlResetMixin, ModuleStoreTestCase):
         url = reverse('course_modes_choose', args=[unicode(self.course.id)])
         response = self.client.get(url)
         # Check whether we were correctly redirected
-        start_flow_url = reverse('verify_student_start_flow', args=[unicode(self.course.id)])
+        purchase_workflow = "?purchase_workflow=single"
+        start_flow_url = reverse('verify_student_start_flow', args=[unicode(self.course.id)]) + purchase_workflow
         self.assertRedirects(response, start_flow_url)
 
     def test_no_id_redirect_otto(self):
@@ -194,7 +195,8 @@ class CourseModeViewTest(UrlResetMixin, ModuleStoreTestCase):
 
         # Since the only available track is professional ed, expect that
         # we're redirected immediately to the start of the payment flow.
-        start_flow_url = reverse('verify_student_start_flow', args=[unicode(self.course.id)])
+        purchase_workflow = "?purchase_workflow=single"
+        start_flow_url = reverse('verify_student_start_flow', args=[unicode(self.course.id)]) + purchase_workflow
         self.assertRedirects(response, start_flow_url)
 
         # Now enroll in the course
diff --git a/common/djangoapps/course_modes/views.py b/common/djangoapps/course_modes/views.py
index 2bf337e1b28..57695c9da27 100644
--- a/common/djangoapps/course_modes/views.py
+++ b/common/djangoapps/course_modes/views.py
@@ -88,12 +88,15 @@ class ChooseModeView(View):
         # If there are both modes, default to non-id-professional.
         has_enrolled_professional = (CourseMode.is_professional_slug(enrollment_mode) and is_active)
         if CourseMode.has_professional_mode(modes) and not has_enrolled_professional:
-            redirect_url = reverse('verify_student_start_flow', kwargs={'course_id': unicode(course_key)})
+            purchase_workflow = request.GET.get("purchase_workflow", "single")
+            verify_url = reverse('verify_student_start_flow', kwargs={'course_id': unicode(course_key)})
+            redirect_url = "{url}?purchase_workflow={workflow}".format(url=verify_url, workflow=purchase_workflow)
             if ecommerce_service.is_enabled(request.user):
                 professional_mode = modes.get(CourseMode.NO_ID_PROFESSIONAL_MODE) or modes.get(CourseMode.PROFESSIONAL)
-                if professional_mode.sku:
+                if purchase_workflow == "single" and professional_mode.sku:
                     redirect_url = ecommerce_service.checkout_page_url(professional_mode.sku)
-
+                if purchase_workflow == "bulk" and professional_mode.bulk_sku:
+                    redirect_url = ecommerce_service.checkout_page_url(professional_mode.bulk_sku)
             return redirect(redirect_url)
 
         # If there isn't a verified mode available, then there's nothing
diff --git a/common/djangoapps/student/helpers.py b/common/djangoapps/student/helpers.py
index a8c4e500fff..8ea71514a91 100644
--- a/common/djangoapps/student/helpers.py
+++ b/common/djangoapps/student/helpers.py
@@ -200,7 +200,7 @@ def auth_pipeline_urls(auth_entry, redirect_url=None):
 
 # Query string parameters that can be passed to the "finish_auth" view to manage
 # things like auto-enrollment.
-POST_AUTH_PARAMS = ('course_id', 'enrollment_action', 'course_mode', 'email_opt_in')
+POST_AUTH_PARAMS = ('course_id', 'enrollment_action', 'course_mode', 'email_opt_in', 'purchase_workflow')
 
 
 def get_next_url_for_login_page(request):
diff --git a/lms/djangoapps/verify_student/tests/test_integration.py b/lms/djangoapps/verify_student/tests/test_integration.py
index c2401b62316..c45c9cd350b 100644
--- a/lms/djangoapps/verify_student/tests/test_integration.py
+++ b/lms/djangoapps/verify_student/tests/test_integration.py
@@ -32,7 +32,7 @@ class TestProfEdVerification(ModuleStoreTestCase):
             min_price=self.MIN_PRICE,
             suggested_prices=''
         )
-
+        purchase_workflow = "?purchase_workflow=single"
         self.urls = {
             'course_modes_choose': reverse(
                 'course_modes_choose',
@@ -42,7 +42,7 @@ class TestProfEdVerification(ModuleStoreTestCase):
             'verify_student_start_flow': reverse(
                 'verify_student_start_flow',
                 args=[unicode(self.course_key)]
-            ),
+            ) + purchase_workflow,
         }
 
     def test_start_flow(self):
diff --git a/lms/djangoapps/verify_student/views.py b/lms/djangoapps/verify_student/views.py
index 3484d9af6e0..21715ba979f 100644
--- a/lms/djangoapps/verify_student/views.py
+++ b/lms/djangoapps/verify_student/views.py
@@ -334,6 +334,10 @@ class PayAndVerifyView(View):
         # Redirect the user to a more appropriate page if the
         # messaging won't make sense based on the user's
         # enrollment / payment / verification status.
+        sku_to_use = relevant_course_mode.sku
+        purchase_workflow = request.GET.get('purchase_workflow', 'single')
+        if purchase_workflow == 'bulk' and relevant_course_mode.bulk_sku:
+            sku_to_use = relevant_course_mode.bulk_sku
         redirect_response = self._redirect_if_necessary(
             message,
             already_verified,
@@ -342,7 +346,7 @@ class PayAndVerifyView(View):
             course_key,
             user_is_trying_to_pay,
             request.user,
-            relevant_course_mode.sku
+            sku_to_use
         )
         if redirect_response is not None:
             return redirect_response
diff --git a/lms/static/js/student_account/views/FinishAuthView.js b/lms/static/js/student_account/views/FinishAuthView.js
index 8ec1da6ab70..8e5c3955dab 100644
--- a/lms/static/js/student_account/views/FinishAuthView.js
+++ b/lms/static/js/student_account/views/FinishAuthView.js
@@ -51,7 +51,8 @@
                     enrollmentAction: $.url( '?enrollment_action' ),
                     courseId: $.url( '?course_id' ),
                     courseMode: $.url( '?course_mode' ),
-                    emailOptIn: $.url( '?email_opt_in' )
+                    emailOptIn: $.url( '?email_opt_in' ),
+                    purchaseWorkflow: $.url( '?purchase_workflow' )
                 };
                 for (var key in queryParams) {
                     if (queryParams[key]) {
@@ -63,6 +64,7 @@
                 this.courseMode = queryParams.courseMode;
                 this.emailOptIn = queryParams.emailOptIn;
                 this.nextUrl = this.urls.defaultNextUrl;
+                this.purchaseWorkflow = queryParams.purchaseWorkflow;
                 if (queryParams.next) {
                     // Ensure that the next URL is internal for security reasons
                     if ( ! window.isExternal( queryParams.next ) ) {
-- 
GitLab