POS系統結合iPASS一卡通範例

12219570_10153067567121541_5370508928219483252_n
12208306_10153067535151541_1778219125832501158_n
一卡通(iPASS)是與悠遊卡(EasyCard)都是台灣的電子票證智慧卡,二者都是使用RFID(菲利浦的MIFARE)技術。今天來分享大致的技術~
首先,您得先到一卡通官方網站中的”加入特約商店“,提供相關資料向一卡通票證公司申請,並向一卡通公司申請測試機器及卡片。
審核通過後,iPASS一卡通公司會提供一台一卡通的測試機器,以及相關的文件及SDK。SDK與之前刷卡系統神似~
主要是輸入長度62的in.txt;輸出的out.txt則為109~928(有無交易記錄)。
1447167886093

POS系統介接與信用卡機連線的作法

Delphi或C#與信用卡連接的方式,有直接透過com port通訊或是透過呼叫exe的方式,利用in.txt、out.txt做溝通。然後回傳信用卡卡號/授權碼/刷卡金額等資訊。
示範一下Delphi與C#如何做信用卡線上刷卡。
Delphi

  public
    { Public declarations }
    ExecInfo : TShellExecuteInfo;   // use shellapi
    i:integer;
  end;
procedure TForm1.Button1Click(Sender: TObject);
var 
  s:String;
  ts:TStringlist;
begin
  ZeroMemory(@ExecInfo,SizeOf(ExecInfo));
  with ExecInfo do begin
    cbSize := SizeOf(ExecInfo);
    fMask := SEE_MASK_NOCLOSEPROCESS;
    lpVerb := 'open';
    lpFile := 'ecr.exe'; 
      Wnd := self.Handle;
    nShow := SW_HIDE; 
  end;
  s:='xxxxxxxxxxxxxxxxxxxx'; // 填上信用卡的溝通格式
  ts := Tstringlist.Create;
  ts.Clear;
  ts.Add(s);
  ts.SaveToFile('in.dat');
  ts.Free; 
  ShellExecuteEx(@ExecInfo);
  deletefile('out.dat');
  caption := '刷卡中...';
  timer1.Enabled := True;
  i:=0;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var ts1:Tstringlist;
sstatus,smoney,scard,sappno:String;
begin
  i:=i+1;
  if fileexists('out.dat') then begin
     Timer1.Enabled := false;
     ts1 := Tstringlist.Create;
     ts1.LoadFromFile('out.dat');
     if ts1.Count >0 then begin
       // 讀入檔案,解析格式
     end;
     ts1.Free;
  end;
end;
string dir = System.Windows.Forms.Application.StartupPath;
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        private void uForm1_Load(object sender, EventArgs e)
        {
            try
            {
                if (File.Exists(dir + "/out.txt")) //刪除out.txt
                    File.Delete(dir + "/out.txt");
                string code = "";  // in.txt格式
                using (StreamWriter sw = new StreamWriter(dir + "/in.txt"))   //小寫TXT     
                sw.Write(code);    
                IntPtr PDC = FindWindow(null, "ecr");  //開啟PosDataCom
                if (PDC == (IntPtr)0)
                {
                    try
                    {
                        Process p = new Process();
                        p.StartInfo.FileName = dir + "/ecrnccc.exe";
                        p.StartInfo.WorkingDirectory = dir;
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardInput = true;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.RedirectStandardError = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.Start();
                    }
                    catch (Exception exp)
                    {
                        return;
                    }
                }
               
               
                this.timer1.Enabled = true;
               
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (times == 5)
            {                
                times = 0;
                i++;
                if (i > 4)
                {
                    i = 0;
                    try
                {
                if (File.Exists(dir + "/out.txt"))
                {
                   
                    using (StreamReader sr = new StreamReader(dir + "/out.txt"))     //小寫TXT
                    {
                        String line;
                      
                        if ((line = sr.ReadLine()) != null)
                        {
                          // 解析
                        }
                        else
                            return false;
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;           
            }
                }
            }
            times++;
        }

讓佛祖、耶酥一起保佑程式碼無BUG,比乖乖有效?

微博上看到各國機房為了確保主機穩定不出怪事,請出法師、大師、神父到機房作法、禱告。

台灣最聞名的就是放乖乖了,然後要記得是綠燈(正常)的椰子乖乖:)

最近程式設計師流行直接寫在CODE裡,像上次寫的機器上除了要擺乖乖外,程式碼也該加上佛祖保佑,今天更出現耶穌版本。

[pascal]
// |~~~~~~~|
// | |
// | |
// | |
// | |
// | |
// |~.\\\_\~~~~~~~~~~~~~~xx~~~ ~~~~~~~~~~~~~~~~~~~~~/_//;~|
// | \ o \_ ,XXXXX), _..-~ o / |
// | ~~\ ~-. XXXXX`)))), _.–~~ .-~~~ |
// ~~~~~~~`\ ~\~~~XXX’ _/ ‘;)) |~~~~~~..-~ _.-~ ~~~~~~~
// `\ ~~–`_\~\, ;;;\)__.—.~~~ _.-~
// ~-. `:;;/;; \ _..-~~
// ~-._ `” /-~-~
// `\ / /
// | , | |
// | ‘ / |
// \/; |
// ;; |
// `; . |
// |~~~—–…..|
// | \ \
// | /\~~–…__ |
// (| `\ __-\|
// || \_ /~ |
// |) \~-‘ |
// | | \ ‘
// | | \ :
// \ | | |
// | ) ( )
// \ /; /\ |
// | |/ |
// | | |
// \ .’ ||
// | | | |
// ( | | |
// | \ \ |
// || o `.)|
// |`\\\\) |
// | |
// | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 耶穌保佑 永無 BUG
[/pascal]
同場加映
[pascal]
//
// _oo0oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/`—‘\___
// .’ \\| |// ‘.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ – /// | |
// | \_| ”\—/” |_/ |
// \ .-\__ ‘-‘ ___/-. /
// ___’. .’ /–.–\ `. .’___
// ."" ‘< `.___\_<|>_/___.’ >’ "".
// | | : `- \`.;`\ _ /`;.`/ – ` : | |
// \ \ `_. \_ __\ /__ _/ .-` / /
// =====`-.____`.___ \_____/___.-`___.-‘=====
// `=—=’
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 佛祖保佑 永無bug
//
//***************************************************
[/pascal]