views.py 5.3 KB
Newer Older
1 2 3
""" Commerce views. """
import logging

4
from django.conf import settings
5
from django.contrib.auth.decorators import login_required
6
from django.core.cache import cache
7
from django.http import HttpResponseBadRequest
8
from django.utils.translation import ugettext as _
9
from django.views.decorators.csrf import csrf_exempt
10 11
from django.views.decorators.http import require_http_methods
from opaque_keys.edx.locator import CourseLocator
12

13
from course_modes.models import CourseMode
14
from edxmako.shortcuts import render_to_response
15
from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification
16
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
17
from openedx.core.djangoapps.theming.helpers import is_request_in_themed_site
18
from shoppingcart.processors.CyberSource2 import is_user_payment_error
19 20
from student.models import CourseEnrollment
from util.json_request import JsonResponse
21

22 23
from .models import CommerceConfiguration

24 25 26
log = logging.getLogger(__name__)


27
@csrf_exempt
28 29
def checkout_cancel(_request):
    """ Checkout/payment cancellation view. """
30 31 32 33 34
    context = {
        'payment_support_email': configuration_helpers.get_value(
            'payment_support_email', settings.PAYMENT_SUPPORT_EMAIL,
        )
    }
35
    return render_to_response("commerce/checkout_cancel.html", context)
36 37 38


@csrf_exempt
Tasawer committed
39 40
def checkout_error(_request):
    """ Checkout/payment error view. """
41 42 43 44 45
    context = {
        'payment_support_email': configuration_helpers.get_value(
            'payment_support_email', settings.PAYMENT_SUPPORT_EMAIL,
        )
    }
Tasawer committed
46 47 48 49
    return render_to_response("commerce/checkout_error.html", context)


@csrf_exempt
50 51 52
@login_required
def checkout_receipt(request):
    """ Receipt view. """
53 54 55

    page_title = _('Receipt')
    is_payment_complete = True
56
    payment_support_email = configuration_helpers.get_value('payment_support_email', settings.PAYMENT_SUPPORT_EMAIL)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    payment_support_link = '<a href=\"mailto:{email}\">{email}</a>'.format(email=payment_support_email)

    is_cybersource = all(k in request.POST for k in ('signed_field_names', 'decision', 'reason_code'))
    if is_cybersource and request.POST['decision'] != 'ACCEPT':
        # Cybersource may redirect users to this view if it couldn't recover
        # from an error while capturing payment info.
        is_payment_complete = False
        page_title = _('Payment Failed')
        reason_code = request.POST['reason_code']
        # if the problem was with the info submitted by the user, we present more detailed messages.
        if is_user_payment_error(reason_code):
            error_summary = _("There was a problem with this transaction. You have not been charged.")
            error_text = _(
                "Make sure your information is correct, or try again with a different card or another form of payment."
            )
        else:
            error_summary = _("A system error occurred while processing your payment. You have not been charged.")
            error_text = _("Please wait a few minutes and then try again.")
        for_help_text = _("For help, contact {payment_support_link}.").format(payment_support_link=payment_support_link)
    else:
        # if anything goes wrong rendering the receipt, it indicates a problem fetching order data.
        error_summary = _("An error occurred while creating your receipt.")
        error_text = None  # nothing particularly helpful to say if this happens.
        for_help_text = _(
            "If your course does not appear on your dashboard, contact {payment_support_link}."
        ).format(payment_support_link=payment_support_link)

84 85 86 87 88 89 90
    commerce_configuration = CommerceConfiguration.current()
    # user order cache should be cleared when a new order is placed
    # so user can see new order in their order history.
    if is_payment_complete and commerce_configuration.enabled and commerce_configuration.is_cache_enabled:
        cache_key = commerce_configuration.CACHE_KEY + '.' + str(request.user.id)
        cache.delete(cache_key)

91
    context = {
92 93
        'page_title': page_title,
        'is_payment_complete': is_payment_complete,
94
        'platform_name': configuration_helpers.get_value('platform_name', settings.PLATFORM_NAME),
95 96 97 98 99
        'verified': SoftwareSecurePhotoVerification.verification_valid_or_pending(request.user).exists(),
        'error_summary': error_summary,
        'error_text': error_text,
        'for_help_text': for_help_text,
        'payment_support_email': payment_support_email,
Tasawer committed
100
        'username': request.user.username,
101
        'nav_hidden': True,
102
        'is_request_in_themed_site': is_request_in_themed_site()
103 104
    }
    return render_to_response('commerce/checkout_receipt.html', context)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123


@require_http_methods(["GET"])
@login_required
def user_verification_status(request):
    """
    Check for user verification status.
    :return 'True' if the user enrollment for the course belongs to verified modes e.g. Verified, Professional.
    """
    course_id = request.GET.get('course_id', None)

    if course_id is None:
        return HttpResponseBadRequest()

    course_key = CourseLocator.from_string(course_id)
    enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user(request.user, course_key)
    is_verification_required = enrollment_mode in CourseMode.VERIFIED_MODES

    return JsonResponse({'is_verification_required': is_verification_required})