Cancel subscription on membership deactivation + some more reorganizes + import logger

This commit is contained in:
PCoder 2017-12-24 14:25:04 +01:00
parent 707e1897b3
commit 29c24574aa

View file

@ -1,3 +1,5 @@
import logging
from django.conf import settings from django.conf import settings
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
@ -35,10 +37,7 @@ from .mixins import (
MembershipRequiredMixin, IsNotMemberMixin, ChangeMembershipStatusMixin MembershipRequiredMixin, IsNotMemberMixin, ChangeMembershipStatusMixin
) )
from .models import MembershipType, Membership, MembershipOrder, Booking, BookingPrice,\ logger = logging.getLogger(__name__)
BookingOrder, BookingCancellation
from .mixins import MembershipRequiredMixin, IsNotMemberMixin
class IndexView(TemplateView): class IndexView(TemplateView):
@ -282,7 +281,6 @@ class BookingPaymentView(LoginRequiredMixin, MembershipRequiredMixin, FormView):
booking_data = { booking_data = {
'start_date': start_date, 'start_date': start_date,
'end_date': end_date, 'end_date': end_date,
'start_date': start_date,
'free_days': free_days, 'free_days': free_days,
'price': normal_price, 'price': normal_price,
'final_price': final_price, 'final_price': final_price,
@ -529,8 +527,29 @@ class MembershipDeactivateView(LoginRequiredMixin, UpdateView):
def post(self, *args, **kwargs): def post(self, *args, **kwargs):
membership = self.get_object() membership = self.get_object()
membership.deactivate() membership.deactivate()
messages.add_message(
messages.add_message(self.request, messages.SUCCESS, self.success_message) self.request, messages.SUCCESS, self.success_message
)
# cancel Stripe subscription
stripe_utils = StripeUtils()
membership_order = MembershipOrder.objects.filter(
customer__user=self.request.user
).last()
if membership_order.subscription_id:
result = stripe_utils.unsubscribe_customer(
subscription_id=membership_order.subscription_id
)
stripe_subscription_obj = result.get('response_object')
# Check if the subscription was canceled
if (stripe_subscription_obj is None or
stripe_subscription_obj.status != 'canceled'):
error_msg = result.get('error')
logger.error(
"Could not cancel Digital Glarus subscription. Reason: "
"{reason}".format(
reason=error_msg
)
)
return HttpResponseRedirect(self.success_url) return HttpResponseRedirect(self.success_url)