Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stripe: Void Stripe invoice when deleting a fixed-price plan offer.

Aman Agrawal21f6a37cb8bc
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index f7846b08dd9..fd20a7bd8f2 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -1730,6 +1730,18 @@ class BillingSession(ABC):
customer=customer, status=CustomerPlanOffer.CONFIGURED
).first()
assert fixed_price_offer is not None
+ if fixed_price_offer.sent_invoice_id is not None:
+ # Void the Stripe invoice and local Invoice object associated
+ # with this CustomerPlanOffer.
+ stripe_invoice = stripe.Invoice.retrieve(fixed_price_offer.sent_invoice_id)
+ if stripe_invoice.status == "open":
+ stripe.Invoice.void_invoice(fixed_price_offer.sent_invoice_id)
+ local_invoice = Invoice.objects.get(
+ customer=customer,
+ stripe_invoice_id=fixed_price_offer.sent_invoice_id,
+ )
+ local_invoice.status = Invoice.VOID
+ local_invoice.save(update_fields=["status"])
fixed_price_offer.delete()
return "Fixed-price plan offer deleted"
diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py
index 8fc88bef8da..56f9f16c307 100644
--- a/corporate/tests/test_stripe.py
+++ b/corporate/tests/test_stripe.py
@@ -8908,6 +8908,65 @@ class TestRemoteRealmBillingFlow(StripeTestCase, RemoteRealmBillingTestCase):
realm_complimentary_access_plan.refresh_from_db()
self.assertEqual(realm_complimentary_access_plan.status, CustomerPlan.ACTIVE)
+ @responses.activate
+ def test_delete_configured_fixed_price_plan_offer_with_sent_invoice(self) -> None:
+ self.login("iago")
+ self.add_mock_response()
+ with time_machine.travel(self.now, tick=False):
+ send_server_data_to_push_bouncer(consider_usage_statistics=False)
+
+ self.client_post(
+ "/activity/remote/support",
+ {
+ "remote_realm_id": f"{self.remote_realm.id}",
+ "required_plan_tier": CustomerPlan.TIER_SELF_HOSTED_BASIC,
+ },
+ )
+
+ annual_fixed_price = 1200
+ sent_invoice_id = "test_sent_invoice_id"
+ stripe_customer_id = "cus_123"
+ hamlet = self.example_user("hamlet")
+ mock_invoice = MagicMock()
+ mock_invoice.status = "open"
+ with (
+ patch(
+ "stripe.Customer.retrieve",
+ return_value=Mock(id=stripe_customer_id, email=hamlet.delivery_email),
+ ),
+ patch("stripe.Invoice.retrieve", return_value=mock_invoice),
+ ):
+ self.client_post(
+ "/activity/remote/support",
+ {
+ "remote_realm_id": f"{self.remote_realm.id}",
+ "fixed_price": annual_fixed_price,
+ "sent_invoice_id": sent_invoice_id,
+ },
+ )
+
+ customer = Customer.objects.get(remote_realm=self.remote_realm)
+ fixed_price_plan_offer = CustomerPlanOffer.objects.get(customer=customer)
+ self.assertEqual(fixed_price_plan_offer.status, CustomerPlanOffer.CONFIGURED)
+ self.assertEqual(fixed_price_plan_offer.sent_invoice_id, sent_invoice_id)
+ local_invoice = Invoice.objects.get(stripe_invoice_id=sent_invoice_id)
+ self.assertEqual(local_invoice.status, Invoice.SENT)
+
+ with (
+ patch("stripe.Invoice.retrieve", return_value=mock_invoice),
+ patch("stripe.Invoice.void_invoice") as mock_void,
+ ):
+ support_request = SupportViewRequest(
+ support_type=SupportType.delete_fixed_price_next_plan,
+ )
+ success_message = self.billing_session.process_support_view_request(support_request)
+ self.assertEqual(success_message, "Fixed-price plan offer deleted")
+ mock_void.assert_called_once_with(sent_invoice_id)
+
+ self.assertFalse(CustomerPlanOffer.objects.exists())
+ local_invoice.refresh_from_db()
+ self.assertEqual(local_invoice.status, Invoice.VOID)
+
@responses.activate
@mock_stripe()
def test_schedule_fixed_price_plan_upgrade_to_another_fixed_price_plan(