[FIX] website_sale_payment_terms: fix errors in the payment process

* clear order terms if user rejects agreement
* set payment transaction amount to amount_due_today
* fix bad view inheritance spec
* fix default selected term when partner_term is not in website_terms
* do not render payment bypass form if amount_total is 0

H5924
This commit is contained in:
Cedric Collins
2021-10-25 13:41:51 -05:00
parent 7d758539c0
commit b79f9487b9
6 changed files with 119 additions and 11 deletions

View File

@@ -51,9 +51,7 @@ class WebsiteSalePaymentTerms(WebsiteSaleDelivery):
def reject_term_agreement(self, **kw): def reject_term_agreement(self, **kw):
order = request.website.sale_get_order() order = request.website.sale_get_order()
if order: if order:
partner = request.env.user.partner_id order.payment_term_id = False
order.write({'payment_term_id': request.website.sale_get_payment_term(partner),
'require_payment': True})
return request.redirect('/shop/cart') return request.redirect('/shop/cart')
# Confirm order without taking payment # Confirm order without taking payment

View File

@@ -2,4 +2,5 @@ from . import account
from . import payment from . import payment
from . import res_config from . import res_config
from . import sale from . import sale
from . import sale_patch
from . import website from . import website

View File

@@ -1,4 +1,6 @@
from odoo import models, _ from odoo import models, _
import logging
_logger = logging.getLogger(__name__)
class PaymentTransaction(models.Model): class PaymentTransaction(models.Model):
@@ -15,7 +17,30 @@ class PaymentTransaction(models.Model):
self._log_payment_transaction_sent() self._log_payment_transaction_sent()
return self.acquirer_id.with_context(submit_class='btn btn-primary', submit_txt=submit_txt or _('Pay Now')).sudo().render( return self.acquirer_id.with_context(submit_class='btn btn-primary', submit_txt=submit_txt or _('Pay Now')).sudo().render(
self.reference, self.reference,
order.amount_total_deposit or order.amount_total, order.amount_due_today,
order.pricelist_id.currency_id.id, order.pricelist_id.currency_id.id,
values=values, values=values,
) )
# Override to confirm payments totaling the amount_due_today
def _check_amount_and_confirm_order(self):
self.ensure_one()
for order in self.sale_order_ids.filtered(lambda so: so.state in ('draft', 'sent')):
amount = order.amount_due_today
if order.currency_id.compare_amounts(self.amount, amount) == 0:
order.with_context(send_email=True).action_confirm()
else:
_logger.warning(
'<%s> transaction AMOUNT MISMATCH for order %s (ID %s): expected %r, got %r',
self.acquirer_id.provider, order.name, order.id,
amount, self.amount,
)
order.message_post(
subject=_("Amount Mismatch (%s)") % self.acquirer_id.provider,
body=_(
"The order was not confirmed despite response from the acquirer (%s): order total is %r but acquirer replied with %r.") % (
self.acquirer_id.provider,
amount,
self.amount,
)
)

View File

