From ed51e843a20e2fea2b6f4f35527f5629071ff3b1 Mon Sep 17 00:00:00 2001 From: --global Date: Fri, 23 May 2014 09:53:12 +0200 Subject: [PATCH 1/2] [IMP] website_sale: change 'pay now' button behavior. Now: intercept the call, make an asynchronous call to create the tx, then continue to the acquirer. Previous this was done by a redirection, leading to a get request beign done. However it should be a post request. --- addons/website_sale/controllers/main.py | 32 ++++++++----------- .../static/src/js/website_sale_payment.js | 12 +++++++ 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index ff4761a848c..bbd8f83fd7d 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -490,7 +490,6 @@ class website_sale(http.Controller): values['acquirers'] = payment_obj.browse(cr, uid, acquirer_ids, context=context) render_ctx = dict(context, submit_class='btn btn-primary', submit_txt='Pay Now') for acquirer in values['acquirers']: - render_ctx['tx_url'] = '/shop/payment/transaction/%s' % acquirer.id acquirer.button = payment_obj.render( cr, SUPERUSER_ID, acquirer.id, order.name, @@ -504,20 +503,17 @@ class website_sale(http.Controller): return request.website.render("website_sale.payment", values) - @http.route(['/shop/payment/transaction/'], type='http', methods=['POST'], auth="public", website=True) - def payment_transaction(self, acquirer_id, **post): - """ Hook method that creates a payment.transaction and redirect to the - acquirer, using post values to re-create the post action. + @http.route(['/shop/payment/transaction/'], type='json', auth="public", website=True) + def payment_transaction(self, acquirer_id): + """ 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 + to the acquirer website. :param int acquirer_id: id of a payment.acquirer record. If not set the user is redirected to the checkout page - :param dict post: should coutain all post data for the acquirer """ - # @TDEFIXME: don't know why we received those data, but should not be send to the acquirer - post.pop('submit.x', None) - post.pop('submit.y', None) cr, uid, context = request.cr, request.uid, request.context - payment_obj = request.registry.get('payment.acquirer') transaction_obj = request.registry.get('payment.transaction') sale_order_obj = request.registry['sale.order'] order = request.website.sale_get_order(context=context) @@ -529,7 +525,13 @@ class website_sale(http.Controller): # find an already existing transaction tx = request.website.sale_get_transaction() - if not tx: + if tx: + if tx.state == 'draft': # button cliked but no more info -> rewrite on tx or create a new one ? + tx.write({ + 'acquirer_id': acquirer_id, + }) + tx_id = tx.id + else: tx_id = transaction_obj.create(cr, SUPERUSER_ID, { 'acquirer_id': acquirer_id, 'type': 'form', @@ -541,10 +543,6 @@ class website_sale(http.Controller): 'sale_order_id': order.id, }, context=context) request.session['sale_transaction_id'] = tx_id - elif tx and tx.state == 'draft': # button cliked but no more info -> rewrite on tx or create a new one ? - tx.write({ - 'acquirer_id': acquirer_id, - }) # update quotation sale_order_obj.write( @@ -555,9 +553,7 @@ class website_sale(http.Controller): # confirm the quotation sale_order_obj.action_button_confirm(cr, SUPERUSER_ID, [order.id], context=request.context) - acquirer_form_post_url = payment_obj.get_form_action_url(cr, uid, acquirer_id, context=context) - acquirer_total_url = '%s?%s' % (acquirer_form_post_url, werkzeug.url_encode(post)) - return request.redirect(acquirer_total_url) + return tx_id @http.route('/shop/payment/get_status/', type='json', auth="public", website=True) def payment_get_status(self, sale_order_id, **post): diff --git a/addons/website_sale/static/src/js/website_sale_payment.js b/addons/website_sale/static/src/js/website_sale_payment.js index d20f00d0305..d991c86a6b9 100644 --- a/addons/website_sale/static/src/js/website_sale_payment.js +++ b/addons/website_sale/static/src/js/website_sale_payment.js @@ -9,4 +9,16 @@ $(document).ready(function () { }) .find("input[name='acquirer']:checked").click(); + // When clicking on payment button: create the tx using json then continue to the acquirer + $payment.on("click", "button[name='submit']", function (ev) { + var acquirer_id = $(ev.currentTarget).parents('div.oe_sale_acquirer_button').first().data('id'); + if (! acquirer_id) { + return false; + } + var def = openerp.jsonRpc('/shop/payment/transaction/' + acquirer_id, 'call', {}); + $.when(def).then(function (data) { + return true; + }); + }); + }); From a6379e2e67ffa1436d818fd55f37faed4893a87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 27 May 2014 11:34:55 +0200 Subject: [PATCH 2/2] [IMP] payment addons: fixed submit buttons: now have type=submit (and not image or nothing) and removed name, because this may cause issues when trying to submit a form manually with type=submit and a name. Also improved js code in website_sale to correctly handle the click binding, the asynchronous call to the server to create the transaction, and then submit the form. As form submission is synchronous in javascript, we cannot use the deferred. Instead we prevent the click event from bubbling, wait for the server to create the transaction, adn then manually submit the form. --- addons/payment_adyen/views/adyen.xml | 2 +- addons/payment_ogone/views/ogone.xml | 2 +- addons/payment_paypal/views/paypal.xml | 2 +- addons/payment_transfer/views/transfer.xml | 2 +- .../static/src/js/website_sale_payment.js | 20 ++++++++++--------- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/addons/payment_adyen/views/adyen.xml b/addons/payment_adyen/views/adyen.xml index 75e166f92fe..79281f43a99 100644 --- a/addons/payment_adyen/views/adyen.xml +++ b/addons/payment_adyen/views/adyen.xml @@ -20,7 +20,7 @@ -