|
1 | | -from ..constants import ( |
2 | | - BASE_URL, |
3 | | - BILLING_KINDS, |
4 | | - BILLING_METHODS, |
5 | | -) |
6 | | -from ..products import Product |
7 | | -from .models import ( |
8 | | - BillingResponse, |
9 | | - Customer, |
10 | | -) |
11 | | -from ..base.client import BaseClient |
12 | 1 | from logging import getLogger |
| 2 | +from typing import Optional |
| 3 | +from ..base.client import BaseClient |
| 4 | +from ..constants import BASE_URL |
| 5 | +from .models import Billing, BillingIn, BillingList |
| 6 | +from ..utils.helpers import prepare_data |
13 | 7 |
|
14 | 8 | logger = getLogger(__name__) |
15 | 9 |
|
16 | 10 |
|
17 | 11 | class BillingClient(BaseClient): |
18 | | - def create( |
19 | | - self, |
20 | | - products: list[Product], |
21 | | - returnURL: str, |
22 | | - completionUrl: str, |
23 | | - methods: list[BILLING_METHODS] = ["PIX"], |
24 | | - frequency: BILLING_KINDS = "ONE_TIME", |
25 | | - customerId: str | None = None, |
26 | | - customer: Customer | None = None |
27 | | - ) -> BillingResponse: |
28 | | - |
29 | | - |
| 12 | + def create(self, data: Optional[BillingIn | dict] = None, **kwargs) -> Billing: |
30 | 13 | """ |
31 | 14 | Create a new billing. |
32 | 15 |
|
33 | 16 | Args: |
34 | | - products: List of products to be billed. |
35 | | - returnURL: The URL the user will be redirected to after the billing is completed. |
36 | | - completionUrl: The URL the API will make a POST request after the billing is completed. |
37 | | - methods: The payment methods to be accepted. Defaults to ["PIX"]. |
38 | | - frequency: The frequency of the billing. Defaults to "ONE_TIME". |
39 | | - customerId: The ID of the customer. If provided, the customer information won't be required. |
40 | | - customer: The customer information. If customerId is provided, this parameter is ignored. |
| 17 | + data (BillingIn): an instance of `abacatepay.billings.models.BillingIn` a dict \ |
| 18 | + or the named params following the model schema. |
| 19 | + |
| 20 | + Keyword args: |
| 21 | + products (List[Product]): List of products to be billed. |
| 22 | + returnURL (str): The URL the user will be redirected to after the billing is completed. |
| 23 | + completionUrl (str): The URL the API will make a POST request after the billing is completed. |
| 24 | + methods (List[BILLING_METHODS]): The payment methods to be accepted. Defaults to ["PIX"]. |
| 25 | + frequency (BILLING_KINDS): The frequency of the billing. Defaults to "ONE_TIME". |
| 26 | + customerId (str): The ID of the customer. If provided, the customer information won't be required. |
| 27 | + customer (CustomerMetadata): The customer information. If customerId is provided, this parameter is ignored. |
41 | 28 |
|
42 | 29 | Returns: |
43 | | - BillingResponse: The response with the billing data. |
| 30 | + Billing: The response with the billing data. |
44 | 31 | """ |
| 32 | + json_data = prepare_data(data or kwargs, BillingIn) |
| 33 | + logger.debug('creating billing: %s', json_data) |
| 34 | + |
45 | 35 | response = self._request( |
46 | 36 | f"{BASE_URL}/billing/create", |
47 | 37 | method="POST", |
48 | | - json={ |
49 | | - "products": [product.model_dump() for product in products], |
50 | | - "returnUrl": returnURL, |
51 | | - "completionUrl": completionUrl, |
52 | | - "methods": methods, |
53 | | - "frequency": frequency, |
54 | | - "customerId": customerId, |
55 | | - **({"customer": customer.model_dump()} if customer is not None else {}), |
56 | | - } |
| 38 | + json=json_data, |
57 | 39 | ) |
| 40 | + return Billing(**response.json()["data"]) |
58 | 41 |
|
59 | | - billing_data = BillingResponse(data=response.json()["data"]) |
60 | | - return billing_data |
61 | | - |
62 | | - def list(self) -> list[BillingResponse]: |
| 42 | + def list(self) -> BillingList: |
63 | 43 | """ |
64 | 44 | List all bills. |
65 | 45 |
|
66 | 46 | Returns: |
67 | | - list[BillingResponse]: A list of billing responses. |
| 47 | + BillingList: A list of billing objects. |
68 | 48 | """ |
69 | 49 | logger.debug(f"Listing bills with URL: {BASE_URL}/billing/list") |
70 | 50 | response = self._request(f"{BASE_URL}/billing/list", method="GET") |
71 | | - return [BillingResponse(data=bill) for bill in response.json()["data"]] |
| 51 | + return BillingList.model_validate({'data': response.json()["data"]}) |
0 commit comments