Skip to content
5 changes: 5 additions & 0 deletions lago_python_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .customers.applied_coupons_client import CustomerAppliedCouponsClient
from .customers.credit_notes_client import CustomerCreditNotesClient
from .customers.invoices_client import CustomerInvoicesClient
from .customers.payment_methods_client import CustomerPaymentMethodsClient
from .customers.payments_client import CustomerPaymentsClient
from .customers.payment_requests_client import CustomerPaymentRequestsClient
from .customers.subscriptions_client import CustomerSubscriptionsClient
Expand Down Expand Up @@ -111,6 +112,10 @@ def customer_credit_notes(self) -> CustomerCreditNotesClient:
def customer_invoices(self) -> CustomerInvoicesClient:
return CustomerInvoicesClient(self.base_api_url, self.api_key)

@callable_cached_property
def customer_payment_methods(self) -> CustomerPaymentMethodsClient:
return CustomerPaymentMethodsClient(self.base_api_url, self.api_key)

@callable_cached_property
def customer_payments(self) -> CustomerPaymentsClient:
return CustomerPaymentsClient(self.base_api_url, self.api_key)
Expand Down
49 changes: 49 additions & 0 deletions lago_python_client/customers/payment_methods_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import ClassVar, Type

from ..base_client import BaseClient
from ..customers.clients import CustomerClient
from ..mixins import FindAllChildrenCommandMixin
from ..models.payment_method import PaymentMethodResponse
from ..services.request import make_headers, make_url, send_delete_request, send_put_request
from ..services.response import get_response_data, prepare_object_response, Response


class CustomerPaymentMethodsClient(FindAllChildrenCommandMixin[PaymentMethodResponse], BaseClient):
PARENT_API_RESOURCE: ClassVar[str] = CustomerClient.API_RESOURCE
API_RESOURCE: ClassVar[str] = "payment_methods"
RESPONSE_MODEL: ClassVar[Type[PaymentMethodResponse]] = PaymentMethodResponse
ROOT_NAME: ClassVar[str] = "payment_method"

def destroy(self, customer_external_id: str, payment_method_id: str) -> PaymentMethodResponse:
api_response: Response = send_delete_request(
url=make_url(
origin=self.base_url,
path_parts=(self.PARENT_API_RESOURCE, customer_external_id, self.API_RESOURCE, payment_method_id),
),
headers=make_headers(api_key=self.api_key),
)

return prepare_object_response(
response_model=self.RESPONSE_MODEL,
data=get_response_data(response=api_response, key=self.ROOT_NAME),
)

def set_as_default(self, customer_external_id: str, payment_method_id: str) -> PaymentMethodResponse:
api_response: Response = send_put_request(
url=make_url(
origin=self.base_url,
path_parts=(
self.PARENT_API_RESOURCE,
customer_external_id,
self.API_RESOURCE,
payment_method_id,
"set_as_default",
),
),
headers=make_headers(api_key=self.api_key),
)

return prepare_object_response(
response_model=self.RESPONSE_MODEL,
data=get_response_data(response=api_response, key=self.ROOT_NAME),
)
10 changes: 9 additions & 1 deletion lago_python_client/invoices/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
CreateCommandMixin,
)
from ..models.invoice import InvoicePreview, InvoiceResponse
from ..models.payment_method import PaymentMethod
from ..services.json import to_json
from ..services.request import (
make_headers,
Expand Down Expand Up @@ -47,12 +48,19 @@ def download(self, resource_id: str) -> Optional[InvoiceResponse]:
data=response_data,
)

def retry_payment(self, resource_id: str) -> Optional[InvoiceResponse]:
def retry_payment(
self, resource_id: str, payment_method: Optional[PaymentMethod] = None
) -> Optional[InvoiceResponse]:
payload = {}
if payment_method:
payload["payment_method"] = payment_method.dict(exclude_none=True)

api_response: Response = send_post_request(
url=make_url(
origin=self.base_url,
path_parts=(self.API_RESOURCE, resource_id, "retry_payment"),
),
content=to_json(payload) if payload else None,
headers=make_headers(api_key=self.api_key),
)

