diff --git a/payment_mollie_official/__manifest__.py b/payment_mollie_official/__manifest__.py index e5d3eb3..c6ce12c 100644 --- a/payment_mollie_official/__manifest__.py +++ b/payment_mollie_official/__manifest__.py @@ -26,6 +26,7 @@ 'security/ir.model.access.csv', 'views/payment_views.xml', 'views/payment_mollie_templates.xml', + 'views/account_move_view.xml', 'data/payment_acquirer_data.xml', ], diff --git a/payment_mollie_official/models/__init__.py b/payment_mollie_official/models/__init__.py index 4150353..8f279e8 100644 --- a/payment_mollie_official/models/__init__.py +++ b/payment_mollie_official/models/__init__.py @@ -5,3 +5,4 @@ from . import mollie_method from . import mollie_issuers from . import res_partner +from . import account_move diff --git a/payment_mollie_official/models/account_move.py b/payment_mollie_official/models/account_move.py new file mode 100644 index 0000000..c6a3fa8 --- /dev/null +++ b/payment_mollie_official/models/account_move.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +from odoo import models, fields, _ +from odoo.exceptions import UserError + +import logging + +_logger = logging.getLogger(__name__) + + +class AccountMove(models.Model): + _inherit = "account.move" + + valid_for_mollie_refund = fields.Boolean(compute="_compute_valid_for_mollie_refund") + mollie_refund_reference = fields.Char() + + def _compute_valid_for_mollie_refund(self): + for move in self: + has_mollie_tx = False + if move.type == 'out_refund' and move._find_valid_mollie_transactions() and move.state == "posted" and move.invoice_payment_state != 'paid': + has_mollie_tx = True + move.valid_for_mollie_refund = has_mollie_tx + + def mollie_process_refund(self): + self.ensure_one() + mollie_transactions = self._find_valid_mollie_transactions() + + # TODO: Need to handle multiple transection + if len(mollie_transactions) > 1: + raise UserError(_("Multiple mollie transactions are linked with invoice. Please refund manually from mollie portal")) + + if mollie_transactions: + + # Create payment record and post the payment + AccountPayment = self.env['account.payment'].with_context(active_ids=self.ids, active_model='account.move', active_id=self.id) + payment_obj = AccountPayment.create({ + 'journal_id': mollie_transactions.payment_id.journal_id.id, + 'payment_method_id': mollie_transactions.payment_id.payment_method_id.id + }) + payment_obj.post() + + # Create refund in mollie via API + refund = mollie_transactions.acquirer_id._api_mollie_refund(self.amount_total, self.currency_id, mollie_transactions.acquirer_reference) + if refund['status'] == 'refunded': + self.mollie_refund_reference = refund['id'] + + def _find_valid_mollie_transactions(self): + self.ensure_one() + return self.reversed_entry_id.transaction_ids.filtered(lambda tx: tx.state == 'done' and tx.acquirer_id.provider == 'mollie') diff --git a/payment_mollie_official/models/payment_acquirer.py b/payment_mollie_official/models/payment_acquirer.py index 57b4043..1516a9d 100644 --- a/payment_mollie_official/models/payment_acquirer.py +++ b/payment_mollie_official/models/payment_acquirer.py @@ -324,6 +324,27 @@ def _api_mollie_get_active_payment_methods(self, api_type=None): return result + def _api_mollie_refund(self, amount, currency, transection_reference): + payment_record = self._mollie_get_payment_data(transection_reference) + transection_id = False + if payment_record['resource'] == 'order': + payments = payment_record.get('_embedded', {}).get('payments', []) + if payments: + # TODO: handle multiple payment for same order + transection_id = payments[0]['id'] + elif payment_record['resource'] == 'payment': + transection_id = payment_record['id'] + + mollie_client = self._api_mollie_get_client() + payment_rec = mollie_client.payments.get(transection_id) + refund = mollie_client.payment_refunds.on(payment_rec).create({ + 'amount': { + 'value': "%.2f" % amount, + 'currency': currency.name + } + }) + return refund + # ----------------------------------------------- # Methods that create mollie order payload # ----------------------------------------------- diff --git a/payment_mollie_official/static/description/index.html b/payment_mollie_official/static/description/index.html index b6b0785..1edfc27 100644 --- a/payment_mollie_official/static/description/index.html +++ b/payment_mollie_official/static/description/index.html @@ -55,7 +55,7 @@
Inline issuer selector shows available issuer in Odoo itself. - It reduces one step at payment portal and make checkout faster. + It reduces one step at payment portal and makes checkout faster.