Gousgounis / VivaWalletCardSwipe

// ==UserScript==
// @name        VivaWalletCardSwipe
// @namespace   Gousgounis
// @description Επιτρέπει την χρήση αναγνώστη μαγνητικής ταινίας για να συμπληρωθούν αυτόματα τα πεδία της φόρμας VirtualPOS στο επαγγελματικό Viva Wallet
// @include     https://members.vivawallet.com/selfcare/el-gr/sales/virtualpos
// @version     1.2
// @license GPL-3.0+; http://www.gnu.org/licenses/gpl-3.0.txt
// @grant       none
// ==/UserScript==


function swipe_cc() {
	var card_data = window.prompt("Scan Card");

	// See http://en.wikipedia.org/wiki/ISO/IEC_7813
	var track1_regex = new RegExp("^%B([\\d]{1,19})\\^(.{2,26})\\^([\\d]{0,4}|\\^)([\\d]{0,3}|\\^).+");
	var track2_regex = new RegExp(";([\\d]{1,19})\\=(\\d{4}|\\=)");

	var track1_match = track1_regex.exec(card_data);
	var track2_match = track2_regex.exec(card_data);

	var number, name, expiry;

	if(track1_match) {
		number = track1_match[1];
		name = track1_match[2];
		expiry = track1_match[3];
	} else if (track2_match) {
		number = track2_match[1];
		name = "";
		expiry = track2_match[2];
	} else {
		window.alert('Could not read credit card data!');
		return;
	}

	//expiry = expiry.substr(2, 2) + "/" + '20' + expiry.substr(0, 2);

          //inputs to be filled within WEBapp

document.getElementById("Reference").value = "Πληρωμή με κάρτα";
document.getElementById("Email").value = ""; //για την απόδειξη
document.getElementById("Description").value = "Παροχή υπηρεσίας μηχανικού";
document.getElementById("Mobile").value = "";  //Αυτό γιατί το θέλει;
document.getElementById("CardNumber").value = number; //αριθμός κάρτας
document.getElementById("FullName").value = name; //parsed
document.getElementById("CardCVV").value = ""; // not present on magstripe

var sel = document.getElementById('Year');
  var opts = sel.options;
  for (var opt, j = 0; opt = opts[j]; j++) {
    if (opt.value == expiry.substr(0, 2)) {
      sel.selectedIndex = j;
    }
  }

sel = document.getElementById('Month');
  opts = sel.options;
  for (opt, j = 0; opt = opts[j]; j++) {
    if (opt.value == expiry.substr(2, 2)) {
      sel.selectedIndex = j;
    }
  }

document.getElementById('SelectedSourceCode').selectedIndex = document.getElementById('SelectedSourceCode').length - 1;
          
}

var parseBTN = document.getElementsByTagName("button");
var iBTN;
for (iBTN = 0; iBTN < parseBTN.length; iBTN++) {
    if (parseBTN[iBTN].innerHTML == "Προέγκριση Ποσού") {
        parseBTN[iBTN].innerHTML = "Προέγκριση Ποσού</button><button class=\"btn\" onclick=\"swipe_cc();\">Swipe";
        //alert(parseBTN[iBTN].innerHTML);
    }
}