Skip to content

Commit

Permalink
payments: Use current package price on repeated payment
Browse files Browse the repository at this point in the history
This allows price changes and smooth upgrades during the cycle.
  • Loading branch information
nijel committed Jul 28, 2023
1 parent eb887ec commit c1c1c4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 6 additions & 2 deletions payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

from __future__ import annotations

import os.path
import uuid

Expand Down Expand Up @@ -278,7 +280,9 @@ def get_payment_backend_class(self):
def get_payment_backend(self):
return self.get_payment_backend_class()(self)

def repeat_payment(self, skip_previous: bool = False, **kwargs):
def repeat_payment(
self, skip_previous: bool = False, amount: int | None = None, **kwargs
):
# Check if backend is still valid
try:
self.get_payment_backend_class()
Expand All @@ -305,7 +309,7 @@ def repeat_payment(self, skip_previous: bool = False, **kwargs):
extra.update(self.extra)
extra.update(kwargs)
return Payment.objects.create(
amount=self.amount,
amount=self.amount if amount is None else amount,
backend=self.backend,
description=self.description,
recurring="",
Expand Down
12 changes: 9 additions & 3 deletions weblate_web/management/commands/recurring_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

from __future__ import annotations

from datetime import datetime, timedelta

from django.conf import settings
Expand Down Expand Up @@ -121,7 +123,7 @@ def handle_services():
service.create_backup()

@staticmethod
def peform_payment(payment, past_payments):
def peform_payment(payment, past_payments, amount: int | None = None):
# Alllow at most three failures of current payment method
rejected_payments = past_payments.filter(
state=Payment.REJECTED, repeat=payment.repeat or payment
Expand All @@ -132,7 +134,7 @@ def peform_payment(payment, past_payments):
return

# Create repeated payment
repeated = payment.repeat_payment()
repeated = payment.repeat_payment(amount=amount)

# Backend does not support it
if not repeated:
Expand Down Expand Up @@ -168,7 +170,11 @@ def handle_subscriptions(cls):
continue

# Trigger recurring payment
cls.peform_payment(payment, subscription.list_payments())
cls.peform_payment(
payment,
subscription.list_payments(),
amount=subscription.package_obj.price,
)

@classmethod
def handle_donations(cls):
Expand Down

0 comments on commit c1c1c4e

Please sign in to comment.