From 06fe263a150f42ec36c91e6219fdc8f19fd50798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 1 Sep 2017 11:47:55 +0200 Subject: [PATCH] [IMP] payment: add new explicit parameters to the payment widget Purpose of this commit is to ease the use of the payment widget and avoid having to perform too much custom code in the various routes used when doing payments : * when instantiating the widget give him its parent element data so that parameters given when calling the payment form template are automatically present in the widget options; * support more parameters when calling the route for form-based acquirers including access_token, URLs and callback method; * when doing a form-based payment a call to a JSON route returning the rendered form is done. Payment widget now uses all data from that form including the URL of the acquirer website. This way form-based acquirers are really dynamic and values update is easier; * remove hidden display of form-based acquirers as all data come now from the called JSON route that returns the form; * add a warning about partner-id not being set as it is an issue some people may encounter; --- addons/payment/static/src/js/payment_form.js | 33 +++++++++++++------- addons/payment/views/payment_templates.xml | 21 ++++++++++--- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/addons/payment/static/src/js/payment_form.js b/addons/payment/static/src/js/payment_form.js index d2dd252f442..1cd09b04689 100644 --- a/addons/payment/static/src/js/payment_form.js +++ b/addons/payment/static/src/js/payment_form.js @@ -17,10 +17,15 @@ odoo.define('payment.payment_form', function (require) { 'click input[type="radio"]': 'radioClickEvent' }, + init: function(parent, options) { + this._super.apply(this, arguments); + this.options = _.extend(options || {}, { + }); + }, + start: function () { this.updateNewPaymentDisplayStatus(); $('[data-toggle="tooltip"]').tooltip(); - }, payEvent: function (ev) { @@ -47,6 +52,9 @@ odoo.define('payment.payment_form', function (require) { // if the user is adding a new payment if (this.isNewPaymentRadio(checked_radio)) { + if (this.options.partnerId === undefined) { + console.warn('payment_form: unset partner_id when adding new token; things could go wrong'); + } var form_data = this.getFormData(inputs_form); var empty_inputs = false; @@ -115,12 +123,6 @@ odoo.define('payment.payment_form', function (require) { } // if the user is going to pay with a form payment, then else if (this.isFormPaymentRadio(checked_radio)) { - // if neither the dataset or the action url is defined, then we just stop the execution of the function - if (!ds || !ds.dataset.actionUrl) { - console.warn('payment_form: invalid dataset / actionurl'); - return; - } - var $tx_url = this.$el.find('input[name="prepare_tx_url"]'); // if there's a prepare tx url set if ($tx_url.length === 1) { @@ -130,14 +132,19 @@ odoo.define('payment.payment_form', function (require) { ajax.jsonRpc($tx_url[0].value, 'call', { 'acquirer_id': parseInt(acquirer_id), 'save_token': form_save_token, + 'access_token': self.options.accessToken, + 'success_url': self.options.successUrl, + 'error_url': self.options.errorUrl, + 'callback_method': self.options.callbackMethod, }).then(function (result) { if (result) { // if the server sent us the html form, we create a form element var newForm = document.createElement('form'); newForm.setAttribute("method", "post"); // set it to post - newForm.setAttribute("action", ds.dataset.actionUrl); // set the action url newForm.hidden = true; // hide it newForm.innerHTML = result; // put the html sent by the server inside the form + var action_url = $(newForm).find('input[name="data_set"]').data('actionUrl'); + newForm.setAttribute("action", action_url); // set the action url document.getElementsByTagName('body')[0].appendChild(newForm); // append the form to the body $(newForm).find('input[data-remove-me]').remove(); // remove all the input that should be removed newForm.submit(); // and finally submit the form @@ -379,13 +386,15 @@ odoo.define('payment.payment_form', function (require) { }, }); - - require('web.dom_ready'); // only start this when dom is ready if (!$('.o_payment_form').length) { return $.Deferred().reject("DOM doesn't contain '.o_payment_form'"); } - var form = new PaymentForm(); - form.attachTo($('.o_payment_form')); + $('.o_payment_form').each(function () { + var $elem = $(this); + var form = new PaymentForm(null, $elem.data()); + form.attachTo($elem); + }); + }); diff --git a/addons/payment/views/payment_templates.xml b/addons/payment/views/payment_templates.xml index 81e73aa464b..446d248680f 100644 --- a/addons/payment/views/payment_templates.xml +++ b/addons/payment/views/payment_templates.xml @@ -12,7 +12,7 @@ - 'submit_txt' the text displayed inside the submit button - 'submit_class' the css classes to style the submit button - 'icon_class' font awesome class (e.g. 'fa-trash', 'fa-lock') - - 'form_action' the URI to the page that will handle the form values + - 'form_action' the URI to the page that will handle the form values given for server2server - 'pms' the tokens - 'checked_pm_id' the payment token that should be checked (for radio buttons) - 'mode' can take two values, either 'payment' or 'manage'. 'manage' displays the add a new card and delete buttons. 'payment' @@ -22,10 +22,22 @@ - 'verify_validity' if we need to verify if the payment method is valid when adding a new one - 'prepare_tx_url' the url of the route which will handle the creation of a transaction for a form base payment (handles if the transaction is form or form_save) --> -
+ + + + + + +
@@ -69,7 +81,9 @@
- + + +
@@ -103,7 +117,6 @@
-