Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
corporate: Handle Stripe invoice.voided webhook events.
Aman Agrawalcc388a66c684
diff --git a/corporate/lib/stripe_event_handler.py b/corporate/lib/stripe_event_handler.pyindex c0989364090..3a378520f24 100644--- a/corporate/lib/stripe_event_handler.py+++ b/corporate/lib/stripe_event_handler.py@@ -214,3 +214,9 @@ def handle_invoice_paid_event(stripe_invoice: stripe.Invoice, invoice: Invoice)complimentary_access_plan=complimentary_access_plan,stripe_invoice_paid=True,)+++@stripe_event_handler_decorator+def handle_invoice_voided_event(stripe_invoice: stripe.Invoice, invoice: Invoice) -> None:+ invoice.status = Invoice.VOID+ invoice.save(update_fields=["status"])diff --git a/corporate/lib/test_stripe_class.py b/corporate/lib/test_stripe_class.pyindex 9859fa5c0fc..74927236694 100644--- a/corporate/lib/test_stripe_class.py+++ b/corporate/lib/test_stripe_class.py@@ -488,6 +488,7 @@ HANDLED_STRIPE_EVENT_TYPES: frozenset[str] = frozenset({"checkout.session.completed","invoice.paid",+ "invoice.voided",})diff --git a/corporate/models/stripe_state.py b/corporate/models/stripe_state.pyindex 5765232ac07..787afe24308 100644--- a/corporate/models/stripe_state.py+++ b/corporate/models/stripe_state.py@@ -162,9 +162,11 @@ class Invoice(models.Model):def get_last_associated_event(self) -> Event | None:if self.status == Invoice.PAID:event_type = "invoice.paid"- # TODO: Add test for this case. Not sure how to trigger naturally.- else: # nocoverage- return None # nocoverage+ elif self.status == Invoice.VOID:+ event_type = "invoice.voided"+ # A SENT invoice has no associated event yet.+ else:+ return Nonereturn get_last_associated_event_by_type(self, event_type)def to_dict(self) -> dict[str, Any]:diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Customer.create.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Customer.create.1.jsonnew file mode 100644index 00000000000..c1abfe314fa--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Customer.create.1.json@@ -0,0 +1,33 @@+{+ "address": null,+ "balance": 0,+ "created": 1000000000,+ "currency": null,+ "customer_account": null,+ "default_source": null,+ "delinquent": false,+ "description": "zulip (Zulip Dev)",+ "discount": null,+ "email": "[email protected]",+ "id": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "invoice_prefix": "NORMALIZED",+ "invoice_settings": {+ "custom_fields": null,+ "default_payment_method": null,+ "footer": null,+ "rendering_options": null+ },+ "livemode": false,+ "metadata": {+ "realm_id": "1",+ "realm_str": "zulip"+ },+ "name": null,+ "next_invoice_sequence": 1,+ "object": "customer",+ "phone": null,+ "preferred_locales": [],+ "shipping": null,+ "tax_exempt": "none",+ "test_clock": null+}diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Customer.retrieve.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Customer.retrieve.1.jsonnew file mode 100644index 00000000000..c1abfe314fa--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Customer.retrieve.1.json@@ -0,0 +1,33 @@+{+ "address": null,+ "balance": 0,+ "created": 1000000000,+ "currency": null,+ "customer_account": null,+ "default_source": null,+ "delinquent": false,+ "description": "zulip (Zulip Dev)",+ "discount": null,+ "email": "[email protected]",+ "id": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "invoice_prefix": "NORMALIZED",+ "invoice_settings": {+ "custom_fields": null,+ "default_payment_method": null,+ "footer": null,+ "rendering_options": null+ },+ "livemode": false,+ "metadata": {+ "realm_id": "1",+ "realm_str": "zulip"+ },+ "name": null,+ "next_invoice_sequence": 1,+ "object": "customer",+ "phone": null,+ "preferred_locales": [],+ "shipping": null,+ "tax_exempt": "none",+ "test_clock": null+}diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Event.list.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Event.list.1.jsonnew file mode 100644index 00000000000..b21715938af--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Event.list.1.json@@ -0,0 +1,180 @@+{+ "data": [+ {+ "api_version": "2025-11-17.clover",+ "created": 1000000000,+ "data": {+ "object": {+ "account_country": "US",+ "account_name": "NORMALIZED",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_overpaid": 0,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": false,+ "automatic_tax": {+ "disabled_reason": null,+ "enabled": false,+ "liability": null,+ "provider": null,+ "status": null+ },+ "automatically_finalizes_at": null,+ "billing_reason": "manual",+ "collection_method": "send_invoice",+ "created": 1000000000,+ "currency": "usd",+ "custom_fields": null,+ "customer": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "customer_account": null,+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": "If paying by bank transfer outside of the US, use the OUR option for who should pay bank transfer fees, to make sure that your invoice is paid in full.",+ "discounts": [],+ "due_date": 1000000000,+ "effective_at": 1000000000,+ "ending_balance": 0,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": "https://invoice.stripe.com/i/acct_NORMALIZED/test_NORMALIZED?s=ap",+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice_pdf": "https://pay.stripe.com/invoice/acct_NORMALIZED/test_NORMALIZED/pdf?s=ap",+ "issuer": {+ "type": "self"+ },+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "parent": {+ "invoice_item_details": {+ "invoice_item": "ii_NORMALIZED",+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "subscription": null+ },+ "license_fee_subscription_details": null,+ "subscription_item_details": null,+ "type": "invoice_item_details"+ },+ "period": {+ "end": 1362193445,+ "start": 1330657445+ },+ "pretax_credit_amounts": [],+ "pricing": {+ "price_details": {+ "price": "price_NORMALIZED",+ "product": "prod_NORMALIZED"+ },+ "type": "price_details",+ "unit_amount_decimal": "8000"+ },+ "quantity": 123,+ "quantity_decimal": "123",+ "subtotal": 984000,+ "taxes": []+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/free_trial_upgrade_by_invoice_voided--Event.list.1.json/lines"+ },+ "livemode": false,+ "metadata": {+ "billing_schedule": "1",+ "current_plan_id": "1",+ "license_management": "manual",+ "licenses": "123",+ "on_free_trial": "True",+ "plan_tier": "1",+ "user_id": "10"+ },+ "next_payment_attempt": null,+ "number": "NORMALIZED",+ "object": "invoice",+ "on_behalf_of": null,+ "parent": null,+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1000000000,+ "period_start": 1000000000,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "letter"+ },+ "template": null,+ "template_version": null+ },+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "void",+ "status_transitions": {+ "finalized_at": 1000000000,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": 1000000000+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_pretax_credit_amounts": [],+ "total_taxes": [],+ "webhooks_delivered_at": null+ }+ },+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "livemode": false,+ "object": "event",+ "pending_webhooks": 0,+ "request": {+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "idempotency_key": "00000000-0000-0000-0000-000000000000"+ },+ "type": "invoice.voided"+ }+ ],+ "has_more": false,+ "object": "list",+ "url": "/v1/events"+}diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.create.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.create.1.jsonnew file mode 100644index 00000000000..a28fec8ea29--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.create.1.json@@ -0,0 +1,114 @@+{+ "account_country": "US",+ "account_name": "NORMALIZED",+ "account_tax_ids": null,+ "amount_due": 0,+ "amount_overpaid": 0,+ "amount_paid": 0,+ "amount_remaining": 0,+ "amount_shipping": 0,+ "application": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": true,+ "automatic_tax": {+ "disabled_reason": null,+ "enabled": false,+ "liability": null,+ "provider": null,+ "status": null+ },+ "automatically_finalizes_at": 1000000000,+ "billing_reason": "manual",+ "collection_method": "send_invoice",+ "created": 1000000000,+ "currency": "usd",+ "custom_fields": null,+ "customer": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "customer_account": null,+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": "If paying by bank transfer outside of the US, use the OUR option for who should pay bank transfer fees, to make sure that your invoice is paid in full.",+ "discounts": [],+ "due_date": 1000000000,+ "effective_at": null,+ "ending_balance": null,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": null,+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice_pdf": null,+ "issuer": {+ "type": "self"+ },+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [],+ "has_more": false,+ "object": "list",+ "total_count": 0,+ "url": "/v1/invoices/free_trial_upgrade_by_invoice_voided--Event.list.1.json/lines"+ },+ "livemode": false,+ "metadata": {+ "billing_schedule": "1",+ "current_plan_id": "1",+ "license_management": "manual",+ "licenses": "123",+ "on_free_trial": "True",+ "plan_tier": "1",+ "user_id": "10"+ },+ "next_payment_attempt": null,+ "number": null,+ "object": "invoice",+ "on_behalf_of": null,+ "parent": null,+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1000000000,+ "period_start": 1000000000,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "auto"+ },+ "template": null,+ "template_version": null+ },+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "draft",+ "status_transitions": {+ "finalized_at": null,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": null+ },+ "subtotal": 0,+ "subtotal_excluding_tax": 0,+ "test_clock": null,+ "total": 0,+ "total_discount_amounts": [],+ "total_excluding_tax": 0,+ "total_pretax_credit_amounts": [],+ "total_taxes": [],+ "webhooks_delivered_at": null+}diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.finalize_invoice.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.finalize_invoice.1.jsonnew file mode 100644index 00000000000..f44aa7ac17a--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.finalize_invoice.1.json@@ -0,0 +1,158 @@+{+ "account_country": "US",+ "account_name": "NORMALIZED",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_overpaid": 0,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": true,+ "automatic_tax": {+ "disabled_reason": null,+ "enabled": false,+ "liability": null,+ "provider": null,+ "status": null+ },+ "automatically_finalizes_at": null,+ "billing_reason": "manual",+ "collection_method": "send_invoice",+ "created": 1000000000,+ "currency": "usd",+ "custom_fields": null,+ "customer": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "customer_account": null,+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": "If paying by bank transfer outside of the US, use the OUR option for who should pay bank transfer fees, to make sure that your invoice is paid in full.",+ "discounts": [],+ "due_date": 1000000000,+ "effective_at": 1000000000,+ "ending_balance": 0,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": "https://invoice.stripe.com/i/acct_NORMALIZED/test_NORMALIZED?s=ap",+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice_pdf": "https://pay.stripe.com/invoice/acct_NORMALIZED/test_NORMALIZED/pdf?s=ap",+ "issuer": {+ "type": "self"+ },+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "parent": {+ "invoice_item_details": {+ "invoice_item": "ii_NORMALIZED",+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "subscription": null+ },+ "license_fee_subscription_details": null,+ "subscription_item_details": null,+ "type": "invoice_item_details"+ },+ "period": {+ "end": 1362193445,+ "start": 1330657445+ },+ "pretax_credit_amounts": [],+ "pricing": {+ "price_details": {+ "price": "price_NORMALIZED",+ "product": "prod_NORMALIZED"+ },+ "type": "price_details",+ "unit_amount_decimal": "8000"+ },+ "quantity": 123,+ "quantity_decimal": "123",+ "subtotal": 984000,+ "taxes": []+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/free_trial_upgrade_by_invoice_voided--Event.list.1.json/lines"+ },+ "livemode": false,+ "metadata": {+ "billing_schedule": "1",+ "current_plan_id": "1",+ "license_management": "manual",+ "licenses": "123",+ "on_free_trial": "True",+ "plan_tier": "1",+ "user_id": "10"+ },+ "next_payment_attempt": null,+ "number": "NORMALIZED",+ "object": "invoice",+ "on_behalf_of": null,+ "parent": null,+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1000000000,+ "period_start": 1000000000,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "letter"+ },+ "template": null,+ "template_version": null+ },+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "open",+ "status_transitions": {+ "finalized_at": 1000000000,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": null+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_pretax_credit_amounts": [],+ "total_taxes": [],+ "webhooks_delivered_at": null+}diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.list.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.list.1.jsonnew file mode 100644index 00000000000..b89cb564c3a--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.list.1.json@@ -0,0 +1,165 @@+{+ "data": [+ {+ "account_country": "US",+ "account_name": "NORMALIZED",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_overpaid": 0,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": true,+ "automatic_tax": {+ "disabled_reason": null,+ "enabled": false,+ "liability": null,+ "provider": null,+ "status": null+ },+ "automatically_finalizes_at": null,+ "billing_reason": "manual",+ "collection_method": "send_invoice",+ "created": 1000000000,+ "currency": "usd",+ "custom_fields": null,+ "customer": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "customer_account": null,+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": "If paying by bank transfer outside of the US, use the OUR option for who should pay bank transfer fees, to make sure that your invoice is paid in full.",+ "discounts": [],+ "due_date": 1000000000,+ "effective_at": 1000000000,+ "ending_balance": 0,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": "https://invoice.stripe.com/i/acct_NORMALIZED/test_NORMALIZED?s=ap",+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice_pdf": "https://pay.stripe.com/invoice/acct_NORMALIZED/test_NORMALIZED/pdf?s=ap",+ "issuer": {+ "type": "self"+ },+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "parent": {+ "invoice_item_details": {+ "invoice_item": "ii_NORMALIZED",+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "subscription": null+ },+ "license_fee_subscription_details": null,+ "subscription_item_details": null,+ "type": "invoice_item_details"+ },+ "period": {+ "end": 1362193445,+ "start": 1330657445+ },+ "pretax_credit_amounts": [],+ "pricing": {+ "price_details": {+ "price": "price_NORMALIZED",+ "product": "prod_NORMALIZED"+ },+ "type": "price_details",+ "unit_amount_decimal": "8000"+ },+ "quantity": 123,+ "quantity_decimal": "123",+ "subtotal": 984000,+ "taxes": []+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/free_trial_upgrade_by_invoice_voided--Event.list.1.json/lines"+ },+ "livemode": false,+ "metadata": {+ "billing_schedule": "1",+ "current_plan_id": "1",+ "license_management": "manual",+ "licenses": "123",+ "on_free_trial": "True",+ "plan_tier": "1",+ "user_id": "10"+ },+ "next_payment_attempt": null,+ "number": "NORMALIZED",+ "object": "invoice",+ "on_behalf_of": null,+ "parent": null,+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1000000000,+ "period_start": 1000000000,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "letter"+ },+ "template": null,+ "template_version": null+ },+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "open",+ "status_transitions": {+ "finalized_at": 1000000000,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": null+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_pretax_credit_amounts": [],+ "total_taxes": [],+ "webhooks_delivered_at": null+ }+ ],+ "has_more": false,+ "object": "list",+ "url": "/v1/invoices"+}diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.void_invoice.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.void_invoice.1.jsonnew file mode 100644index 00000000000..4931c00270f--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--Invoice.void_invoice.1.json@@ -0,0 +1,158 @@+{+ "account_country": "US",+ "account_name": "NORMALIZED",+ "account_tax_ids": null,+ "amount_due": 984000,+ "amount_overpaid": 0,+ "amount_paid": 0,+ "amount_remaining": 984000,+ "amount_shipping": 0,+ "application": null,+ "attempt_count": 0,+ "attempted": false,+ "auto_advance": false,+ "automatic_tax": {+ "disabled_reason": null,+ "enabled": false,+ "liability": null,+ "provider": null,+ "status": null+ },+ "automatically_finalizes_at": null,+ "billing_reason": "manual",+ "collection_method": "send_invoice",+ "created": 1000000000,+ "currency": "usd",+ "custom_fields": null,+ "customer": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "customer_account": null,+ "customer_address": null,+ "customer_email": "[email protected]",+ "customer_name": null,+ "customer_phone": null,+ "customer_shipping": null,+ "customer_tax_exempt": "none",+ "customer_tax_ids": [],+ "default_payment_method": null,+ "default_source": null,+ "default_tax_rates": [],+ "description": "If paying by bank transfer outside of the US, use the OUR option for who should pay bank transfer fees, to make sure that your invoice is paid in full.",+ "discounts": [],+ "due_date": 1000000000,+ "effective_at": 1000000000,+ "ending_balance": 0,+ "footer": null,+ "from_invoice": null,+ "hosted_invoice_url": "https://invoice.stripe.com/i/acct_NORMALIZED/test_NORMALIZED?s=ap",+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice_pdf": "https://pay.stripe.com/invoice/acct_NORMALIZED/test_NORMALIZED/pdf?s=ap",+ "issuer": {+ "type": "self"+ },+ "last_finalization_error": null,+ "latest_revision": null,+ "lines": {+ "data": [+ {+ "amount": 984000,+ "currency": "usd",+ "description": "Zulip Cloud Standard",+ "discount_amounts": [],+ "discountable": false,+ "discounts": [],+ "id": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "invoice": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "livemode": false,+ "metadata": {},+ "object": "line_item",+ "parent": {+ "invoice_item_details": {+ "invoice_item": "ii_NORMALIZED",+ "proration": false,+ "proration_details": {+ "credited_items": null+ },+ "subscription": null+ },+ "license_fee_subscription_details": null,+ "subscription_item_details": null,+ "type": "invoice_item_details"+ },+ "period": {+ "end": 1362193445,+ "start": 1330657445+ },+ "pretax_credit_amounts": [],+ "pricing": {+ "price_details": {+ "price": "price_NORMALIZED",+ "product": "prod_NORMALIZED"+ },+ "type": "price_details",+ "unit_amount_decimal": "8000"+ },+ "quantity": 123,+ "quantity_decimal": "123",+ "subtotal": 984000,+ "taxes": []+ }+ ],+ "has_more": false,+ "object": "list",+ "total_count": 1,+ "url": "/v1/invoices/free_trial_upgrade_by_invoice_voided--Event.list.1.json/lines"+ },+ "livemode": false,+ "metadata": {+ "billing_schedule": "1",+ "current_plan_id": "1",+ "license_management": "manual",+ "licenses": "123",+ "on_free_trial": "True",+ "plan_tier": "1",+ "user_id": "10"+ },+ "next_payment_attempt": null,+ "number": "NORMALIZED",+ "object": "invoice",+ "on_behalf_of": null,+ "parent": null,+ "payment_settings": {+ "default_mandate": null,+ "payment_method_options": null,+ "payment_method_types": null+ },+ "period_end": 1000000000,+ "period_start": 1000000000,+ "post_payment_credit_notes_amount": 0,+ "pre_payment_credit_notes_amount": 0,+ "receipt_number": null,+ "rendering": {+ "amount_tax_display": null,+ "pdf": {+ "page_size": "letter"+ },+ "template": null,+ "template_version": null+ },+ "shipping_cost": null,+ "shipping_details": null,+ "starting_balance": 0,+ "statement_descriptor": "Zulip Cloud Standard",+ "status": "void",+ "status_transitions": {+ "finalized_at": 1000000000,+ "marked_uncollectible_at": null,+ "paid_at": null,+ "voided_at": 1000000000+ },+ "subtotal": 984000,+ "subtotal_excluding_tax": 984000,+ "test_clock": null,+ "total": 984000,+ "total_discount_amounts": [],+ "total_excluding_tax": 984000,+ "total_pretax_credit_amounts": [],+ "total_taxes": [],+ "webhooks_delivered_at": null+}diff --git a/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--InvoiceItem.create.1.json b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--InvoiceItem.create.1.jsonnew file mode 100644index 00000000000..29c497176e1--- /dev/null+++ b/corporate/tests/stripe_fixtures/free_trial_upgrade_by_invoice_voided--InvoiceItem.create.1.json@@ -0,0 +1,34 @@+{+ "amount": 984000,+ "currency": "usd",+ "customer": "free_trial_upgrade_by_invoice_voided--Customer.create.1.json",+ "customer_account": null,+ "date": 1000000000,+ "description": "Zulip Cloud Standard",+ "discountable": false,+ "discounts": [],+ "id": "free_trial_upgrade_by_invoice_voided--InvoiceItem.create.1.json",+ "invoice": "free_trial_upgrade_by_invoice_voided--Event.list.1.json",+ "livemode": false,+ "metadata": {},+ "net_amount": 984000,+ "object": "invoiceitem",+ "parent": null,+ "period": {+ "end": 1362193445,+ "start": 1330657445+ },+ "pricing": {+ "price_details": {+ "price": "price_NORMALIZED",+ "product": "prod_NORMALIZED"+ },+ "type": "price_details",+ "unit_amount_decimal": "8000"+ },+ "proration": false,+ "quantity": 123,+ "quantity_decimal": "123",+ "tax_rates": [],+ "test_clock": null+}diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex a2d8a12d67a..0db13bd0100 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -1012,6 +1012,36 @@ class StripeTest(StripeTestCase):# No additional ledger entries are created.self.assertEqual(before_ledger_count, after_ledger_count)+ @mock_stripe()+ def test_free_trial_upgrade_by_invoice_voided(self, *mocks: Mock) -> None:+ user = self.example_user("hamlet")+ self.login_user(user)++ with self.settings(CLOUD_FREE_TRIAL_DAYS=60), time_machine.travel(self.now, tick=False):+ self.upgrade(invoice=True)++ customer = Customer.objects.get(realm=user.realm)+ stripe_customer_id = assert_is_not_none(customer.stripe_customer_id)+ [stripe_invoice] = iter(stripe.Invoice.list(customer=stripe_customer_id))+ assert stripe_invoice.id is not None+ self.assertEqual(stripe_invoice.status, "open")++ invoice = Invoice.objects.get(customer=customer, stripe_invoice_id=stripe_invoice.id)+ self.assertEqual(invoice.status, Invoice.SENT)+ self.assertIsNone(invoice.get_last_associated_event())++ # Support voids the unpaid invoice in Stripe, delivering an invoice.voided webhook.+ cursor = self.pin_event_cursor()+ stripe.Invoice.void_invoice(stripe_invoice.id)+ self.send_stripe_webhook_events(cursor, "invoice.voided")++ invoice.refresh_from_db()+ self.assertEqual(invoice.status, Invoice.VOID)+ event = invoice.get_last_associated_event()+ assert event is not None+ self.assertEqual(event.type, "invoice.voided")+ self.assertEqual(event.status, Event.EVENT_HANDLER_SUCCEEDED)+def test_make_end_of_cycle_updates_errors_without_free_trial_invoice(self) -> None:realm = get_realm("zulip")customer = Customer.objects.create(realm=realm, stripe_customer_id="cus_123")diff --git a/corporate/views/webhook.py b/corporate/views/webhook.pyindex e40452a2177..8d8c0b9c56a 100644--- a/corporate/views/webhook.py+++ b/corporate/views/webhook.py@@ -20,6 +20,7 @@ def stripe_webhook(request: HttpRequest) -> HttpResponse: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", "")@@ -55,6 +56,7 @@ def stripe_webhook(request: HttpRequest) -> HttpResponse:if stripe_event.type not in ["checkout.session.completed","invoice.paid",+ "invoice.voided",]:return HttpResponse(status=200)@@ -74,7 +76,7 @@ def stripe_webhook(request: HttpRequest) -> HttpResponse:event.object_id = session.idevent.save()handle_checkout_session_completed_event(stripe_session, event)- elif stripe_event.type == "invoice.paid":+ elif stripe_event.type in ("invoice.paid", "invoice.voided"):stripe_invoice = stripe_event.data.objectassert isinstance(stripe_invoice, stripe.Invoice)try:@@ -84,7 +86,10 @@ def stripe_webhook(request: HttpRequest) -> HttpResponse:event.content_type = ContentType.objects.get_for_model(Invoice)event.object_id = invoice.idevent.save()- handle_invoice_paid_event(stripe_invoice, event)+ 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.