stripe: Create void_all_open_invoices.

This commit is contained in:
Vishnu KS 2020-08-13 14:09:25 +05:30 committed by Tim Abbott
parent 0d30f59c97
commit b8b2e443bc
10 changed files with 50 additions and 1 deletions

View File

@ -636,3 +636,17 @@ def downgrade_now_without_creating_additional_invoices(realm: Realm) -> None:
plan.invoiced_through = LicenseLedger.objects.filter(plan=plan).order_by('id').last()
plan.next_invoice_date = next_invoice_date(plan)
plan.save(update_fields=["invoiced_through", "next_invoice_date"])
def void_all_open_invoices(realm: Realm) -> int:
customer = get_customer_by_realm(realm)
if customer is None:
return 0
invoices = stripe.Invoice.list(customer=customer.stripe_customer_id)
voided_invoices_count = 0
for invoice in invoices:
if invoice.status == "open":
stripe.Invoice.void_invoice(
invoice.id
)
voided_invoices_count += 1
return voided_invoices_count

View File

@ -40,6 +40,7 @@ from corporate.lib.stripe import (
update_license_ledger_for_automanaged_plan,
update_license_ledger_if_needed,
update_or_create_stripe_customer,
void_all_open_invoices,
)
from corporate.models import (
Customer,
@ -201,7 +202,7 @@ MOCKED_STRIPE_FUNCTION_NAMES = [f"stripe.{name}" for name in [
"Charge.create", "Charge.list",
"Coupon.create",
"Customer.create", "Customer.retrieve", "Customer.save",
"Invoice.create", "Invoice.finalize_invoice", "Invoice.list", "Invoice.pay", "Invoice.upcoming",
"Invoice.create", "Invoice.finalize_invoice", "Invoice.list", "Invoice.pay", "Invoice.upcoming", "Invoice.void_invoice",
"InvoiceItem.create", "InvoiceItem.list",
"Plan.create",
"Product.create",
@ -1816,6 +1817,35 @@ class StripeTest(StripeTestCase):
self.assertEqual(old_plan.next_invoice_date, None)
self.assertEqual(old_plan.status, CustomerPlan.ENDED)
@mock_stripe()
def test_void_all_open_invoices(self, *mock: Mock) -> None:
iago = self.example_user("iago")
self.assertEqual(void_all_open_invoices(iago.realm), 0)
customer = update_or_create_stripe_customer(iago)
stripe.InvoiceItem.create(
currency='usd',
customer=customer.stripe_customer_id,
description="Zulip standard upgrade",
discountable=False,
unit_amount=800,
quantity=8
)
stripe_invoice = stripe.Invoice.create(
auto_advance=True,
billing="send_invoice",
customer=customer.stripe_customer_id,
days_until_due=30,
statement_descriptor='Zulip Standard'
)
stripe.Invoice.finalize_invoice(stripe_invoice)
self.assertEqual(void_all_open_invoices(iago.realm), 1)
invoices = stripe.Invoice.list(customer=customer.stripe_customer_id)
self.assertEqual(len(invoices), 1)
for invoice in invoices:
self.assertEqual(invoice.status, "void")
class RequiresBillingAccessTest(ZulipTestCase):
def setUp(self) -> None:
super().setUp()

View File

@ -47,6 +47,7 @@ class Customer:
class Invoice:
id: str
auto_advance: bool
amount_due: int
billing: str
@ -81,6 +82,10 @@ class Invoice:
def pay(invoice: Invoice) -> Invoice:
...
@staticmethod
def void_invoice(id: str) -> None:
...
def get(self, key: str) -> Any:
...