ozuma5119

js/Phishing/PayPay/jquery.creditCardValidator.js

Oct 24th, 2020 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.   var $, Range, Trie,
  3.     indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  4.  
  5.   Trie = (function() {
  6.     function Trie() {
  7.       this.trie = {};
  8.     }
  9.  
  10.     Trie.prototype.push = function(value) {
  11.       var char, i, j, len, obj, ref, results;
  12.       value = value.toString();
  13.       obj = this.trie;
  14.       ref = value.split('');
  15.       results = [];
  16.       for (i = j = 0, len = ref.length; j < len; i = ++j) {
  17.         char = ref[i];
  18.         if (obj[char] == null) {
  19.           if (i === (value.length - 1)) {
  20.             obj[char] = null;
  21.           } else {
  22.             obj[char] = {};
  23.           }
  24.         }
  25.         results.push(obj = obj[char]);
  26.       }
  27.       return results;
  28.     };
  29.  
  30.     Trie.prototype.find = function(value) {
  31.       var char, i, j, len, obj, ref;
  32.       value = value.toString();
  33.       obj = this.trie;
  34.       ref = value.split('');
  35.       for (i = j = 0, len = ref.length; j < len; i = ++j) {
  36.         char = ref[i];
  37.         if (obj.hasOwnProperty(char)) {
  38.           if (obj[char] === null) {
  39.             return true;
  40.           }
  41.         } else {
  42.           return false;
  43.         }
  44.         obj = obj[char];
  45.       }
  46.     };
  47.  
  48.     return Trie;
  49.  
  50.   })();
  51.  
  52.   Range = (function() {
  53.     function Range(trie1) {
  54.       this.trie = trie1;
  55.       if (this.trie.constructor !== Trie) {
  56.         throw Error('Range constructor requires a Trie parameter');
  57.       }
  58.     }
  59.  
  60.     Range.rangeWithString = function(ranges) {
  61.       var j, k, len, n, r, range, ref, ref1, trie;
  62.       if (typeof ranges !== 'string') {
  63.         throw Error('rangeWithString requires a string parameter');
  64.       }
  65.       ranges = ranges.replace(/ /g, '');
  66.       ranges = ranges.split(',');
  67.       trie = new Trie;
  68.       for (j = 0, len = ranges.length; j < len; j++) {
  69.         range = ranges[j];
  70.         if (r = range.match(/^(\d+)-(\d+)$/)) {
  71.           for (n = k = ref = r[1], ref1 = r[2]; ref <= ref1 ? k <= ref1 : k >= ref1; n = ref <= ref1 ? ++k : --k) {
  72.             trie.push(n);
  73.           }
  74.         } else if (range.match(/^\d+$/)) {
  75.           trie.push(range);
  76.         } else {
  77.           throw Error("Invalid range '" + r + "'");
  78.         }
  79.       }
  80.       return new Range(trie);
  81.     };
  82.  
  83.     Range.prototype.match = function(number) {
  84.       return this.trie.find(number);
  85.     };
  86.  
  87.     return Range;
  88.  
  89.   })();
  90.  
  91.   $ = jQuery;
  92.  
  93.   $.fn.validateCreditCard = function(callback, options) {
  94.     var bind, card, card_type, card_types, get_card_type, is_valid_length, is_valid_luhn, j, len, normalize, ref, validate, validate_number;
  95.     card_types = [
  96.       {
  97.         name: 'amex',
  98.         range: '34,37',
  99.         valid_length: [15]
  100.       }, {
  101.         name: 'diners_club_carte_blanche',
  102.         range: '300-305',
  103.         valid_length: [14]
  104.       }, {
  105.         name: 'diners_club_international',
  106.         range: '36',
  107.         valid_length: [14]
  108.       }, {
  109.         name: 'jcb',
  110.         range: '3528-3589',
  111.         valid_length: [16]
  112.       }, {
  113.         name: 'laser',
  114.         range: '6304, 6706, 6709, 6771',
  115.         valid_length: [16, 17, 18, 19]
  116.       }, {
  117.         name: 'visa_electron',
  118.         range: '4026, 417500, 4508, 4844, 4913, 4917',
  119.         valid_length: [16]
  120.       }, {
  121.         name: 'visa',
  122.         range: '4',
  123.         valid_length: [13, 14, 15, 16, 17, 18, 19]
  124.       }, {
  125.         name: 'mastercard',
  126.         range: '51-55,2221-2720',
  127.         valid_length: [16]
  128.       }, {
  129.         name: 'discover',
  130.         range: '6011, 622126-622925, 644-649, 65',
  131.         valid_length: [16]
  132.       }, {
  133.         name: 'dankort',
  134.         range: '5019',
  135.         valid_length: [16]
  136.       }, {
  137.         name: 'maestro',
  138.         range: '50, 56-69',
  139.         valid_length: [12, 13, 14, 15, 16, 17, 18, 19]
  140.       }, {
  141.         name: 'uatp',
  142.         range: '1',
  143.         valid_length: [15]
  144.       }
  145.     ];
  146.     bind = false;
  147.     if (callback) {
  148.       if (typeof callback === 'object') {
  149.         options = callback;
  150.         bind = false;
  151.         callback = null;
  152.       } else if (typeof callback === 'function') {
  153.         bind = true;
  154.       }
  155.     }
  156.     if (options == null) {
  157.       options = {};
  158.     }
  159.     if (options.accept == null) {
  160.       options.accept = (function() {
  161.         var j, len, results;
  162.         results = [];
  163.         for (j = 0, len = card_types.length; j < len; j++) {
  164.           card = card_types[j];
  165.           results.push(card.name);
  166.         }
  167.         return results;
  168.       })();
  169.     }
  170.     ref = options.accept;
  171.     for (j = 0, len = ref.length; j < len; j++) {
  172.       card_type = ref[j];
  173.       if (indexOf.call((function() {
  174.         var k, len1, results;
  175.         results = [];
  176.         for (k = 0, len1 = card_types.length; k < len1; k++) {
  177.           card = card_types[k];
  178.           results.push(card.name);
  179.         }
  180.         return results;
  181.       })(), card_type) < 0) {
  182.         throw Error("Credit card type '" + card_type + "' is not supported");
  183.       }
  184.     }
  185.     get_card_type = function(number) {
  186.       var k, len1, r, ref1;
  187.       ref1 = (function() {
  188.         var l, len1, ref1, results;
  189.         results = [];
  190.         for (l = 0, len1 = card_types.length; l < len1; l++) {
  191.           card = card_types[l];
  192.           if (ref1 = card.name, indexOf.call(options.accept, ref1) >= 0) {
  193.             results.push(card);
  194.           }
  195.         }
  196.         return results;
  197.       })();
  198.       for (k = 0, len1 = ref1.length; k < len1; k++) {
  199.         card_type = ref1[k];
  200.         r = Range.rangeWithString(card_type.range);
  201.         if (r.match(number)) {
  202.           return card_type;
  203.         }
  204.       }
  205.       return null;
  206.     };
  207.     is_valid_luhn = function(number) {
  208.       var digit, k, len1, n, ref1, sum;
  209.       sum = 0;
  210.       ref1 = number.split('').reverse();
  211.       for (n = k = 0, len1 = ref1.length; k < len1; n = ++k) {
  212.         digit = ref1[n];
  213.         digit = +digit;
  214.         if (n % 2) {
  215.           digit *= 2;
  216.           if (digit < 10) {
  217.             sum += digit;
  218.           } else {
  219.             sum += digit - 9;
  220.           }
  221.         } else {
  222.           sum += digit;
  223.         }
  224.       }
  225.       return sum % 10 === 0;
  226.     };
  227.     is_valid_length = function(number, card_type) {
  228.       var ref1;
  229.       return ref1 = number.length, indexOf.call(card_type.valid_length, ref1) >= 0;
  230.     };
  231.     validate_number = function(number) {
  232.       var length_valid, luhn_valid;
  233.       card_type = get_card_type(number);
  234.       luhn_valid = false;
  235.       length_valid = false;
  236.       if (card_type != null) {
  237.         luhn_valid = is_valid_luhn(number);
  238.         length_valid = is_valid_length(number, card_type);
  239.       }
  240.       return {
  241.         card_type: card_type,
  242.         valid: luhn_valid && length_valid,
  243.         luhn_valid: luhn_valid,
  244.         length_valid: length_valid
  245.       };
  246.     };
  247.     validate = (function(_this) {
  248.       return function() {
  249.         var number;
  250.         number = normalize($(_this).val());
  251.         return validate_number(number);
  252.       };
  253.     })(this);
  254.     normalize = function(number) {
  255.       return number.replace(/[ -]/g, '');
  256.     };
  257.     if (!bind) {
  258.       return validate();
  259.     }
  260.     this.on('input.jccv', (function(_this) {
  261.       return function() {
  262.         $(_this).off('keyup.jccv');
  263.         return callback.call(_this, validate());
  264.       };
  265.     })(this));
  266.     this.on('keyup.jccv', (function(_this) {
  267.       return function() {
  268.         return callback.call(_this, validate());
  269.       };
  270.     })(this));
  271.     callback.call(this, validate());
  272.     return this;
  273.   };
  274.  
  275. }).call(this);
Add Comment
Please, Sign In to add comment