Skip to content

Commit

Permalink
feat: use invoices for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Oct 24, 2024
1 parent 5f95e7e commit 40e0993
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
12 changes: 6 additions & 6 deletions weblate_web/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,19 @@ def create_service(self, years=1, days=0, recurring="y", package="extended"):
),
Package(
name="hosted:test-1-m",
verbose="Hosted (basic)",
verbose="Weblate hosting (basic)",
price=42,
category=PackageCategory.PACKAGE_DEDICATED,
),
Package(
name="hosted:test-1",
verbose="Hosted (basic)",
verbose="Weblate hosting (basic)",
price=420,
category=PackageCategory.PACKAGE_DEDICATED,
),
Package(
name="hosted:test-2",
verbose="Hosted (upgraded)",
verbose="Weblate hosting (upgraded)",
price=840,
category=PackageCategory.PACKAGE_DEDICATED,
),
Expand Down Expand Up @@ -1249,7 +1249,7 @@ def test_hosted_pay(self):
self.assertRedirects(response, payment_url)
response = self.client.post(payment_url, {"method": "pay"}, follow=True)
self.assertRedirects(response, reverse("user"))
self.assertContains(response, "Weblate: Hosted (basic)")
self.assertContains(response, "Weblate hosting (basic)")

service = Service.objects.get(pk=service.pk)
hosted = service.hosted_subscriptions
Expand Down Expand Up @@ -1281,7 +1281,7 @@ def test_hosted_pay_yearly(self):
self.assertRedirects(response, payment_url)
response = self.client.post(payment_url, {"method": "pay"}, follow=True)
self.assertRedirects(response, reverse("user"))
self.assertContains(response, "Weblate: Hosted (basic)")
self.assertContains(response, "Weblate hosting (basic)")

service = Service.objects.get(pk=service.pk)
hosted = service.hosted_subscriptions
Expand Down Expand Up @@ -1316,7 +1316,7 @@ def test_hosted_upgrade(self):
self.assertRedirects(response, payment_url)
response = self.client.post(payment_url, {"method": "pay"}, follow=True)
self.assertRedirects(response, reverse("user"))
self.assertContains(response, "Weblate: Hosted (upgraded)")
self.assertContains(response, "Weblate hosting (upgraded)")

service = Service.objects.get(pk=service.pk)
hosted = service.hosted_subscriptions
Expand Down
34 changes: 22 additions & 12 deletions weblate_web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@
EditNameForm,
MethodForm,
)
from weblate_web.invoices.models import Invoice, InvoiceCategory, InvoiceKind
from weblate_web.models import (
PAYMENTS_ORIGIN,
REWARD_LEVELS,
TOPIC_DICT,
Donation,
Package,
PackageCategory,
Post,
Project,
Service,
Expand Down Expand Up @@ -867,14 +869,18 @@ def subscription_pay(request, pk):
subscription.package = subscription.yearly_package
subscription.save(update_fields=["package"])
with override("en"):
payment = Payment.objects.create(
amount=subscription.package.price,
# pylint: disable=no-member
description=f"Weblate: {subscription.package}",
recurring=subscription.package.get_repeat(),
customer = get_customer(request, subscription.service)
invoice = Invoice.objects.create(
customer=customer,
extra={"subscription": subscription.pk},
customer=get_customer(request, subscription.service),
vat_rate=customer.vat_rate,
kind=InvoiceKind.DRAFT,
category=InvoiceCategory.SUPPORT
if subscription.package.category == PackageCategory.PACKAGE_SUPPORT
else InvoiceCategory.HOSTING,
)
invoice.invoiceitem_set.create(package=subscription.package)
payment = invoice.create_payment(subscription.package.get_repeat())
return redirect(payment.get_payment_url())


Expand Down Expand Up @@ -910,15 +916,19 @@ def subscription_new(request):
service = None

subscription = Subscription(package=package)
customer = get_customer(request, service)
with override("en"):
payment = Payment.objects.create(
amount=subscription.package.price,
# pylint: disable=no-member
description=f"Weblate: {subscription.package}",
recurring=subscription.package.get_repeat(),
invoice = Invoice.objects.create(
customer=customer,
extra={"subscription": plan, "service": service.pk if service else None},
customer=get_customer(request, service),
vat_rate=customer.vat_rate,
kind=InvoiceKind.DRAFT,
category=InvoiceCategory.SUPPORT
if subscription.package.category == PackageCategory.PACKAGE_SUPPORT
else InvoiceCategory.HOSTING,
)
invoice.invoiceitem_set.create(package=subscription.package)
payment = invoice.create_payment(subscription.package.get_repeat())
return redirect(payment.get_payment_url())


Expand Down

0 comments on commit 40e0993

Please sign in to comment.