validator.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /*browser:true*/
  6. /*global define*/
  7. define([
  8. 'underscore'
  9. ], function (_) {
  10. 'use strict';
  11. return {
  12. config: {},
  13. /**
  14. * Set configuration
  15. * @param {Object} config
  16. */
  17. setConfig: function (config) {
  18. this.config = config;
  19. },
  20. /**
  21. * Get List of available card types
  22. * @returns {*|exports.defaults.availableCardTypes|{}}
  23. */
  24. getAvailableCardTypes: function () {
  25. return this.config.availableCardTypes;
  26. },
  27. /**
  28. * Get list of card types
  29. * @returns {Object}
  30. */
  31. getCcTypesMapper: function () {
  32. return this.config.ccTypesMapper;
  33. },
  34. /**
  35. * Find mage card type by Braintree type
  36. * @param {String} type
  37. * @param {Object} availableTypes
  38. * @returns {*}
  39. */
  40. getMageCardType: function (type, availableTypes) {
  41. var storedCardType = null,
  42. mapper = this.getCcTypesMapper();
  43. if (type && typeof mapper[type] !== 'undefined') {
  44. storedCardType = mapper[type];
  45. if (_.indexOf(availableTypes, storedCardType) !== -1) {
  46. return storedCardType;
  47. }
  48. }
  49. return null;
  50. },
  51. /**
  52. * Filter list of available card types
  53. * @param {Object} availableTypes
  54. * @param {Object} countrySpecificCardTypes
  55. * @returns {Object}
  56. */
  57. collectTypes: function (availableTypes, countrySpecificCardTypes) {
  58. var key,
  59. filteredTypes = [];
  60. for (key in availableTypes) {
  61. if (_.indexOf(countrySpecificCardTypes, availableTypes[key]) !== -1) {
  62. filteredTypes.push(availableTypes[key]);
  63. }
  64. }
  65. return filteredTypes;
  66. },
  67. /**
  68. * Get list of card types for country
  69. * @param {String} countryId
  70. * @returns {*}
  71. */
  72. getCountrySpecificCardTypes: function (countryId) {
  73. if (typeof this.config.countrySpecificCardTypes[countryId] !== 'undefined') {
  74. return this.config.countrySpecificCardTypes[countryId];
  75. }
  76. return false;
  77. }
  78. };
  79. });