|
1 | 1 | from typing import List, Optional, Union
|
2 | 2 |
|
3 |
| -from pydantic import BaseModel, Field |
| 3 | +from pydantic import BaseModel, ConfigDict, Field |
4 | 4 |
|
5 | 5 | from checkout.entities.amount import Amount
|
6 | 6 | from checkout.entities.discount import Discount
|
7 |
| -from checkout.entities.instrument import Instrument |
8 | 7 | from checkout.entities.item import Item
|
| 8 | +from checkout.entities.name_value_pair import NameValuePair |
9 | 9 | from checkout.entities.payment_modifier import PaymentModifier
|
10 | 10 | from checkout.entities.person import Person
|
11 | 11 | from checkout.entities.recurring import Recurring
|
|
15 | 15 | class Payment(BaseModel, FieldsMixin):
|
16 | 16 | reference: str = Field(..., description="Payment reference")
|
17 | 17 | description: str = Field(default="", description="Description of the payment")
|
18 |
| - amount: Optional[Amount] = Field(default=None, description="Amount information") |
19 |
| - allowPartial: bool = Field(default=False, description="Allow partial payments") |
| 18 | + amount: Amount = Field(default=..., description="Amount information") |
| 19 | + allow_partial: bool = Field(default=False, description="Allow partial payments", alias="allowPartial") |
20 | 20 | shipping: Optional[Person] = Field(default=None, description="Shipping details")
|
21 | 21 | items: List[Item] = Field(default_factory=list, description="List of items")
|
22 | 22 | recurring: Optional[Recurring] = Field(default=None, description="Recurring payment details")
|
23 |
| - payment: Optional[Instrument] = Field(default=None, description="Instrument payment details") |
24 | 23 | discount: Optional[Discount] = Field(default=None, description="Discount information")
|
25 | 24 | subscribe: bool = Field(default=False, description="Subscribe flag")
|
26 | 25 | agreement: Optional[int] = Field(default=None, description="Agreement ID")
|
27 |
| - agreementType: str = Field(default="", description="Type of agreement") |
| 26 | + agreement_type: str = Field(default="", description="Type of agreement", alias="agreementType") |
28 | 27 | modifiers: List[PaymentModifier] = Field(default_factory=list, description="List of payment modifiers")
|
| 28 | + custom_fields: List[NameValuePair] = Field(default=[], description="Additional fields for the payment") |
| 29 | + |
| 30 | + model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True) |
29 | 31 |
|
30 | 32 | def set_items(self, items: Union[List[dict], List[Item]]) -> None:
|
31 | 33 | """
|
@@ -55,18 +57,26 @@ def to_dict(self) -> dict:
|
55 | 57 | """
|
56 | 58 | Convert the Payment object to a dictionary, including nested objects.
|
57 | 59 | """
|
58 |
| - return { |
| 60 | + data = { |
59 | 61 | "reference": self.reference,
|
60 | 62 | "description": self.description,
|
61 |
| - "amount": self.amount.model_dump() if self.amount else None, |
62 |
| - "allowPartial": self.allowPartial, |
63 |
| - "shipping": self.shipping.model_dump() if self.shipping else None, |
| 63 | + "amount": self.amount.to_dict(), |
| 64 | + "allowPartial": self.allow_partial, |
64 | 65 | "items": self.items_to_array(),
|
65 |
| - "recurring": self.recurring.model_dump() if self.recurring else None, |
66 |
| - "discount": self.discount.model_dump() if self.discount else None, |
67 | 66 | "subscribe": self.subscribe,
|
68 |
| - "agreement": self.agreement, |
69 |
| - "agreementType": self.agreementType, |
70 | 67 | "modifiers": self.modifiers_to_array(),
|
71 | 68 | "fields": self.fields_to_array(),
|
72 | 69 | }
|
| 70 | + |
| 71 | + if self.shipping: |
| 72 | + data["shipping"] = self.shipping.to_dict() |
| 73 | + if self.recurring: |
| 74 | + data["recurring"] = self.recurring.to_dict() |
| 75 | + if self.discount: |
| 76 | + data["discount"] = self.discount.to_dict() |
| 77 | + if self.agreement: |
| 78 | + data["agreement"] = self.agreement |
| 79 | + if self.agreement_type: |
| 80 | + data["agreementType"] = self.agreement_type |
| 81 | + |
| 82 | + return data |
0 commit comments