# Part of Odoo. See LICENSE file for full copyright and licensing details. from hashlib import sha1 from odoo import api, fields, tools, http, models, _ from odoo.http import request from odoo.exceptions import AccessError, UserError, ValidationError import logging _logger = logging.getLogger(__name__) import pprint import payplug payplug.set_api_version("2019-08-06") import requests import json class PaymentAcquirerPayPlug(models.Model): _inherit = 'payment.provider' # FOR CREATE SIMULATION ONEY BY API def _oney_payment_simulation(self, amount, country): acquirer = request.env['payment.provider'].sudo().search([('code','=','payplug')], limit=1) if acquirer.state != 'disabled' and acquirer.payment_type_oney == True: # ONEY IS ACTIVE secret_payplug_key = request.env['payment.provider']._key_acquirer_state(acquirer) URL = 'https://api.payplug.com/v1/oney_payment_simulations' headers = { 'Content-Type': 'application/json', 'PayPlug-Version': '2019-08-06', 'Authorization': 'Bearer '+secret_payplug_key, } values = { 'amount': int(amount*100), 'country': 'FR', 'operations': ['x3_with_fees', 'x4_with_fees'], } # REQUEST TO THE API PAYPLUY/ONEY THE PAYMENT SIMULATION result = requests.post(url=URL, json=values, headers=headers).json() vals={} if result and amount >= 100 and amount <= 3000 and result.get('x3_with_fees'): vals['payment_3x']={ 'contribution_of_3x': "%.2f" % (int(result['x3_with_fees']['down_payment_amount'])/100), '1st_monthly_payment_3x': "%.2f" % (int(result['x3_with_fees']['installments'][0]['amount'])/100), '2nd_monthly_payment_3x': "%.2f" % (int(result['x3_with_fees']['installments'][1]['amount'])/100), 'total_payment_3x': "%.2f" % (int(result['x3_with_fees']['down_payment_amount'])/100 \ +int(result['x3_with_fees']['installments'][0]['amount'])/100 \ +int(result['x3_with_fees']['installments'][1]['amount'])/100), } vals['payment_4x']={ 'contribution_of_4x': "%.2f" % (int(result['x4_with_fees']['down_payment_amount'])/100), '1st_monthly_payment_4x': "%.2f" % (int(result['x4_with_fees']['installments'][0]['amount'])/100), '2nd_monthly_payment_4x': "%.2f" % (int(result['x4_with_fees']['installments'][1]['amount'])/100), '3nd_monthly_payment_4x': "%.2f" % (int(result['x4_with_fees']['installments'][2]['amount'])/100), 'total_payment_4x': "%.2f" % (int(result['x4_with_fees']['down_payment_amount'])/100 \ + int(result['x4_with_fees']['installments'][0]['amount'])/100 \ + int(result['x4_with_fees']['installments'][1]['amount'])/100 \ + int(result['x4_with_fees']['installments'][2]['amount'])/100), } else: vals['payment_3x']={ 'contribution_of_3x': "0.00", '1st_monthly_payment_3x': "0.00", '2nd_monthly_payment_3x': "0.00", 'total_payment_3x': "0.00", } vals['payment_4x']={ 'contribution_of_4x': "0.00", '1st_monthly_payment_4x': "0.00", '2nd_monthly_payment_4x': "0.00", '3nd_monthly_payment_4x': "0.00", 'total_payment_4x': "0.00", } return vals # RETURN TEST KEY OR LIVE KEY def _key_acquirer_state(self, acquirer): if acquirer.code == 'payplug' and acquirer.code != 'disabled': if acquirer.state == 'test': secret_payplug_key = acquirer.payplug_secret_test_key else: secret_payplug_key = acquirer.payplug_secret_live_key return secret_payplug_key code = fields.Selection( selection_add=[('payplug', "PayPlug")], ondelete={'payplug': 'set default'}) payplug_secret_test_key = fields.Char( string="Secret Test Key", help="Enter the TEST key of your PayPlug account", required_if_provider='payplug', groups='base.group_user') payplug_secret_live_key = fields.Char( string="Secret Live Key", help="Enter the LIVE key of your PayPlug account", required_if_provider='payplug', groups='base.group_user') # PAYPLUG VALUES payplug_display_as = fields.Char( string="PayPlug displayed as", help="Description of the acquirer for customers", translate=True) payment_type_payplug = fields.Boolean("Payment PayPlug", default=True) available_payplug_country_ids = fields.Many2many( string="Payplug countries", comodel_name='res.country', relation='payplug_payment_country_rel', column1='payment_id', column2='country_id', help="The countries for which this payment acquirer is available.\n" "If none is set, it is available for all countries.") # AMEX VALUES amex_display_as = fields.Char( string="Amex displayed as", help="Description of the acquirer for customers", translate=True) payment_type_amex = fields.Boolean("Payment AmEx") available_amex_country_ids = fields.Many2many( string="Amex countries", comodel_name='res.country', relation='amex_payment_country_rel', column1='payment_id', column2='country_id', help="The countries for which this payment acquirer is available.\n" "If none is set, it is available for all countries.") # BANCONTACT VALUES bancontact_display_as = fields.Char( string="Bancontact displayed as", help="Description of the acquirer for customers", translate=True) payment_type_bancontact = fields.Boolean("Payment Bancontact") available_bancontact_country_ids = fields.Many2many( string="Bancontact countries", comodel_name='res.country', relation='bancontact_payment_country_rel', column1='payment_id', column2='country_id', help="The countries for which this payment acquirer is available.\n" "If none is set, it is available for all countries.") # ONEY VALUES oney_display_as = fields.Char( string="PayLater displayed as", help="Description of the acquirer for customers", translate=True) payment_type_oney = fields.Boolean("Payment PayLater") available_oney_country_ids = fields.Many2many( string="Paylater countries", comodel_name='res.country', relation='oney_payment_country_rel', column1='payment_id', column2='country_id', help="The countries for which this payment acquirer is available.\n" "If none is set, it is available for all countries.") oney_information_message = fields.Char( 'Information message', translate=True, required_if_provider='oney', groups='base.group_user') oney_footer_message = fields.Text( 'Footer pop-up message', translate=True, required_if_provider='oney', groups='base.group_user') # CREATE DIGITAL KEY def _playplug_generate_digital_sign(self, values): keys = "reference customer_name customer_postcode".split() def get_value(key): if values.get(key): return values[key] return '' values = dict(values or {}) sign = ''.join('%s=%s' % (k, get_value(k)) for k in keys) shasign = sha1(sign.encode('utf-8')).hexdigest() return shasign