[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;
This commit is contained in:
Thibault Delavallée
2017-09-06 10:40:46 +02:00
parent faea658382
commit 06fe263a15
2 changed files with 38 additions and 16 deletions
+21 -12
View File
@@ -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);
});
});
+17 -4
View File
@@ -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)
-->
<form t-if="s2s_acquirers or pms or form_acquirers" method="post" t-att-action="form_action if form_action else '#'" class="o_payment_form">
<form t-if="s2s_acquirers or pms or form_acquirers" method="post" class="o_payment_form"
t-att-action="form_action if form_action else '#'"
t-att-data-success-url="success_url or ''"
t-att-data-error-url="error_url or ''"
t-att-data-access-token="access_token or ''"
t-att-data-partner-id="partner_id"
t-att-data-callback-method="callback_method or ''">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
<input type="hidden" t-if="verify_validity==True" name="verify_validity" value="1"/>
<input type="hidden" t-if="prepare_tx_url" name="prepare_tx_url" t-att-value="prepare_tx_url"/>
<!-- s2s form submission -->
<input type="hidden" t-if="access_token" name="access_token" t-att-value="access_token"/>
<input type="hidden" t-if="success_url" name="success_url" t-att-value="success_url"/>
<input type="hidden" t-if="error_url" name="error_url" t-att-value="error_url"/>
<input type="hidden" t-if="callback_method" name="callback_method" t-att-value="callback_method"/>
<div class="panel panel-default">
<t t-set="acquirers_count" t-value="(len(s2s_acquirers) if s2s_acquirers else 0) + (len(form_acquirers) if form_acquirers else 0)"/>
<t t-set="pms_count" t-value="len(pms) if pms else 0"/>
@@ -69,7 +81,9 @@
<div t-attf-id="o_payment_add_token_acq_{{acq.id}}" t-attf-class="panel-footer {{'hidden' if (len(s2s_acquirers)&gt;1 and len(pms)==0 and s2s_acquirers[0]!=acq) else 'hidden' if len(pms)&gt;0 else ''}}">
<div class="clearfix">
<t t-call="{{acq.sudo().get_s2s_form_xml_id()}}">
<t t-set="id" t-value="acq.id"/>
<t t-set="id" t-value="acq.id"/>
<t t-set="partner_id" t-value="partner_id"/>
<t t-set="return_url" t-value="''"/>
</t>
</div>
</div>
@@ -103,7 +117,6 @@
</div>
<div t-attf-id="o_payment_form_acq_{{acq.id}}" t-attf-class="hidden {{'panel-footer' if acq.save_token == 'ask' else ''}}">
<label t-if="acq.save_token == 'ask'"><input type="checkbox" name="o_payment_form_save_token" data-remove-me=""/> Save my payment data</label>
<t t-raw="acq.form"/>
</div>
</t>
<t t-foreach="pms" t-as="pm">