utils.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from odoo.addons.payment import utils as payment_utils
  2. def format_partner_name(partner_name):
  3. """ Format the partner name to comply with the payload structure of the API request.
  4. :param str partner_name: The name of the partner making the payment.
  5. :return: The formatted partner name.
  6. :rtype: dict
  7. """
  8. first_name, last_name = payment_utils.split_partner_name(partner_name)
  9. return {
  10. 'firstName': first_name,
  11. 'lastName': last_name,
  12. }
  13. def include_partner_addresses(tx_sudo):
  14. """ Include the billing and delivery addresses of the related sales order to the payload of the
  15. API request.
  16. If no related sales order exists, the addresses are not included.
  17. Note: `self.ensure_one()`
  18. :param payment.transaction tx_sudo: The sudoed transaction of the payment.
  19. :return: The subset of the API payload that includes the billing and delivery addresses.
  20. :rtype: dict
  21. """
  22. tx_sudo.ensure_one()
  23. if 'sale_order_ids' in tx_sudo._fields: # The module `sale` is installed.
  24. order = tx_sudo.sale_order_ids[:1]
  25. if order:
  26. return {
  27. 'billingAddress': format_partner_address(order.partner_invoice_id),
  28. 'deliveryAddress': format_partner_address(order.partner_shipping_id),
  29. }
  30. return {}
  31. def format_partner_address(partner):
  32. """ Format the partner address to comply with the payload structure of the API request.
  33. :param res.partner partner: The partner making the payment.
  34. :return: The formatted partner address.
  35. :rtype: dict
  36. """
  37. street_data = partner._get_street_split()
  38. return {
  39. 'city': partner.city,
  40. 'country': partner.country_id.code or 'ZZ', # 'ZZ' if the country is not known.
  41. 'stateOrProvince': partner.state_id.code,
  42. 'postalCode': partner.zip,
  43. # Fill in the address fields if the format is supported, or fallback to the raw address.
  44. 'street': street_data.get('street_name', partner.street),
  45. 'houseNumberOrName': street_data.get('street_number'),
  46. }