Skip to content

Instantly share code, notes, and snippets.

@jakubboucek
Last active June 5, 2017 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakubboucek/c1c7606b0c9315b30f96036cc4289aa0 to your computer and use it in GitHub Desktop.
Save jakubboucek/c1c7606b0c9315b30f96036cc4289aa0 to your computer and use it in GitHub Desktop.
McDonald's receipt number validator
// Part of JS file of Surwey
// https://portal.fast-insight.com/iwr.asp?project=20a8ef&blankedLayout=1 (POST request)
function(selectedQuestionID, inputElement2) {
/// SCRIPT FOR VINGERE & BASE36
//Promo code from receipt
var encryptedString=inputElement2.value;
// 0000-0000-0000 -> 000000000000
if (encryptedString.substr(4,1) === "-") {
var encryptedString = encryptedString.substr(0,4) + encryptedString.substr(5,4) + encryptedString.substr(10,4);
}
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://survey.fast-insight.com/mcd/cz/codecheck.php?code=" + encryptedString, false);
xhr.send();
//alert(xhr.responseText);
if (xhr.responseText.toLowerCase() !== "yes") {
document.getElementById("error2").innerHTML = "Code already used.";
} else {
if (encryptedString == "999999999999") {
var storeID = "9999";
var posID = "01";
var day = "01";
var month = "01";
var hour = "12";
var minutes = "12";
var orderID = "01";
} else {
var storeID = "";
var posID = "";
var day = "";
var month = "";
var hour = "";
var minutes = "";
var orderID = ""
try {
var inputCodeBase = encryptedString.replace(new RegExp("[^0-9A-Z]+","ig"), "").toLowerCase();
var s1 = "oqiljzyvuxw";
var s2 = "00111ijlloq";
for (var i = 0; i < 11; i++) {
while(inputCodeBase.indexOf(s1[i],0) >= 0)
{
inputCodeBase = inputCodeBase.replace(s1[i],s2[i]);
}
}
// console.log("recovered base-30:"+inputCodeBase);
inputCodeBase = ("0000000000000000" + inputCodeBase);
var inputCodeBase10 = inputCodeBase.substring(inputCodeBase.length - 12, inputCodeBase.length - 5);
var storenumberpart = "00000000000" + (parseInt(inputCodeBase10, 30) * 243);
storenumberpart = storenumberpart.substring(storenumberpart.length - 13, storenumberpart.length - 5);
// console.log("begin part:"+storenumberpart);
inputCodeBase10 = inputCodeBase.substring(inputCodeBase.length - 10, inputCodeBase.length);
var idecodedCodeBase10 = parseInt(inputCodeBase10, 30) ;
var decodedCodeBase10 = idecodedCodeBase10.toString();
decodedCodeBase10 = storenumberpart + decodedCodeBase10.substring(decodedCodeBase10.length - 10, decodedCodeBase10.length);
// console.log("decoded:"+decodedCodeBase10);
var checksum = decodedCodeBase10.substring(decodedCodeBase10.length - 2, decodedCodeBase10.length);
decodedCodeBase10 = decodedCodeBase10.substring(decodedCodeBase10.length - 18, decodedCodeBase10.length - 2);
var CRC = "000";
// Calculate CRC-8
{
var crci, crcj;
var crcc;
var iRC = 0;
for (crcj = 0; crcj < 16; crcj++) {
crcc = decodedCodeBase10.charCodeAt(crcj);
iRC ^= crcc;
for (crci = 0; crci < 8; crci++)
if (iRC & 0x80)
iRC = (iRC << 1) ^ 0x07;
else
iRC <<= 1;
iRC &= 0xff;
}
CRC = "000" + iRC.toString();
}
CRC = CRC.substring(CRC.length - 2, CRC.length);
// console.log("crc: ["+checksum+"] <> [" + CRC + "] :crc calculated");
if (CRC == checksum) {
//checking of data validity
storeID = decodedCodeBase10.substring(0, 4);
posID = decodedCodeBase10.substring(12, 14);
var istoreID = parseInt(storeID, 10);
var iposID = parseInt(posID, 10);
if (istoreID < 1 || istoreID > 1000) {
// wrong store number = error
}
if (iposID < 1 || iposID > 38) {
// wrong pos number = error
}
var calendarDate = decodedCodeBase10.substring(4, 8);
var day = calendarDate.substring(0, 2);
var month = calendarDate.substring(2, 4);
// Check Date range ?
// date not bigger than today !
// date not earlier than 30 days ?
var timeOfSale = decodedCodeBase10.substring(8, 12);
var hour = timeOfSale.substring(0, 2);
var minutes = timeOfSale.substring(2, 4);
var orderID = decodedCodeBase10.substring(14, 16);
console.log("sid:"+storeID+",pos:"+posID);
} else {
// wrong checksum = error
}
} catch (ex) {
alert(ex.message);
}
}
// Date
var surveyyear = new Date().getFullYear();
var surveydate = surveyyear+"-"+month+"-"+day;
// Time
var surveytime = hour+minutes;
var surveytime2 = hour+":"+minutes;
if (surveytime >= 0600 && surveytime <= 1200) var surveytimeoption = 1;
if (surveytime > 1200 && surveytime <= 1500) var surveytimeoption = 2;
if (surveytime > 1500 && surveytime <= 1900) var surveytimeoption = 3;
if (surveytime > 1900 && surveytime <= 2100) var surveytimeoption = 4;
if (surveytime > 2100 && surveytime <= 2400) var surveytimeoption = 5;
if (surveytime > 0100 && surveytime <= 0600) var surveytimeoption = 6;
if (surveytime >= 0000 && surveytime <= 0100) var surveytimeoption = 5;
// 1 Theke
// 2 Tischservice
// 3 Drive
// 4 Selforder
// 5 Lieferservice
var posid = posID;
if (posid > 0 && posid < 12) var posidposition = 1;
if (posid > 13 && posid < 17) var posidposition = 2;
if (posid > 16 && posid < 20) var posidposition = 3;
if (posid > 24 && posid < 33) var posidposition = 4;
var orderid = orderID;
var n = 18;
if (n < 18 || day > 31 || month > 12 || hour > 24 || minutes > 60 || posid > 33) {
document.getElementById("error2").innerHTML = ODP.Surveo.globalization.getLiteralForCurrentLanguage(ODP.Surveo.params.questionData[selectedQuestionID].invalidLocationLiteralID);
} else {
if(storeID === "" || (storeID in ODP.Surveo.params.surveoValidIWRCodes)) {
ODP.Surveo.params.buttonsLock = 0;
} else {
ODP.Surveo.params.buttonsLock = 1;
//alert(ODP.Surveo.globalization.getLiteralForCurrentLanguage(ODP.Surveo.params.questionData[selectedQuestionID].invalidLocationLiteralID));
document.getElementById("error2").innerHTML = ODP.Surveo.globalization.getLiteralForCurrentLanguage(ODP.Surveo.params.questionData[selectedQuestionID].invalidLocationLiteralID);
return;
}
// 8601 Zeitfenster
sm_setmultipleanswer("9279", surveytimeoption);
// 8627 Datum
ODP.Surveo.methods.sm_setcomment("9354", surveydate);
// 8592 Bestellnummer
// sm_setcomment("8592", orderid);
// 8595 Restaurantbereich
sm_setmultipleanswer("9278", posidposition);
// 8595 posID
ODP.Surveo.methods.sm_setcomment("9357", posID);
// promo
ODP.Surveo.methods.sm_setcomment("9356", encryptedString);
// receipt time
ODP.Surveo.methods.sm_setcomment("9353", surveytime2);
ODP.Surveo.params.buttonsLock = 0;
ODP.Surveo.params.blockRotationHandling = false;
ODP.Surveo.methods.sm_setcomment(selectedQuestionID, storeID, true);
ODP.Surveo.methods.sm_mother(selectedQuestionID);
}
}
}
{
"dataset": {
"schema": {
"manifest": {
"datasetname": "/Apps/SM/Survey/IWRGetCountriesAndLocationsWithCode",
"datafieldsmode": "D",
"dbsqltype": "P",
"schemaformat": "JSON",
"dataformat": "simple",
"encoding": "utf-8",
"security": {},
"cacheable": {},
"hasoutputparameters": false,
"meta": {}
},
"parameters": [],
"columns": [
[{
"name": "CountryCode"
}, {
"name": "CountryName"
}],
[{
"name": "Code"
}, {
"name": "MystLocationCountry"
}, {
"name": "Value"
}]
]
},
"data": ["CZCzech Republic", "0121CZ, 9999CZ, 0075CZBrno, Masarykova 320105CZBrno, Skandinávská 185/160094CZFrýdek-Místek, Na příkopě 36240064CZHradec Králové, Brněnská 1825/23a0021CZHradec Králové, Pilnáčkova 3800044CZKladno, Italská 500087CZLoket, dálnice D1, 66km0024CZMěřín, dálnice D1, 139km0039CZOlomouc, Kafkova 20100CZOlomouc, Polská 10086CZOpava, Hlučínská 1702/670066CZOstrava, Rudná 3121/1280013CZPlzeň, Americká 460065CZPlzeň, Lochotínská 180074CZPlzeň, Rokycanská 1424/1280052CZPlzeň, U Letiště 2/10740090CZPlzeň, U Prazdroje 220079CZPraha, Aviatická 1017/20043CZPraha, Bránická0011CZPraha, dálnice D1, 4,5km0103CZPraha, Nákupní 389/30088CZPraha, Náměstí republiky 1078/10099CZPraha, Průmyslová 700/30a0072CZPraha, Řevnická 121/10056CZPraha, Skandinávská 162/270004CZPraha, Sokolovská 20800049CZPraha, Sulická0018CZPraha, Václavské náměstí 812/590091CZPraha, Václavské náměstí 90046CZRozvadov, odpočívka Svatá Kateřina"]
}
}
{
"0121": {
"code": "0121",
"countryCode": "CZ",
"country": "Czech Republic",
"address": ", "
},
"9999": {
"code": "9999",
"countryCode": "CZ",
"country": "Czech Republic",
"address": ", "
},
"0075": {
"code": "0075",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Brno, Masarykova 32"
},
"0105": {
"code": "0105",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Brno, Skandinávská 185/16"
},
"0094": {
"code": "0094",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Frýdek-Místek, Na příkopě 3624"
},
"0064": {
"code": "0064",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Hradec Králové, Brněnská 1825/23a"
},
"0021": {
"code": "0021",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Hradec Králové, Pilnáčkova 380"
},
"0044": {
"code": "0044",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Kladno, Italská 50"
},
"0087": {
"code": "0087",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Loket, dálnice D1, 66km"
},
"0024": {
"code": "0024",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Měřín, dálnice D1, 139km"
},
"0039": {
"code": "0039",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Olomouc, Kafkova 2"
},
"0100": {
"code": "0100",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Olomouc, Polská 1"
},
"0086": {
"code": "0086",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Opava, Hlučínská 1702/67"
},
"0066": {
"code": "0066",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Ostrava, Rudná 3121/128"
},
"0013": {
"code": "0013",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Plzeň, Americká 46"
},
"0065": {
"code": "0065",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Plzeň, Lochotínská 18"
},
"0074": {
"code": "0074",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Plzeň, Rokycanská 1424/128"
},
"0052": {
"code": "0052",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Plzeň, U Letiště 2/1074"
},
"0090": {
"code": "0090",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Plzeň, U Prazdroje 22"
},
"0079": {
"code": "0079",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Aviatická 1017/2"
},
"0043": {
"code": "0043",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Bránická"
},
"0011": {
"code": "0011",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, dálnice D1, 4,5km"
},
"0103": {
"code": "0103",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Nákupní 389/3"
},
"0088": {
"code": "0088",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Náměstí republiky 1078/1"
},
"0099": {
"code": "0099",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Průmyslová 700/30a"
},
"0072": {
"code": "0072",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Řevnická 121/1"
},
"0056": {
"code": "0056",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Skandinávská 162/27"
},
"0004": {
"code": "0004",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Sokolovská 2080"
},
"0049": {
"code": "0049",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Sulická"
},
"0018": {
"code": "0018",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Václavské náměstí 812/59"
},
"0091": {
"code": "0091",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Praha, Václavské náměstí 9"
},
"0046": {
"code": "0046",
"countryCode": "CZ",
"country": "Czech Republic",
"address": "Rozvadov, odpočívka Svatá Kateřina"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment