From 348060b9ee66a3c7966caae5a362de4e96bb70e6 Mon Sep 17 00:00:00 2001 From: Anna Velentsevich Date: Mon, 9 Nov 2026 17:15:51 +0100 Subject: [PATCH] feat: add structured payment_term support --- lago_python_client/invoices/clients.py | 2 +- lago_python_client/models/__init__.py | 1 + lago_python_client/models/billing_entity.py | 4 ++ lago_python_client/models/customer.py | 3 + lago_python_client/models/invoice.py | 6 +- lago_python_client/models/organization.py | 2 + lago_python_client/models/payment_request.py | 4 +- lago_python_client/models/payment_term.py | 10 +++ lago_python_client/models/subscription.py | 3 + lago_python_client/models/wallet.py | 3 + tests/fixtures/billing_entity.json | 1 + tests/fixtures/billing_entity_index.json | 2 + tests/fixtures/customer.json | 1 + tests/fixtures/invoice.json | 1 + tests/fixtures/invoice_index.json | 2 + tests/fixtures/one_off_invoice.json | 1 + tests/fixtures/organization.json | 1 + tests/fixtures/payment_request.json | 3 + tests/fixtures/payment_request_index.json | 1 + tests/fixtures/pending_subscription.json | 1 + tests/fixtures/subscription.json | 1 + tests/fixtures/subscription_index.json | 1 + tests/fixtures/voided_invoice.json | 1 + tests/fixtures/wallet.json | 1 + tests/fixtures/wallet_index.json | 2 + tests/test_billing_entity_client.py | 57 +++++++++++++++- tests/test_customer_client.py | 55 +++++++++++++++ tests/test_invoice_client.py | 71 ++++++++++++++++++++ tests/test_organization_client.py | 2 + tests/test_subscription_client.py | 54 +++++++++++++++ tests/test_wallet_client.py | 52 ++++++++++++++ 31 files changed, 345 insertions(+), 4 deletions(-) create mode 100644 lago_python_client/models/payment_term.py diff --git a/lago_python_client/invoices/clients.py b/lago_python_client/invoices/clients.py index 30732fbf..b82d2ff5 100644 --- a/lago_python_client/invoices/clients.py +++ b/lago_python_client/invoices/clients.py @@ -163,7 +163,7 @@ def preview(self, input_object: InvoicePreview) -> InvoiceResponse: origin=self.base_url, path_parts=(self.API_RESOURCE, "preview"), ), - content=to_json(input_object.dict()), + content=to_json(input_object.dict(exclude_none=True)), headers=make_headers(api_key=self.api_key), ) diff --git a/lago_python_client/models/__init__.py b/lago_python_client/models/__init__.py index bf1c43ac..444dcbee 100644 --- a/lago_python_client/models/__init__.py +++ b/lago_python_client/models/__init__.py @@ -277,6 +277,7 @@ PaymentReceiptsResponse as PaymentReceiptsResponse, ) from .payment_request import PaymentRequest as PaymentRequest +from .payment_term import PaymentTerm as PaymentTerm from .payment_method import PaymentMethod as PaymentMethod, PaymentMethodResponse as PaymentMethodResponse from .plan import Plan as Plan from .quote import ( diff --git a/lago_python_client/models/billing_entity.py b/lago_python_client/models/billing_entity.py index e92b2a69..fcdc1f3c 100644 --- a/lago_python_client/models/billing_entity.py +++ b/lago_python_client/models/billing_entity.py @@ -3,6 +3,7 @@ from ..base_model import BaseModel, BaseResponseModel from .invoice_custom_section import InvoiceCustomSectionsResponseList +from .payment_term import PaymentTerm from .tax import TaxesResponse @@ -31,6 +32,7 @@ class BillingEntity(BaseModel): document_number_prefix: Optional[str] finalize_zero_amount_invoice: Optional[bool] net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] eu_tax_management: Optional[bool] logo_url: Optional[str] legal_name: Optional[str] @@ -57,6 +59,7 @@ class BillingEntityUpdate(BaseModel): document_number_prefix: Optional[str] finalize_zero_amount_invoice: Optional[bool] net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] eu_tax_management: Optional[bool] logo_url: Optional[str] legal_name: Optional[str] @@ -87,6 +90,7 @@ class BillingEntityResponse(BaseResponseModel): document_number_prefix: Optional[str] finalize_zero_amount_invoice: Optional[bool] net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] eu_tax_management: Optional[bool] logo_url: Optional[str] legal_name: Optional[str] diff --git a/lago_python_client/models/customer.py b/lago_python_client/models/customer.py index 77cf91ce..1f45adf2 100644 --- a/lago_python_client/models/customer.py +++ b/lago_python_client/models/customer.py @@ -5,6 +5,7 @@ from ..base_model import BaseResponseModel from .error_detail import ErrorDetailsResponse from .invoice_custom_section import InvoiceCustomSectionsResponseList +from .payment_term import PaymentTerm from .tax import TaxesResponse @@ -94,6 +95,7 @@ class Customer(BaseModel): legal_name: Optional[str] legal_number: Optional[str] net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] external_salesforce_id: Optional[str] tax_identification_number: Optional[str] logo_url: Optional[str] @@ -134,6 +136,7 @@ class CustomerResponse(BaseResponseModel): legal_name: Optional[str] legal_number: Optional[str] net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] external_salesforce_id: Optional[str] tax_identification_number: Optional[str] logo_url: Optional[str] diff --git a/lago_python_client/models/invoice.py b/lago_python_client/models/invoice.py index 02806af9..fb97032f 100644 --- a/lago_python_client/models/invoice.py +++ b/lago_python_client/models/invoice.py @@ -11,6 +11,7 @@ from .fee import FeesResponse from .invoice_custom_section import InvoiceCustomSectionInput from .payment_method import PaymentMethod +from .payment_term import PaymentTerm from .subscription import Subscriptions, SubscriptionsResponse from .usage_threshold import UsageThreshold @@ -56,6 +57,7 @@ class OneOffInvoice(BaseModel): fees: Optional[InvoiceFeesList] error_details: Optional[ErrorDetailsResponse] payment_method: Optional[PaymentMethod] + payment_term: Optional[PaymentTerm] invoice_custom_section: Optional[InvoiceCustomSectionInput] billing_entity_code: Optional[str] purchase_order_number: Optional[str] @@ -69,6 +71,7 @@ class InvoicePreview(BaseModel): customer: Optional[Customer] subscriptions: Optional[Subscriptions] billing_entity_code: Optional[str] + payment_term: Optional[PaymentTerm] class InvoiceAppliedTax(BaseResponseModel): @@ -121,7 +124,8 @@ class InvoiceResponse(BaseResponseModel): payment_dispute_lost_at: Optional[str] payment_due_date: Optional[str] payment_overdue: bool - net_payment_term: int + net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] invoice_type: str version_number: int status: str diff --git a/lago_python_client/models/organization.py b/lago_python_client/models/organization.py index 6f17a04c..b1563d74 100644 --- a/lago_python_client/models/organization.py +++ b/lago_python_client/models/organization.py @@ -3,6 +3,7 @@ from lago_python_client.base_model import BaseModel from ..base_model import BaseResponseModel +from .payment_term import PaymentTerm class OrganizationBillingConfiguration(BaseModel): @@ -51,6 +52,7 @@ class OrganizationResponse(BaseResponseModel): legal_name: Optional[str] legal_number: Optional[str] net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] document_numbering: Optional[str] document_number_prefix: Optional[str] tax_identification_number: Optional[str] diff --git a/lago_python_client/models/payment_request.py b/lago_python_client/models/payment_request.py index 1671d70e..8fe36267 100644 --- a/lago_python_client/models/payment_request.py +++ b/lago_python_client/models/payment_request.py @@ -2,6 +2,7 @@ from ..base_model import BaseModel, BaseResponseModel from .customer import CustomerResponse +from .payment_term import PaymentTerm class PaymentRequest(BaseModel): @@ -18,7 +19,8 @@ class PaymentRequestInvoiceResponse(BaseResponseModel): payment_dispute_lost_at: Optional[str] payment_due_date: Optional[str] payment_overdue: bool - net_payment_term: int + net_payment_term: Optional[int] + payment_term: Optional[PaymentTerm] invoice_type: str version_number: int status: str diff --git a/lago_python_client/models/payment_term.py b/lago_python_client/models/payment_term.py new file mode 100644 index 00000000..55c77fd9 --- /dev/null +++ b/lago_python_client/models/payment_term.py @@ -0,0 +1,10 @@ +from typing import Optional + +from lago_python_client.base_model import BaseModel + + +class PaymentTerm(BaseModel): + term_type: str + days: Optional[int] + day_of_month: Optional[int] + month_offset: Optional[int] diff --git a/lago_python_client/models/subscription.py b/lago_python_client/models/subscription.py index 96b73140..6e81d3d4 100644 --- a/lago_python_client/models/subscription.py +++ b/lago_python_client/models/subscription.py @@ -6,6 +6,7 @@ from .entitlement import SubscriptionEntitlementsResponse from .invoice_custom_section import AppliedInvoiceCustomSections, InvoiceCustomSectionInput from .payment_method import PaymentMethod +from .payment_term import PaymentTerm from .plan import PlanOverrides from .usage_threshold import ApplicableUsageThresholdsResponse, UsageThresholdsOverrides @@ -25,6 +26,7 @@ class Subscription(BaseModel): ending_at: Optional[str] plan_overrides: Optional[PlanOverrides] payment_method: Optional[PaymentMethod] + payment_term: Optional[PaymentTerm] invoice_custom_section: Optional[InvoiceCustomSectionInput] consolidate_invoice: Optional[bool] billing_entity_code: Optional[str] @@ -76,6 +78,7 @@ class SubscriptionResponse(BaseResponseModel): on_termination_credit_note: Optional[str] on_termination_invoice: Optional[str] payment_method: Optional[PaymentMethod] + payment_term: Optional[PaymentTerm] applied_invoice_custom_sections: Optional[AppliedInvoiceCustomSections] consolidate_invoice: Optional[bool] billing_entity_code: Optional[str] diff --git a/lago_python_client/models/wallet.py b/lago_python_client/models/wallet.py index ee3e2eb4..9974586b 100644 --- a/lago_python_client/models/wallet.py +++ b/lago_python_client/models/wallet.py @@ -5,6 +5,7 @@ from ..base_model import BaseResponseModel from .invoice_custom_section import AppliedInvoiceCustomSections, InvoiceCustomSectionInput from .payment_method import PaymentMethod +from .payment_term import PaymentTerm class RecurringTransactionRule(BaseModel): @@ -83,6 +84,7 @@ class Wallet(BaseModel): ignore_paid_top_up_limits_on_creation: Optional[bool] metadata: Optional[Dict[str, Optional[str]]] payment_method: Optional[PaymentMethod] + payment_term: Optional[PaymentTerm] invoice_custom_section: Optional[InvoiceCustomSectionInput] billing_entity_code: Optional[str] purchase_order_number: Optional[str] @@ -117,6 +119,7 @@ class WalletResponse(BaseResponseModel): paid_top_up_min_amount_cents: Optional[int] metadata: Optional[Dict[str, Optional[str]]] payment_method: Optional[PaymentMethod] + payment_term: Optional[PaymentTerm] applied_invoice_custom_sections: Optional[AppliedInvoiceCustomSections] billing_entity_code: Optional[str] purchase_order_number: Optional[str] diff --git a/tests/fixtures/billing_entity.json b/tests/fixtures/billing_entity.json index 73e34327..5c8856a6 100644 --- a/tests/fixtures/billing_entity.json +++ b/tests/fixtures/billing_entity.json @@ -17,6 +17,7 @@ "document_number_prefix": "TEST-", "finalize_zero_amount_invoice": true, "net_payment_term": 30, + "payment_term": {"term_type": "net", "days": 30}, "eu_tax_management": false, "logo_url": "https://example.com/logo.png", "legal_name": "Test Company Legal Name", diff --git a/tests/fixtures/billing_entity_index.json b/tests/fixtures/billing_entity_index.json index 906329ca..264ef040 100644 --- a/tests/fixtures/billing_entity_index.json +++ b/tests/fixtures/billing_entity_index.json @@ -18,6 +18,7 @@ "document_number_prefix": "TEST1-", "finalize_zero_amount_invoice": true, "net_payment_term": 30, + "payment_term": {"term_type": "net", "days": 30}, "eu_tax_management": false, "logo_url": "https://example.com/logo.png", "legal_name": "Test Company 1 Legal Name", @@ -55,6 +56,7 @@ "document_number_prefix": "TEST2-", "finalize_zero_amount_invoice": true, "net_payment_term": 30, + "payment_term": {"term_type": "net", "days": 30}, "eu_tax_management": true, "logo_url": "https://example.com/logo_2.png", "legal_name": "Test Company 2 Legal Name", diff --git a/tests/fixtures/customer.json b/tests/fixtures/customer.json index 7261552a..f7895967 100644 --- a/tests/fixtures/customer.json +++ b/tests/fixtures/customer.json @@ -29,6 +29,7 @@ "timezone": "Europe/Paris", "applicable_timezone": "Europe/Paris", "net_payment_term": null, + "payment_term": null, "external_salesforce_id": "sf_001", "finalize_zero_amount_invoice": "inherit", "billing_configuration": { diff --git a/tests/fixtures/invoice.json b/tests/fixtures/invoice.json index 0e6688e8..e2975394 100644 --- a/tests/fixtures/invoice.json +++ b/tests/fixtures/invoice.json @@ -9,6 +9,7 @@ "payment_due_date": "2022-06-02", "payment_overdue": false, "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "invoice_type": "type1", "version_number": 2, "status": "finalized", diff --git a/tests/fixtures/invoice_index.json b/tests/fixtures/invoice_index.json index f005d667..87350580 100644 --- a/tests/fixtures/invoice_index.json +++ b/tests/fixtures/invoice_index.json @@ -8,6 +8,7 @@ "payment_due_date": "2022-06-02", "payment_overdue": false, "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "invoice_type": "subscription", "version_number": 2, "status": "finalized", @@ -53,6 +54,7 @@ "payment_due_date": "2022-06-02", "payment_overdue": false, "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "invoice_type": "add_on", "version_number": 2, "status": "finalized", diff --git a/tests/fixtures/one_off_invoice.json b/tests/fixtures/one_off_invoice.json index a710a59e..6bab84db 100644 --- a/tests/fixtures/one_off_invoice.json +++ b/tests/fixtures/one_off_invoice.json @@ -7,6 +7,7 @@ "payment_due_date": "2022-06-02", "payment_overdue": false, "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "invoice_type": "one_off", "version_number": 2, "status": "finalized", diff --git a/tests/fixtures/organization.json b/tests/fixtures/organization.json index f0126b30..6a64eb67 100644 --- a/tests/fixtures/organization.json +++ b/tests/fixtures/organization.json @@ -19,6 +19,7 @@ "document_numbering": "per_customer", "document_number_prefix": "ORG-1234", "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "tax_identification_number": "EU123456789", "timezone": "America/New_York", "finalize_zero_amount_invoice": true, diff --git a/tests/fixtures/payment_request.json b/tests/fixtures/payment_request.json index d75c7369..2b7b56c0 100644 --- a/tests/fixtures/payment_request.json +++ b/tests/fixtures/payment_request.json @@ -28,6 +28,7 @@ "timezone": "Europe/Paris", "applicable_timezone": "Europe/Paris", "net_payment_term": 30, + "payment_term": {"term_type": "net", "days": 30}, "tax_identification_number": "EU123456789", "sequential_id": 101, "slug": "customer_101_slug", @@ -74,6 +75,7 @@ "payment_status": "pending", "currency": "EUR", "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "fees_amount_cents": 10000, "taxes_amount_cents": 2000, "coupons_amount_cents": 0, @@ -101,6 +103,7 @@ "payment_status": "failed", "currency": "EUR", "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "fees_amount_cents": 7000, "taxes_amount_cents": 955, "coupons_amount_cents": 0, diff --git a/tests/fixtures/payment_request_index.json b/tests/fixtures/payment_request_index.json index a2a1612c..9e141254 100644 --- a/tests/fixtures/payment_request_index.json +++ b/tests/fixtures/payment_request_index.json @@ -50,6 +50,7 @@ "payment_status": "pending", "currency": "EUR", "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "fees_amount_cents": 100, "taxes_amount_cents": 20, "coupons_amount_cents": 0, diff --git a/tests/fixtures/pending_subscription.json b/tests/fixtures/pending_subscription.json index c22fa229..ea380f9e 100644 --- a/tests/fixtures/pending_subscription.json +++ b/tests/fixtures/pending_subscription.json @@ -3,6 +3,7 @@ "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", "lago_customer_id": "99a6094e-199b-4101-896a-54e927ce7bd7", "external_customer_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + "payment_term": null, "canceled_at": "2022-04-29T08:59:51Z", "created_at": "2022-04-29T08:59:51Z", "plan_code": "eartha lynch", diff --git a/tests/fixtures/subscription.json b/tests/fixtures/subscription.json index a698ed35..a303fcb5 100644 --- a/tests/fixtures/subscription.json +++ b/tests/fixtures/subscription.json @@ -3,6 +3,7 @@ "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", "lago_customer_id": "99a6094e-199b-4101-896a-54e927ce7bd7", "external_customer_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + "payment_term": {"term_type": "day_of_month", "day_of_month": 15, "month_offset": 1}, "canceled_at": "2022-04-29T08:59:51Z", "created_at": "2022-04-29T08:59:51Z", "plan_code": "eartha lynch", diff --git a/tests/fixtures/subscription_index.json b/tests/fixtures/subscription_index.json index 35437453..e7f6aa13 100644 --- a/tests/fixtures/subscription_index.json +++ b/tests/fixtures/subscription_index.json @@ -4,6 +4,7 @@ "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", "lago_customer_id": "99a6094e-199b-4101-896a-54e927ce7bd7", "external_customer_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + "payment_term": null, "canceled_at": "2022-04-29T08:59:51Z", "created_at": "2022-04-29T08:59:51Z", "plan_code": "eartha lynch", diff --git a/tests/fixtures/voided_invoice.json b/tests/fixtures/voided_invoice.json index f0153023..8cd997a4 100644 --- a/tests/fixtures/voided_invoice.json +++ b/tests/fixtures/voided_invoice.json @@ -7,6 +7,7 @@ "payment_due_date": "2022-06-02", "payment_overdue": false, "net_payment_term": 0, + "payment_term": {"term_type": "due_on_receipt"}, "invoice_type": "type1", "version_number": 2, "status": "voided", diff --git a/tests/fixtures/wallet.json b/tests/fixtures/wallet.json index 8fd59fbd..c994335f 100644 --- a/tests/fixtures/wallet.json +++ b/tests/fixtures/wallet.json @@ -3,6 +3,7 @@ "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", "lago_customer_id": "12345", "external_customer_id": "external-12345", + "payment_term": {"term_type": "net_end_of_month", "days": 10}, "status": "active", "currency": "EUR", "name": "name", diff --git a/tests/fixtures/wallet_index.json b/tests/fixtures/wallet_index.json index 265f5532..18073712 100644 --- a/tests/fixtures/wallet_index.json +++ b/tests/fixtures/wallet_index.json @@ -4,6 +4,7 @@ "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", "lago_customer_id": "12345", "external_customer_id": "external-12345", + "payment_term": null, "status": "active", "currency": "EUR", "name": "name1", @@ -43,6 +44,7 @@ "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac1111", "lago_customer_id": "12345", "external_customer_id": "external-54321", + "payment_term": null, "status": "active", "currency": "EUR", "name": "name2", diff --git a/tests/test_billing_entity_client.py b/tests/test_billing_entity_client.py index 0d2656ba..ebe68095 100644 --- a/tests/test_billing_entity_client.py +++ b/tests/test_billing_entity_client.py @@ -6,7 +6,7 @@ from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError -from lago_python_client.models import BillingEntity, BillingEntityBillingConfiguration, BillingEntityUpdate +from lago_python_client.models import BillingEntity, BillingEntityBillingConfiguration, BillingEntityUpdate, PaymentTerm def mock_response(mock="billing_entity"): @@ -243,3 +243,58 @@ def test_invalid_update_billing_entity_request(httpx_mock: HTTPXMock): with pytest.raises(LagoApiError): client.billing_entities.update(update_billing_entity_input(), billing_entity_code) + + +def test_valid_create_billing_entity_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + billing_entity_input = create_billing_entity_input() + billing_entity_input.net_payment_term = None + billing_entity_input.payment_term = PaymentTerm(term_type="net", days=30) + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/billing_entities", + content=mock_response(), + ) + response = client.billing_entities.create(billing_entity_input) + + body = json.loads(httpx_mock.get_request().content) + assert body["billing_entity"]["payment_term"] == {"term_type": "net", "days": 30} + assert "net_payment_term" not in body["billing_entity"] + assert response.payment_term.term_type == "net" + assert response.payment_term.days == 30 + assert response.payment_term.day_of_month is None + assert response.net_payment_term == 30 + + +def test_valid_update_billing_entity_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + billing_entity_code = "test-company" + billing_entity_input = update_billing_entity_input() + billing_entity_input.payment_term = PaymentTerm(term_type="end_of_month") + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/billing_entities/" + billing_entity_code, + content=mock_response(), + ) + client.billing_entities.update(billing_entity_input, billing_entity_code) + + body = json.loads(httpx_mock.get_request().content) + assert body["billing_entity"]["payment_term"] == {"term_type": "end_of_month"} + + +def test_valid_update_billing_entity_request_without_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + billing_entity_code = "test-company" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/billing_entities/" + billing_entity_code, + content=mock_response(), + ) + client.billing_entities.update(update_billing_entity_input(), billing_entity_code) + + body = json.loads(httpx_mock.get_request().content) + assert body["billing_entity"]["net_payment_term"] == 15 + assert "payment_term" not in body["billing_entity"] diff --git a/tests/test_customer_client.py b/tests/test_customer_client.py index c1e5c88c..79505343 100644 --- a/tests/test_customer_client.py +++ b/tests/test_customer_client.py @@ -14,6 +14,7 @@ IntegrationCustomersList, Metadata, MetadataList, + PaymentTerm, ) @@ -110,6 +111,7 @@ def test_valid_create_customers_request(httpx_mock: HTTPXMock): assert response.currency == "EUR" assert response.tax_identification_number == "EU123456789" assert response.net_payment_term is None + assert response.payment_term is None assert response.billing_configuration.invoice_grace_period == 3 assert response.billing_configuration.subscription_invoice_issuing_date_anchor == "current_period_end" assert response.billing_configuration.subscription_invoice_issuing_date_adjustment == "keep_anchor" @@ -432,3 +434,56 @@ def test_invalid_destroy_customer_request(httpx_mock: HTTPXMock): with pytest.raises(LagoApiError): client.customers.destroy(external_id) + + +def test_create_customers_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/customers", + content=mock_response(), + ) + customer = Customer( + external_id="5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + payment_term=PaymentTerm(term_type="day_of_month", day_of_month=15, month_offset=1), + ) + client.customers.create(customer) + + body = json.loads(httpx_mock.get_request().content) + assert body["customer"]["payment_term"] == { + "term_type": "day_of_month", + "day_of_month": 15, + "month_offset": 1, + } + assert "net_payment_term" not in body["customer"] + + +def test_create_customers_request_with_only_net_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/customers", + content=mock_response(), + ) + client.customers.create(Customer(external_id="5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", net_payment_term=15)) + + body = json.loads(httpx_mock.get_request().content) + assert body["customer"]["net_payment_term"] == 15 + assert "payment_term" not in body["customer"] + + +def test_create_customers_request_without_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/customers", + content=mock_response(), + ) + client.customers.create(Customer(external_id="5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba")) + + body = json.loads(httpx_mock.get_request().content) + assert "payment_term" not in body["customer"] + assert "net_payment_term" not in body["customer"] diff --git a/tests/test_invoice_client.py b/tests/test_invoice_client.py index c1f43f61..053cac6d 100644 --- a/tests/test_invoice_client.py +++ b/tests/test_invoice_client.py @@ -1,3 +1,4 @@ +import json import os import pytest @@ -16,6 +17,7 @@ InvoicePreview, OneOffInvoice, PaymentMethod, + PaymentTerm, ) @@ -392,6 +394,9 @@ def test_valid_preview_request(httpx_mock: HTTPXMock): assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" assert response.status == "finalized" assert response.payment_status == "failed" + assert response.net_payment_term == 0 + assert response.payment_term.term_type == "due_on_receipt" + assert "payment_term" not in json.loads(httpx_mock.get_request().content) def test_valid_void_invoice_request_no_options(httpx_mock: HTTPXMock): @@ -445,3 +450,69 @@ def test_valid_create_one_off_invoice_with_invoice_custom_section(httpx_mock: HT assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" assert response.invoice_type == "one_off" + + +def test_valid_create_one_off_invoice_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/invoices", + content=mock_response(mock="one_off_invoice"), + ) + invoice = one_off_invoice_object() + invoice.payment_term = PaymentTerm(term_type="net_end_of_month", days=10) + response = client.invoices.create(invoice) + + body = json.loads(httpx_mock.get_request().content) + assert body["invoice"]["payment_term"] == {"term_type": "net_end_of_month", "days": 10} + assert response.payment_term.term_type == "due_on_receipt" + + +def test_create_one_off_invoice_without_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/invoices", + content=mock_response(mock="one_off_invoice"), + ) + client.invoices.create(one_off_invoice_object()) + + body = json.loads(httpx_mock.get_request().content) + assert "payment_term" not in body["invoice"] + + +def test_valid_preview_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/invoices/preview", + content=mock_response(), + ) + preview = invoice_preview() + preview.payment_term = PaymentTerm(term_type="days_end_of_month", days=15) + client.invoices.preview(preview) + + body = json.loads(httpx_mock.get_request().content) + assert body["payment_term"] == {"term_type": "days_end_of_month", "days": 15} + assert "payment_term" not in body["customer"] + + +def test_valid_find_invoice_request_with_null_net_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + invoice_id = "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + payload = json.loads(mock_response()) + payload["invoice"]["net_payment_term"] = None + payload["invoice"]["payment_term"] = {"term_type": "end_of_month"} + + httpx_mock.add_response( + method="GET", + url="https://api.getlago.com/api/v1/invoices/" + invoice_id, + content=json.dumps(payload).encode(), + ) + response = client.invoices.find(invoice_id) + + assert response.net_payment_term is None + assert response.payment_term.term_type == "end_of_month" diff --git a/tests/test_organization_client.py b/tests/test_organization_client.py index 9bb598cf..13d4ae22 100644 --- a/tests/test_organization_client.py +++ b/tests/test_organization_client.py @@ -45,6 +45,8 @@ def test_valid_update_organization_request(httpx_mock: HTTPXMock): assert response.tax_identification_number == "EU123456789" assert response.document_number_prefix == "ORG-1234" assert response.net_payment_term == 0 + assert response.payment_term.term_type == "due_on_receipt" + assert "payment_term" not in json.loads(httpx_mock.get_request().content)["organization"] assert "slug" not in json.loads(httpx_mock.get_request().content)["organization"] diff --git a/tests/test_subscription_client.py b/tests/test_subscription_client.py index 91c726e0..5af99142 100644 --- a/tests/test_subscription_client.py +++ b/tests/test_subscription_client.py @@ -15,6 +15,7 @@ FixedCharge, InvoiceCustomSectionInput, PaymentMethod, + PaymentTerm, Subscription, ) from lago_python_client.models.alert import Alert, AlertsList, AlertThreshold @@ -92,6 +93,10 @@ def test_valid_create_subscriptions_request(httpx_mock: HTTPXMock): assert response.ending_at == "2022-08-29T08:59:51Z" assert response.consolidate_invoice is False assert response.purchase_order_number == "PO-123" + assert response.payment_term.term_type == "day_of_month" + assert response.payment_term.day_of_month == 15 + assert response.payment_term.month_offset == 1 + assert response.payment_term.days is None entitlement = response.entitlements.__root__[0] assert entitlement.code == "seats" @@ -1320,3 +1325,52 @@ def test_update_subscription_with_custom_timeout(httpx_mock: HTTPXMock): response = client.subscriptions.update(Subscription(name="name"), identifier, timeout=httpx.Timeout(30.0)) assert response.external_customer_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" + + +def test_valid_create_subscriptions_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/subscriptions", + content=mock_response(), + ) + subscription = create_subscription() + subscription.payment_term = PaymentTerm(term_type="net", days=45) + client.subscriptions.create(subscription) + + body = json.loads(httpx_mock.get_request().content) + assert body["subscription"]["payment_term"] == {"term_type": "net", "days": 45} + + +def test_valid_update_subscription_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "sub_id" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/subscriptions/" + identifier, + content=mock_response(), + ) + client.subscriptions.update( + Subscription(payment_term=PaymentTerm(term_type="due_on_receipt")), + identifier, + ) + + body = json.loads(httpx_mock.get_request().content) + assert body == {"subscription": {"payment_term": {"term_type": "due_on_receipt"}}} + + +def test_valid_update_subscription_request_without_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + identifier = "sub_id" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/subscriptions/" + identifier, + content=mock_response(), + ) + client.subscriptions.update(Subscription(name="name"), identifier) + + body = json.loads(httpx_mock.get_request().content) + assert body == {"subscription": {"name": "name"}} diff --git a/tests/test_wallet_client.py b/tests/test_wallet_client.py index 9db70ab6..b1c4739c 100644 --- a/tests/test_wallet_client.py +++ b/tests/test_wallet_client.py @@ -1,3 +1,4 @@ +import json import os import pytest @@ -9,6 +10,7 @@ AppliesTo, InvoiceCustomSectionInput, PaymentMethod, + PaymentTerm, RecurringTransactionRule, RecurringTransactionRuleList, Wallet, @@ -702,3 +704,53 @@ def test_valid_update_wallet_with_grants_target_top_up_on_recurring_transaction_ assert response.lago_id == "b7ab2926-1de8-4428-9bcd-779314ac129b" assert response.recurring_transaction_rules.__root__[0].grants_target_top_up is True + + +def test_valid_create_wallet_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + + httpx_mock.add_response( + method="POST", + url="https://api.getlago.com/api/v1/wallets", + content=mock_response(), + ) + wallet = wallet_object() + wallet.payment_term = PaymentTerm(term_type="net_end_of_month", days=10) + response = client.wallets.create(wallet) + + body = json.loads(httpx_mock.get_request().content) + assert body["wallet"]["payment_term"] == {"term_type": "net_end_of_month", "days": 10} + assert response.payment_term.term_type == "net_end_of_month" + assert response.payment_term.days == 10 + + +def test_valid_update_wallet_request_with_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + wallet_id = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/wallets/" + wallet_id, + content=mock_response(), + ) + wallet = wallet_object() + wallet.payment_term = PaymentTerm(term_type="due_on_receipt") + client.wallets.update(wallet, wallet_id) + + body = json.loads(httpx_mock.get_request().content) + assert body["wallet"]["payment_term"] == {"term_type": "due_on_receipt"} + + +def test_valid_update_wallet_request_without_payment_term(httpx_mock: HTTPXMock): + client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d") + wallet_id = "b7ab2926-1de8-4428-9bcd-779314ac129b" + + httpx_mock.add_response( + method="PUT", + url="https://api.getlago.com/api/v1/wallets/" + wallet_id, + content=mock_response(), + ) + client.wallets.update(wallet_object(), wallet_id) + + body = json.loads(httpx_mock.get_request().content) + assert "payment_term" not in body["wallet"]