diff --git a/develop/develop/settings.py b/develop/develop/settings.py index 1179ecd4..569b20fa 100644 --- a/develop/develop/settings.py +++ b/develop/develop/settings.py @@ -63,6 +63,8 @@ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + # Add the account middleware: + "allauth.account.middleware.AccountMiddleware", ] ROOT_URLCONF = 'develop.urls' diff --git a/requirements.dev.txt b/requirements.dev.txt index deb674ae..9c1829fc 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -141,4 +141,4 @@ typing-extensions==4.7.1 # dj-database-url # mypy urllib3==2.0.3 - # via requests + # via requests \ No newline at end of file diff --git a/src/vendor/api/v1/stripe/views.py b/src/vendor/api/v1/stripe/views.py index b369ef1d..434d4b76 100644 --- a/src/vendor/api/v1/stripe/views.py +++ b/src/vendor/api/v1/stripe/views.py @@ -2,6 +2,7 @@ import logging import stripe +from django import dispatch from django.conf import settings from django.core.exceptions import ObjectDoesNotExist from django.http.response import HttpResponse @@ -21,22 +22,22 @@ logger = logging.getLogger(__name__) -# TODO: Need to add more validation to function example: -# The lowest number can only 50 which transaltes to $0.50 -# Probably should also added it to the processor as a static function -def convert_integer_to_float(number): - number_string = str(number) - return float(f"{number_string[:-2]}.{number_string[-2:]}") + +########## +# SIGNALS +stripe_invoice_upcoming = dispatch.Signal() class StripeEvents(TextChoices): INVOICE_PAID = 'invoice.paid', _('Invoice Paid') INVOICE_PAYMENT_FAILED = 'invoice.payment_failed', _('Invoice Payment Failed') INOVICE_PAYMENT_SUCCEEDED = 'invoice.payment_succeeded', _('Invoice Payment Succeeded') + INVOICE_UPCOMING = 'invoice.upcoming', _('Upcoming Invoice') PAYMENT_INTENT_SUCCEDED = 'payment_intent.succeeded', _("Payment Succeeded") CHARGE_SUCCEEDED = 'charge.succeeded', _('Charge Succeeded') SOURCE_EXPIRED = 'customer.source.expired', _('Source Expired') + SUBSCRIPTION_TRIAL_END = 'customer.subscription.trial_will_end', _('Trial Period Will End') class StripeBaseAPI(View): @@ -93,8 +94,6 @@ class StripeSubscriptionInvoicePaid(StripeBaseAPI): def post(self, request, *args, **kwargs): stripe_invoice = self.event.data.object - - site = get_site_from_request(self.request) processor = StripeProcessor(site) @@ -360,3 +359,26 @@ def post(self, request, *args, **kwargs): # Subscription Invoice return process_stripe_invoice_subscription_payment_succeded(stripe_invoice, site) + +class StripeInvoiceUpcomingEvent(StripeBaseAPI): + def post(self, request, *args, **kwargs): + site = get_site_from_request(request) + processor = StripeProcessor(site) + + if not self.is_valid_post(site): + logger.error("StripeInvoicePaid error: invalid post") + return HttpResponse(status=200, content="StripeInvoicePaid error: invalid post") + + if not self.is_incoming_event_correct(self.event, StripeEvents.INVOICE_UPCOMING): + logger.error(f"StripeInvoicePaid error: invalid event {self.event}") + return HttpResponse(status=200, content=f"StripeInvoicePaid error: invalid event {self.event}") + + stripe_invoice = self.event.data.object + customer_profile, stripe_customer = processor.get_customer_profile_and_stripe_customer(stripe_invoice.customer) + if not customer_profile or not stripe_customer: + logger.error(f"error retrieving customer information for request: {self.event}") + return HttpResponse(status=200) + + logger.info(f"Upcoming Invoice for stripe_customer: {stripe_customer} customer_profile: {customer_profile}") + + stripe_invoice_upcoming.send(sender=self.__class__, customer_profile=customer_profile) diff --git a/src/vendor/api/v1/urls.py b/src/vendor/api/v1/urls.py index eca5dbf7..f49e6b0c 100644 --- a/src/vendor/api/v1/urls.py +++ b/src/vendor/api/v1/urls.py @@ -28,4 +28,5 @@ path('stripe/subscription/invoice/payment/failed/', stripe_views.StripeSubscriptionPaymentFailed.as_view(), name='api-stripe-subscription-invoice-payment-failed'), path('stripe/sync/objects/', stripe_views.StripeSyncObjects.as_view(), name='api-stripe-sync-objects'), path('stripe/card/expiring/', stripe_views.StripeCardExpiring.as_view(), name='api-stripe-card-expiring'), + path('stripe/invoice/upcoming/', stripe_views.StripeInvoiceUpcomingEvent.as_view(), name='api-stripe-invoice-upcoming'), ] \ No newline at end of file