Refactor code and log messages for possible errors

This commit is contained in:
PCoder 2017-12-24 14:38:32 +01:00
parent 29c24574aa
commit eb067f5708

View file

@ -535,21 +535,35 @@ class MembershipDeactivateView(LoginRequiredMixin, UpdateView):
membership_order = MembershipOrder.objects.filter( membership_order = MembershipOrder.objects.filter(
customer__user=self.request.user customer__user=self.request.user
).last() ).last()
if membership_order.subscription_id: if membership_order:
result = stripe_utils.unsubscribe_customer( if membership_order.subscription_id:
subscription_id=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 stripe_subscription_obj = result.get('response_object')
if (stripe_subscription_obj is None or # Check if the subscription was canceled
stripe_subscription_obj.status != 'canceled'): if (stripe_subscription_obj is None or
error_msg = result.get('error') stripe_subscription_obj.status != 'canceled'):
error_msg = result.get('error')
logger.error(
"Could not cancel Digital Glarus subscription. "
"Reason: {reason}".format(
reason=error_msg
)
)
else:
logger.error( logger.error(
"Could not cancel Digital Glarus subscription. Reason: " "User {user} may have Stripe subscriptions created "
"{reason}".format( "manually. Please check.".format(
reason=error_msg user=self.request.user.name
) )
) )
else:
logger.error(
"MembershipOrder for {user} not found".format(
user=self.request.user.name
)
)
return HttpResponseRedirect(self.success_url) return HttpResponseRedirect(self.success_url)