Expand Down
1 change: 1 addition & 0 deletions lago_python_client/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,5 @@
)
from .payment_request import PaymentRequest as PaymentRequest
from .payment import Payment as Payment
from .payment_method import PaymentMethod as PaymentMethod, PaymentMethodResponse as PaymentMethodResponse
from .lifetime_usage import LifetimeUsageResponse as LifetimeUsageResponse
2 changes: 2 additions & 0 deletions lago_python_client/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .credit import CreditsResponse
from .customer import Customer, CustomerResponse
from .fee import FeesResponse
from .payment_method import PaymentMethod
from .subscription import Subscriptions, SubscriptionsResponse
from .error_detail import ErrorDetailsResponse
from ..base_model import BaseResponseModel
Expand Down Expand Up @@ -53,6 +54,7 @@ class OneOffInvoice(BaseModel):
currency: Optional[str]
fees: Optional[InvoiceFeesList]
error_details: Optional[ErrorDetailsResponse]
payment_method: Optional[PaymentMethod]


class InvoicePreview(BaseModel):
Expand Down
20 changes: 20 additions & 0 deletions lago_python_client/models/payment_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Optional

from lago_python_client.base_model import BaseModel

from ..base_model import BaseResponseModel


class PaymentMethod(BaseModel):
payment_method_type: Optional[str]
payment_method_id: Optional[str]


class PaymentMethodResponse(BaseResponseModel):
lago_id: str
is_default: Optional[bool]
payment_provider_code: Optional[str]
payment_provider_name: Optional[str]
payment_provider_type: Optional[str]
provider_method_id: Optional[str]
created_at: Optional[str]
3 changes: 3 additions & 0 deletions lago_python_client/models/subscription.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List, Optional

from lago_python_client.base_model import BaseModel
from .payment_method import PaymentMethod
from .plan import PlanOverrides
from ..base_model import BaseResponseModel

Expand All @@ -14,6 +15,7 @@ class Subscription(BaseModel):
billing_time: Optional[str]
ending_at: Optional[str]
plan_overrides: Optional[PlanOverrides]
payment_method: Optional[PaymentMethod]


class Subscriptions(BaseModel):
Expand Down Expand Up @@ -48,6 +50,7 @@ class SubscriptionResponse(BaseResponseModel):
current_billing_period_ending_at: Optional[str]
on_termination_credit_note: Optional[str]
on_termination_invoice: Optional[str]
payment_method: Optional[PaymentMethod]


class SubscriptionsResponse(BaseResponseModel):
Expand Down
5 changes: 5 additions & 0 deletions lago_python_client/models/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from lago_python_client.base_model import BaseModel

from .payment_method import PaymentMethod
from ..base_model import BaseResponseModel


Expand All @@ -19,6 +20,7 @@ class RecurringTransactionRule(BaseModel):
transaction_metadata: Optional[List[Dict[str, str]]]
transaction_name: Optional[str]
ignore_paid_top_up_limits: Optional[bool]
payment_method: Optional[PaymentMethod]


class RecurringTransactionRuleResponse(BaseModel):
Expand All @@ -37,6 +39,7 @@ class RecurringTransactionRuleResponse(BaseModel):
transaction_metadata: Optional[List[Dict[str, str]]]
transaction_name: Optional[str]
ignore_paid_top_up_limits: Optional[bool]
payment_method: Optional[PaymentMethod]


class RecurringTransactionRuleList(BaseModel):
Expand Down Expand Up @@ -71,6 +74,7 @@ class Wallet(BaseModel):
paid_top_up_min_amount_cents: Optional[int]
ignore_paid_top_up_limits_on_creation: Optional[bool]
metadata: Optional[Dict[str, Optional[str]]]
payment_method: Optional[PaymentMethod]


class WalletResponse(BaseResponseModel):
Expand Down Expand Up @@ -101,3 +105,4 @@ class WalletResponse(BaseResponseModel):
paid_top_up_max_amount_cents: Optional[int]
paid_top_up_min_amount_cents: Optional[int]
metadata: Optional[Dict[str, Optional[str]]]
payment_method: Optional[PaymentMethod]
3 changes: 3 additions & 0 deletions lago_python_client/models/wallet_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from lago_python_client.base_model import BaseModel

from .payment_method import PaymentMethod
from ..base_model import BaseResponseModel


