diff --git a/openedx/features/edly/api/v1/urls.py b/openedx/features/edly/api/v1/urls.py index 9b10c5a80e9c..543df19f17ee 100644 --- a/openedx/features/edly/api/v1/urls.py +++ b/openedx/features/edly/api/v1/urls.py @@ -3,6 +3,7 @@ from openedx.features.edly.api.v1.views.course_enrollments import EdlyCourseEnrollmentViewSet from openedx.features.edly.api.v1.views.enrollment_count import EdlyProgramEnrollmentCountViewSet from openedx.features.edly.api.v1.views.user_mutisites import MultisitesViewset +from openedx.features.edly.api.v1.views.user_paid_for_course import UserPaidForCourseViewSet from openedx.features.edly.api.v1.views.user_sites import UserSitesViewSet router = routers.SimpleRouter() @@ -21,4 +22,10 @@ base_name='program_enrollment_count', ) +router.register( + r'user_paid_for_course', + UserPaidForCourseViewSet, + base_name='user_paid_for_course' +) + urlpatterns = router.urls diff --git a/openedx/features/edly/api/v1/views/user_paid_for_course.py b/openedx/features/edly/api/v1/views/user_paid_for_course.py new file mode 100644 index 000000000000..76d979ca7ea6 --- /dev/null +++ b/openedx/features/edly/api/v1/views/user_paid_for_course.py @@ -0,0 +1,43 @@ +from opaque_keys import InvalidKeyError +from opaque_keys.edx.keys import CourseKey +from rest_framework import viewsets +from rest_framework.permissions import IsAuthenticated + +from common.djangoapps.student.models import CourseEnrollment +from common.djangoapps.util.json_request import JsonResponse + + +class UserPaidForCourseViewSet(viewsets.ViewSet): + """ + **Use Case** + + Get the status of a user's paid status for a given course. + + **Example Request** + + GET /api/v1/user_paid_for_course/{course_id} + + **GET Parameters** + + * pk: The course id of the course to retrieve the user's paid status for. + + **Response Values** + + If the request is successful, the request returns an HTTP 200 "OK" response. + + The HTTP 200 response has the following values. + + * has_user_paid: True if the user has paid for the course, False otherwise. + """ + permission_classes = [IsAuthenticated] + + def retrieve(self, request, pk=None): + """Get the status of a user's paid status for a given course.""" + try: + course_key = CourseKey.from_string(pk) + course_enrollment = CourseEnrollment.get_enrollment(request.user, course_key) + except InvalidKeyError: + return JsonResponse({'has_user_paid': False}, status=406) + + paid_status = course_enrollment.get_order_attribute_value('order_number') + return JsonResponse({'has_user_paid': bool(paid_status)}, status=200)