@@ -0,0 +1,86 @@
from odoo.exceptions import ValidationError
from odoo.addons.sale.models.sale import SaleOrder
def _create_payment_transaction(self, vals):
# Code copied from sale_payment_deposit to use order.amount_due_today
#
# This is a copy job from odoo.addons.sale.models.sale due to the closed nature of the vals.update(dict) call
# Ultimately, only the 'vals.update' with the new amount is really used.
'''Similar to self.env['payment.transaction'].create(vals) but the values are filled with the
current sales orders fields (e.g. the partner or the currency).
:param vals: The values to create a new payment.transaction.
:return: The newly created payment.transaction record.
'''
# Ensure the currencies are the same.
# extract variable for use later.
sale = self[0]
currency = sale.pricelist_id.currency_id
if any([so.pricelist_id.currency_id != currency for so in self]):
raise ValidationError(_('A transaction can\'t be linked to sales orders having different currencies.'))
# Ensure the partner are the same.
partner = sale.partner_id
if any([so.partner_id != partner for so in self]):
raise ValidationError(_('A transaction can\'t be linked to sales orders having different partners.'))
# Try to retrieve the acquirer. However, fallback to the token's acquirer.
acquirer_id = vals.get('acquirer_id')
acquirer = False
payment_token_id = vals.get('payment_token_id')
if payment_token_id:
payment_token = self.env['payment.token'].sudo().browse(payment_token_id)
# Check payment_token/acquirer matching or take the acquirer from token
if acquirer_id:
acquirer = self.env['payment.acquirer'].browse(acquirer_id)
if payment_token and payment_token.acquirer_id != acquirer:
raise ValidationError(_('Invalid token found! Token acquirer %s != %s') % (
payment_token.acquirer_id.name, acquirer.name))
if payment_token and payment_token.partner_id != partner:
raise ValidationError(_('Invalid token found! Token partner %s != %s') % (
payment_token.partner.name, partner.name))
else:
acquirer = payment_token.acquirer_id
# Check an acquirer is there.
if not acquirer_id and not acquirer:
raise ValidationError(_('A payment acquirer is required to create a transaction.'))
if not acquirer:
acquirer = self.env['payment.acquirer'].browse(acquirer_id)
# Check a journal is set on acquirer.
if not acquirer.journal_id:
raise ValidationError(_('A journal must be specified of the acquirer %s.' % acquirer.name))
if not acquirer_id and acquirer:
vals['acquirer_id'] = acquirer.id
# Override for Deposit
amount = sum(self.mapped('amount_total'))
# This is a patch, all databases will run this code even if this field doesn't exist.
if hasattr(sale, 'amount_due_today'):
amount = sum(self.mapped('amount_due_today'))
vals.update({
'amount': amount,
'currency_id': currency.id,
'partner_id': partner.id,
'sale_order_ids': [(6, 0, self.ids)],
})
transaction = self.env['payment.transaction'].create(vals)
# Process directly if payment_token
if transaction.payment_token_id:
transaction.s2s_do_transaction()
return transaction
# Patch core implementation.
SaleOrder._create_payment_transaction = _create_payment_transaction

View File

@@ -4,12 +4,10 @@
<record id="view_payment_term_form" model="ir.ui.view"> <record id="view_payment_term_form" model="ir.ui.view">
<field name="name">view.payment.term.form.inherit.website</field> <field name="name">view.payment.term.form.inherit.website</field>
<field name="model">account.payment.term</field> <field name="model">account.payment.term</field>
<field name="inherit_id" ref="account.view_payment_term_form"/> <field name="inherit_id" ref="sale_payment_deposit.view_payment_term_form_inherit"/>
<field name="arch" type="xml"> <field name="arch" type="xml">
<xpath expr="//field[@name='note']" position="before"> <xpath expr="//field[@name='deposit_flat']" position="after">
<group> <field name="allow_in_website_sale"/>
<field name="allow_in_website_sale"/>
</group>
</xpath> </xpath>
</field> </field>
</record> </record>

View File

@@ -13,7 +13,7 @@
t-att-data-deposit-flat="partner_term.deposit_flat or '0'" t-att-data-deposit-flat="partner_term.deposit_flat or '0'"
name="payment_term_id" name="payment_term_id"
t-att-id="'payment_term_%i' % partner_term.id" t-att-id="'payment_term_%i' % partner_term.id"
t-att-checked="term == selected_term" t-att-checked="partner_term == selected_term"
type="radio"/> type="radio"/>
<label t-att-for="'payment_term_%i' % partner_term.id" <label t-att-for="'payment_term_%i' % partner_term.id"
t-field="partner_term.name" t-field="partner_term.name"
@@ -57,7 +57,7 @@
</xpath> </xpath>
<xpath expr="//div[@id='payment_method']" position="after"> <xpath expr="//div[@id='payment_method']" position="after">
<!-- Bypass Validation for users with 0 deposit payment terms --> <!-- Bypass Validation for users with 0 deposit payment terms -->
<div class="mt-3" t-att-style="'display: none;' if website_sale_order.amount_due_today else 'display: block;'" id="non_payment_method"> <div class="mt-3" t-if="website_sale_order.amount_total" t-att-style="'display: none;' if website_sale_order.amount_due_today else 'display: block;'" id="non_payment_method">
<div class="float-left mt-2"> <div class="float-left mt-2">
<a role="button" href="/shop/cart" class="btn btn-secondary"> <a role="button" href="/shop/cart" class="btn btn-secondary">
<i class="fa fa-chevron-left"/> <i class="fa fa-chevron-left"/>