Expand All @@ -14,6 +15,7 @@ class WalletTransaction(BaseModel):
metadata: Optional[List[Dict[str, str]]]
name: Optional[str]
ignore_paid_top_up_limits: Optional[bool]
payment_method: Optional[PaymentMethod]


class WalletTransactionResponse(BaseResponseModel):
Expand All @@ -31,3 +33,4 @@ class WalletTransactionResponse(BaseResponseModel):
metadata: Optional[List[Dict[str, str]]]
name: Optional[str]
invoice_requires_successful_payment: Optional[bool]
payment_method: Optional[PaymentMethod]
11 changes: 11 additions & 0 deletions tests/fixtures/payment_method.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"payment_method": {
"lago_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"is_default": true,
"payment_provider_code": "stripe_1",
"payment_provider_name": "Stripe",
"payment_provider_type": "stripe",
"provider_method_id": "pm_1234567890",
"created_at": "2024-01-01T00:00:00Z"
}
}
29 changes: 29 additions & 0 deletions tests/fixtures/payment_method_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"payment_methods": [
{
"lago_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"is_default": true,
"payment_provider_code": "stripe_1",
"payment_provider_name": "Stripe",
"payment_provider_type": "stripe",
"provider_method_id": "pm_1234567890",
"created_at": "2024-01-01T00:00:00Z"
},
{
"lago_id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
"is_default": false,
"payment_provider_code": "adyen_1",
"payment_provider_name": "Adyen",
"payment_provider_type": "adyen",
"provider_method_id": "pm_0987654321",
"created_at": "2024-01-02T00:00:00Z"
}
],
"meta": {
"current_page": 1,
"next_page": null,
"prev_page": null,
"total_pages": 1,
"total_count": 2
}
}
6 changes: 5 additions & 1 deletion tests/fixtures/subscription.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"next_plan_code": null,
"downgrade_plan_date": null,
"current_billing_period_started_at": "2022-04-29T08:59:51Z",
"current_billing_period_ending_at": "2022-05-29T23:59:59Z"
"current_billing_period_ending_at": "2022-05-29T23:59:59Z",
"payment_method": {
"payment_method_type": "card",
"payment_method_id": "pm_123"
}
}
}
10 changes: 9 additions & 1 deletion tests/fixtures/wallet.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
"started_at": null,
"target_ongoing_balance": "200.0",
"transaction_name": "Recurring Transaction Rule",
"ignore_paid_top_up_limits": true
"ignore_paid_top_up_limits": true,
"payment_method": {
"payment_method_type": "card",
"payment_method_id": "pm_123"
}
}
],
"ongoing_balance_cents": 800,
Expand All @@ -43,6 +47,10 @@
"metadata": {
"key1": "value1",
"key2": null
},
"payment_method": {
"payment_method_type": "card",
"payment_method_id": "pm_123"
}
}
}
18 changes: 15 additions & 3 deletions tests/fixtures/wallet_transaction.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
"credit_amount": "10",
"settled_at": "2022-04-29T08:59:51Z",
"created_at": "2022-04-29T08:59:51Z",
"name": "Transaction Name"
"name": "Transaction Name",
"payment_method": {
"payment_method_type": "card",
"payment_method_id": "pm_123"
}
},
{
"lago_id": "b7ab2926-1de8-4428-9bcd-779314ac1222",
Expand All @@ -24,7 +28,11 @@
"credit_amount": "10",
"settled_at": "2022-04-29T08:59:51Z",
"created_at": "2022-04-29T08:59:51Z",
"name": "Transaction Name"
"name": "Transaction Name",
"payment_method": {
"payment_method_type": "card",
"payment_method_id": "pm_123"
}
},
{
"lago_id": "b7ab2926-1de8-4428-9bcd-779314ac1333",
Expand All @@ -38,7 +46,11 @@
"settled_at": "2022-04-29T08:59:51Z",
"failed_at": "2022-04-29T08:59:51Z",
"created_at": "2022-04-29T08:59:51Z",
"name": "Transaction Name"
"name": "Transaction Name",
"payment_method": {
"payment_method_type": "card",
"payment_method_id": "pm_123"
}
}
]
}
Loading