[REF] website_quote: use new sale_payment module

Online quote now uses JS code from payment module in order to make online
payments. Front-end template is updated to use o_payment_acquirer_button
generic class for payments and data is added to be managed by payment.js.
This allows to remove some strange code parsing the url to find the order
ID and token instead of using rendered values.

A payment controller is removed because it was only a shortcut to another
one. In transaction controller token parameter is renamed to access_token
to avoid confusion with payment token and match the payment and website
sale naming.

Online Quote controller now uses tools method from sale_payment module
like website_sale. This way it is now standardized and moved out of
controllers that should not perform so much model-level computation.
This commit is contained in:
Thibault Delavallée
2017-06-06 11:05:25 +02:00
parent 8564f1e8a1
commit ef2d828463
3 changed files with 25 additions and 94 deletions
+18 -49
View File
@@ -158,11 +158,7 @@ class sale_quote(http.Controller):
# note dbo: website_sale code
@http.route(['/quote/<int:order_id>/transaction/<int:acquirer_id>'], type='json', auth="public", website=True)
def payment_transaction(self, acquirer_id, order_id):
return self.payment_transaction_token(acquirer_id, order_id, None)
@http.route(['/quote/<int:order_id>/transaction/<int:acquirer_id>/<token>'], type='json', auth="public", website=True)
def payment_transaction_token(self, acquirer_id, order_id, token):
def payment_transaction_token(self, acquirer_id, order_id, access_token=None, tx_type=None):
""" Json method that creates a payment.transaction, used to create a
transaction when the user clicks on 'pay now' button. After having
created the transaction, the event continues and the user is redirected
@@ -171,51 +167,24 @@ class sale_quote(http.Controller):
:param int acquirer_id: id of a payment.acquirer record. If not set the
user is redirected to the checkout page
"""
PaymentTransaction = request.env['payment.transaction'].sudo()
Order = request.env['sale.order'].sudo().browse(order_id)
if not Order or not Order.order_line or acquirer_id is None:
order = request.env['sale.order'].sudo().browse(order_id)
if not order or not order.order_line or acquirer_id is None:
return request.redirect("/quote/%s" % order_id)
# find an already existing transaction
Transaction = PaymentTransaction.search([('reference', '=', Order.name)])
if Transaction:
if Transaction.sale_order_id != Order or Transaction.state in ['error', 'cancel'] or Transaction.acquirer_id.id != acquirer_id:
Transaction = False
elif Transaction.state == 'draft':
Transaction.write({
'amount': Order.amount_total,
})
if not Transaction:
Transaction = PaymentTransaction.create({
'acquirer_id': acquirer_id,
'type': Order._get_payment_type(),
'amount': Order.amount_total,
'currency_id': Order.pricelist_id.currency_id.id,
'partner_id': Order.partner_id.id,
'reference': PaymentTransaction.get_next_reference(Order.name),
'sale_order_id': Order.id,
'callback_model_id': request.env['ir.model'].sudo().search([('model', '=', Order._name)], limit=1).id,
'callback_res_id': Order.id,
'callback_method': '_confirm_online_quote',
})
request.session['quote_%s_transaction_id' % Order.id] = Transaction.id
acquirer = request.env['payment.acquirer'].browse(int(acquirer_id))
token = request.env['payment.token'].sudo() # currently no support of payment tokens
tx = request.env['payment.transaction'].sudo().search([('reference', '=', order.name)], limit=1)
tx_type = order._get_payment_type()
tx = tx.check_or_create_sale_tx(order, acquirer, payment_token=token, tx_type=tx_type, add_tx_values={
'callback_model_id': request.env['ir.model'].sudo().search([('model', '=', order._name)], limit=1).id,
'callback_res_id': order.id,
'callback_method': '_confirm_online_quote',
})
request.session['quote_%s_transaction_id' % order.id] = tx.id
# update quotation
Order.write({
'payment_tx_id': Transaction.id
})
return Transaction.acquirer_id.with_context(
submit_class='btn btn-primary',
submit_txt=_('Pay & Confirm')).render(
Transaction.reference,
Order.amount_total,
Order.pricelist_id.currency_id.id,
values={
'return_url': '/quote/%s/%s' % (order_id, token) if token else '/quote/%s' % order_id,
'type': Order._get_payment_type(),
'alias_usage': _('If we store your payment information on our server, subscription payments will be made automatically.'),
'partner_id': Order.partner_shipping_id.id or Order.partner_invoice_id.id,
'billing_partner_id': Order.partner_invoice_id.id,
})
return tx.render_sale_button(order, '/quote/%s/%s' % (order_id, token) if token else '/quote/%s' % order_id,
submit_txt=_('Pay & Confirm'), render_values={
'type': order._get_payment_type(),
'alias_usage': _('If we store your payment information on our server, subscription payments will be made automatically.'),
})
@@ -4,7 +4,6 @@ odoo.define('website_quote.website_quote', function (require) {
var ajax = require('web.ajax');
var config = require('web.config');
var Widget = require('web.Widget');
var website = require('website.website');
if(!$('.o_website_quote').length) {
return $.Deferred().reject("DOM doesn't contain '.o_website_quote'");
@@ -200,44 +199,3 @@ if(!$('.o_website_quote').length) {
},
});
});
odoo.define('website_quote.payment_method', function (require) {
'use strict';
require('website.website');
var ajax = require('web.ajax');
if(!$('#payment_method').length) {
return $.Deferred().reject("DOM doesn't contain '#payment_method'");
}
// dbo note: website_sale code for payment
// if we standardize payment somehow, this should disappear
// When choosing an acquirer, display its Pay Now button
var $payment = $("#payment_method");
$payment.on("click", "input[name='acquirer']", function (ev) {
var payment_id = $(ev.currentTarget).val();
$("div.oe_quote_acquirer_button[data-id]", $payment).addClass("hidden");
$("div.oe_quote_acquirer_button[data-id='"+payment_id+"']", $payment).removeClass("hidden");
})
.find("input[name='acquirer']:checked").click();
// When clicking on payment button: create the tx using json then continue to the acquirer
$('.oe_quote_acquirer_button').on("click", 'button[type="submit"],button[name="submit"]', function (ev) {
ev.preventDefault();
ev.stopPropagation();
var $form = $(ev.currentTarget).parents('form');
var acquirer_id = $(ev.currentTarget).parents('.oe_quote_acquirer_button').first().data('id');
if (! acquirer_id) {
return false;
}
var href = $(location).attr("href");
var order_id = href.match(/quote\/([0-9]+)/)[1];
var token = href.match(/quote\/[0-9]+\/([^\/?]*)/);
token = token ? token[1] : '';
ajax.jsonRpc('/quote/' + order_id +'/transaction/' + acquirer_id + (token ? '/' + token : ''), 'call', {}).then(function (data) {
$form.html(data);
$form.submit();
});
});
});
@@ -366,9 +366,13 @@
</div>
<div class="col-md-6">
<t t-foreach="acquirers or []" t-as="acquirer">
<div t-att-data-id="acquirer.id" class="oe_quote_acquirer_button hidden pull-right">
<div t-raw="buttons.get(acquirer.id)"/>
<div t-field="acquirer.pre_msg"/>
<div class="o_payment_acquirer_button hidden pull-right"
t-att-data-id="acquirer.id"
t-att-data-access-token="token or ''"
t-att-data-tx-type="form"
t-att-data-url="'/quote/%s/transaction/%s' % (quotation.id, acquirer.id)">
<div t-raw="buttons.get(acquirer.id)"/>
<div t-field="acquirer.pre_msg"/>
</div>
</t>
<button type="button" class="btn btn-link" data-dismiss="modal" style="padding: 0">Cancel</button>