diff --git a/website_sale_payment_terms/controllers/main.py b/website_sale_payment_terms/controllers/main.py
index f699ab8b..94625114 100644
--- a/website_sale_payment_terms/controllers/main.py
+++ b/website_sale_payment_terms/controllers/main.py
@@ -51,9 +51,7 @@ class WebsiteSalePaymentTerms(WebsiteSaleDelivery):
def reject_term_agreement(self, **kw):
order = request.website.sale_get_order()
if order:
- partner = request.env.user.partner_id
- order.write({'payment_term_id': request.website.sale_get_payment_term(partner),
- 'require_payment': True})
+ order.payment_term_id = False
return request.redirect('/shop/cart')
# Confirm order without taking payment
diff --git a/website_sale_payment_terms/models/__init__.py b/website_sale_payment_terms/models/__init__.py
index dc24d95a..b17172fe 100644
--- a/website_sale_payment_terms/models/__init__.py
+++ b/website_sale_payment_terms/models/__init__.py
@@ -2,4 +2,5 @@ from . import account
from . import payment
from . import res_config
from . import sale
+from . import sale_patch
from . import website
diff --git a/website_sale_payment_terms/models/payment.py b/website_sale_payment_terms/models/payment.py
index 748c891e..5bebe138 100644
--- a/website_sale_payment_terms/models/payment.py
+++ b/website_sale_payment_terms/models/payment.py
@@ -1,4 +1,6 @@
from odoo import models, _
+import logging
+_logger = logging.getLogger(__name__)
class PaymentTransaction(models.Model):
@@ -15,7 +17,30 @@ class PaymentTransaction(models.Model):
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(
self.reference,
- order.amount_total_deposit or order.amount_total,
+ order.amount_due_today,
order.pricelist_id.currency_id.id,
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,
+ )
+ )
diff --git a/website_sale_payment_terms/models/sale_patch.py b/website_sale_payment_terms/models/sale_patch.py
new file mode 100644
index 00000000..07d85994
--- /dev/null
+++ b/website_sale_payment_terms/models/sale_patch.py
@@ -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
diff --git a/website_sale_payment_terms/views/account_views.xml b/website_sale_payment_terms/views/account_views.xml
index f2828904..b480a686 100644
--- a/website_sale_payment_terms/views/account_views.xml
+++ b/website_sale_payment_terms/views/account_views.xml
@@ -4,12 +4,10 @@
view.payment.term.form.inherit.websiteaccount.payment.term
-
+
-
-
-
-
+
+
diff --git a/website_sale_payment_terms/views/website_templates.xml b/website_sale_payment_terms/views/website_templates.xml
index 93de8c70..b5b782b6 100644
--- a/website_sale_payment_terms/views/website_templates.xml
+++ b/website_sale_payment_terms/views/website_templates.xml
@@ -13,7 +13,7 @@
t-att-data-deposit-flat="partner_term.deposit_flat or '0'"
name="payment_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"/>