forked from MicrosoftDocs/winrt-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario1.js
96 lines (83 loc) · 4.19 KB
/
scenario1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//// Copyright (c) Microsoft Corporation. All rights reserved
(function () {
"use strict";
var page = WinJS.UI.Pages.define("/html/scenario1.html", {
ready: function (element, options) {
document.getElementById("startReadButton").addEventListener("click", startRead, false);
document.getElementById("endReadButton").addEventListener("click", endRead, false);
document.getElementById("startReadButton").disabled = false;
document.getElementById("endReadButton").disabled = false;
},
unload: function () {
if (document.getElementById("startReadButton").disabled) {
if (_claimedReader !== null) {
_claimedReader.removeEventListener("bankcarddatareceived", onBankCardDataReceived);
_claimedReader.close();
_claimedReader = null;
}
_reader = null;
}
}
});
//<SnippetCreateMagStripeReaderJS>
//Creates the magnetic stripe reader, claims it for exclusive use, adds a data event listener,
//and enables it to receive data.
var _reader = null;
var _claimedReader = null;
function startRead() {
Windows.Devices.PointOfService.MagneticStripeReader.getDefaultAsync().then(function (reader) {
if (reader !== null) {
_reader = reader;
reader.claimReaderAsync().done(function (claimedReader) {
_claimedReader = claimedReader;
claimedReader.isDecodeDataEnabled = true;
claimedReader.addEventListener("bankcarddatareceived", onBankCardDataReceived);
claimedReader.enableAsync().done(function () {
document.getElementById("startReadButton").disabled = true;
document.getElementById("endReadButton").disabled = false;
}, function error(e) {
// "Failed to enable the magnetic stripe reader."
});
}, function error(e) {
//"Could not claim the magnetic stripe reader."
});
}
else {
//"Could not claim the magnetic stripe reader."
}
}, function error(e) {
//"Magnetic stripe reader not found. Connect a magnetic stripe reader."
});
}
//</SnippetCreateMagStripeReaderJS>
//<SnippetMagStripeReaderDataReceivedJS>
function onBankCardDataReceived(args) {
WinJS.log && WinJS.log("Got data", "sample", "status");
document.getElementById("expirationDate").textContent = args.expirationDate;
document.getElementById("firstName").textContent = args.firstName;
document.getElementById("middleInitial").textContent = args.middleInitial;
document.getElementById("serviceCode").textContent = args.serviceCode;
document.getElementById("suffix").textContent = args.suffix;
document.getElementById("surname").textContent = args.surname;
document.getElementById("title").textContent = args.title;
}
//</SnippetMagStripeReaderDataReceivedJS>
function endRead() {
if (_claimedReader !== null) {
_claimedReader.removeEventListener("bankcarddatareceived", onBankCardDataReceived);
_claimedReader.close();
_claimedReader = null;
}
_reader = null;
WinJS.log("Click the Start Receiving Data Button..", "sample", "status");
document.getElementById("startReadButton").disabled = false;
document.getElementById("endReadButton").disabled = true;
document.getElementById("expirationDate").textContent = "No data";
document.getElementById("firstName").textContent = "No data";
document.getElementById("middleInitial").textContent = "No data";
document.getElementById("serviceCode").textContent = "No data";
document.getElementById("suffix").textContent = "No data";
document.getElementById("surname").textContent = "No data";
document.getElementById("title").textContent = "No data";
}
})();