PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/js/gomage/directpost.js

https://bitbucket.org/acidel/buykoala
JavaScript | 383 lines | 329 code | 30 blank | 24 comment | 55 complexity | 69c7e243f03bb479e5b8208f6027fd50 MD5 | raw file
  1. /**
  2. * Magento
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * This source file is subject to the Academic Free License (AFL 3.0)
  7. * that is bundled with this package in the file LICENSE_AFL.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://opensource.org/licenses/afl-3.0.php
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@magentocommerce.com so we can send you a copy immediately.
  13. *
  14. * DISCLAIMER
  15. *
  16. * Do not edit or add to this file if you wish to upgrade Magento to newer
  17. * versions in the future. If you wish to customize Magento for your
  18. * needs please refer to http://www.magentocommerce.com for more information.
  19. *
  20. * @category Mage
  21. * @package js
  22. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. */
  25. var directPost = Class.create();
  26. directPost.prototype = {
  27. initialize : function(methodCode, iframeId, controller, orderSaveUrl,
  28. cgiUrl, nativeAction) {
  29. this.iframeId = iframeId;
  30. this.controller = controller;
  31. this.orderSaveUrl = orderSaveUrl;
  32. this.nativeAction = nativeAction;
  33. this.cgiUrl = cgiUrl;
  34. this.code = methodCode;
  35. this.inputs = ['cc_type', 'cc_number', 'expiration', 'expiration_yr', 'cc_cid'];
  36. this.headers = [];
  37. this.isValid = true;
  38. this.paymentRequestSent = false;
  39. this.isResponse = false;
  40. this.orderIncrementId = false;
  41. this.successUrl = false;
  42. this.hasError = false;
  43. this.tmpForm = false;
  44. this.onSaveOnepageOrderSuccess = this.saveOnepageOrderSuccess.bindAsEventListener(this);
  45. this.onLoadIframe = this.loadIframe.bindAsEventListener(this);
  46. this.onLoadOrderIframe = this.loadOrderIframe.bindAsEventListener(this);
  47. this.onSubmitAdminOrder = this.submitAdminOrder.bindAsEventListener(this);
  48. this.preparePayment();
  49. },
  50. validate : function() {
  51. this.isValid = true;
  52. this.inputs.each(function(elemIndex) {
  53. if ($(this.code + '_' + elemIndex)) {
  54. if (!Validation.validate($(this.code + '_' + elemIndex))) {
  55. this.isValid = false;
  56. }
  57. }
  58. }, this);
  59. return this.isValid;
  60. },
  61. changeInputOptions : function(param, value) {
  62. this.inputs.each(function(elemIndex) {
  63. if ($(this.code + '_' + elemIndex)) {
  64. $(this.code + '_' + elemIndex).writeAttribute(param, value);
  65. }
  66. }, this);
  67. },
  68. preparePayment : function() {
  69. this.changeInputOptions('autocomplete', 'off');
  70. if ($(this.iframeId)) {
  71. switch (this.controller) {
  72. case 'onepage':
  73. break;
  74. case 'sales_order_create':
  75. case 'sales_order_edit':
  76. var buttons = document.getElementsByClassName('scalable save');
  77. for ( var i = 0; i < buttons.length; i++) {
  78. buttons[i].writeAttribute('onclick', '');
  79. buttons[i].observe('click', this.onSubmitAdminOrder);
  80. }
  81. $('order-' + this.iframeId).observe('load', this.onLoadOrderIframe);
  82. break;
  83. }
  84. $(this.iframeId).observe('load', this.onLoadIframe);
  85. }
  86. },
  87. loadIframe : function() {
  88. if (this.paymentRequestSent) {
  89. switch (this.controller) {
  90. case 'onepage':
  91. this.paymentRequestSent = false;
  92. if (!this.hasError) {
  93. this.returnQuote();
  94. }
  95. break;
  96. case 'sales_order_edit':
  97. case 'sales_order_create':
  98. if (!this.orderRequestSent) {
  99. this.paymentRequestSent = false;
  100. if (!this.hasError) {
  101. this.returnQuote();
  102. } else {
  103. this.changeInputOptions('disabled', false);
  104. toggleSelectsUnderBlock($('loading-mask'), true);
  105. $('loading-mask').hide();
  106. enableElements('save');
  107. }
  108. }
  109. break;
  110. }
  111. if (this.tmpForm) {
  112. document.body.removeChild(this.tmpForm);
  113. }
  114. }
  115. },
  116. loadOrderIframe : function() {
  117. if (this.orderRequestSent) {
  118. $(this.iframeId).hide();
  119. var data = $('order-' + this.iframeId).contentWindow.document.body.innerHTML;
  120. this.saveAdminOrderSuccess(data);
  121. this.orderRequestSent = false;
  122. }
  123. },
  124. showError : function(msg) {
  125. this.hasError = true;
  126. if (this.controller == 'onepage') {
  127. $(this.iframeId).hide();
  128. this.resetLoadWaiting();
  129. }
  130. alert(msg);
  131. },
  132. returnQuote : function() {
  133. var url = this.orderSaveUrl.replace('place', 'returnQuote');
  134. new Ajax.Request(url, {
  135. onSuccess : function(transport) {
  136. try {
  137. response = eval('(' + transport.responseText + ')');
  138. } catch (e) {
  139. response = {};
  140. }
  141. if (response.error_message) {
  142. alert(response.error_message);
  143. }
  144. $(this.iframeId).show();
  145. switch (this.controller) {
  146. case 'onepage':
  147. this.resetLoadWaiting();
  148. break;
  149. case 'sales_order_edit':
  150. case 'sales_order_create':
  151. this.changeInputOptions('disabled', false);
  152. toggleSelectsUnderBlock($('loading-mask'), true);
  153. $('loading-mask').hide();
  154. enableElements('save');
  155. break;
  156. }
  157. }.bind(this)
  158. });
  159. },
  160. setLoadWaiting : function() {
  161. this.headers.each(function(header) {
  162. header.removeClassName('allow');
  163. });
  164. },
  165. resetLoadWaiting : function() {
  166. this.headers.each(function(header) {
  167. header.addClassName('allow');
  168. });
  169. },
  170. saveOnepageOrder : function() {
  171. this.hasError = false;
  172. this.setLoadWaiting();
  173. var params = Form.serialize(payment.form);
  174. params += '&controller=' + this.controller;
  175. new Ajax.Request(this.orderSaveUrl, {
  176. method : 'post',
  177. parameters : params,
  178. onComplete : this.onSaveOnepageOrderSuccess,
  179. onFailure : function(transport) {
  180. this.resetLoadWaiting();
  181. if (transport.status == 403) {
  182. checkout.ajaxFailure();
  183. }
  184. }
  185. });
  186. },
  187. saveOnepageOrderSuccess : function(transport) {
  188. if (transport.status == 403) {
  189. checkout.ajaxFailure();
  190. }
  191. try {
  192. response = eval('(' + transport.responseText + ')');
  193. } catch (e) {
  194. response = {};
  195. }
  196. if (response.success && response.directpost) {
  197. this.orderIncrementId = response.directpost.fields.x_invoice_num;
  198. var paymentData = {};
  199. for ( var key in response.directpost.fields) {
  200. paymentData[key] = response.directpost.fields[key];
  201. }
  202. var preparedData = this.preparePaymentRequest(paymentData);
  203. this.sendPaymentRequest(preparedData);
  204. } else {
  205. var msg = response.error_messages;
  206. if (typeof (msg) == 'object') {
  207. msg = msg.join("\n");
  208. }
  209. if (msg) {
  210. alert(msg);
  211. }
  212. }
  213. },
  214. submitAdminOrder : function() {
  215. if (editForm.validate()) {
  216. var paymentMethodEl = $(editForm.formId).getInputs('radio','payment[method]').find(function(radio) {
  217. return radio.checked;
  218. });
  219. this.hasError = false;
  220. if (paymentMethodEl.value == this.code) {
  221. toggleSelectsUnderBlock($('loading-mask'), false);
  222. $('loading-mask').show();
  223. setLoaderPosition();
  224. this.changeInputOptions('disabled', 'disabled');
  225. this.paymentRequestSent = true;
  226. this.orderRequestSent = true;
  227. $(editForm.formId).writeAttribute('action', this.orderSaveUrl);
  228. $(editForm.formId).writeAttribute('target',
  229. $('order-' + this.iframeId).readAttribute('name'));
  230. $(editForm.formId).appendChild(this.createHiddenElement('controller', this.controller));
  231. disableElements('save');
  232. $(editForm.formId).submit();
  233. } else {
  234. $(editForm.formId).writeAttribute('action', this.nativeAction);
  235. $(editForm.formId).writeAttribute('target', '_top');
  236. disableElements('save');
  237. $(editForm.formId).submit();
  238. }
  239. }
  240. },
  241. recollectQuote : function() {
  242. var area = [ 'sidebar', 'items', 'shipping_method', 'billing_method', 'totals', 'giftmessage' ];
  243. area = order.prepareArea(area);
  244. var url = order.loadBaseUrl + 'block/' + area;
  245. var info = $('order-items_grid').select('input', 'select', 'textarea');
  246. var data = {};
  247. for ( var i = 0; i < info.length; i++) {
  248. if (!info[i].disabled && (info[i].type != 'checkbox' || info[i].checked)) {
  249. data[info[i].name] = info[i].getValue();
  250. }
  251. }
  252. data.reset_shipping = true;
  253. data.update_items = true;
  254. if ($('coupons:code') && $F('coupons:code')) {
  255. data['order[coupon][code]'] = $F('coupons:code');
  256. }
  257. data.json = true;
  258. new Ajax.Request(url, {
  259. parameters : data,
  260. loaderArea : 'html-body',
  261. onSuccess : function(transport) {
  262. $(editForm.formId).submit();
  263. }.bind(this)
  264. });
  265. },
  266. saveAdminOrderSuccess : function(data) {
  267. try {
  268. response = eval('(' + data + ')');
  269. } catch (e) {
  270. response = {};
  271. }
  272. if (response.directpost) {
  273. this.orderIncrementId = response.directpost.fields.x_invoice_num;
  274. var paymentData = {};
  275. for ( var key in response.directpost.fields) {
  276. paymentData[key] = response.directpost.fields[key];
  277. }
  278. var preparedData = this.preparePaymentRequest(paymentData);
  279. this.sendPaymentRequest(preparedData);
  280. } else {
  281. if (response.redirect) {
  282. window.location = response.redirect;
  283. }
  284. if (response.error_messages) {
  285. var msg = response.error_messages;
  286. if (typeof (msg) == 'object') {
  287. msg = msg.join("\n");
  288. }
  289. if (msg) {
  290. alert(msg);
  291. }
  292. }
  293. }
  294. },
  295. preparePaymentRequest : function(data) {
  296. if ($(this.code + '_cc_cid')) {
  297. data.x_card_code = $(this.code + '_cc_cid').value;
  298. }
  299. var year = $(this.code + '_expiration_yr').value;
  300. if (year.length > 2) {
  301. year = year.substring(2);
  302. }
  303. var month = parseInt($(this.code + '_expiration').value, 10);
  304. if (month < 10) {
  305. month = '0' + month;
  306. }
  307. data.x_exp_date = month + '/' + year;
  308. data.x_card_num = $(this.code + '_cc_number').value;
  309. return data;
  310. },
  311. sendPaymentRequest : function(preparedData) {
  312. this.recreateIframe();
  313. this.tmpForm = document.createElement('form');
  314. this.tmpForm.style.display = 'none';
  315. this.tmpForm.enctype = 'application/x-www-form-urlencoded';
  316. this.tmpForm.method = 'POST';
  317. document.body.appendChild(this.tmpForm);
  318. this.tmpForm.action = this.cgiUrl;
  319. this.tmpForm.target = $(this.iframeId).readAttribute('name');
  320. this.tmpForm.setAttribute('target', $(this.iframeId).readAttribute('name'));
  321. for ( var param in preparedData) {
  322. this.tmpForm.appendChild(this.createHiddenElement(param, preparedData[param]));
  323. }
  324. this.paymentRequestSent = true;
  325. this.tmpForm.submit();
  326. },
  327. createHiddenElement : function(name, value) {
  328. var field;
  329. if (isIE) {
  330. field = document.createElement('<input type="hidden" name="' + name + '" value="' + value + '" />');
  331. } else {
  332. field = document.createElement('input');
  333. field.type = 'hidden';
  334. field.name = name;
  335. field.value = value;
  336. }
  337. return field;
  338. },
  339. recreateIframe : function() {
  340. if ($(this.iframeId)) {
  341. var nextElement = $(this.iframeId).next();
  342. var src = $(this.iframeId).readAttribute('src');
  343. var name = $(this.iframeId).readAttribute('name');
  344. $(this.iframeId).stopObserving();
  345. $(this.iframeId).remove();
  346. var iframe = '<iframe id="' + this.iframeId +
  347. '" allowtransparency="true" frameborder="0" name="' + name +
  348. '" style="display:none;width:100%;background-color:transparent" src="' + src + '" />';
  349. Element.insert(nextElement, {'before':iframe});
  350. $(this.iframeId).observe('load', this.onLoadIframe);
  351. }
  352. }
  353. };