Guest User

Untitled

a guest
Oct 20th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. var credit_card_data = "";
  2. var timer = null;
  3.  
  4. function parse(val){
  5. var tracks = val.split("?");
  6. var parts = tracks[0].split("^");
  7. var card = parts[0].replace("%B", "");
  8. var card_type = get_card_type(card);
  9. var names = parts[1].split("/")
  10. var name = names[1].trim()+" "+names[0].trim();
  11. var yy = parts[2].substr(0, 2);
  12. var mm = parts[2].substr(2, 2);
  13. return {
  14. card: card,
  15. card_type: card_type,
  16. name: name,
  17. yy: yy,
  18. mm: mm
  19. }
  20. }
  21.  
  22. function get_card_type(number){
  23. // visa
  24. var re = new RegExp("^4");
  25. if (number.match(re) != null) return "Visa";
  26.  
  27. // Mastercard
  28. // Updated for Mastercard 2017 BINs expansion
  29. if (/^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/.test(number)) return "Master Card";
  30.  
  31. // AMEX
  32. re = new RegExp("^3[47]");
  33. if (number.match(re) != null) return "American Express";
  34.  
  35. // Discover
  36. re = new RegExp("^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)");
  37. if (number.match(re) != null) return "Discover";
  38.  
  39. // Diners
  40. re = new RegExp("^36");
  41. if (number.match(re) != null) return "Diners";
  42.  
  43. // Diners - Carte Blanche
  44. re = new RegExp("^30[0-5]");
  45. if (number.match(re) != null) return "Diners - Carte Blanche";
  46.  
  47. // JCB
  48. re = new RegExp("^35(2[89]|[3-8][0-9])");
  49. if (number.match(re) != null) return "JCB";
  50.  
  51. // Visa Electron
  52. re = new RegExp("^(4026|417500|4508|4844|491(3|7))");
  53. if (number.match(re) != null) return "Visa Electron";
  54.  
  55. return "";
  56. }
  57.  
  58. function apply(values){
  59. console.log(values);
  60. $("#CardNumber").val(values.card);
  61. $("#AccountName").val(values.name);
  62. $("#YY option").each(function(){
  63. var v = $(this).text().trim();
  64. if(v == values.yy){
  65. $(this).prop("selected", true);
  66. }
  67. })
  68. $("#MM option").each(function(){
  69. var v = $(this).text().trim();
  70. if(v == values.mm){
  71. $(this).prop("selected", true);
  72. }
  73. })
  74. $("input:radio").each(function(){
  75. var v = $(this).val().trim();
  76. if(v == values.card_type){
  77. $(this).prop("checked", true);
  78. }
  79. })
  80. }
  81.  
  82. $(document).ready(function(){
  83. $(document).on("keydown", function(d){
  84. if(d.key != "Shift" && d.key != "Enter" && d.key != "Alt"){
  85. if(!timer){
  86. timer = setTimeout(function(){
  87. values = parse(credit_card_data);
  88. apply(values);
  89. credit_card_data = "";
  90. clearTimeout(timer);
  91. timer = null;
  92. validate();
  93. }, 500);
  94. }
  95. credit_card_data = credit_card_data + d.key;
  96. }
  97. });
  98. });
Add Comment
Please, Sign In to add comment