Skip to content

Commit

Permalink
[ADD] website_sale_sepa_dd_payment
Browse files Browse the repository at this point in the history
  • Loading branch information
remytms committed Nov 26, 2024
1 parent 7e2ad59 commit 28a1782
Show file tree
Hide file tree
Showing 15 changed files with 761 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup/website_sale_sepa_dd_payment/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
70 changes: 70 additions & 0 deletions website_sale_sepa_dd_payment/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
==========================================================
E-commerce SEPA Direct Debit Payment for specific products
==========================================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:d5514507c207937042ea64484cf364dd8c11232d97806933a51e12dcf7c534f9
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-coopiteasy%2Faddons-lightgray.png?logo=github
:target: https://github.com/coopiteasy/addons/tree/16.0/website_sale_sepa_dd_payment
:alt: coopiteasy/addons

|badge1| |badge2| |badge3|

Form to order subscription product

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/coopiteasy/addons/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/coopiteasy/addons/issues/new?body=module:%20website_sale_sepa_dd_payment%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Coop IT Easy SC

Contributors
~~~~~~~~~~~~

* `Coop IT Easy SC <https://coopiteasy.be>`_:

* Rémy Taymans

Maintainers
~~~~~~~~~~~

.. |maintainer-remytms| image:: https://github.com/remytms.png?size=40px
:target: https://github.com/remytms
:alt: remytms

Current maintainer:

|maintainer-remytms|

This module is part of the `coopiteasy/addons <https://github.com/coopiteasy/addons/tree/16.0/website_sale_sepa_dd_payment>`_ project on GitHub.

You are welcome to contribute.
5 changes: 5 additions & 0 deletions website_sale_sepa_dd_payment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import models
from . import controllers
26 changes: 26 additions & 0 deletions website_sale_sepa_dd_payment/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

{
"name": "E-commerce SEPA Direct Debit Payment for specific products",
"summary": """
Pay via SEPA Direct Debit for some products.""",
"version": "16.0.1.0.0",
"category": "Website",
"website": "https://github.com/coopiteasy/addons",
"author": "Coop IT Easy SC",
"maintainers": ["remytms"],
"license": "AGPL-3",
"application": False,
"depends": [
"website_sale",
],
"excludes": [],
"data": [
"views/product_views.xml",
"views/templates.xml",
],
"demo": [],
"qweb": [],
}
4 changes: 4 additions & 0 deletions website_sale_sepa_dd_payment/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import main
36 changes: 36 additions & 0 deletions website_sale_sepa_dd_payment/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import http
from odoo.exceptions import ValidationError
from odoo.http import request

from odoo.addons.website_sale.controllers.main import WebsiteSale


class WebsiteSaleSEPADirectDebit(WebsiteSale):
@http.route(
"/shop/payment", type="http", auth="public", website=True, sitemap=False
)
def shop_payment(self, **post):
sepa_dd_errors = []
sepa_dd_iban = ""
# Set default values
if request.env.user.partner_id.bank_ids:
sepa_dd_iban = request.env.user.partner_id.bank_ids[0].acc_numbre
# Process form
if request.httprequest.method == "POST":
order = request.website.sale_get_order()
order.sepa_dd_iban = request.params.get("sepa-dd-iban", "")
try:
order.with_context(send_email=True).action_confirm()
except ValidationError as err:
sepa_dd_errors.append(str(err))
else:
return request.redirect(order.get_portal_url())

result = super().shop_payment(**post)
result.qcontext["sepa_dd_errors"] = sepa_dd_errors
result.qcontext["sepa_dd_iban"] = sepa_dd_iban
return result
5 changes: 5 additions & 0 deletions website_sale_sepa_dd_payment/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import sale_order
from . import product_template
11 changes: 11 additions & 0 deletions website_sale_sepa_dd_payment/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import fields, models


class SaleOrder(models.Model):
_inherit = "product.template"

allow_sepa_dd_payment = fields.Boolean(string="Allow SEPA Direct Debit Payment")
84 changes: 84 additions & 0 deletions website_sale_sepa_dd_payment/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import datetime

from odoo import api, fields, models
from odoo.exceptions import ValidationError

from odoo.addons.base.models.res_bank import sanitize_account_number
from odoo.addons.base_iban.models.res_partner_bank import validate_iban


class SaleOrder(models.Model):
_inherit = "sale.order"

allow_sepa_dd_payment = fields.Boolean(compute="_compute_allow_sepa_dd_payment")
sepa_dd_iban = fields.Char()

@api.depends("order_line", "payment_mode_id")
def _compute_allow_sepa_dd_payment(self):
"""Allow sepa direct debit payment if payment is already set or
if products accept this type of payment.
"""
payment_mode = self.env["account.payment.mode"].search(
[("payment_method_id.code", "=", "sepa_direct_debit")],
limit=1,
)
for order in self:
only_contracts = all(
order.order_line.mapped("product_id").mapped("allow_sepa_dd_payment")
)
order.allow_sepa_dd_payment = (
payment_mode == order.payment_mode_id or only_contracts
)

def action_confirm(self):
self.create_sepa_dd_mandate()
return super().action_confirm()

def validate_sepa_dd_iban(self):
"""Raise ValidationError if sepa_dd_iban is not correct"""
self.ensure_one()
validated_iban = validate_iban(self.sepa_dd_iban)
sanitized_sepa_dd_iban = sanitize_account_number(validated_iban)
res_partner_bank = self.env["res.partner.bank"].search(
[("sanitized_acc_number", "=", sanitized_sepa_dd_iban)],
limit=1,
)
if res_partner_bank and res_partner_bank.partner_id != self.partner_id:
raise ValidationError(
f"The account number {sanitized_sepa_dd_iban} does not belongs "
f"to {self.partner_id.name}."
)
return res_partner_bank

def sepa_dd_mandate_vals(self):
self.ensure_one()
return {
"format": "sepa",
"type": "recurrent",
"scheme": "CORE",
"recurrent_sequence_type": "recurring",
"signature_date": datetime.datetime.now(),
"state": "valid",
}

def create_sepa_dd_mandate(self):
for order in self:
res_partner_bank = order.validate_sepa_dd_iban()
if not res_partner_bank:
res_partner_bank = self.env["res.partner.bank"].create(
{
"partner_id": order.partner_id.id,
"acc_number": order.sepa_dd_iban,
}
)
res_partner_bank.write(
{
"mandate_ids": [
(0, 0, order.sepa_dd_mandate_vals()),
],
}
)
3 changes: 3 additions & 0 deletions website_sale_sepa_dd_payment/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Coop IT Easy SC <https://coopiteasy.be>`_:

* Rémy Taymans
1 change: 1 addition & 0 deletions website_sale_sepa_dd_payment/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Form to order subscription product
Loading

0 comments on commit 28a1782

Please sign in to comment.