summaryrefslogtreecommitdiff
path: root/addons/account_payment/controllers/payment.py
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2017-09-01 16:42:07 +0200
committerThibault Delavallée <tde@odoo.com>2017-09-06 11:41:25 +0200
commit01f69f65fe59941a75a093dd180cab836af0dbb2 (patch)
treec402df3a3618f82375e9141f91a74ce5343396ad /addons/account_payment/controllers/payment.py
parent699aa47b00a1a64315dbb2107b33977b9a90d67f (diff)
[IMP] account_payment: add invoice payment support in customer portal
This commit adds support of payment in the account customer portal. This is done in the account_payment module. This way customers can now pay invoices directly on the customer portal and have access to the status of their invoices. It replaces the old website payment mechanism that was only allowing to pay without generating payments and reconciliating them. Two routes are defined in this commit, one for form-based payments and one for server2server-based payment. Please refer to commit 1fdb10f0ac20a15a9fb5c7e8e65a5dd80955bccd for more details about the new payment form. Tools methods are added to handle transaction / invoice matching, the check of invoice and the automatic payment and reconciliation.
Diffstat (limited to 'addons/account_payment/controllers/payment.py')
-rw-r--r--addons/account_payment/controllers/payment.py103
1 files changed, 103 insertions, 0 deletions
diff --git a/addons/account_payment/controllers/payment.py b/addons/account_payment/controllers/payment.py
new file mode 100644
index 00000000000..ecd331d2d9c
--- /dev/null
+++ b/addons/account_payment/controllers/payment.py
@@ -0,0 +1,103 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from werkzeug import url_encode
+
+from odoo import http, _
+from odoo.http import request, route
+
+
+class PaymentPortal(http.Controller):
+
+ @route('/invoice/pay/<int:invoice_id>/form_tx', type='json', auth="public", website=True)
+ def invoice_pay_form(self, acquirer_id, invoice_id, save_token=False, access_token=None, **kwargs):
+ """ Json method that creates a payment.transaction, used to create a
+ transaction when the user clicks on 'pay now' button on the payment
+ form.
+
+ :return html: form containing all values related to the acquirer to
+ redirect customers to the acquirer website """
+ success_url = kwargs.get('success_url', '/my')
+ callback_method = kwargs.get('callback_method', '')
+
+ invoice_sudo = request.env['account.invoice'].sudo().browse(invoice_id)
+ if not invoice_sudo:
+ return False
+
+ try:
+ acquirer = request.env['payment.acquirer'].browse(int(acquirer_id))
+ except:
+ return False
+
+ token = request.env['payment.token'].sudo() # currently no support of payment tokens
+ tx = request.env['payment.transaction'].sudo()._check_or_create_invoice_tx(
+ invoice_sudo,
+ acquirer,
+ payment_token=token,
+ tx_type='form_save' if save_token else 'form',
+ add_tx_values={
+ 'callback_model_id': request.env['ir.model'].sudo().search([('model', '=', invoice_sudo._name)], limit=1).id,
+ 'callback_res_id': invoice_sudo.id,
+ 'callback_method': callback_method,
+ })
+
+ # set the transaction id into the session
+ request.session['portal_invoice_%s_transaction_id' % invoice_sudo.id] = tx.id
+
+ return tx.render_invoice_button(
+ invoice_sudo,
+ success_url,
+ submit_txt=_('Pay & Confirm'),
+ render_values={
+ 'type': 'form_save' if save_token else 'form',
+ 'alias_usage': _('If we store your payment information on our server, subscription payments will be made automatically.'),
+ }
+ )
+
+ @http.route('/invoice/pay/<int:invoice_id>/s2s_token_tx', type='http', auth='public', website=True)
+ def invoice_pay_token(self, invoice_id, pm_id=None, **kwargs):
+ """ Use a token to perform a s2s transaction """
+ error_url = kwargs.get('error_url', '/my')
+ success_url = kwargs.get('success_url', '/my')
+ callback_method = kwargs.get('callback_method', '')
+ access_token = kwargs.get('access_token')
+ params = {}
+ if access_token:
+ params['access_token'] = access_token
+
+ invoice_sudo = request.env['account.invoice'].sudo().browse(invoice_id)
+ if not invoice_sudo:
+ params['error'] = 'pay_invoice_invalid_doc'
+ return request.redirect('%s?%s' % (error_url, url_encode(params)))
+
+ try:
+ token = request.env['payment.token'].sudo().browse(int(pm_id))
+ except (ValueError, TypeError):
+ token = False
+ if not token:
+ params['error'] = 'pay_invoice_invalid_token'
+ return request.redirect('%s?%s' % (error_url, url_encode(params)))
+
+ # find an existing tx or create a new one
+ tx = request.env['payment.transaction'].sudo()._check_or_create_invoice_tx(
+ invoice_sudo,
+ token.acquirer_id,
+ payment_token=token,
+ tx_type='server2server',
+ add_tx_values={
+ 'callback_model_id': request.env['ir.model'].sudo().search([('model', '=', invoice_sudo._name)], limit=1).id,
+ 'callback_res_id': invoice_sudo.id,
+ 'callback_method': callback_method,
+ })
+
+ # set the transaction id into the session
+ request.session['portal_invoice_%s_transaction_id' % invoice_sudo.id] = tx.id
+
+ # proceed to the payment
+ res = tx.confirm_invoice_token()
+ if res is not True:
+ params['error'] = res
+ return request.redirect('%s?%s' % (error_url, url_encode(params)))
+
+ params['success'] = 'pay_invoice'
+ return request.redirect('%s?%s' % (success_url, url_encode(params)))