New version 16.0

This commit is contained in:
vandekul
2023-09-15 14:42:14 +02:00
parent ac2194498a
commit 7810629596
36 changed files with 403 additions and 1681 deletions
+1 -1
View File
@@ -1 +1 @@
import btcpay
from . import payment_provider, payment_transaction
-82
View File
@@ -1,82 +0,0 @@
#******************************************************************************
# PAYMENT BTCPAY FOR ODOO
#
# Copyright (C) 2020 Susanna Fort <susannafm@gmail.com>
#
#******************************************************************************
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# For a full copy of the GNU General Public License see the LICENSE.txt file.
#
#******************************************************************************
from openerp import api, fields, models, _
from openerp.osv import osv
from ..controller import crypto as bku
from ..controller.client import BTCPayClient
import logging
import pprint
from openerp import http, SUPERUSER_ID
_logger = logging.getLogger(__name__)
class AcquirerBtcPay(models.Model):
_inherit = 'payment.acquirer'
def _get_providers(self, cr, uid, context=None):
providers = super(AcquirerBtcPay, self)._get_providers(cr, uid, context=context)
providers.append(['btcpay', 'btcpay'])
return providers
token = fields.Char('Token', help='Access Token to BTCPay')
privateKey = fields.Text('Private Key', help='Private Key for BTCPay Client')
facade = fields.Char('Facade', help='Token facade type: merchant/pos/payroll') #merchant
pairingCode = fields.Char('Pairing Code', help='Create a paring Code in your BTCPay server and put here')
location = fields.Char('Location', size=64)
confirmationURL = fields.Char('Confirmation URL', help='Confirmation URL to return after Btcpay payment')
buyerNotification = fields.Boolean('Odoo confirmation mail to buyer', help='If it is checked, Odoo will send the confirmation mail defined')
_defaults = {
'facade': 'merchant',
'location':'https://testnet.demo.btcpayserver.org', #Testnet BTCPay
'confirmationURL':'http://odoo-dev.zynthian.org/shop/confirmation',
'buyerNotification': 'True',
}
def create(self, cr, uid, values, context=None):
if values.get('provider') == 'btcpay' and not values.get('privateKey'):
values['privateKey'] = bku.generate_privkey()
return super(AcquirerBtcPay, self).create(cr, uid, values, context=context)
@api.onchange('pairingCode')
def _onchange_pairingCode(self):
if not self.token and self.provider == 'btcpay':
client = BTCPayClient(host=self.location, pem=self.privateKey)
token = client.pair_client(self.pairingCode)
self.token = token.get(self.facade)
@api.onchange('token')
def _onchange_token(self):
if self.provider == 'btcpay':
self.pairingCode = ''
class BtcPayTransaction(models.Model):
_inherit = "payment.transaction"
btcpay_invoiceId = fields.Char("Invoice Id")
btcpay_txid = fields.Char("Transaction Id")
btcpay_status = fields.Char("Transaction Status")
btcpay_buyerMailNotification = fields.Char("Buyer Mail Notification")
acquirer_name = fields.Selection(related='acquirer_id.provider')
+54
View File
@@ -0,0 +1,54 @@
import logging
from odoo import _, api, fields, models
from btcpay import BTCPayClient
from btcpay import crypto
_logger = logging.getLogger(__name__)
class PaymentProvider(models.Model):
_inherit = 'payment.provider'
code = fields.Selection(
selection_add=[('btcpay', "BTCPay")], ondelete={'btcpay': 'set default'})
btcpay_location = fields.Char(string='Location', size=64, default='https://btcpay.evolus.net')
btcpay_confirmationURL = fields.Char(string='Confirmation URL', help='Confirmation URL to return after Btcpay payment', default='http://odoo-dev.zynthian.org/shop/confirmation')
btcpay_buyerNotification = fields.Boolean(string='Odoo confirmation mail to buyer', help='If it is checked, Odoo will send the confirmation mail defined', default=True)
btcpay_token = fields.Char(string='Token', help='Access Token to BTCPay')
btcpay_privateKey = fields.Text(string='Private Key', help='Private Key for BTCPay Client')
btcpay_facade = fields.Char(string='Facade', help='Token facade type: merchant/pos/payroll', default='merchant') #merchant
btcpay_pairingCode = fields.Char(string='Pairing Code', help='Create paring Code in your BTCPay server and put here')
def create(self, values_list):
if values_list[0]['code'] == 'btcpay':
values_list[0]['btcpay_privateKey'] = crypto.generate_privkey()
return super(PaymentProvider, self).create(values_list)
@api.onchange('btcpay_pairingCode')
def _onchange_pairingCode(self):
if not self.btcpay_token and self.code == 'btcpay' and not self.btcpay_pairingCode == '':
#_logger.info("ONCHANGE PAIRING CODE***SELF: %s %s %s", self.btcpay_location, self.btcpay_privateKey, self.btcpay_pairingCode)
self.btcpay_privateKey = crypto.generate_privkey()
client = BTCPayClient(host=self.btcpay_location, pem=self.btcpay_privateKey)
token = client.pair_client(self.btcpay_pairingCode)
self.btcpay_token = token.get(self.btcpay_facade)
@api.onchange('btcpay_token')
def _onchange_token(self):
if self.code == 'btcpay':
self.btcpay_pairingCode = ''
#_logger.info("ONCHANGE TOKEN")
@api.onchange('btcpay_location')
def _onchange_location(self):
if self.code == 'btcpay':
self.btcpay_token = ''
#_logger.info("ONCHANGE LOCATION ***SELF: %s %s %s", self.btcpay_location, self.btcpay_privateKey, self.btcpay_pairingCode)
self.btcpay_privateKey = ''
self.btcpay_pairingCode = ''
@@ -0,0 +1,134 @@
import logging
import pprint
from werkzeug import urls
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.addons.payment import utils as payment_utils
from ..controllers.main import BTCPayController
_logger = logging.getLogger(__name__)
class PaymentTransaction(models.Model):
_inherit = 'payment.transaction'
btcpay_invoiceId = fields.Char("Invoice Id")
btcpay_txid = fields.Char("Transaction Id")
btcpay_status = fields.Char("Transaction Status")
api_url = '/btcpay/checkout'
def _get_specific_rendering_values(self, processing_values):
""" Override of payment to return Paypal-specific rendering values.
Note: self.ensure_one() from `_get_processing_values`
:param dict processing_values: The generic and specific processing values of the transaction
:return: The dict of provider-specific processing values
:rtype: dict
"""
res = super()._get_specific_rendering_values(processing_values)
if self.provider_code != 'btcpay':
return res
base_url = self.provider_id.get_base_url()
_logger.info('Hola! API URL: %s', base_url)
partner_first_name, partner_last_name = payment_utils.split_partner_name(self.partner_name)
return {
'address1': self.partner_address,
'amount': self.amount,
'city': self.partner_city,
'country': self.partner_country_id.code,
'currency_code': self.currency_id.name,
'email': self.partner_email,
'first_name': partner_first_name,
'item_name': f"{self.company_id.name}: {self.reference}",
'item_number': self.reference,
'last_name': partner_last_name,
'lc': self.partner_lang,
'state': self.partner_state_id.name,
'zip_code': self.partner_zip,
'api_url': BTCPayController._checkout_url,
'notify_url': base_url + BTCPayController._notify_url,
}
def _get_tx_from_notification_data(self, provider_code, notification_data):
""" Override of payment to find the transaction based on BTCPay data.
:param str provider_code: The code of the provider that handled the transaction
:param dict notification_data: The notification data sent by the provider
:return: The transaction if found
:rtype: recordset of `payment.transaction`
:raise: ValidationError if the data match no transaction
"""
tx = super()._get_tx_from_notification_data(provider_code, notification_data)
_logger.info('GET TX FROM NOTIFICATION Notification_data %s', pprint.pformat(notification_data))
if provider_code != 'btcpay' or len(tx) == 1:
return tx
reference = notification_data.get('reference')
tx = self.search([('reference', '=', reference), ('provider_code', '=', 'btcpay')])
if not tx:
raise ValidationError(
"BTCPay: " + _("No transaction found matching reference %s.", reference)
)
return tx
def _handle_notification_data(self, provider_code, notification_data):
""" Match the transaction with the notification data, update its state and return it.
:param str provider_code: The code of the provider handling the transaction.
:param dict notification_data: The notification data sent by the provider.
:return: The transaction.
:rtype: recordset of `payment.transaction`
"""
tx = self._get_tx_from_notification_data(provider_code, notification_data)
tx._process_notification_data(notification_data)
tx._execute_callback()
return tx
def _process_notification_data(self, notification_data):
""" Override of payment to process the transaction based on BTCPay data.
Note: self.ensure_one()
:param dict notification_data: The notification data sent by the provider
:return: None
:raise: ValidationError if inconsistent data were received
"""
super()._process_notification_data(notification_data)
if self.provider_code != 'btcpay':
return
#_logger.info("_process_notification_data %s", pprint.pformat(notification_data))
txn_id = notification_data.get('reference')
if not all(txn_id):
raise ValidationError(
"BTCPay: " + _("Missing value for txn_id (%(txn_id)s)).", txn_id=txn_id))
self.provider_reference = txn_id
self.btcpay_txid = notification_data.get('txid')
self.btcpay_status = notification_data.get('status')
if self.btcpay_status in ['paid']:
self._set_pending(state_message=notification_data.get('pending_reason'))
elif self.btcpay_status in ['confirmed']:
self._set_done()
confirmed_orders = self._check_amount_and_confirm_order()
confirmed_orders._send_order_confirmation_mail()
elif self.btcpay_status in ['new']:
self.btcpay_invoiceId = notification_data.get('invoiceID')
elif self.btcpay_status in ['cancel']:
self._set_canceled()
elif self.btcpay_status in ['invalid']:
_logger.info(
"received data with invalid payment status (%s) for transaction with reference %s",
self.btcpay_status, self.reference
)
self._set_error(
"BTCPay: " + _("Received data with invalid payment status: %s", self.btcpay_status)
)