Log: improve logging in gold.views module
-# -*- coding: utf-8 -*-
-
"""Gold subscription views."""
import json
@@ -16,17 +14,16 @@ from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from vanilla import DetailView, FormView, GenericView
-from rest_framework.exceptions import APIException
from rest_framework.views import APIView
from rest_framework import permissions
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from readthedocs.core.mixins import PrivateViewMixin
-from readthedocs.projects.models import Domain, Project
+from readthedocs.projects.models import Project
from .forms import GoldProjectForm, GoldSubscriptionForm
-from .models import GoldUser, LEVEL_CHOICES
+from .models import GoldUser
log = structlog.get_logger(__name__)
@@ -222,6 +219,7 @@ class StripeEventView(APIView):
def post(self, request, format=None):
try:
event = stripe.Event.construct_from(request.data, settings.STRIPE_SECRET)
+ log.bind(event=event.type)
if event.type not in self.EVENTS:
log.warning('Unhandled Stripe event.', event_type=event.type)
return Response({
@@ -230,14 +228,18 @@ class StripeEventView(APIView):
})
stripe_customer = event.data.object.customer
+ log.bind(stripe_customer=stripe_customer)
if event.type == self.EVENT_CHECKOUT_COMPLETED:
username = event.data.object.client_reference_id
+ log.bind(user_username=username)
mode = event.data.object.mode
if mode == 'subscription':
# Gold Membership
user = User.objects.get(username=username)
subscription = stripe.Subscription.retrieve(event.data.object.subscription)
+ log.bind(gold_level=subscription.plan.id)
+ log.info('Gold Membership subscrpition.')
gold, _ = GoldUser.objects.get_or_create(
user=user,
stripe_id=stripe_customer,
@@ -252,6 +254,10 @@ class StripeEventView(APIView):
from readthedocsext.donate.utils import handle_payment_webhook
stripe_session = event.data.object.id
price_in_cents = event.data.object.amount_total
+ log.info(
+ 'Gold Membership one-time donation.',
+ price_in_cents=price_in_cents,
+ )
handle_payment_webhook(
username,
stripe_customer,
@@ -266,20 +272,16 @@ class StripeEventView(APIView):
elif event.type == self.EVENT_CHECKOUT_PAYMENT_FAILED:
username = event.data.object.client_reference_id
+ log.bind(user_username=username)
# TODO: add user notification saying it failed
- log.exception(
- 'Gold User payment failed.',
- user_username=username,
- stripe_customer=stripe_customer,
- )
+ log.exception('Gold User payment failed.')
elif event.type == self.EVENT_CUSTOMER_SUBSCRIPTION_UPDATED:
subscription = event.data.object
level = subscription.plan.id
log.info(
'Gold User subscription updated.',
- stripe_customer=stripe_customer,
- level=level,
+ gold_level=level,
)
(
GoldUser.objects
@@ -291,11 +293,8 @@ class StripeEventView(APIView):
)
if subscription.status != 'active':
- # TODO: check if the subscription was canceled, past due, etc
- # and take the according action. Only acummulate errors on Sentry for now.
- log.error(
+ log.warning(
'GoldUser is not active anymore.',
- stripe_customer=stripe_customer,
)
except Exception:
log.exception('Unexpected data in Stripe Event object')
