Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
97 lines
3.9 KiB
Python
97 lines
3.9 KiB
Python
import json
|
|
import logging
|
|
|
|
from django.conf import settings
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.http import HttpRequest, HttpResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from corporate.models.stripe_state import Event, Invoice, Session
|
|
from zproject.config import get_secret
|
|
|
|
billing_logger = logging.getLogger("corporate.stripe")
|
|
|
|
|
|
@csrf_exempt
|
|
def stripe_webhook(request: HttpRequest) -> HttpResponse:
|
|
import stripe
|
|
|
|
from corporate.lib.stripe import STRIPE_API_VERSION
|
|
from corporate.lib.stripe_event_handler import (
|
|
handle_checkout_session_completed_event,
|
|
handle_invoice_paid_event,
|
|
handle_invoice_voided_event,
|
|
)
|
|
|
|
stripe_webhook_endpoint_secret = get_secret("stripe_webhook_endpoint_secret", "")
|
|
if (
|
|
stripe_webhook_endpoint_secret and not settings.TEST_SUITE
|
|
): # nocoverage: We can't verify the signature in test suite since we fetch the events
|
|
# from Stripe events API and manually post to the webhook endpoint.
|
|
stripe_signature = request.headers.get("Stripe-Signature")
|
|
if stripe_signature is None:
|
|
return HttpResponse(status=400)
|
|
try:
|
|
stripe_event = stripe.Webhook.construct_event(
|
|
request.body,
|
|
stripe_signature,
|
|
stripe_webhook_endpoint_secret,
|
|
)
|
|
except ValueError:
|
|
return HttpResponse(status=400)
|
|
except stripe.SignatureVerificationError:
|
|
return HttpResponse(status=400)
|
|
else:
|
|
assert not settings.PRODUCTION
|
|
try:
|
|
stripe_event = stripe.Event.construct_from(json.loads(request.body), stripe.api_key)
|
|
except Exception:
|
|
return HttpResponse(status=400)
|
|
|
|
if stripe_event.api_version != STRIPE_API_VERSION:
|
|
error_message = f"Mismatch between billing system Stripe API version({STRIPE_API_VERSION}) and Stripe webhook event API version({stripe_event.api_version})."
|
|
billing_logger.error(error_message)
|
|
return HttpResponse(status=400)
|
|
|
|
if stripe_event.type not in [
|
|
"checkout.session.completed",
|
|
"invoice.paid",
|
|
"invoice.voided",
|
|
]:
|
|
return HttpResponse(status=200)
|
|
|
|
if Event.objects.filter(stripe_event_id=stripe_event.id).exists():
|
|
return HttpResponse(status=200)
|
|
|
|
event = Event(stripe_event_id=stripe_event.id, type=stripe_event.type)
|
|
|
|
if stripe_event.type == "checkout.session.completed":
|
|
stripe_session = stripe_event.data.object
|
|
assert isinstance(stripe_session, stripe.checkout.Session)
|
|
try:
|
|
session = Session.objects.get(stripe_session_id=stripe_session.id)
|
|
except Session.DoesNotExist:
|
|
return HttpResponse(status=200)
|
|
event.content_type = ContentType.objects.get_for_model(Session)
|
|
event.object_id = session.id
|
|
event.save()
|
|
handle_checkout_session_completed_event(stripe_session, event)
|
|
elif stripe_event.type in ("invoice.paid", "invoice.voided"):
|
|
stripe_invoice = stripe_event.data.object
|
|
assert isinstance(stripe_invoice, stripe.Invoice)
|
|
try:
|
|
invoice = Invoice.objects.get(stripe_invoice_id=stripe_invoice.id)
|
|
except Invoice.DoesNotExist:
|
|
return HttpResponse(status=200)
|
|
event.content_type = ContentType.objects.get_for_model(Invoice)
|
|
event.object_id = invoice.id
|
|
event.save()
|
|
if stripe_event.type == "invoice.paid":
|
|
handle_invoice_paid_event(stripe_invoice, event)
|
|
else:
|
|
handle_invoice_voided_event(stripe_invoice, event)
|
|
# We don't need to process failed payments via webhooks since we directly charge users
|
|
# when they click on "Purchase" button and immediately provide feedback for failed payments.
|
|
# If the feedback is not immediate, our event_status handler checks for payment status and informs the user.
|
|
return HttpResponse(status=200)
|