diff --git a/src/multisafepay/api/paths/orders/order_id/capture/request/capture_request.py b/src/multisafepay/api/paths/orders/order_id/capture/request/capture_request.py index c899835..da9053c 100644 --- a/src/multisafepay/api/paths/orders/order_id/capture/request/capture_request.py +++ b/src/multisafepay/api/paths/orders/order_id/capture/request/capture_request.py @@ -41,14 +41,14 @@ class CaptureOrderRequest(RequestModel): def add_amount( self: "CaptureOrderRequest", - amount: Union[Amount, int], + amount: Optional[Union[Amount, int]], ) -> "CaptureOrderRequest": """ Adds the amount to the capture request. Parameters ---------- - amount (Amount | int): The amount to add. + amount (Optional[Amount | int]): The amount to add. Returns ------- @@ -57,19 +57,19 @@ def add_amount( """ if isinstance(amount, int): amount = Amount(amount=amount) - self.amount = amount.get() + self.amount = amount.get() if amount is not None else None return self def add_new_order_id( self: "CaptureOrderRequest", - new_order_id: str, + new_order_id: Optional[str], ) -> "CaptureOrderRequest": """ Adds the new order ID to the capture request. Parameters ---------- - new_order_id (str): The new order ID to add. + new_order_id (Optional[str]): The new order ID to add. Returns ------- @@ -81,14 +81,14 @@ def add_new_order_id( def add_new_order_status( self: "CaptureOrderRequest", - new_order_status: str, + new_order_status: Optional[str], ) -> "CaptureOrderRequest": """ Adds the new order status to the capture request. Parameters ---------- - new_order_status (str): The new order status to add. + new_order_status (Optional[str]): The new order status to add. Returns ------- @@ -100,14 +100,14 @@ def add_new_order_status( def add_invoice_id( self: "CaptureOrderRequest", - invoice_id: str, + invoice_id: Optional[str], ) -> "CaptureOrderRequest": """ Adds the invoice ID to the capture request. Parameters ---------- - invoice_id (str): The invoice ID to add. + invoice_id (Optional[str]): The invoice ID to add. Returns ------- @@ -119,14 +119,14 @@ def add_invoice_id( def add_carrier( self: "CaptureOrderRequest", - carrier: str, + carrier: Optional[str], ) -> "CaptureOrderRequest": """ Adds the carrier information to the capture request. Parameters ---------- - carrier (str): The carrier information to add. + carrier (Optional[str]): The carrier information to add. Returns ------- @@ -138,14 +138,14 @@ def add_carrier( def add_reason( self: "CaptureOrderRequest", - reason: str, + reason: Optional[str], ) -> "CaptureOrderRequest": """ Adds the reason for the capture to the capture request. Parameters ---------- - reason (str): The reason to add. + reason (Optional[str]): The reason to add. Returns ------- @@ -157,14 +157,14 @@ def add_reason( def add_tracktrace_code( self: "CaptureOrderRequest", - tracktrace_code: str, + tracktrace_code: Optional[str], ) -> "CaptureOrderRequest": """ Adds the tracktrace code to the capture request. Parameters ---------- - tracktrace_code (str): The tracktrace code to add. + tracktrace_code (Optional[str]): The tracktrace code to add. Returns ------- @@ -176,14 +176,14 @@ def add_tracktrace_code( def add_description( self: "CaptureOrderRequest", - description: str, + description: Optional[str], ) -> "CaptureOrderRequest": """ Adds a description to the capture request. Parameters ---------- - description (str): The description to add. + description (Optional[str]): The description to add. Returns ------- diff --git a/src/multisafepay/api/paths/orders/order_id/refund/request/components/checkout_data.py b/src/multisafepay/api/paths/orders/order_id/refund/request/components/checkout_data.py index 93dc720..ae6667c 100644 --- a/src/multisafepay/api/paths/orders/order_id/refund/request/components/checkout_data.py +++ b/src/multisafepay/api/paths/orders/order_id/refund/request/components/checkout_data.py @@ -29,56 +29,63 @@ class CheckoutData(RequestModel): def add_items( self: "CheckoutData", - items: List[CartItem] = (), + items: Optional[List[CartItem]] = None, ) -> "CheckoutData": """ Adds multiple items to the checkout data. Parameters ---------- - items (List[CartItem]): The list of items to add. + items (Optional[List[CartItem]]): The list of items to add. Defaults to None. Returns ------- CheckoutData: The updated checkout data. """ + if items is None: + return self if self.items is None: self.items = [] for item in items: self.add_item(item) return self - def add_item(self: "CheckoutData", item: CartItem) -> "CheckoutData": + def add_item( + self: "CheckoutData", + item: Optional[CartItem], + ) -> "CheckoutData": """ Adds a single item to the checkout data. Parameters ---------- - item (CartItem): The item to add. + item (Optional[CartItem]): The item to add. Returns ------- CheckoutData: The updated checkout data. """ + if item is None: + return self if self.items is None: self.items = [] self.items.append(item) return self - def get_items(self: "CheckoutData") -> List[CartItem]: + def get_items(self: "CheckoutData") -> Optional[List[CartItem]]: """ Retrieves all items from the checkout data. Returns ------- - List[CartItem]: The list of items. + Optional[List[CartItem]]: The list of items, or None if no items exist. """ return self.items - def get_item(self: "CheckoutData", index: int) -> CartItem: + def get_item(self: "CheckoutData", index: int) -> Optional[CartItem]: """ Retrieves an item by its index from the checkout data. @@ -88,28 +95,33 @@ def get_item(self: "CheckoutData", index: int) -> CartItem: Returns ------- - CartItem: The retrieved item. + Optional[CartItem]: The retrieved item, or None if items is None. """ + if self.items is None: + return None return self.items[index] def generate_from_shopping_cart( self: "CheckoutData", - shopping_cart: ShoppingCart, - tax_table_selector: str = "", + shopping_cart: Optional[ShoppingCart], + tax_table_selector: Optional[str] = None, ) -> None: """ Generates checkout data from a shopping cart. Parameters ---------- - shopping_cart (ShoppingCart): The shopping cart to generate data from. - tax_table_selector (str): The tax table selector to use. + shopping_cart (Optional[ShoppingCart]): The shopping cart to generate data from. + tax_table_selector (Optional[str]): The tax table selector to use. """ if shopping_cart is None: return - for shopping_cart_item in shopping_cart.get_items(): + items = shopping_cart.get_items() + if items is None: + return + for shopping_cart_item in items: if tax_table_selector: shopping_cart_item.add_tax_table_selector(tax_table_selector) self.add_item(shopping_cart_item) @@ -132,7 +144,7 @@ def refund_by_merchant_item_id( InvalidArgumentException: If no items are provided or the item is not found. """ - if len(self.items) < 1: + if self.items is None or len(self.items) < 1: raise InvalidArgumentException( "No items provided in checkout data", ) @@ -167,6 +179,11 @@ def get_item_by_merchant_item_id( InvalidArgumentException: If no item is found with the given merchant item ID. """ + if self.items is None: + raise InvalidArgumentException( + "No items provided in checkout data", + ) + for item in self.items: if item.merchant_item_id == merchant_item_id: return item diff --git a/src/multisafepay/api/paths/orders/order_id/refund/request/refund_request.py b/src/multisafepay/api/paths/orders/order_id/refund/request/refund_request.py index 35f78df..a839448 100644 --- a/src/multisafepay/api/paths/orders/order_id/refund/request/refund_request.py +++ b/src/multisafepay/api/paths/orders/order_id/refund/request/refund_request.py @@ -38,14 +38,14 @@ class RefundOrderRequest(RequestModel): def add_currency( self: "RefundOrderRequest", - currency: Union[Currency, str], + currency: Optional[Union[Currency, str]], ) -> "RefundOrderRequest": """ Adds the currency to the refund request. Parameters ---------- - currency (Currency | str): The currency to add. + currency (Optional[Currency | str]): The currency to add. Returns ------- @@ -54,19 +54,19 @@ def add_currency( """ if isinstance(currency, str): currency = Currency(currency=currency) - self.currency = currency.get() + self.currency = currency.get() if currency is not None else None return self def add_amount( self: "RefundOrderRequest", - amount: Union[Amount, int], + amount: Optional[Union[Amount, int]], ) -> "RefundOrderRequest": """ Adds the amount to the refund request. Parameters ---------- - amount (Amount | int): The amount to add. + amount (Optional[Amount | int]): The amount to add. Returns ------- @@ -75,19 +75,19 @@ def add_amount( """ if isinstance(amount, int): amount = Amount(amount=amount) - self.amount = amount.get() + self.amount = amount.get() if amount is not None else None return self def add_description( self: "RefundOrderRequest", - description: Union[Description, str], + description: Optional[Union[Description, str]], ) -> "RefundOrderRequest": """ Adds the description to the refund request. Parameters ---------- - description (Description | str): The description to add. + description (Optional[Description | str]): The description to add. Returns ------- @@ -96,19 +96,21 @@ def add_description( """ if isinstance(description, str): description = Description(description=description) - self.description = description.get() + self.description = ( + description.get() if description is not None else None + ) return self def add_checkout_data( self: "RefundOrderRequest", - checkout_data: CheckoutData, + checkout_data: Optional[CheckoutData], ) -> "RefundOrderRequest": """ Adds the checkout data to the refund request. Parameters ---------- - checkout_data (CheckoutData): The checkout data to add. + checkout_data (Optional[CheckoutData]): The checkout data to add. Returns ------- diff --git a/src/multisafepay/api/paths/orders/request/components/checkout_options.py b/src/multisafepay/api/paths/orders/request/components/checkout_options.py index e4f051f..b70c419 100644 --- a/src/multisafepay/api/paths/orders/request/components/checkout_options.py +++ b/src/multisafepay/api/paths/orders/request/components/checkout_options.py @@ -34,14 +34,14 @@ class CheckoutOptions(RequestModel): def add_tax_tables( self: "CheckoutOptions", - tax_tables: CheckoutOptionsApiModel, + tax_tables: Optional[CheckoutOptionsApiModel], ) -> "CheckoutOptions": """ Add tax tables to the checkout options. Parameters ---------- - tax_tables (CheckoutOptionsApiModel): The tax tables to be added. + tax_tables (Optional[CheckoutOptionsApiModel]): The tax tables to be added. Returns ------- @@ -72,7 +72,7 @@ def add_validate_cart( @staticmethod def generate_from_shopping_cart( - shopping_cart: ShoppingCart, + shopping_cart: Optional[ShoppingCart], ) -> Optional["CheckoutOptions"]: """ Generate checkout options from a shopping cart. @@ -81,13 +81,15 @@ def generate_from_shopping_cart( Parameters ---------- - shopping_cart (ShoppingCart): The shopping cart containing items. + shopping_cart (Optional[ShoppingCart]): The shopping cart containing items. Returns ------- Optional[CheckoutOptions]: The generated CheckoutOptions instance or None if no items are present. """ + if shopping_cart is None or shopping_cart.items is None: + return None if shopping_cart.items: if not isinstance(shopping_cart.items, list): raise InvalidArgumentException( diff --git a/src/multisafepay/api/paths/orders/request/components/gateway_info/account.py b/src/multisafepay/api/paths/orders/request/components/gateway_info/account.py index 6daac31..7dc343a 100644 --- a/src/multisafepay/api/paths/orders/request/components/gateway_info/account.py +++ b/src/multisafepay/api/paths/orders/request/components/gateway_info/account.py @@ -33,14 +33,14 @@ class Account(RequestModel): def add_account_id( self: "Account", - account_id: Union[IbanNumber, str], + account_id: Optional[Union[IbanNumber, str]], ) -> "Account": """ Adds an account ID to the account. Parameters ---------- - account_id (IbanNumber | str): The account ID to add. Can be an IbanNumber object or a string. + account_id (Optional[IbanNumber] | str): The account ID to add. Can be an IbanNumber object or a string. Returns ------- @@ -49,19 +49,19 @@ def add_account_id( """ if isinstance(account_id, str): account_id = IbanNumber(iban_number=account_id) - self.account_id = account_id.get() + self.account_id = account_id.get() if account_id is not None else None return self def add_account_holder_name( self: "Account", - account_holder_name: str, + account_holder_name: Optional[str], ) -> "Account": """ Adds an account holder name to the account. Parameters ---------- - account_holder_name (str): The account holder name to add. + account_holder_name (Optional[str]): The account holder name to add. Returns ------- @@ -73,14 +73,14 @@ def add_account_holder_name( def add_account_holder_iban( self: "Account", - account_holder_iban: IbanNumber, + account_holder_iban: Optional[Union[IbanNumber, str]], ) -> "Account": """ Adds an account holder IBAN to the account. Parameters ---------- - account_holder_iban (IbanNumber): The account holder IBAN to add. Can be an IbanNumber object or a string. + account_holder_iban (Optional[IbanNumber] | str): The account holder IBAN to add. Can be an IbanNumber object or a string. Returns ------- @@ -89,16 +89,20 @@ def add_account_holder_iban( """ if isinstance(account_holder_iban, str): account_holder_iban = IbanNumber(iban_number=account_holder_iban) - self.account_holder_iban = account_holder_iban.get() + self.account_holder_iban = ( + account_holder_iban.get() + if account_holder_iban is not None + else None + ) return self - def add_emandate(self: "Account", emandate: str) -> "Account": + def add_emandate(self: "Account", emandate: Optional[str]) -> "Account": """ Adds an e-mandate to the account. Parameters ---------- - emandate (str): The e-mandate to add. + emandate (Optional[str]): The e-mandate to add. Returns ------- diff --git a/src/multisafepay/api/paths/orders/request/components/gateway_info/creditcard.py b/src/multisafepay/api/paths/orders/request/components/gateway_info/creditcard.py index 575b48d..4227fb5 100644 --- a/src/multisafepay/api/paths/orders/request/components/gateway_info/creditcard.py +++ b/src/multisafepay/api/paths/orders/request/components/gateway_info/creditcard.py @@ -59,14 +59,14 @@ def add_card_number( def add_card_holder_name( self: "Creditcard", - card_holder_name: str, + card_holder_name: Optional[str], ) -> "Creditcard": """ Adds a card holder name to the credit card. Parameters ---------- - card_holder_name (str): The card holder name to add. + card_holder_name (Optional[str]): The card holder name to add. Returns ------- @@ -78,14 +78,14 @@ def add_card_holder_name( def add_card_expiry_date( self: "Creditcard", - card_expiry_date: str, + card_expiry_date: Optional[str], ) -> "Creditcard": """ Adds a card expiry date to the credit card. Parameters ---------- - card_expiry_date (str): The card expiry date to add. + card_expiry_date (Optional[str]): The card expiry date to add. Returns ------- @@ -95,13 +95,16 @@ def add_card_expiry_date( self.card_expiry_date = card_expiry_date return self - def add_cvc(self: "Creditcard", cvc: Union[Cvc, str]) -> "Creditcard": + def add_cvc( + self: "Creditcard", + cvc: Optional[Union[Cvc, str]], + ) -> "Creditcard": """ Adds a CVC code to the credit card. Parameters ---------- - cvc (Cvc): The CVC code to add. + cvc (Optional[Cvc | str]): The CVC code to add. Returns ------- @@ -110,7 +113,7 @@ def add_cvc(self: "Creditcard", cvc: Union[Cvc, str]) -> "Creditcard": """ if isinstance(cvc, str): cvc = Cvc(cvc=cvc) - self.cvc = cvc.get() + self.cvc = cvc.get() if cvc is not None else None return self def add_flexible_3d(self: "Creditcard", flexible_3d: bool) -> "Creditcard": @@ -129,13 +132,16 @@ def add_flexible_3d(self: "Creditcard", flexible_3d: bool) -> "Creditcard": self.flexible_3d = flexible_3d return self - def add_term_url(self: "Creditcard", term_url: str) -> "Creditcard": + def add_term_url( + self: "Creditcard", + term_url: Optional[str], + ) -> "Creditcard": """ Adds a term URL to the credit card. Parameters ---------- - term_url (str): The term URL to add. + term_url (Optional[str]): The term URL to add. Returns ------- diff --git a/src/multisafepay/api/paths/orders/request/components/gateway_info/destination_holder.py b/src/multisafepay/api/paths/orders/request/components/gateway_info/destination_holder.py index 34ebe1f..b04622a 100644 --- a/src/multisafepay/api/paths/orders/request/components/gateway_info/destination_holder.py +++ b/src/multisafepay/api/paths/orders/request/components/gateway_info/destination_holder.py @@ -34,13 +34,16 @@ class DestinationHolder(RequestModel): iban: Optional[str] swift: Optional[str] - def add_name(self: "DestinationHolder", name: str) -> "DestinationHolder": + def add_name( + self: "DestinationHolder", + name: Optional[str], + ) -> "DestinationHolder": """ Adds a name to the destination holder. Parameters ---------- - name (str): The name to add. + name (Optional[str]): The name to add. Returns ------- @@ -50,13 +53,16 @@ def add_name(self: "DestinationHolder", name: str) -> "DestinationHolder": self.name = name return self - def add_city(self: "DestinationHolder", city: str) -> "DestinationHolder": + def add_city( + self: "DestinationHolder", + city: Optional[str], + ) -> "DestinationHolder": """ Adds a city to the destination holder. Parameters ---------- - city (str): The city to add. + city (Optional[str]): The city to add. Returns ------- @@ -68,14 +74,14 @@ def add_city(self: "DestinationHolder", city: str) -> "DestinationHolder": def add_country( self: "DestinationHolder", - country: Union[Country, str], + country: Optional[Union[Country, str]], ) -> "DestinationHolder": """ Adds a country to the destination holder. Parameters ---------- - country (Country | str): The country to add. Can be a Country object or a string. + country (Optional[Country | str]): The country to add. Can be a Country object or a string. Returns ------- @@ -84,19 +90,19 @@ def add_country( """ if isinstance(country, str): country = Country(code=country) - self.country = country.get_code() + self.country = country.get_code() if country is not None else None return self def add_iban( self: "DestinationHolder", - iban: Union[IbanNumber, str], + iban: Optional[Union[IbanNumber, str]], ) -> "DestinationHolder": """ Adds an IBAN to the destination holder. Parameters ---------- - iban (IbanNumber | str): The IBAN to add. Can be an IbanNumber object or a string. + iban (Optional[IbanNumber | str]): The IBAN to add. Can be an IbanNumber object or a string. Returns ------- @@ -105,19 +111,19 @@ def add_iban( """ if isinstance(iban, str): iban = IbanNumber(iban_number=iban) - self.iban = iban.get() + self.iban = iban.get() if iban is not None else None return self def add_swift( self: "DestinationHolder", - swift: str, + swift: Optional[str], ) -> "DestinationHolder": """ Adds a SWIFT code to the destination holder. Parameters ---------- - swift (str): The SWIFT code to add. + swift (Optional[str]): The SWIFT code to add. Returns ------- diff --git a/src/multisafepay/api/paths/orders/request/components/gateway_info/meta.py b/src/multisafepay/api/paths/orders/request/components/gateway_info/meta.py index a6dbef1..d10ede4 100644 --- a/src/multisafepay/api/paths/orders/request/components/gateway_info/meta.py +++ b/src/multisafepay/api/paths/orders/request/components/gateway_info/meta.py @@ -37,13 +37,16 @@ class Meta(RequestModel): email_address: Optional[str] gender: Optional[str] - def add_birthday(self: "Meta", birthday: Union[Date, str]) -> "Meta": + def add_birthday( + self: "Meta", + birthday: Optional[Union[Date, str]], + ) -> "Meta": """ Adds a birthday to the Meta object. Parameters ---------- - birthday (Date | str): The birthday to be added. + birthday (Optional[Date] | str): The birthday to be added. Returns ------- @@ -52,19 +55,19 @@ def add_birthday(self: "Meta", birthday: Union[Date, str]) -> "Meta": """ if isinstance(birthday, str): birthday = Date(date=birthday) - self.birthday = birthday.get() + self.birthday = birthday.get() if birthday is not None else None return self def add_bank_account( self: "Meta", - bank_account: Union[BankAccount, str], + bank_account: Optional[Union[BankAccount, str]], ) -> "Meta": """ Adds a bank account to the Meta object. Parameters ---------- - bank_account (BankAccount | str): The bank account to be added. + bank_account (Optional[BankAccount] | str): The bank account to be added. Returns ------- @@ -73,16 +76,21 @@ def add_bank_account( """ if isinstance(bank_account, str): bank_account = BankAccount(bank_account=bank_account) - self.bank_account = bank_account.get() + self.bank_account = ( + bank_account.get() if bank_account is not None else None + ) return self - def add_phone(self: "Meta", phone: Union[PhoneNumber, str]) -> "Meta": + def add_phone( + self: "Meta", + phone: Optional[Union[PhoneNumber, str]], + ) -> "Meta": """ Adds a phone number to the Meta object. Parameters ---------- - phone (PhoneNumber | str): The phone number to be added. + phone (Optional[PhoneNumber] | str): The phone number to be added. Returns ------- @@ -94,14 +102,14 @@ def add_phone(self: "Meta", phone: Union[PhoneNumber, str]) -> "Meta": def add_email_address( self: "Meta", - email_address: Union[EmailAddress, str], + email_address: Optional[Union[EmailAddress, str]], ) -> "Meta": """ Adds an email address to the Meta object. Parameters ---------- - email_address (EmailAddress | str): The email address to be added. + email_address (Optional[EmailAddress] | str): The email address to be added. Returns ------- @@ -110,16 +118,21 @@ def add_email_address( """ if isinstance(email_address, str): email_address = EmailAddress(email_address=email_address) - self.email_address = email_address.get() + self.email_address = ( + email_address.get() if email_address is not None else None + ) return self - def add_gender(self: "Meta", gender: Union[Gender, str]) -> "Meta": + def add_gender( + self: "Meta", + gender: Optional[Union[Gender, str]], + ) -> "Meta": """ Adds a gender to the Meta object. Parameters ---------- - gender (Gender | str): The gender to be added. + gender (Optional[Gender] | str): The gender to be added. Returns ------- @@ -128,5 +141,5 @@ def add_gender(self: "Meta", gender: Union[Gender, str]) -> "Meta": """ if isinstance(gender, str): gender = Gender(gender=gender) - self.gender = gender.get() + self.gender = gender.get() if gender is not None else None return self diff --git a/src/multisafepay/api/paths/orders/request/order_request.py b/src/multisafepay/api/paths/orders/request/order_request.py index ef6750f..f460ab3 100644 --- a/src/multisafepay/api/paths/orders/request/order_request.py +++ b/src/multisafepay/api/paths/orders/request/order_request.py @@ -109,13 +109,16 @@ class OrderRequest(RequestModel): var2: Optional[str] var3: Optional[str] - def add_type(self: "OrderRequest", order_type: str) -> "OrderRequest": + def add_type( + self: "OrderRequest", + order_type: Optional[str], + ) -> "OrderRequest": """ Adds the type of the order request. Parameters ---------- - order_type (str): The type of the order request. Must be one of the allowed types. + order_type (Optional[str]): The type of the order request. Must be one of the allowed types. Raises ------ @@ -126,7 +129,7 @@ def add_type(self: "OrderRequest", order_type: str) -> "OrderRequest": OrderRequest: The updated OrderRequest object. """ - if order_type not in ALLOWED_TYPES: + if order_type is not None and order_type not in ALLOWED_TYPES: msg = f'Type "{order_type}" is not a known type. Available types: {", ".join(ALLOWED_TYPES)}' raise InvalidArgumentException(msg) self.type = order_type @@ -134,14 +137,14 @@ def add_type(self: "OrderRequest", order_type: str) -> "OrderRequest": def add_recurring_model( self: "OrderRequest", - recurring_model: str, + recurring_model: Optional[str], ) -> "OrderRequest": """ Adds the recurring model of the order request. Parameters ---------- - recurring_model (str): The recurring model of the order request. Must be one of the allowed recurring models. + recurring_model (Optional[str]): The recurring model of the order request. Must be one of the allowed recurring models. Raises ------ @@ -152,19 +155,25 @@ def add_recurring_model( OrderRequest: The updated OrderRequest object. """ - if recurring_model not in ALLOWED_RECURRING_MODELS: + if ( + recurring_model is not None + and recurring_model not in ALLOWED_RECURRING_MODELS + ): msg = f'Type "{recurring_model}" is not a known type. Available types: {", ".join(ALLOWED_RECURRING_MODELS)}' raise InvalidArgumentException(msg) self.recurring_model = recurring_model return self - def add_order_id(self: "OrderRequest", order_id: str) -> "OrderRequest": + def add_order_id( + self: "OrderRequest", + order_id: Optional[str], + ) -> "OrderRequest": """ Adds the order ID to the order request. Parameters ---------- - order_id (str): The order ID. + order_id (Optional[str]): The order ID. Returns ------- @@ -176,14 +185,14 @@ def add_order_id(self: "OrderRequest", order_id: str) -> "OrderRequest": def add_currency( self: "OrderRequest", - currency: Union[Currency, str], + currency: Optional[Union[Currency, str]], ) -> "OrderRequest": """ Adds the currency to the order request. Parameters ---------- - currency (Currency | str): The currency as a Currency object or a string. + currency (Optional[Currency | str]): The currency as a Currency object or a string. Returns ------- @@ -192,19 +201,19 @@ def add_currency( """ if isinstance(currency, str): currency = Currency(currency=currency) - self.currency = currency.get() + self.currency = currency.get() if currency is not None else None return self def add_amount( self: "OrderRequest", - amount: Union[Amount, int], + amount: Optional[Union[Amount, int]], ) -> "OrderRequest": """ Adds the amount to the order request. Parameters ---------- - amount (Amount | int): The amount as an Amount object or an integer. + amount (Optional[Amount | int]): The amount as an Amount object or an integer. Returns ------- @@ -213,16 +222,19 @@ def add_amount( """ if isinstance(amount, int): amount = Amount(amount=amount) - self.amount = amount.get() + self.amount = amount.get() if amount is not None else None return self - def add_capture(self: "OrderRequest", capture: str) -> "OrderRequest": + def add_capture( + self: "OrderRequest", + capture: Optional[str], + ) -> "OrderRequest": """ Adds the capture type to the order request. Parameters ---------- - capture (str): The capture type. + capture (Optional[str]): The capture type. Returns ------- @@ -234,14 +246,14 @@ def add_capture(self: "OrderRequest", capture: str) -> "OrderRequest": def add_payment_options( self: "OrderRequest", - payment_options: PaymentOptions, + payment_options: Optional[PaymentOptions], ) -> "OrderRequest": """ Adds the payment options to the order request. Parameters ---------- - payment_options (PaymentOptions): The payment options. + payment_options (Optional[PaymentOptions]): The payment options. Returns ------- @@ -253,14 +265,14 @@ def add_payment_options( def add_customer( self: "OrderRequest", - customer: Customer, + customer: Optional[Customer], ) -> "OrderRequest": """ Adds the customer to the order request. Parameters ---------- - customer (Customer): The customer. + customer (Optional[Customer]): The customer. Returns ------- @@ -270,13 +282,16 @@ def add_customer( self.customer = customer return self - def add_gateway(self: "OrderRequest", gateway: str) -> "OrderRequest": + def add_gateway( + self: "OrderRequest", + gateway: Optional[str], + ) -> "OrderRequest": """ Adds the gateway to the order request. Parameters ---------- - gateway (str): The gateway. + gateway (Optional[str]): The gateway. Returns ------- @@ -288,7 +303,7 @@ def add_gateway(self: "OrderRequest", gateway: str) -> "OrderRequest": def add_gateway_info( self: "OrderRequest", - gateway_info: dict, + gateway_info: Optional[dict], ) -> "OrderRequest": """ Adds the gateway information to the order request. @@ -307,14 +322,14 @@ def add_gateway_info( def add_description( self: "OrderRequest", - description: Union[Description, str], + description: Optional[Union[Description, str]], ) -> "OrderRequest": """ Adds the description to the order request. Parameters ---------- - description (Description | str): The description as a Description object or a string. + description (Optional[Description | str]): The description as a Description object or a string. Returns ------- @@ -323,19 +338,21 @@ def add_description( """ if isinstance(description, str): description = Description(description=description) - self.description = description.get() + self.description = ( + description.get() if description is not None else None + ) return self def add_plugin( self: "OrderRequest", - plugin: Plugin, + plugin: Optional[Plugin], ) -> "OrderRequest": """ Adds the plugin details to the order request. Parameters ---------- - plugin (Plugin): The plugin details. + plugin (Optional[Plugin]): The plugin details. Returns ------- @@ -347,14 +364,14 @@ def add_plugin( def add_recurring_id( self: "OrderRequest", - recurring_id: str, + recurring_id: Optional[str], ) -> "OrderRequest": """ Adds the recurring ID to the order request. Parameters ---------- - recurring_id (str): The recurring ID. + recurring_id (Optional[str]): The recurring ID. Returns ------- @@ -366,14 +383,14 @@ def add_recurring_id( def add_second_chance( self: "OrderRequest", - second_chance: SecondChance, + second_chance: Optional[SecondChance], ) -> "OrderRequest": """ Adds the second chance information to the order request. Parameters ---------- - second_chance (SecondChance): The second chance information. + second_chance (Optional[SecondChance]): The second chance information. Returns ------- @@ -385,14 +402,14 @@ def add_second_chance( def add_google_analytics( self: "OrderRequest", - google_analytics: GoogleAnalytics, + google_analytics: Optional[GoogleAnalytics], ) -> "OrderRequest": """ Adds the Google Analytics information to the order request. Parameters ---------- - google_analytics (GoogleAnalytics): The Google Analytics information. + google_analytics (Optional[GoogleAnalytics]): The Google Analytics information. Returns ------- @@ -404,14 +421,14 @@ def add_google_analytics( def add_shopping_cart( self: "OrderRequest", - shopping_cart: ShoppingCart, + shopping_cart: Optional[ShoppingCart], ) -> "OrderRequest": """ Adds the shopping cart to the order request. Parameters ---------- - shopping_cart (ShoppingCart): The shopping cart. + shopping_cart (Optional[ShoppingCart]): The shopping cart. Returns ------- @@ -423,14 +440,14 @@ def add_shopping_cart( def add_delivery( self: "OrderRequest", - delivery: Delivery, + delivery: Optional[Delivery], ) -> "OrderRequest": """ Adds the delivery information to the order request. Parameters ---------- - delivery (Delivery): The delivery information. + delivery (Optional[Delivery]): The delivery information. Returns ------- @@ -442,14 +459,14 @@ def add_delivery( def add_checkout_options( self: "OrderRequest", - checkout_options: CheckoutOptions, + checkout_options: Optional[CheckoutOptions], ) -> "OrderRequest": """ Adds the checkout options to the order request. Parameters ---------- - checkout_options (CheckoutOptions): The checkout options. + checkout_options (Optional[CheckoutOptions]): The checkout options. Returns ------- @@ -461,14 +478,14 @@ def add_checkout_options( def add_seconds_active( self: "OrderRequest", - seconds: int, + seconds: Optional[int], ) -> "OrderRequest": """ Adds the seconds active to the order request. Parameters ---------- - seconds (int): The number of seconds the order is active. + seconds (Optional[int]): The number of seconds the order is active. Returns ------- @@ -478,13 +495,16 @@ def add_seconds_active( self.seconds_active = seconds return self - def add_days_active(self: "OrderRequest", days: int) -> "OrderRequest": + def add_days_active( + self: "OrderRequest", + days: Optional[int], + ) -> "OrderRequest": """ Adds the days active to the order request. Parameters ---------- - days (int): The number of days the order is active. + days (Optional[int]): The number of days the order is active. Returns ------- @@ -496,14 +516,14 @@ def add_days_active(self: "OrderRequest", days: int) -> "OrderRequest": def add_custom_info( self: "OrderRequest", - custom_info: CustomInfo, + custom_info: Optional[CustomInfo], ) -> "OrderRequest": """ Adds the custom information to the order request. Parameters ---------- - custom_info (CustomInfo): The custom information. + custom_info (Optional[CustomInfo]): The custom information. Returns ------- @@ -513,13 +533,13 @@ def add_custom_info( self.custom_info = custom_info return self - def add_var1(self: "OrderRequest", var1: str) -> "OrderRequest": + def add_var1(self: "OrderRequest", var1: Optional[str]) -> "OrderRequest": """ Adds the first custom variable to the order request. Parameters ---------- - var1 (str): The first custom variable. + var1 (Optional[str]): The first custom variable. Returns ------- @@ -529,13 +549,13 @@ def add_var1(self: "OrderRequest", var1: str) -> "OrderRequest": self.var1 = var1 return self - def add_var2(self: "OrderRequest", var2: str) -> "OrderRequest": + def add_var2(self: "OrderRequest", var2: Optional[str]) -> "OrderRequest": """ Adds the second custom variable to the order request. Parameters ---------- - var2 (str): The second custom variable. + var2 (Optional[str]): The second custom variable. Returns ------- @@ -545,13 +565,13 @@ def add_var2(self: "OrderRequest", var2: str) -> "OrderRequest": self.var2 = var2 return self - def add_var3(self: "OrderRequest", var3: str) -> "OrderRequest": + def add_var3(self: "OrderRequest", var3: Optional[str]) -> "OrderRequest": """ Adds the third custom variable to the order request. Parameters ---------- - var3 (str): The third custom variable. + var3 (Optional[str]): The third custom variable. Returns ------- diff --git a/src/multisafepay/api/shared/cart/shopping_cart.py b/src/multisafepay/api/shared/cart/shopping_cart.py index 462928e..36a3524 100644 --- a/src/multisafepay/api/shared/cart/shopping_cart.py +++ b/src/multisafepay/api/shared/cart/shopping_cart.py @@ -25,49 +25,55 @@ class ShoppingCart(ApiModel): items: Optional[List[CartItem]] - def get_items(self: "ShoppingCart") -> List[CartItem]: + def get_items(self: "ShoppingCart") -> Optional[List[CartItem]]: """ Get the list of items in the shopping cart. Returns ------- - List[CartItem]: The list of items in the shopping cart. + Optional[List[CartItem]]: The list of items in the shopping cart, or None if no items exist. """ return self.items def add_items( self: "ShoppingCart", - items: List[CartItem], + items: Optional[List[CartItem]], ) -> "ShoppingCart": """ Add multiple items to the shopping cart. Parameters ---------- - items: (List[CartItem]) The list of items to be added to the shopping cart. + items: (Optional[List[CartItem]]) The list of items to be added to the shopping cart. Returns ------- ShoppingCart: The updated ShoppingCart instance. """ - self.items = items + if items is not None: + self.items = items return self - def add_item(self: "ShoppingCart", item: CartItem) -> "ShoppingCart": + def add_item( + self: "ShoppingCart", + item: Optional[CartItem], + ) -> "ShoppingCart": """ Add a single item to the shopping cart. Parameters ---------- - item: (CartItem) The item to be added to the shopping cart. + item: (Optional[CartItem]) The item to be added to the shopping cart. Returns ------- ShoppingCart: The updated ShoppingCart instance. """ + if item is None: + return self if self.items is None: self.items = [] self.items.append(item) diff --git a/src/multisafepay/api/shared/checkout/checkout_options.py b/src/multisafepay/api/shared/checkout/checkout_options.py index ea109e0..32d7fb0 100644 --- a/src/multisafepay/api/shared/checkout/checkout_options.py +++ b/src/multisafepay/api/shared/checkout/checkout_options.py @@ -68,20 +68,22 @@ def add_alternate( def add_tax_rule( self: "CheckoutOptions", - tax_rule: TaxRule, + tax_rule: Optional[TaxRule], ) -> "CheckoutOptions": """ Add a tax rule to the checkout options. Parameters ---------- - tax_rule (TaxRule): The tax rule to be added. + tax_rule (Optional[TaxRule]): The tax rule to be added. Returns ------- CheckoutOptions: The updated CheckoutOptions instance. """ + if tax_rule is None: + return self if self.alternate is None: self.alternate = [] self.alternate.append(tax_rule) diff --git a/src/multisafepay/api/shared/checkout/tax_rule.py b/src/multisafepay/api/shared/checkout/tax_rule.py index eeac997..cb391be 100644 --- a/src/multisafepay/api/shared/checkout/tax_rule.py +++ b/src/multisafepay/api/shared/checkout/tax_rule.py @@ -80,22 +80,25 @@ def add_standalone( self.standalone = standalone return self - def add_rule(self: "TaxRule", rule: TaxRate) -> "TaxRule": + def add_rule(self: "TaxRule", rule: Optional[TaxRate]) -> "TaxRule": """ Add a single tax rate to the tax rule. Parameters ---------- - rule (TaxRate): The tax rate to be added. + rule (Optional[TaxRate]): The tax rate to be added. Returns ------- TaxRule: The updated TaxRule instance. """ + if rule is None: + return self if self.rules is None: self.rules = [] self.rules.append(rule) + return self @staticmethod def from_dict(d: Optional[Dict]) -> Optional["TaxRule"]: diff --git a/src/multisafepay/api/shared/customer.py b/src/multisafepay/api/shared/customer.py index 0081f78..aec30b3 100644 --- a/src/multisafepay/api/shared/customer.py +++ b/src/multisafepay/api/shared/customer.py @@ -34,13 +34,13 @@ class Customer(Delivery): user_agent: Optional[str] reference: Optional[str] - def add_locale(self: "Customer", locale: str) -> "Customer": + def add_locale(self: "Customer", locale: Optional[str]) -> "Customer": """ Add a locale to the customer. Parameters ---------- - locale (str): The locale to add. + locale (Optional[str]): The locale to add. Returns ------- @@ -52,14 +52,14 @@ def add_locale(self: "Customer", locale: str) -> "Customer": def add_ip_address( self: "Customer", - ip_address: Union[IpAddress, str], + ip_address: Optional[Union[IpAddress, str]], ) -> "Customer": """ Add an IP address to the customer. Parameters ---------- - ip_address (IpAddress | str): The IP address to add. + ip_address (Optional[IpAddress] | str): The IP address to add. Returns ------- @@ -68,19 +68,19 @@ def add_ip_address( """ if isinstance(ip_address, str): ip_address = IpAddress(ip_address=ip_address) - self.ip_address = ip_address.get() + self.ip_address = ip_address.get() if ip_address is not None else None return self def add_forwarded_ip( self: "Customer", - forwarded_ip: Union[IpAddress, str], + forwarded_ip: Optional[Union[IpAddress, str]], ) -> "Customer": """ Add a forwarded IP address to the customer. Parameters ---------- - forwarded_ip (IpAddress | str): The forwarded IP address to add. + forwarded_ip (Optional[IpAddress] | str): The forwarded IP address to add. Returns ------- @@ -89,16 +89,18 @@ def add_forwarded_ip( """ if isinstance(forwarded_ip, str): forwarded_ip = IpAddress(ip_address=forwarded_ip) - self.forwarded_ip = forwarded_ip.get() + self.forwarded_ip = ( + forwarded_ip.get() if forwarded_ip is not None else None + ) return self - def add_referrer(self: "Customer", referrer: str) -> "Customer": + def add_referrer(self: "Customer", referrer: Optional[str]) -> "Customer": """ Add a referrer URL to the customer. Parameters ---------- - referrer (str): The referrer URL to add. + referrer (Optional[str]): The referrer URL to add. Returns ------- @@ -108,13 +110,16 @@ def add_referrer(self: "Customer", referrer: str) -> "Customer": self.referrer = referrer return self - def add_user_agent(self: "Customer", user_agent: str) -> "Customer": + def add_user_agent( + self: "Customer", + user_agent: Optional[str], + ) -> "Customer": """ Add a user agent string to the customer. Parameters ---------- - user_agent (str): The user agent string to add. + user_agent (Optional[str]): The user agent string to add. Returns ------- @@ -124,13 +129,16 @@ def add_user_agent(self: "Customer", user_agent: str) -> "Customer": self.user_agent = user_agent return self - def add_reference(self: "Customer", reference: str) -> "Customer": + def add_reference( + self: "Customer", + reference: Optional[str], + ) -> "Customer": """ Add a reference to the customer. Parameters ---------- - reference (str): The reference to add. + reference (Optional[str]): The reference to add. Returns ------- diff --git a/src/multisafepay/api/shared/delivery.py b/src/multisafepay/api/shared/delivery.py index ba1364f..8686c4d 100644 --- a/src/multisafepay/api/shared/delivery.py +++ b/src/multisafepay/api/shared/delivery.py @@ -202,7 +202,7 @@ def add_country( """ if isinstance(country, str): country = Country(code=country) - self.country = country.get_code() + self.country = country.get_code() if country is not None else None return self def add_phone( @@ -223,7 +223,7 @@ def add_phone( """ if isinstance(phone, str): phone = PhoneNumber(phone_number=phone) - self.phone = phone.get() + self.phone = phone.get() if phone is not None else None return self def add_email( @@ -244,7 +244,7 @@ def add_email( """ if isinstance(email, str): email = EmailAddress(email_address=email) - self.email = email.get() + self.email = email.get() if email is not None else None return self @staticmethod diff --git a/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py b/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py index 812311c..8c9e94e 100644 --- a/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py +++ b/tests/multisafepay/integration/api/path/orders/request/components/test_integration_orders_components_checkout_options.py @@ -8,7 +8,6 @@ """Test module for integration testing.""" -import pytest from multisafepay.api.shared.checkout.tax_rate import TaxRate from multisafepay.api.shared.checkout.tax_rule import TaxRule from multisafepay.api.paths.orders.request.components.checkout_options import ( @@ -104,12 +103,14 @@ def test_generate_from_shopping_cart_none(): """ Test the generate_from_shopping_cart method of CheckoutOptions with a None shopping cart. - This test checks if an AttributeError is raised when the shopping cart is None. + This test checks if None is returned when the shopping cart is None. """ shopping_cart = None - with pytest.raises(AttributeError): - CheckoutOptions.generate_from_shopping_cart(shopping_cart) + generated_checkout_options = CheckoutOptions.generate_from_shopping_cart( + shopping_cart, + ) + assert generated_checkout_options is None def test_generate_from_shopping_cart_no_items(): diff --git a/tests/multisafepay/integration/api/shared/checkout/test_integration_tax_rule.py b/tests/multisafepay/integration/api/shared/checkout/test_integration_tax_rule.py index 659a54b..b2f27ff 100644 --- a/tests/multisafepay/integration/api/shared/checkout/test_integration_tax_rule.py +++ b/tests/multisafepay/integration/api/shared/checkout/test_integration_tax_rule.py @@ -70,7 +70,7 @@ def test_adds_rules(): assert tax_rule.rules[0].country == "NL" -def test_adds_single_rule(): +def test_adds_a_single_rule(): """ Test that a single rule is added to a TaxRule instance. @@ -81,3 +81,25 @@ def test_adds_single_rule(): assert len(tax_rule.rules) == 1 assert tax_rule.rules[0].rate == 21 assert tax_rule.rules[0].country == "NL" + + +def test_add_rule_method_chaining(): + """ + Test that add_rule supports method chaining (fluent interface). + + This verifies that add_rule returns self to enable chaining. + + """ + tax_rule = TaxRule() + tax_rate1 = TaxRate(rate=21, country="NL") + tax_rate2 = TaxRate(rate=19, country="DE") + + # Test method chaining + result = tax_rule.add_rule(tax_rate1).add_rule(tax_rate2) + + assert result is tax_rule + assert len(tax_rule.rules) == 2 + assert tax_rule.rules[0].rate == 21 + assert tax_rule.rules[0].country == "NL" + assert tax_rule.rules[1].rate == 19 + assert tax_rule.rules[1].country == "DE"