|
| 1 | +############################################################################## |
| 2 | +# For copyright and license notices, see __manifest__.py file in module root |
| 3 | +# directory |
| 4 | +############################################################################## |
| 5 | + |
| 6 | +from odoo import _, http |
| 7 | +from odoo.exceptions import ValidationError |
| 8 | +from odoo.fields import Command |
| 9 | +from odoo.http import request |
| 10 | + |
| 11 | +from odoo.addons.portal.controllers import portal |
| 12 | +from odoo.tools import float_compare |
| 13 | + |
| 14 | + |
| 15 | +class PaymentPortal(portal.CustomerPortal): |
| 16 | + |
| 17 | + @http.route('/payment/invoice_multi_link', type='json', auth='user') |
| 18 | + def invoice_multi_transaction(self, invoice_ids, amount, **kwargs): |
| 19 | + invoices_sudo = request.env['account.move'].sudo() |
| 20 | + for invoice in invoice_ids: |
| 21 | + # Check the invoice id |
| 22 | + invoices_sudo += self._document_check_access('account.move', invoice['id'], invoice.get('token')) |
| 23 | + payment_link = request.env['payment.link.wizard'].sudo().with_context(active_id=invoices_sudo[0].id, active_ids=invoices_sudo.ids, active_model='account.move').create({}) |
| 24 | + |
| 25 | + if float_compare(payment_link.amount_max, amount, precision_rounding=payment_link.currency_id.rounding or 0.01) == -1: |
| 26 | + raise ValidationError(_("Incorrect amount")) |
| 27 | + return payment_link.link |
| 28 | + |
| 29 | + @http.route() |
| 30 | + def payment_pay( |
| 31 | + self, reference=None, amount=None, currency_id=None, partner_id=None, company_id=None, |
| 32 | + acquirer_id=None, access_token=None, invoice_id=None, **kwargs |
| 33 | + ): |
| 34 | + |
| 35 | + amount = self._cast_as_float(amount) |
| 36 | + if 'invoice_ids' in kwargs: |
| 37 | + invoice_ids = [int(x) for x in kwargs['invoice_ids'].split(',') if x.isdigit()] |
| 38 | + invoices_sudo = request.env['account.move'].sudo().browse(invoice_ids).exists() |
| 39 | + if not invoices_sudo: |
| 40 | + raise ValidationError(_("The provided parameters are invalid.")) |
| 41 | + if len(invoices_sudo.mapped('commercial_partner_id')) > 1: |
| 42 | + raise ValidationError(_("Only pay invoices from the same customer.")) |
| 43 | + if len(invoices_sudo.mapped('currency_id')) > 1: |
| 44 | + raise ValidationError(_("Only pay invoices from the same currency.")) |
| 45 | + if len(invoices_sudo.mapped('company_id')) > 1: |
| 46 | + raise ValidationError(_("Only pay invoices from the same company.")) |
| 47 | + first_invoice_sudo = invoices_sudo[0] |
| 48 | + # Check the access token against the invoice values. Done after fetching the invoice |
| 49 | + # as we need the invoice fields to check the access token. |
| 50 | + # if not payment_utils.check_access_token( |
| 51 | + # access_token, first_invoice_sudo.partner_id.id, amount, first_invoice_sudo.currency_id.id |
| 52 | + # ): |
| 53 | + # raise ValidationError(_("The provided parameters are invalid.")) |
| 54 | + currency_id = first_invoice_sudo.currency_id.id |
| 55 | + partner_id = first_invoice_sudo.commercial_partner_id.id |
| 56 | + company_id = first_invoice_sudo.company_id.id |
| 57 | + |
| 58 | + kwargs.update({ |
| 59 | + 'invoice_ids': invoice_ids, |
| 60 | + }) |
| 61 | + return super().payment_pay( |
| 62 | + reference=reference, amount=amount, currency_id=currency_id, partner_id=partner_id, company_id=company_id, |
| 63 | + acquirer_id=acquirer_id, access_token=access_token, invoice_id=invoice_id, **kwargs |
| 64 | + |
| 65 | + ) |
| 66 | + |
| 67 | + def _get_custom_rendering_context_values(self, invoice_ids=None, **kwargs): |
| 68 | + """ Override of `payment` to add the invoice id in the custom rendering context values. |
| 69 | +
|
| 70 | + :param int invoice_id: The invoice for which a payment id made, as an `account.move` id. |
| 71 | + :param dict kwargs: Optional data. This parameter is not used here. |
| 72 | + :return: The extended rendering context values. |
| 73 | + :rtype: dict |
| 74 | + """ |
| 75 | + rendering_context_values = super()._get_custom_rendering_context_values(**kwargs) |
| 76 | + if invoice_ids: |
| 77 | + rendering_context_values['invoice_ids'] = invoice_ids |
| 78 | + |
| 79 | + # Interrupt the payment flow if the invoice has been canceled. |
| 80 | + # invoice_sudo = request.env['account.move'].sudo().browse(invoice_ids) |
| 81 | + #if invoice_sudo.state == 'cancel': |
| 82 | + # rendering_context_values['amount'] = 0.0 |
| 83 | + return rendering_context_values |
| 84 | + |
| 85 | + def _create_transaction(self, *args, invoice_ids=None, custom_create_values=None, **kwargs): |
| 86 | + """ Override of `payment` to add the invoice id in the custom create values. |
| 87 | +
|
| 88 | + :param int invoice_id: The invoice for which a payment id made, as an `account.move` id. |
| 89 | + :param dict custom_create_values: Additional create values overwriting the default ones. |
| 90 | + :param dict kwargs: Optional data. This parameter is not used here. |
| 91 | + :return: The result of the parent method. |
| 92 | + :rtype: recordset of `payment.transaction` |
| 93 | + """ |
| 94 | + ##import pdb; pdb.set_trace() |
| 95 | + if invoice_ids: |
| 96 | + if custom_create_values is None: |
| 97 | + custom_create_values = {} |
| 98 | + custom_create_values['invoice_ids'] = [Command.set(invoice_ids)] |
| 99 | + return super()._create_transaction( |
| 100 | + *args, custom_create_values=custom_create_values, **kwargs |
| 101 | + ) |
0 commit comments