diff options
| author | Nikunj Ladava <nla@odoo.com> | 2019-07-03 11:18:57 +0000 |
|---|---|---|
| committer | Damien Bouvy <dbo@odoo.com> | 2019-08-07 11:27:44 +0000 |
| commit | a4f8616a178662434964de5fbee4c35637b9e1bc (patch) | |
| tree | e5e873fa1e1b17c644672598f19c69e4cc4a681d /addons/payment_authorize/static/src/js/payment_form.js | |
| parent | 26f3d8465d3c6f1da80c18605d5fa8a953feb779 (diff) | |
[IMP] payment_authorize: integration with accept js
add accept js of authorize.net to make s2s flow pci compliance
after clicking on pay now button, one popup display with card inputs
popup is provided by a authorize with all validation facilities
After submitting details, payment flow is
- get the temp token information from authorize
- create a token with that temp token information in odoo
- make a request to authorize for charge
- after successful request, payment will be charged for that card
task- 2025821
Diffstat (limited to 'addons/payment_authorize/static/src/js/payment_form.js')
| -rw-r--r-- | addons/payment_authorize/static/src/js/payment_form.js | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/addons/payment_authorize/static/src/js/payment_form.js b/addons/payment_authorize/static/src/js/payment_form.js new file mode 100644 index 00000000000..3947111ac1e --- /dev/null +++ b/addons/payment_authorize/static/src/js/payment_form.js @@ -0,0 +1,153 @@ +odoo.define('payment_authorize.payment_form', function (require) { +"use strict"; + +var ajax = require('web.ajax'); +var core = require('web.core'); +var PaymentForm = require('payment.payment_form'); + +var _t = core._t; + +PaymentForm.include({ + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * called when clicking on pay now or add payment event to create token for credit card/debit card. + * + * @private + * @param {Event} ev + * @param {DOMElement} checkedRadio + * @param {Boolean} addPmEvent + */ + _createAuthorizeToken: function (ev, $checkedRadio, addPmEvent) { + var self = this; + if (ev.type === 'submit') { + var button = $(ev.target).find('*[type="submit"]')[0] + } else { + var button = ev.target; + } + this.disableButton(button); + var acquirerID = this.getAcquirerIdFromRadio($checkedRadio); + var acquirerForm = this.$('#o_payment_add_token_acq_' + acquirerID); + var inputsForm = $('input', acquirerForm); + var formData = self.getFormData(inputsForm); + if (this.options.partnerId === undefined) { + console.warn('payment_form: unset partner_id when adding new token; things could go wrong'); + } + var AcceptJs = false; + if (formData.acquirer_environment === 'prod') { + AcceptJs = 'https://js.authorize.net/v3/AcceptUI.js'; + } else { + AcceptJs = 'https://jstest.authorize.net/v3/AcceptUI.js'; + } + + window.responseHandler = function (response) { + _.extend(formData, response); + + if (response.messages.resultCode === "Error") { + var errorMessage = ""; + _.each(response.messages.message, function (message) { + errorMessage += message.code + ": " + message.text; + }) + acquirerForm.removeClass('d-none'); + self.enableButton(button); + return self.displayError(_t('Server Error'), errorMessage); + } + + self._rpc({ + route: formData.data_set, + params: formData + }).then (function (data) { + if (addPmEvent) { + if (formData.return_url) { + window.location = formData.return_url; + } else { + window.location.reload(); + } + } else { + $checkedRadio.val(data.id); + self.el.submit(); + } + }).guardedCatch(function (error) { + // if the rpc fails, pretty obvious + error.event.preventDefault(); + acquirerForm.removeClass('d-none'); + self.enableButton(button); + self.displayError( + _t('Server Error'), + _t("We are not able to add your payment method at the moment.") + + error.message.data.message + ); + }); + }; + + if (this.$button === undefined) { + var params = { + 'class': 'AcceptUI d-none', + 'data-apiLoginID': formData.login_id, + 'data-clientKey': formData.client_key, + 'data-billingAddressOptions': '{"show": false, "required": false}', + 'data-responseHandler': 'responseHandler' + }; + this.$button = $('<button>', params); + this.$button.appendTo('body'); + } + ajax.loadJS(AcceptJs).then(function () { + self.$button.trigger('click'); + }); + }, + /** + * @override + */ + updateNewPaymentDisplayStatus: function () { + var $checkedRadio = this.$('input[type="radio"]:checked'); + + if ($checkedRadio.length !== 1) { + return; + } + + // hide add token form for authorize + if ($checkedRadio.data('provider') === 'authorize' && this.isNewPaymentRadio($checkedRadio)) { + this.$('[id*="o_payment_add_token_acq_"]').addClass('d-none'); + } else { + this._super.apply(this, arguments); + } + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @override + */ + payEvent: function (ev) { + ev.preventDefault(); + var $checkedRadio = this.$('input[type="radio"]:checked'); + + // first we check that the user has selected a authorize as s2s payment method + if ($checkedRadio.length === 1 && this.isNewPaymentRadio($checkedRadio) && $checkedRadio.data('provider') === 'authorize') { + this._createAuthorizeToken(ev, $checkedRadio); + } else { + this._super.apply(this, arguments); + } + }, + /** + * @override + */ + addPmEvent: function (ev) { + ev.stopPropagation(); + ev.preventDefault(); + var $checkedRadio = this.$('input[type="radio"]:checked'); + + // first we check that the user has selected a authorize as add payment method + if ($checkedRadio.length === 1 && this.isNewPaymentRadio($checkedRadio) && $checkedRadio.data('provider') === 'authorize') { + this._createAuthorizeToken(ev, $checkedRadio, true); + } else { + this._super.apply(this, arguments); + } + }, +}); +}); |
