direct-post.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. (function (factory) {
  6. if (typeof define === 'function' && define.amd) {
  7. define([
  8. 'jquery',
  9. 'mage/backend/validation',
  10. 'prototype'
  11. ], factory);
  12. } else {
  13. factory(jQuery);
  14. }
  15. }(function (jQuery) {
  16. window.directPost = Class.create();
  17. directPost.prototype = {
  18. initialize: function (methodCode, iframeId, controller, orderSaveUrl, cgiUrl, nativeAction) {
  19. var prepare = function (event, method) {
  20. if (method === 'authorizenet_directpost') {
  21. this.preparePayment();
  22. } else {
  23. jQuery('#edit_form')
  24. .off('submitOrder.authorizenet');
  25. }
  26. };
  27. this.iframeId = iframeId;
  28. this.controller = controller;
  29. this.orderSaveUrl = orderSaveUrl;
  30. this.nativeAction = nativeAction;
  31. this.cgiUrl = cgiUrl;
  32. this.code = methodCode;
  33. this.inputs = ['cc_type', 'cc_number', 'expiration', 'expiration_yr', 'cc_cid'];
  34. this.headers = [];
  35. this.isValid = true;
  36. this.paymentRequestSent = false;
  37. this.orderIncrementId = false;
  38. this.successUrl = false;
  39. this.hasError = false;
  40. this.tmpForm = false;
  41. this.onLoadIframe = this.loadIframe.bindAsEventListener(this);
  42. this.onLoadOrderIframe = this.loadOrderIframe.bindAsEventListener(this);
  43. this.onSubmitAdminOrder = this.submitAdminOrder.bindAsEventListener(this);
  44. jQuery('#edit_form').on('changePaymentMethod', prepare.bind(this));
  45. jQuery('#edit_form').trigger(
  46. 'changePaymentMethod',
  47. [
  48. jQuery('#edit_form').find(':radio[name="payment[method]"]:checked').val()
  49. ]
  50. );
  51. },
  52. validate: function () {
  53. this.isValid = true;
  54. this.inputs.each(function (elemIndex) {
  55. if ($(this.code + '_' + elemIndex)) {
  56. if (!jQuery.validator.validateElement($(this.code + '_' + elemIndex))) {
  57. this.isValid = false;
  58. }
  59. }
  60. }, this);
  61. return this.isValid;
  62. },
  63. changeInputOptions: function (param, value) {
  64. this.inputs.each(function (elemIndex) {
  65. if ($(this.code + '_' + elemIndex)) {
  66. $(this.code + '_' + elemIndex).writeAttribute(param, value);
  67. }
  68. }, this);
  69. },
  70. preparePayment: function () {
  71. this.changeInputOptions('autocomplete', 'off');
  72. jQuery('#edit_form')
  73. .off('submitOrder')
  74. .on('submitOrder.authorizenet', this.submitAdminOrder.bind(this));
  75. if ($(this.iframeId)) {
  76. // Temporary solution will be removed after refactoring Authorize.Net (sales) functionality
  77. jQuery('.scalable.save:not(disabled)').removeAttr('onclick');
  78. jQuery(document).off('click.directPost');
  79. jQuery(document).on(
  80. 'click.directPost',
  81. '.scalable.save:not(disabled)',
  82. jQuery.proxy(this.onSubmitAdminOrder, this)
  83. );
  84. $('order-' + this.iframeId).observe('load', this.onLoadOrderIframe);
  85. $(this.iframeId).observe('load', this.onLoadIframe);
  86. }
  87. },
  88. loadIframe: function () {
  89. if (this.paymentRequestSent) {
  90. if (!this.orderRequestSent) {
  91. this.paymentRequestSent = false;
  92. if (!this.hasError) {
  93. this.returnQuote();
  94. } else {
  95. this.changeInputOptions('disabled', false);
  96. jQuery('body').trigger('processStop');
  97. enableElements('save');
  98. }
  99. }
  100. if (this.tmpForm) {
  101. document.body.removeChild(this.tmpForm);
  102. }
  103. }
  104. },
  105. loadOrderIframe: function () {
  106. if (this.orderRequestSent) {
  107. $(this.iframeId).hide();
  108. var data = $('order-' + this.iframeId).contentWindow.document.body.getElementsByTagName('pre')[0].innerHTML;
  109. this.saveAdminOrderSuccess(data);
  110. this.orderRequestSent = false;
  111. }
  112. },
  113. showError: function (msg) {
  114. this.hasError = true;
  115. if (this.controller == 'onepage') {
  116. $(this.iframeId).hide();
  117. this.resetLoadWaiting();
  118. }
  119. alert(msg);
  120. },
  121. returnQuote: function () {
  122. var url = this.orderSaveUrl.replace('place', 'returnQuote');
  123. new Ajax.Request(url, {
  124. onSuccess: function (transport) {
  125. try {
  126. response = transport.responseText.evalJSON(true);
  127. } catch (e) {
  128. response = {};
  129. }
  130. if (response.error_message) {
  131. alert(response.error_message);
  132. }
  133. $(this.iframeId).show();
  134. this.changeInputOptions('disabled', false);
  135. jQuery('body').trigger('processStop');
  136. enableElements('save');
  137. }.bind(this)
  138. });
  139. },
  140. setLoadWaiting: function () {
  141. this.headers.each(function (header) {
  142. header.removeClassName('allow');
  143. });
  144. checkout.setLoadWaiting('review');
  145. },
  146. resetLoadWaiting: function () {
  147. this.headers.each(function (header) {
  148. header.addClassName('allow');
  149. });
  150. checkout.setLoadWaiting(false);
  151. },
  152. submitAdminOrder: function () {
  153. // Temporary solution will be removed after refactoring Authorize.Net (sales) functionality
  154. var editForm = jQuery('#edit_form');
  155. if (editForm.valid()) {
  156. // Temporary solution will be removed after refactoring Authorize.Net (sales) functionality
  157. paymentMethodEl = editForm.find(':radio[name="payment[method]"]:checked');
  158. this.hasError = false;
  159. if (paymentMethodEl.val() == this.code) {
  160. jQuery('body').trigger('processStart');
  161. setLoaderPosition();
  162. this.changeInputOptions('disabled', 'disabled');
  163. this.paymentRequestSent = true;
  164. this.orderRequestSent = true;
  165. // Temporary solutions will be removed after refactoring Authorize.Net (sales) functionality
  166. editForm.attr('action', this.orderSaveUrl);
  167. editForm.attr('target',
  168. jQuery('#order-' + this.iframeId).attr('name'));
  169. editForm.append(this.createHiddenElement('controller', this.controller));
  170. disableElements('save');
  171. // Temporary solutions will be removed after refactoring Authorize.Net (sales) functionality
  172. order._realSubmit();
  173. } else {
  174. editForm.attr('action', this.nativeAction);
  175. editForm.attr('target', '_top');
  176. disableElements('save');
  177. // Temporary solutions will be removed after refactoring Authorize.Net (sales) functionality
  178. order._realSubmit();
  179. }
  180. }
  181. },
  182. recollectQuote: function () {
  183. var area = ['sidebar', 'items', 'shipping_method', 'billing_method', 'totals', 'giftmessage'];
  184. area = order.prepareArea(area);
  185. var url = order.loadBaseUrl + 'block/' + area;
  186. var info = $('order-items_grid').select('input', 'select', 'textarea');
  187. var data = {};
  188. for (var i = 0; i < info.length; i++) {
  189. if (!info[i].disabled && (info[i].type != 'checkbox' || info[i].checked)) {
  190. data[info[i].name] = info[i].getValue();
  191. }
  192. }
  193. data.reset_shipping = true;
  194. data.update_items = true;
  195. if ($('coupons:code') && $F('coupons:code')) {
  196. data['order[coupon][code]'] = $F('coupons:code');
  197. }
  198. data.json = true;
  199. new Ajax.Request(url, {
  200. parameters: data,
  201. loaderArea: 'html-body',
  202. onSuccess: function (transport) {
  203. jQuery('#edit_form').submit();
  204. }
  205. });
  206. },
  207. saveAdminOrderSuccess: function (data) {
  208. try {
  209. response = data.evalJSON(true);
  210. } catch (e) {
  211. response = {};
  212. }
  213. if (response.directpost) {
  214. this.orderIncrementId = response.directpost.fields.x_invoice_num;
  215. var paymentData = {};
  216. for (var key in response.directpost.fields) {
  217. paymentData[key] = response.directpost.fields[key];
  218. }
  219. var preparedData = this.preparePaymentRequest(paymentData);
  220. this.sendPaymentRequest(preparedData);
  221. } else {
  222. if (response.redirect) {
  223. window.location = response.redirect;
  224. }
  225. if (response.error_messages) {
  226. var msg = response.error_messages;
  227. if (typeof msg == 'object') {
  228. msg = msg.join('\n');
  229. }
  230. if (msg) {
  231. alert(msg);
  232. }
  233. }
  234. }
  235. },
  236. preparePaymentRequest: function (data) {
  237. if ($(this.code + '_cc_cid')) {
  238. data.x_card_code = $(this.code + '_cc_cid').value;
  239. }
  240. var year = $(this.code + '_expiration_yr').value;
  241. if (year.length > 2) {
  242. year = year.substring(2);
  243. }
  244. var month = parseInt($(this.code + '_expiration').value, 10);
  245. if (month < 10) {
  246. month = '0' + month;
  247. }
  248. data.x_exp_date = month + '/' + year;
  249. data.x_card_num = $(this.code + '_cc_number').value;
  250. return data;
  251. },
  252. sendPaymentRequest: function (preparedData) {
  253. this.recreateIframe();
  254. this.tmpForm = document.createElement('form');
  255. this.tmpForm.style.display = 'none';
  256. this.tmpForm.enctype = 'application/x-www-form-urlencoded';
  257. this.tmpForm.method = 'POST';
  258. document.body.appendChild(this.tmpForm);
  259. this.tmpForm.action = this.cgiUrl;
  260. this.tmpForm.target = $(this.iframeId).readAttribute('name');
  261. this.tmpForm.setAttribute('target', $(this.iframeId).readAttribute('name'));
  262. for (var param in preparedData) {
  263. this.tmpForm.appendChild(this.createHiddenElement(param, preparedData[param]));
  264. }
  265. this.paymentRequestSent = true;
  266. this.tmpForm.submit();
  267. },
  268. createHiddenElement: function (name, value) {
  269. var field;
  270. if (isIE) {
  271. field = document.createElement('input');
  272. field.setAttribute('type', 'hidden');
  273. field.setAttribute('name', name);
  274. field.setAttribute('value', value);
  275. } else {
  276. field = document.createElement('input');
  277. field.type = 'hidden';
  278. field.name = name;
  279. field.value = value;
  280. }
  281. return field;
  282. },
  283. recreateIframe: function () {
  284. if ($(this.iframeId)) {
  285. var nextElement = $(this.iframeId).next();
  286. var src = $(this.iframeId).readAttribute('src');
  287. var name = $(this.iframeId).readAttribute('name');
  288. $(this.iframeId).stopObserving();
  289. $(this.iframeId).remove();
  290. var iframe = '<iframe id="' + this.iframeId +
  291. '" allowtransparency="true" frameborder="0" name="' + name +
  292. '" style="display:none;width:100%;background-color:transparent" src="' + src + '" />';
  293. Element.insert(nextElement, {
  294. 'before': iframe
  295. });
  296. $(this.iframeId).observe('load', this.onLoadIframe);
  297. }
  298. }
  299. };
  300. }));