"""Views for ecommerce"""
from decimal import Decimal
import logging

from django.conf import settings
from django.contrib.auth import get_user_model
from django.http.response import Http404
from django.shortcuts import get_object_or_404
from django.urls import reverse
from ipware import get_client_ip
from rest_framework import status as statuses
from rest_framework.authentication import SessionAuthentication
from rest_framework.generics import CreateAPIView, GenericAPIView, RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response
from rest_framework.validators import ValidationError
from rest_framework.views import APIView

from applications.constants import AppStates
from applications.models import BootcampApplication
from backends.edxorg import EdxOrgOAuth2
from ecommerce.api import (
    complete_successful_order,
    create_unfulfilled_order,
    generate_cybersource_sa_payload,
    get_new_order_by_reference_number,
    handle_rejected_order,
    serialize_user_bootcamp_run,
    serialize_user_bootcamp_runs,
)
from ecommerce.constants import CYBERSOURCE_DECISION_ACCEPT, CYBERSOURCE_DECISION_CANCEL
from ecommerce.exceptions import EcommerceException
from ecommerce.models import Line, Order, Receipt
from ecommerce.permissions import IsSignedByCyberSource
from ecommerce.serializers import (
    CheckoutDataSerializer,
    PaymentSerializer,
    OrderSerializer,
)
from hubspot.task_helpers import sync_hubspot_application_from_order
from klasses.models import BootcampRun
from klasses.permissions import CanReadIfSelf
from main.permissions import UserIsOwnerOrAdminPermission
from main.serializers import serialize_maybe_user


log = logging.getLogger(__name__)
User = get_user_model()


class PaymentView(CreateAPIView):
    """
    View for payment API. This creates an Order in our system and provides a dictionary to send to Cybersource.