From 9f57f74955cfb4e27e47ec73cbd3c2e9ba9413f7 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 6 Apr 2023 21:45:11 +0200 Subject: [PATCH] Return 200 code on unknown subscription --- CHANGELOG.md | 6 ++++++ api/exceptions.py | 8 ++++++++ api/v1/services.py | 12 +++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8098dc..e47c5c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.0.36] - 2023-04-06 + +### Fixed + +- Minor bugs [@AivGitHub](https://github.com/AivGitHub/). + ## [0.0.35] - 2023-04-06 ### Improved diff --git a/api/exceptions.py b/api/exceptions.py index d0b95c9..728d3e2 100644 --- a/api/exceptions.py +++ b/api/exceptions.py @@ -17,3 +17,11 @@ def __init__(self, detail=None): if detail is None: detail = 'Not authenticated' super().__init__(detail=detail, code=status.HTTP_401_UNAUTHORIZED) + + +class FeatureNotReady(BaseCustomException): + def __init__(self, detail=None): + if detail is None: + detail = 'Not implemented' + # Should be ``status.HTTP_501_NOT_IMPLEMENTED``, but webhook requires 200-299 response code. + super().__init__(detail=detail, code=status.HTTP_200_OK) diff --git a/api/v1/services.py b/api/v1/services.py index c3ec2b3..9f5b0d8 100644 --- a/api/v1/services.py +++ b/api/v1/services.py @@ -1,3 +1,4 @@ +from api.exceptions import FeatureNotReady from payments.core import stripe from payments.models import get_payment_instance, Subscription @@ -15,7 +16,16 @@ def process_post_request(self): self.customer_subscription_updated() def customer_subscription_updated(self): - payment_instance = Subscription.objects.get(psp_id=self.event.data.object.id) + try: + payment_instance = Subscription.objects.get(psp_id=self.event.data.object.id) + except Subscription.DoesNotExist: + # It can be in case payment was declined or something similar. + # For now, it's not important, subscription not created locally, because payment is no successful. + # I have no time to implement proper webhook handler. + # TODO: https://github.com/AivGitHub/brosfiles/issues/4 task for webhook handler. + # I would appreciate any help in this. + raise FeatureNotReady() + payment_instance.update_from_event(self.event) def checkout_session_completed(self):