You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
7.9 KiB

  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. from hashlib import sha1
  3. from odoo import api, fields, tools, http, models, _
  4. from odoo.http import request
  5. from odoo.exceptions import AccessError, UserError, ValidationError
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. import pprint
  9. import payplug
  10. payplug.set_api_version("2019-08-06")
  11. import requests
  12. import json
  13. class PaymentAcquirerPayPlug(models.Model):
  14. _inherit = 'payment.provider'
  15. # FOR CREATE SIMULATION ONEY BY API
  16. def _oney_payment_simulation(self, amount, country):
  17. acquirer = request.env['payment.provider'].sudo().search([('code','=','payplug')], limit=1)
  18. if acquirer.state != 'disabled' and acquirer.payment_type_oney == True: # ONEY IS ACTIVE
  19. secret_payplug_key = request.env['payment.provider']._key_acquirer_state(acquirer)
  20. URL = 'https://api.payplug.com/v1/oney_payment_simulations'
  21. headers = {
  22. 'Content-Type': 'application/json',
  23. 'PayPlug-Version': '2019-08-06',
  24. 'Authorization': 'Bearer '+secret_payplug_key,
  25. }
  26. values = {
  27. 'amount': int(amount*100),
  28. 'country': 'FR',
  29. 'operations': ['x3_with_fees', 'x4_with_fees'],
  30. }
  31. # REQUEST TO THE API PAYPLUY/ONEY THE PAYMENT SIMULATION
  32. result = requests.post(url=URL, json=values, headers=headers).json()
  33. vals={}
  34. if result and amount >= 100 and amount <= 3000 and result.get('x3_with_fees'):
  35. vals['payment_3x']={
  36. 'contribution_of_3x': "%.2f" % (int(result['x3_with_fees']['down_payment_amount'])/100),
  37. '1st_monthly_payment_3x': "%.2f" % (int(result['x3_with_fees']['installments'][0]['amount'])/100),
  38. '2nd_monthly_payment_3x': "%.2f" % (int(result['x3_with_fees']['installments'][1]['amount'])/100),
  39. 'total_payment_3x': "%.2f" % (int(result['x3_with_fees']['down_payment_amount'])/100 \
  40. +int(result['x3_with_fees']['installments'][0]['amount'])/100 \
  41. +int(result['x3_with_fees']['installments'][1]['amount'])/100),
  42. }
  43. vals['payment_4x']={
  44. 'contribution_of_4x': "%.2f" % (int(result['x4_with_fees']['down_payment_amount'])/100),
  45. '1st_monthly_payment_4x': "%.2f" % (int(result['x4_with_fees']['installments'][0]['amount'])/100),
  46. '2nd_monthly_payment_4x': "%.2f" % (int(result['x4_with_fees']['installments'][1]['amount'])/100),
  47. '3nd_monthly_payment_4x': "%.2f" % (int(result['x4_with_fees']['installments'][2]['amount'])/100),
  48. 'total_payment_4x': "%.2f" % (int(result['x4_with_fees']['down_payment_amount'])/100 \
  49. + int(result['x4_with_fees']['installments'][0]['amount'])/100 \
  50. + int(result['x4_with_fees']['installments'][1]['amount'])/100 \
  51. + int(result['x4_with_fees']['installments'][2]['amount'])/100),
  52. }
  53. else:
  54. vals['payment_3x']={
  55. 'contribution_of_3x': "0.00",
  56. '1st_monthly_payment_3x': "0.00",
  57. '2nd_monthly_payment_3x': "0.00",
  58. 'total_payment_3x': "0.00",
  59. }
  60. vals['payment_4x']={
  61. 'contribution_of_4x': "0.00",
  62. '1st_monthly_payment_4x': "0.00",
  63. '2nd_monthly_payment_4x': "0.00",
  64. '3nd_monthly_payment_4x': "0.00",
  65. 'total_payment_4x': "0.00",
  66. }
  67. return vals
  68. # RETURN TEST KEY OR LIVE KEY
  69. def _key_acquirer_state(self, acquirer):
  70. if acquirer.code == 'payplug' and acquirer.code != 'disabled':
  71. if acquirer.state == 'test':
  72. secret_payplug_key = acquirer.payplug_secret_test_key
  73. else:
  74. secret_payplug_key = acquirer.payplug_secret_live_key
  75. return secret_payplug_key
  76. code = fields.Selection(
  77. selection_add=[('payplug', "PayPlug")], ondelete={'payplug': 'set default'})
  78. payplug_secret_test_key = fields.Char(
  79. string="Secret Test Key",
  80. help="Enter the TEST key of your PayPlug account",
  81. required_if_provider='payplug',
  82. groups='base.group_user')
  83. payplug_secret_live_key = fields.Char(
  84. string="Secret Live Key",
  85. help="Enter the LIVE key of your PayPlug account",
  86. required_if_provider='payplug',
  87. groups='base.group_user')
  88. # PAYPLUG VALUES
  89. payplug_display_as = fields.Char(
  90. string="PayPlug displayed as", help="Description of the acquirer for customers",
  91. translate=True)
  92. payment_type_payplug = fields.Boolean("Payment PayPlug", default=True)
  93. available_payplug_country_ids = fields.Many2many(
  94. string="Payplug countries", comodel_name='res.country', relation='payplug_payment_country_rel',
  95. column1='payment_id', column2='country_id',
  96. help="The countries for which this payment acquirer is available.\n"
  97. "If none is set, it is available for all countries.")
  98. # AMEX VALUES
  99. amex_display_as = fields.Char(
  100. string="Amex displayed as", help="Description of the acquirer for customers",
  101. translate=True)
  102. payment_type_amex = fields.Boolean("Payment AmEx")
  103. available_amex_country_ids = fields.Many2many(
  104. string="Amex countries", comodel_name='res.country', relation='amex_payment_country_rel',
  105. column1='payment_id', column2='country_id',
  106. help="The countries for which this payment acquirer is available.\n"
  107. "If none is set, it is available for all countries.")
  108. # BANCONTACT VALUES
  109. bancontact_display_as = fields.Char(
  110. string="Bancontact displayed as", help="Description of the acquirer for customers",
  111. translate=True)
  112. payment_type_bancontact = fields.Boolean("Payment Bancontact")
  113. available_bancontact_country_ids = fields.Many2many(
  114. string="Bancontact countries", comodel_name='res.country', relation='bancontact_payment_country_rel',
  115. column1='payment_id', column2='country_id',
  116. help="The countries for which this payment acquirer is available.\n"
  117. "If none is set, it is available for all countries.")
  118. # ONEY VALUES
  119. oney_display_as = fields.Char(
  120. string="PayLater displayed as", help="Description of the acquirer for customers",
  121. translate=True)
  122. payment_type_oney = fields.Boolean("Payment PayLater")
  123. available_oney_country_ids = fields.Many2many(
  124. string="Paylater countries", comodel_name='res.country', relation='oney_payment_country_rel',
  125. column1='payment_id', column2='country_id',
  126. help="The countries for which this payment acquirer is available.\n"
  127. "If none is set, it is available for all countries.")
  128. oney_information_message = fields.Char(
  129. 'Information message',
  130. translate=True,
  131. required_if_provider='oney',
  132. groups='base.group_user')
  133. oney_footer_message = fields.Text(
  134. 'Footer pop-up message',
  135. translate=True,
  136. required_if_provider='oney',
  137. groups='base.group_user')
  138. # CREATE DIGITAL KEY
  139. def _playplug_generate_digital_sign(self, values):
  140. keys = "reference customer_name customer_postcode".split()
  141. def get_value(key):
  142. if values.get(key):
  143. return values[key]
  144. return ''
  145. values = dict(values or {})
  146. sign = ''.join('%s=%s' % (k, get_value(k)) for k in keys)
  147. shasign = sha1(sign.encode('utf-8')).hexdigest()
  148. return shasign