├── .nuget
├── NuGet.exe
├── NuGet.Config
└── NuGet.targets
├── zvt
├── zvt.ini
├── packages.config
├── app.config
├── Properties
│ └── AssemblyInfo.cs
├── zvt.csproj
└── Program.cs
├── README.md
├── zvt.sln
├── .gitattributes
└── .gitignore
/.nuget/NuGet.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/prosoftgmbh/zvt/HEAD/.nuget/NuGet.exe
--------------------------------------------------------------------------------
/zvt/zvt.ini:
--------------------------------------------------------------------------------
1 | [settings]
2 | port=COM3
3 | stopbits=2
4 | verbose=true
5 | timeout=65
6 | logs=c:\zvt_logs
--------------------------------------------------------------------------------
/zvt/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/zvt/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # zvt
2 | zvt is a tiny command line utility to control electronic payment terminals using the ZVT interface.
3 |
4 | ## Usage
5 | ### Send payment request
6 | ```
7 | zvt.exe -pay 15.32
8 | ```
9 | Sends a payment request for 15.32 EUR to the terminal. Exit code 0 means success.
10 |
11 | ### Create daily statement
12 | ```
13 | zvt.exe -endofday
14 | ```
15 | Creates a daily statement. Exit code 0 means success.
16 |
17 | ## Known issues
18 | zvt is only tested with Verifone H5000 terminals.
19 |
--------------------------------------------------------------------------------
/zvt.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "zvt", "zvt\zvt.csproj", "{A4E73CBF-C34F-4ED9-A53F-C8352A5781DE}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{2468842B-0A8D-463C-B7D8-C4AB308BC994}"
9 | ProjectSection(SolutionItems) = preProject
10 | .nuget\NuGet.Config = .nuget\NuGet.Config
11 | .nuget\NuGet.exe = .nuget\NuGet.exe
12 | .nuget\NuGet.targets = .nuget\NuGet.targets
13 | EndProjectSection
14 | EndProject
15 | Global
16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
17 | Debug|x86 = Debug|x86
18 | Release|x86 = Release|x86
19 | EndGlobalSection
20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
21 | {A4E73CBF-C34F-4ED9-A53F-C8352A5781DE}.Debug|x86.ActiveCfg = Debug|x86
22 | {A4E73CBF-C34F-4ED9-A53F-C8352A5781DE}.Debug|x86.Build.0 = Debug|x86
23 | {A4E73CBF-C34F-4ED9-A53F-C8352A5781DE}.Release|x86.ActiveCfg = Release|x86
24 | {A4E73CBF-C34F-4ED9-A53F-C8352A5781DE}.Release|x86.Build.0 = Release|x86
25 | EndGlobalSection
26 | GlobalSection(SolutionProperties) = preSolution
27 | HideSolutionNode = FALSE
28 | EndGlobalSection
29 | EndGlobal
30 |
--------------------------------------------------------------------------------
/zvt/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // Allgemeine Informationen über eine Assembly werden über die folgenden
6 | // Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
7 | // die mit einer Assembly verknüpft sind.
8 | [assembly: AssemblyTitle("zvt")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("ProSoft Software-Entwicklung GmbH")]
12 | [assembly: AssemblyProduct("zvt")]
13 | [assembly: AssemblyCopyright("Copyright © ProSoft Software-Entwicklung GmbH 2021")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
18 | // für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
19 | // COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
20 | [assembly: ComVisible(false)]
21 |
22 | // Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
23 | [assembly: Guid("1be89206-83af-4c85-aad3-8b4cce336ef4")]
24 |
25 | // Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
26 | //
27 | // Hauptversion
28 | // Nebenversion
29 | // Buildnummer
30 | // Revision
31 | //
32 | // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
33 | // übernehmen, indem Sie "*" eingeben:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.1")]
36 | [assembly: AssemblyFileVersion("1.0.0.1")]
37 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/zvt/zvt.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | x86
6 | 8.0.30703
7 | 2.0
8 | {A4E73CBF-C34F-4ED9-A53F-C8352A5781DE}
9 | Exe
10 | Properties
11 | zvt
12 | zvt
13 | v4.0
14 | Client
15 | 512
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | ..\
25 | true
26 |
27 |
28 | x86
29 | true
30 | full
31 | false
32 | bin\Debug\
33 | DEBUG;TRACE
34 | prompt
35 | 4
36 |
37 |
38 | x86
39 | pdbonly
40 | true
41 | bin\Release\
42 | TRACE
43 | prompt
44 | 4
45 |
46 |
47 |
48 | ..\packages\ini-parser.2.5.2\lib\net20\INIFileParser.dll
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 | Always
61 |
62 |
63 |
64 |
65 |
66 |
67 | This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
68 |
69 |
70 |
71 |
78 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | project.fragment.lock.json
46 | artifacts/
47 |
48 | *_i.c
49 | *_p.c
50 | *_i.h
51 | *.ilk
52 | *.meta
53 | *.obj
54 | *.pch
55 | *.pdb
56 | *.pgc
57 | *.pgd
58 | *.rsp
59 | *.sbr
60 | *.tlb
61 | *.tli
62 | *.tlh
63 | *.tmp
64 | *.tmp_proj
65 | *.log
66 | *.vspscc
67 | *.vssscc
68 | .builds
69 | *.pidb
70 | *.svclog
71 | *.scc
72 |
73 | # Chutzpah Test files
74 | _Chutzpah*
75 |
76 | # Visual C++ cache files
77 | ipch/
78 | *.aps
79 | *.ncb
80 | *.opendb
81 | *.opensdf
82 | *.sdf
83 | *.cachefile
84 | *.VC.db
85 | *.VC.VC.opendb
86 |
87 | # Visual Studio profiler
88 | *.psess
89 | *.vsp
90 | *.vspx
91 | *.sap
92 |
93 | # TFS 2012 Local Workspace
94 | $tf/
95 |
96 | # Guidance Automation Toolkit
97 | *.gpState
98 |
99 | # ReSharper is a .NET coding add-in
100 | _ReSharper*/
101 | *.[Rr]e[Ss]harper
102 | *.DotSettings.user
103 |
104 | # JustCode is a .NET coding add-in
105 | .JustCode
106 |
107 | # TeamCity is a build add-in
108 | _TeamCity*
109 |
110 | # DotCover is a Code Coverage Tool
111 | *.dotCover
112 |
113 | # NCrunch
114 | _NCrunch_*
115 | .*crunch*.local.xml
116 | nCrunchTemp_*
117 |
118 | # MightyMoose
119 | *.mm.*
120 | AutoTest.Net/
121 |
122 | # Web workbench (sass)
123 | .sass-cache/
124 |
125 | # Installshield output folder
126 | [Ee]xpress/
127 |
128 | # DocProject is a documentation generator add-in
129 | DocProject/buildhelp/
130 | DocProject/Help/*.HxT
131 | DocProject/Help/*.HxC
132 | DocProject/Help/*.hhc
133 | DocProject/Help/*.hhk
134 | DocProject/Help/*.hhp
135 | DocProject/Help/Html2
136 | DocProject/Help/html
137 |
138 | # Click-Once directory
139 | publish/
140 |
141 | # Publish Web Output
142 | *.[Pp]ublish.xml
143 | *.azurePubxml
144 | # TODO: Comment the next line if you want to checkin your web deploy settings
145 | # but database connection strings (with potential passwords) will be unencrypted
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
150 | # checkin your Azure Web App publish settings, but sensitive information contained
151 | # in these scripts will be unencrypted
152 | PublishScripts/
153 |
154 | # NuGet Packages
155 | *.nupkg
156 | # The packages folder can be ignored because of Package Restore
157 | **/packages/*
158 | # except build/, which is used as an MSBuild target.
159 | !**/packages/build/
160 | # Uncomment if necessary however generally it will be regenerated when needed
161 | #!**/packages/repositories.config
162 | # NuGet v3's project.json files produces more ignoreable files
163 | *.nuget.props
164 | *.nuget.targets
165 |
166 | # Microsoft Azure Build Output
167 | csx/
168 | *.build.csdef
169 |
170 | # Microsoft Azure Emulator
171 | ecf/
172 | rcf/
173 |
174 | # Windows Store app package directories and files
175 | AppPackages/
176 | BundleArtifacts/
177 | Package.StoreAssociation.xml
178 | _pkginfo.txt
179 |
180 | # Visual Studio cache files
181 | # files ending in .cache can be ignored
182 | *.[Cc]ache
183 | # but keep track of directories ending in .cache
184 | !*.[Cc]ache/
185 |
186 | # Others
187 | ClientBin/
188 | ~$*
189 | *~
190 | *.dbmdl
191 | *.dbproj.schemaview
192 | *.jfm
193 | *.pfx
194 | *.publishsettings
195 | node_modules/
196 | orleans.codegen.cs
197 |
198 | # Since there are multiple workflows, uncomment next line to ignore bower_components
199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
200 | #bower_components/
201 |
202 | # RIA/Silverlight projects
203 | Generated_Code/
204 |
205 | # Backup & report files from converting an old project file
206 | # to a newer Visual Studio version. Backup files are not needed,
207 | # because we have git ;-)
208 | _UpgradeReport_Files/
209 | Backup*/
210 | UpgradeLog*.XML
211 | UpgradeLog*.htm
212 |
213 | # SQL Server files
214 | *.mdf
215 | *.ldf
216 |
217 | # Business Intelligence projects
218 | *.rdl.data
219 | *.bim.layout
220 | *.bim_*.settings
221 |
222 | # Microsoft Fakes
223 | FakesAssemblies/
224 |
225 | # GhostDoc plugin setting file
226 | *.GhostDoc.xml
227 |
228 | # Node.js Tools for Visual Studio
229 | .ntvs_analysis.dat
230 |
231 | # Visual Studio 6 build log
232 | *.plg
233 |
234 | # Visual Studio 6 workspace options file
235 | *.opt
236 |
237 | # Visual Studio LightSwitch build output
238 | **/*.HTMLClient/GeneratedArtifacts
239 | **/*.DesktopClient/GeneratedArtifacts
240 | **/*.DesktopClient/ModelManifest.xml
241 | **/*.Server/GeneratedArtifacts
242 | **/*.Server/ModelManifest.xml
243 | _Pvt_Extensions
244 |
245 | # Paket dependency manager
246 | .paket/paket.exe
247 | paket-files/
248 |
249 | # FAKE - F# Make
250 | .fake/
251 |
252 | # JetBrains Rider
253 | .idea/
254 | *.sln.iml
255 |
256 | # CodeRush
257 | .cr/
258 |
259 | # Python Tools for Visual Studio (PTVS)
260 | __pycache__/
261 | *.pyc
--------------------------------------------------------------------------------
/.nuget/NuGet.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | false
8 |
9 |
10 | false
11 |
12 |
13 | true
14 |
15 |
16 | false
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
29 |
30 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
31 |
32 |
33 |
34 |
35 | $(SolutionDir).nuget
36 |
37 |
38 |
39 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config
40 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config
41 |
42 |
43 |
44 | $(MSBuildProjectDirectory)\packages.config
45 | $(PackagesProjectConfig)
46 |
47 |
48 |
49 |
50 | $(NuGetToolsPath)\NuGet.exe
51 | @(PackageSource)
52 |
53 | "$(NuGetExePath)"
54 | mono --runtime=v4.0.30319 "$(NuGetExePath)"
55 |
56 | $(TargetDir.Trim('\\'))
57 |
58 | -RequireConsent
59 | -NonInteractive
60 |
61 | "$(SolutionDir) "
62 | "$(SolutionDir)"
63 |
64 |
65 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)
66 | $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols
67 |
68 |
69 |
70 | RestorePackages;
71 | $(BuildDependsOn);
72 |
73 |
74 |
75 |
76 | $(BuildDependsOn);
77 | BuildPackage;
78 |
79 |
80 |
81 |
82 |
83 |
84 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
99 |
100 |
103 |
104 |
105 |
106 |
108 |
109 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
141 |
142 |
143 |
144 |
145 |
--------------------------------------------------------------------------------
/zvt/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Text;
6 | using System.IO.Ports;
7 | using System.Threading;
8 | using IniParser;
9 | using IniParser.Model;
10 | using System.Reflection;
11 |
12 | namespace zvt
13 | {
14 | class Program
15 | {
16 | static string debugFile;
17 |
18 | public const byte DLE = 0x10;
19 | public const byte ACK = 0x06;
20 | public const byte NAK = 0x15;
21 |
22 | static string portName = "com3";
23 | static string logs = @"z:\zvt_logs";
24 |
25 | static bool verbose = false;
26 |
27 | static int timeout = 60000;
28 |
29 | static string stopbits = "1";
30 |
31 | static readonly Dictionary StatusInformationFieldLengths = new Dictionary()
32 | {
33 | // Amount
34 | { 0x04, 6 },
35 | // Trace
36 | { 0x0B, 3 },
37 | // Orig. trace
38 | { 0x37, 3 },
39 | // Time
40 | { 0x0C, 3 },
41 | // Date
42 | { 0x0D, 2 },
43 | // Expiry date
44 | { 0x0E, 2 },
45 | // Sequence number
46 | { 0x17, 2 },
47 | // Payment type
48 | { 0x19, 1 },
49 | // PAN/EF_ID
50 | { 0x22, 0 /*LLVAR*/ },
51 | // Terminal-ID
52 | { 0x29, 4 },
53 | // AID
54 | { 0x3B, 8 },
55 | // CC
56 | { 0x49, 2 },
57 | // Blocked goods groups
58 | { 0x4C, 0 /*LLVAR*/ },
59 | // Receipt no.
60 | { 0x87, 2 },
61 | // Card type
62 | { 0x8A, 1 },
63 | // Card type ID
64 | { 0x8C, 1 },
65 | // Payment record
66 | { 0x9A, 103 },
67 | // AID parameter
68 | { 0xBA, 5 },
69 | // VU number
70 | { 0x2A, 15 },
71 | // Additional text
72 | { 0x3C, 0 /*LLLVAR*/ },
73 | // Result code AS
74 | { 0xA0, 1 },
75 | // Turnover no
76 | { 0x88, 3 },
77 | // Card name
78 | { 0x8B, 0 /*LLVAR*/ },
79 | // Additional data
80 | { 0x06, 0 /*TLV*/ },
81 | };
82 |
83 | static readonly Dictionary VariableLengthStatusInformationFields = new Dictionary()
84 | {
85 | // PAN/EF_ID
86 | { 0x22, VariableLengthType.LLVAR },
87 | // Blocked goods groups
88 | { 0x4C, VariableLengthType.LLVAR },
89 | // Additional text
90 | { 0x3C, VariableLengthType.LLLVAR },
91 | // Card name
92 | { 0x8B, VariableLengthType.LLVAR },
93 | // Additional data
94 | { 0x06, VariableLengthType.TLV },
95 | };
96 |
97 | public static void Debug(string format)
98 | {
99 | if (!verbose) return;
100 |
101 | // LOG-Verzeichnis erstellen falls notwendig
102 | if (!Directory.Exists(logs)) Directory.CreateDirectory(logs);
103 |
104 | if (debugFile == null) debugFile = Path.Combine(logs, string.Format("zvt_{0:yyyyMMddhhmmss}.log", DateTime.Now));
105 |
106 | try
107 | {
108 | File.AppendAllText(debugFile, format);
109 | }
110 | catch { }
111 | }
112 |
113 | static ushort CalcCrc2(IEnumerable data)
114 | {
115 | int crc;
116 | var t = new int[256];
117 |
118 | for (var i = 0; i < 256; i++)
119 | {
120 | crc = i;
121 |
122 | for (int j = 0; j < 8; j++)
123 | {
124 | if ((crc & 1) > 0) crc = (crc >> 1) ^ 0x8408;
125 | else crc = crc >> 1;
126 | }
127 |
128 | t[i] = crc;
129 | }
130 |
131 | crc = 0;
132 |
133 | var l = new List(data);
134 | l.Add(0x03);
135 |
136 | for (int i = 0; i < l.Count; i++)
137 | {
138 | var hb = crc >> 8;
139 | var lb = crc & 0xFF;
140 |
141 | crc = t[lb ^ l[i]] ^ hb;
142 | }
143 |
144 | return (ushort)((crc >> 8) | ((crc & 0xFF) << 8));
145 | }
146 |
147 | static byte[] DecimalToBcd(decimal value)
148 | {
149 | int v = (int)(decimal.Round(value, 2, MidpointRounding.AwayFromZero) * 100);
150 |
151 | var ret = new byte[6];
152 |
153 | for (int i = 0; i < ret.Length; i++)
154 | {
155 | ret[i] = (byte)(v % 10);
156 | v /= 10;
157 |
158 | ret[i] |= (byte)((v % 10) << 4);
159 | v /= 10;
160 | }
161 |
162 | return ret.Reverse().ToArray();
163 | }
164 |
165 | static int SendRawData(SerialPort s, IEnumerable data)
166 | {
167 | Thread.Sleep(100);
168 |
169 | //var checksum = CalcCrc(data);
170 | var checksum = CalcCrc2(data);
171 |
172 | s.Write(new byte[] { 0x10, 0x02 }, 0, 2);
173 |
174 | var l = new List();
175 | foreach (var b in data)
176 | {
177 | l.Add(b);
178 |
179 | if (b == DLE) l.Add(b);
180 | }
181 | s.Write(l.ToArray(), 0, l.Count);
182 |
183 | s.Write(new byte[] { 0x10, 0x03 }, 0, 2);
184 | //1CA2
185 | var cs2 = new byte[] { (byte)(checksum >> 8), (byte)(checksum & 0xFF) };
186 | var cs = new byte[] { 0x1C, 0xA2 };
187 | s.Write(cs2, 0, 2);
188 |
189 | var debugOutput = string.Join(" ", data.Select(i => string.Format("{0:X2}", i)));
190 | Debug("HOST -> TERM: " + debugOutput + "\r\n");
191 |
192 | Thread.Sleep(100);
193 |
194 | return s.ReadByte();
195 | }
196 |
197 | static byte[] RecvRawData(SerialPort s)
198 | {
199 | Thread.Sleep(100);
200 |
201 | var a = s.ReadByte();
202 | var b = s.ReadByte();
203 |
204 | if (a != 0x10 || b != 0x02)
205 | {
206 | Debug("ERROR : " + string.Join(" ", new int[] { a, b }.Select(i => string.Format("{0:X2}", i))) + "\r\n");
207 |
208 | throw new Exception();
209 | }
210 |
211 | var c0 = (byte)s.ReadByte();
212 | var c1 = (byte)s.ReadByte();
213 | var length = (byte)s.ReadByte();
214 |
215 | ushort actualLength = length;
216 |
217 | var arrayOffset = 3;
218 |
219 | var lengthArray = new byte[1] { length };
220 |
221 | if (length == 0xFF)
222 | {
223 | var loByte = (byte)s.ReadByte();
224 | var hiByte = (byte)s.ReadByte();
225 |
226 | actualLength = (ushort)((hiByte << 8) + loByte);
227 | arrayOffset = 5;
228 | lengthArray = new byte[3] { length, loByte, hiByte };
229 | }
230 |
231 | var data = new byte[arrayOffset + actualLength];
232 |
233 | data[0] = c0;
234 | data[1] = c1;
235 |
236 | for (int i = 0; i < lengthArray.Length; i++)
237 | {
238 | var value = lengthArray[i];
239 |
240 | data[2 + i] = value;
241 | }
242 |
243 | for (int i = 0; i < actualLength; i++)
244 | {
245 | var r = (byte)s.ReadByte();
246 |
247 | if (r == DLE) r = (byte)s.ReadByte();
248 |
249 | data[arrayOffset + i] = r;
250 | }
251 |
252 | var checksum = s.ReadByte() << 24 + s.ReadByte() << 16 + s.ReadByte() << 8 + s.ReadByte();
253 |
254 | Thread.Sleep(100);
255 | s.Write(new byte[] { 6 }, 0, 1);
256 |
257 | var debugOutput = string.Join(" ", data.Select(i => string.Format("{0:X2}", i)));
258 | Debug("TERM -> HOST: " + debugOutput + "\r\n");
259 |
260 | return data;
261 | }
262 |
263 | static bool Pay(SerialPort s, decimal amount, decimal cashbackAmount)
264 | {
265 | byte totalLength = 0x12;
266 |
267 | var payCommand = new List(new byte[] { 0x06, 0x01, totalLength, 0x04 });
268 | payCommand.AddRange(DecimalToBcd(amount));
269 |
270 | // TLV-Container Start
271 | payCommand.Add(0x06);
272 | payCommand.Add(0x09);
273 | // Cashback-Tag
274 | payCommand.Add(0x1F);
275 | payCommand.Add(0x25);
276 | // Length
277 | payCommand.Add(0x06);
278 | // Amount in BCD
279 | payCommand.AddRange(DecimalToBcd(cashbackAmount));
280 |
281 | SendRawData(s, payCommand);
282 | RecvRawData(s);
283 |
284 | var pay = RecvRawData(s);
285 | SendRawData(s, new byte[] { 0x80, 0x00, 0x00 });
286 |
287 | // Wenn length = 0xFF ist, sind 3 bytes für die länge und pay[6] muss auf 0 geprüft werden statt pay[4]
288 | if (pay[0] == 0x04 && pay[1] == 0x0F && (pay[2] == 0xFF && pay[6] == 0 || pay[2] != 0xFF && pay[4] == 0))
289 | {
290 | var lengthOffset = pay[2] == 0xFF ? 2 : 0;
291 | var extendedFailed = (pay.Length > 8 + lengthOffset) && pay[5 + lengthOffset] == 0x06 && pay[7 + lengthOffset] == 0x1F && pay[8 + lengthOffset] == 0x16;
292 |
293 | var isSuccessful = false;
294 |
295 | try
296 | {
297 | var r = RecvRawData(s);
298 | // Success = 0x06 0F 00
299 | if (r[0] == 0x06 && r[1] == 0x0F && r[2] == 0x00) isSuccessful = true;
300 |
301 | SendRawData(s, new byte[] { 0x80, 0x00, 0x00 });
302 | }
303 | catch (Exception ex)
304 | {
305 | if (ex.Message != null) Debug($"ERROR : {ex.Message}");
306 | }
307 |
308 | if (extendedFailed) return false;
309 |
310 | if (isSuccessful)
311 | {
312 | WritePayResultJson(pay, 5);
313 | }
314 |
315 | return isSuccessful;
316 | }
317 |
318 | return false;
319 | }
320 |
321 | static void WritePayResultJson(byte[] message, int startIndex)
322 | {
323 | byte? cardType = null;
324 |
325 | for (int i = startIndex; i < message.Length; i++)
326 | {
327 | var bmpCode = message[i];
328 |
329 | if (bmpCode == 0x8A)
330 | {
331 | cardType = message[i + 1];
332 | break;
333 | }
334 | else
335 | {
336 | var length = StatusInformationFieldLengths[bmpCode];
337 |
338 | // Ist es ein Feld mit variabler Länge?
339 | if (length == 0)
340 | {
341 | var variableLengthType = VariableLengthStatusInformationFields[bmpCode];
342 |
343 | switch (variableLengthType)
344 | {
345 | // Beispiel LLVAR: F1 F0 = 10
346 | case VariableLengthType.LLVAR:
347 | {
348 | var f1 = message[i + 1];
349 | var f2 = message[i + 2];
350 |
351 | f1 &= 0x0F;
352 | f2 &= 0x0F;
353 |
354 | length = (byte)((f1 * 10 + f2) + 2);
355 | break;
356 | }// Beispiel LLLVAR: F1 F0 F3 = 103
357 | case VariableLengthType.LLLVAR:
358 | {
359 | var f1 = message[i + 1];
360 | var f2 = message[i + 2];
361 | var f3 = message[i + 3];
362 |
363 | f1 &= 0x0F;
364 | f2 &= 0x0F;
365 | f3 &= 0x0F;
366 |
367 | length = (byte)((f1 * 100 + f2 * 10 + f3) + 3);
368 |
369 | break;
370 | }
371 | case VariableLengthType.TLV:
372 | {
373 | // TLV-Container sollten erst kommen sobald wir unsere Information haben
374 | return;
375 | }// Bei nicht definiertem Fall müssen wir abbrechen
376 | default: return;
377 | }
378 | }
379 |
380 | i += length;
381 | }
382 | }
383 |
384 | // Wenn wir keinen Karten-Typen bekommen haben (warum auch immer) brechen wir ab
385 | if (cardType == null) return;
386 |
387 | var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "result.json");
388 |
389 | var json = "{\"CardType\":" + $"\"{cardType}\"" + "}";
390 |
391 | File.WriteAllText(path, json);
392 | }
393 |
394 | static bool EndOfDay(SerialPort s)
395 | {
396 | int reg = SendRawData(s, new byte[] { 0x06, 0x50, 0x03, 0x00, 0x00, 0x00 });
397 |
398 | if (reg != 0x06) return false;
399 |
400 | var reg_recv = RecvRawData(s);
401 |
402 | if (reg_recv[0] != 0x80 || reg_recv[1] != 0x00 || reg_recv[2] != 0x00) return false;
403 |
404 | var pay = RecvRawData(s);
405 | SendRawData(s, new byte[] { 0x80, 0x00, 0x00 });
406 |
407 | return pay[0] == 0x04 && pay[1] == 0x0F;
408 | }
409 |
410 | static void Registration(SerialPort s)
411 | {
412 | SendRawData(s, new byte[] { 0x06, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00 });
413 | RecvRawData(s);
414 |
415 | RecvRawData(s);
416 | SendRawData(s, new byte[] { 0x80, 0x00, 0x00 });
417 | }
418 |
419 | static void LogOff(SerialPort s)
420 | {
421 | int r = SendRawData(s, new byte[] { 0x06, 0x02, 0x00 });
422 | var a = RecvRawData(s);
423 | }
424 |
425 | static int Main(string[] args)
426 | {
427 | var parser = new FileIniDataParser();
428 |
429 | IniData data;
430 |
431 | var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
432 |
433 | try
434 | {
435 | data = parser.ReadFile(Path.Combine(path, "zvt.ini"));
436 |
437 | portName = data["settings"]["port"];
438 |
439 | stopbits = data["settings"]["stopbits"];
440 |
441 | verbose = bool.Parse(data["settings"]["verbose"]);
442 |
443 | timeout = int.Parse(data["settings"]["timeout"]) * 1000;
444 |
445 | logs = data["settings"]["logs"];
446 | }
447 | catch
448 | {
449 | Console.WriteLine("ERROR: Unable to parse configuration file 'zvt.ini'");
450 |
451 | return -1;
452 | }
453 |
454 | int exitCode = 2;
455 |
456 | var command = args.Length == 0 ? null : args[0].ToLower();
457 |
458 | if (command != "-pay" && command != "-endofday")
459 | {
460 | Console.WriteLine("Usage: zvt.exe -pay amount");
461 | Console.WriteLine("Usage: zvt.exe -endofday");
462 |
463 | return 1;
464 | }
465 |
466 | SerialPort s = null;
467 |
468 | try
469 | {
470 | StopBits sb;
471 |
472 | switch (stopbits.Trim())
473 | {
474 | case "0": sb = StopBits.None; break;
475 | case "1": sb = StopBits.One; break;
476 | case "1.5": sb = StopBits.OnePointFive; break;
477 | case "2": sb = StopBits.Two; break;
478 | default: throw new ArgumentOutOfRangeException("stopbits");
479 | }
480 |
481 | s = new SerialPort(portName, 9600, Parity.None, 8, sb)
482 | {
483 | ReceivedBytesThreshold = 1,
484 | ReadTimeout = timeout
485 | };
486 |
487 | s.Open();
488 |
489 | int reg = SendRawData(s, new byte[] { 0x06, 0x00, 0x00 });
490 | var reg_recv = RecvRawData(s);
491 |
492 | /*var r = RecvRawData(s);
493 | SendRawData(s, new byte[] { 0x80, 0x00, 0x00 });*/
494 |
495 | /*var x = SendRawData(s, new byte[] { 0x06, 0xE0, 0x02, 0xF0, 0x00 });
496 | var y = RecvRawData(s);*/
497 |
498 | /*var x = SendRawData(s, new byte[] { 0x06, 0xB0, 0x00 });
499 | var y = RecvRawData(s);*/
500 |
501 | switch (command)
502 | {
503 | case "-pay":
504 | {
505 | Registration(s);
506 |
507 | var paymentAmount = decimal.Parse(args[1], System.Globalization.CultureInfo.InvariantCulture);
508 |
509 | var cashbackAmount = 0.0m;
510 |
511 | if (args.Length > 2)
512 | {
513 | var secondCommand = args[2].ToLower();
514 |
515 | if (secondCommand == "-cashback")
516 | {
517 | cashbackAmount = decimal.Parse(args[3], System.Globalization.CultureInfo.InvariantCulture);
518 | }
519 | }
520 |
521 | if (Pay(s, paymentAmount, cashbackAmount) == true) exitCode = 0;
522 | break;
523 | }
524 | case "-endofday":
525 | Registration(s);
526 |
527 | if (EndOfDay(s) == true) exitCode = 0;
528 |
529 | //LogOff(s);
530 | break;
531 | }
532 | }
533 | catch (Exception e)
534 | {
535 | Debug(e.Message + "\r\n" + e.StackTrace);
536 |
537 | Console.WriteLine("" + e.Message);
538 | }
539 | finally
540 | {
541 | if (s != null) s.Close();
542 | }
543 |
544 | Console.WriteLine("Exit code = " + exitCode);
545 |
546 | Debug("Exit code = " + exitCode);
547 |
548 | File.WriteAllText(Path.Combine(path, "result.txt"), exitCode.ToString());
549 |
550 | return exitCode;
551 | }
552 | }
553 |
554 | public enum VariableLengthType
555 | {
556 | LLVAR = 0,
557 | LLLVAR = 1,
558 | TLV = 2
559 | }
560 | }
--------------------------------------------------------------------------------