Skip to content
Snippets Groups Projects
Commit 09f6d9e7 authored by Pelle Koster's avatar Pelle Koster
Browse files

Limit max invoice due date and set maximum date for allowing bank transfers

parent 5bf17369
No related branches found
No related tags found
No related merge requests found
......@@ -5,5 +5,7 @@
"STRIPE_TAX_RATE_ID": "txr_1QeddlDSpyjzuj5pPwUcMwTd",
"VISIT_API_KEY": "<visit api key>",
"VISIT_EXPO_ID": "18lm2fafttito",
"SEND_ERROR_EMAILS_TO": []
"SEND_ERROR_EMAILS_TO": [],
"MAX_INVOICE_DUE_DATE": "2025-05-23",
"MAX_BANK_TRANSFER_ALLOWED_DATE": "2025-05-12"
}
\ No newline at end of file
from __future__ import annotations
import datetime
import functools
import logging
import time
......@@ -72,6 +73,33 @@ def parse_address(visit_address: dict, organization: str):
}
def get_invoice_days_until_due(max_days=30):
max_invoice_due_date = getattr(settings, "MAX_INVOICE_DUE_DATE", None)
if not max_invoice_due_date:
return max_days
days_until_max_due_date = (
datetime.date.fromisoformat(max_invoice_due_date) - datetime.date.today()
).days
return max(min(days_until_max_due_date, max_days), 0)
def _is_bank_transfer_allowed():
max_bank_transfer_allowed_date = getattr(
settings, "MAX_BANK_TRANSFER_ALLOWED_DATE", None
)
if not max_bank_transfer_allowed_date:
return True
return datetime.date.today() <= datetime.date.fromisoformat(
max_bank_transfer_allowed_date
)
def get_allowed_payment_methods():
if _is_bank_transfer_allowed():
return ["card", "customer_balance"]
return ["card"]
def create_invoice(
shopping_cart: ShoppingCart,
customer_id,
......@@ -98,8 +126,8 @@ def create_invoice(
invoice = stripe.Invoice.create(
customer=customer_id,
collection_method="send_invoice",
payment_settings={"payment_method_types": ["card", "customer_balance"]},
days_until_due=30,
payment_settings={"payment_method_types": get_allowed_payment_methods()},
days_until_due=get_invoice_days_until_due(),
custom_fields=custom_fields,
rendering={"template": settings.STRIPE_INVOICE_TEMPLATE_ID},
metadata=metadata,
......
import datetime
import pytest
import stripe
from stripe_checkout.stripe_checkout.stripe_ import get_or_create_customer
from stripe_checkout.stripe_checkout.stripe_ import (
get_allowed_payment_methods,
get_invoice_days_until_due,
get_or_create_customer,
)
@pytest.mark.parametrize(
......@@ -95,3 +100,44 @@ def test_updates_visitor_with_address(default_visitor, mock_stripe):
new_address = stripe.Customer.modify.call_args.kwargs["address"]
non_empty = {key: val for key, val in new_address.items() if val}
assert non_empty == {"country": "BE"}
@pytest.mark.parametrize(
"max_due_date_in_days, expected_days_until_due",
[
(None, 30),
(31, 30),
(30, 30),
(29, 29),
(1, 1),
(0, 0),
(-1, 0),
],
)
def test_invoice_due_date(max_due_date_in_days, expected_days_until_due, settings):
if max_due_date_in_days is not None:
max_due_date = datetime.date.today() + datetime.timedelta(
days=max_due_date_in_days
)
settings.MAX_INVOICE_DUE_DATE = max_due_date.isoformat()
assert get_invoice_days_until_due() == expected_days_until_due
@pytest.mark.parametrize(
"days_until_max_bank_transfer_date, expected_payment_methods",
[
(None, ["card", "customer_balance"]),
(1, ["card", "customer_balance"]),
(0, ["card", "customer_balance"]),
(-1, ["card"]),
],
)
def test_get_allowed_payment_methods(
days_until_max_bank_transfer_date, expected_payment_methods, settings
):
if days_until_max_bank_transfer_date is not None:
max_bank_transfer_date = datetime.date.today() + datetime.timedelta(
days=days_until_max_bank_transfer_date
)
settings.MAX_BANK_TRANSFER_ALLOWED_DATE = max_bank_transfer_date.isoformat()
assert get_allowed_payment_methods() == expected_payment_methods
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment