res_config_settings.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. class ResConfigSettings(models.TransientModel):
  5. _inherit = 'res.config.settings'
  6. providers_state = fields.Selection(
  7. selection=[
  8. ('none', 'None'),
  9. ('paypal_only', 'Paypal Only'),
  10. ('other_than_paypal', 'Other than Paypal')
  11. ],
  12. compute='_compute_providers_state')
  13. first_provider_label = fields.Char(
  14. compute='_compute_providers_state')
  15. module_payment_paypal = fields.Boolean(
  16. string='Paypal - Express Checkout')
  17. is_stripe_supported_country = fields.Boolean(
  18. related='company_id.country_id.is_stripe_supported_country')
  19. def _get_activated_providers(self):
  20. self.ensure_one()
  21. wire_transfer = self.env.ref('payment.payment_provider_transfer', raise_if_not_found=False)
  22. return self.env['payment.provider'].search([
  23. ('state', '!=', 'disabled'),
  24. ('id', '!=', wire_transfer.id if wire_transfer else False),
  25. '|',
  26. ('website_id', '=', False),
  27. ('website_id', '=', self.website_id.id)
  28. ])
  29. @api.depends('website_id')
  30. def _compute_providers_state(self):
  31. paypal = self.env.ref('payment.payment_provider_paypal', raise_if_not_found=False)
  32. stripe = self.env.ref('payment.payment_provider_stripe', raise_if_not_found=False)
  33. for config in self:
  34. providers = config._get_activated_providers()
  35. first_provider = stripe if stripe and stripe in providers else providers[0] if providers else providers
  36. config.first_provider_label = _('Configure %s', first_provider.name)
  37. if len(providers) == 1 and providers == paypal:
  38. config.providers_state = 'paypal_only'
  39. elif len(providers) >= 1:
  40. config.providers_state = 'other_than_paypal'
  41. else:
  42. config.providers_state = 'none'
  43. def action_activate_stripe(self):
  44. self.ensure_one()
  45. if not self.is_stripe_supported_country:
  46. return False
  47. menu = self.env.ref('website.menu_website_website_settings', raise_if_not_found=False)
  48. menu_id = menu and menu.id
  49. return self.env.company._run_payment_onboarding_step(menu_id=menu_id)
  50. def action_configure_first_provider(self):
  51. self.ensure_one()
  52. stripe = self.env['payment.provider'].search(
  53. [('company_id', '=', self.env.company.id), ('code', '=', 'stripe')], limit=1
  54. )
  55. providers = self._get_activated_providers()
  56. return {
  57. 'name': self.first_provider_label,
  58. 'type': 'ir.actions.act_window',
  59. 'view_mode': 'form',
  60. 'res_model': 'payment.provider',
  61. 'views': [[False, 'form']],
  62. 'res_id': stripe.id if stripe in providers else providers[0].id,
  63. }