Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable unenroll option for paid course #604

Open
wants to merge 2 commits into
base: develop-koa
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions openedx/features/edly/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
60 changes: 60 additions & 0 deletions openedx/features/edly/api/v1/views/user_paid_for_course.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from enum import Enum

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 PaymentStatus(Enum):
PAID = "paid"
UNPAID = "unpaid"
NOT_ENROLLED = "not_enrolled"
ERROR = "error"


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.

* status: The status of the user's paid status for the course.
Possible values are:
* "paid": The user has paid for the course.
* "unpaid": The user has not paid for the course.
* "error": An error occurred while retrieving the user's paid status.
"""
permission_classes = [IsAuthenticated]

def retrieve(self, request, pk=None):
try:
course_key = CourseKey.from_string(pk)
enrollment = CourseEnrollment.get_enrollment(request.user, course_key)
if not enrollment:
return JsonResponse({"status": PaymentStatus.NOT_ENROLLED.value}, status=200)

order_exists = enrollment.get_order_attribute_value('order_number')
payment_status = PaymentStatus.PAID if order_exists else PaymentStatus.UNPAID

return JsonResponse({"status": payment_status.value}, status=200)

except (InvalidKeyError, Exception):
return JsonResponse({"status": PaymentStatus.ERROR.value}, status=406)