Skip to content
Permalink
Browse files

[IMP] sale: Generate custom payment link

Purpose of the task is,
there is no easy way to request custom online payments on sales orders or invoices,
The only way is to create an URL manually: /website_payment/pay?reference=inv-008&amount=123&currency_id=1.

So Added Menu 'Generate payment link' in action in sales order.

Task ID : 1938635
Closes : #31575
  • Loading branch information...
sza-odoo committed Mar 5, 2019
1 parent 3cbb8d5 commit 8c2143321bf87f513625bda702f995d4347ca649
@@ -62,6 +62,7 @@
'data/account_incoterms_data.xml',
'views/digest_views.xml',
'wizard/account_invoice_send_views.xml',
'wizard/account_invoice_payment_link_view.xml',
],
'demo': [
'demo/account_demo.xml',
@@ -14,3 +14,4 @@
from . import setup_wizards
from . import wizard_tax_adjustments
from . import account_invoice_send
from . import account_invoice_payment_link
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class InvoicePaymentLink(models.TransientModel):
_name = "account.invoice.payment.link"
_description = "Generate Invoice Payment Link"

@api.model
def default_get(self, fields):
res = super(InvoicePaymentLink, self).default_get(fields)
active_id = self._context.get('active_id')
active_model = self._context.get('active_model')
record = self.env[active_model].browse(active_id)
res['order_id'] = active_id
res['amount'] = record.amount_total
return res

order_id = fields.Many2one("account.invoice", string="Invoice order", require=True)
amount = fields.Float(required=True)
link = fields.Char(string="Payment link")
description = fields.Char("Description")

@api.multi
def generate_payment_link(self):
self.ensure_one()
self.order_id._portal_ensure_token()
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
if self.order_id.amount_total >= self.amount > 0:
self.link = '%s/website_payment/pay?reference=%s&model=%s&amount=%s&access_token=%s' % (base_url, self.order_id.id, self._context.get('active_model'), self.amount, self.order_id.access_token)
else:
raise UserError(_("Please set an amount smaller than %s. Please set a positive amount") % (self.order_id.amount_total))
return {
'name': _('Payment Link'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'account.invoice.payment.link',
'res_id': self.id,
'target': 'new',
}
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="invoice_generate_payment_link_view_form" model="ir.ui.view">
<field name="name">account.invoice.payment.link.form</field>
<field name="model">account.invoice.payment.link</field>
<field name="arch" type="xml">
<form string="Generate Payment Link">
<group name="details" attrs="{'invisible': [('link', '!=', False)]}">
<field name="order_id" widget="selection"/>
<field name="amount"/>
<field name="description"/>
</group>
<group>
<field name="link" readonly="1" widget="CopyClipboardChar" attrs="{'invisible': [('link', '=', False)]}" options="{'string': 'Copy Link'}"/>
</group>
<footer>
<button string="Generate Link" name="generate_payment_link" type="object" attrs="{'invisible': [('link','!=', False)]}" class="btn-primary"/>
<button string="Cancel" special="cancel" attrs="{'invisible': [('link','!=', False)]}"/>
<button string="Ok" class="btn-primary" special="cancel" attrs="{'invisible': [('link','==', False)]}"/>
</footer>
</form>
</field>
</record>

<act_window name="Generate a Payment Link"
res_model="account.invoice.payment.link"
src_model="account.invoice"
view_mode="form"
target="new"
view_type="form"
view_id="invoice_generate_payment_link_view_form"
id="action_invoice_order_generate_link"
/>
</odoo>
@@ -34,6 +34,7 @@
'views/payment_views.xml',
'views/product_attribute_views.xml',
'wizard/sale_product_configurator_views.xml',
'wizard/sale_generate_payment_link_views.xml',
],
'demo': [
'data/sale_demo.xml',
@@ -3,4 +3,5 @@

from . import payment_acquirer_onboarding_wizard
from . import sale_make_invoice_advance
from . import sale_product_configurator
from . import sale_product_configurator
from . import sale_generate_payment_link
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class SalePaymentLink(models.TransientModel):
_name = "sale.payment.link"
_description = "Generate Sales Payment Link"

@api.model
def default_get(self, fields):
res = super(SalePaymentLink, self).default_get(fields)
active_id = self._context.get('active_id')
active_model = self._context.get('active_model')
record = self.env[active_model].browse(active_id)
amount_total = sum(record.invoice_ids.mapped('amount_total'))
res['order_id'] = active_id
res['amount'] = record.amount_total - amount_total if amount_total else record.amount_total
return res

order_id = fields.Many2one("sale.order", string="Sales order", require=True)
amount = fields.Float(required=True)
link = fields.Char(string="Payment link")
description = fields.Char("Description")

@api.multi
def generate_payment_link(self):
self.ensure_one()
self.order_id._portal_ensure_token()
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
if self.order_id.amount_total >= self.amount > 0:
self.link = '%s/website_payment/pay?reference=%s&model=%s&amount=%s&access_token=%s' % (base_url, self.order_id.id, self._context.get('active_model'), self.amount, self.order_id.access_token)
else:
raise UserError(_("Please set an amount smaller than %s. Please set a positive amount") % (self.order_id.amount_total))
return {
'name': _('Payment Link'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'sale.payment.link',
'res_id': self.id,
'target': 'new',
}
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="sale_generate_payment_link_view_form" model="ir.ui.view">
<field name="name">sale.payment.link.form</field>
<field name="model">sale.payment.link</field>
<field name="arch" type="xml">
<form string="Generate Payment Link">
<group name="details" attrs="{'invisible': [('link', '!=', False)]}">
<field name="order_id" widget="selection"/>
<field name="amount"/>
<field name="description"/>
</group>
<group>
<field name="link" readonly="1" widget="CopyClipboardChar" attrs="{'invisible': [('link', '=', False)]}" options="{'string': 'Copy Link'}"/>
</group>
<footer>
<button string="Generate Link" name="generate_payment_link" type="object" attrs="{'invisible': [('link','!=', False)]}" class="btn-primary"/>
<button string="Cancel" special="cancel" attrs="{'invisible': [('link','!=', False)]}"/>
<button string="Ok" class="btn-primary" special="cancel" attrs="{'invisible': [('link','==', False)]}"/>
</footer>
</form>
</field>
</record>

<act_window name="Generate a Payment Link"
res_model="sale.payment.link"
src_model="sale.order"
view_mode="form"
target="new"
view_type="form"
view_id="sale_generate_payment_link_view_form"
id="action_sale_order_generate_link"
/>
</odoo>

0 comments on commit 8c21433

Please sign in to comment.
You can’t perform that action at this time.