📄 readcard.c
字号:
/*******************************************************************/
/* POS-MIS系统 --- 商场 */
/* 工商银行昆明市分行 - 昆百大集团 */
/* 杭州新利软件有限公司 */
/* <READCARD.C> */
/* ---- 前台读卡程序 */
/* */
/* Modification History: */
/* Author : LiuSong */
/* Date : 2001.04 */
/* Action : Modify Write */
/* */
/*******************************************************************/
#include <stdio.h>
#include "card.h"
#define PINKEY "FDC32F7756DB1E76"
#define CARDHEADLEN 7
PTOH ptoh;
HTOP htop;
CARDOUT CardRej;
char result[256], cmdline[100];
char track[142];
char _track3[105];
char CardNo[17];
int _DebugFlag=0, _DemoFlag=0, tcp_sd;
int GetDateAndTime(char *str);
/*******************************************************************
main() -- 主程序
usage: readcard [flag] [posid] [operid] [trcode] [amount]
[unitcardflag]
********************************************************************/
main ( int argc, char **argv )
{
int i;
char trcode, no[8];
FILE *nofp, *savefp;
time_t beg;
memset(&ptoh, 0, sizeof(PTOH));
memset(&htop, 0, sizeof(HTOP));
memset(&CardRej, 0, sizeof(CardRej));
strcpy(ptoh.PosId, argv[1]);
strcpy(ptoh.OperId, argv[2]);
ptoh.TrCode = argv[3][0];
trcode = ptoh.TrCode;
if (trcode == 'Q' || trcode == 'B')
strcpy(ptoh.Amount, "0.0");
else
strcpy(ptoh.Amount, argv[4]);
printf("\ndebug 1\n");
// ptoh.UnitCardFlag = argv[6][0];
if ( GetCardno(ptoh.Track2, ptoh.Track3) != 0 ) {
strcpy(htop.RejCode, "A1");
strcpy(htop.ErrDisp, "无效卡! 可能刷卡不正确");
PubProcess();
return -1;
}
if( strlen(ptoh.Track3) > 104 )
ptoh.Track3[104] = 0;
if( strlen(ptoh.Track3) <= 100)
ptoh.Track3[0] = 0;
GetTime();
memcpy(track, ptoh.Track2, 38);
if(strlen(ptoh.Track2) > 37 || strlen(ptoh.Track2) < 20) {
strcpy(_track3, ptoh.Track3);
memcpy(ptoh.Track3, ptoh.Track2, 37);
memcpy(ptoh.Track3 + 37, _track3, 1);
memcpy(ptoh.Track3 + 38, _track3, 66);
ptoh.Track3[104] = 0;
ptoh.Track2[0] = 0;
}
if(strlen(ptoh.Track2))
memcpy(CardNo, ptoh.Track2, 16);
else
memcpy(CardNo, ptoh.Track3, 16);
CardNo[16] = 0;
if ( (trcode==POS_SALE || trcode==POS_VOID || trcode==POS_INQ) ) {
if ( GetHolderpin() ) {
strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
return -1;
}
else
strcpy(ptoh.Pin, result + 1);
}
GetMAC();
nofp = fopen("nofile", "r");
if ( nofp == NULL ) {
strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
return -1;
}
if( fgets(no, 7, nofp) == NULL ) {
strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
return -1;
}
if(memcmp(no + 3, "998", 3) == 0)
memcpy(no + 3, "000", 3);
no[6] = 0;
fclose(nofp);
nofp = fopen("nofile", "w");
fprintf(nofp, "%06ld", (atol(no) + 1));
fclose(nofp);
sprintf(ptoh.Flag, "%s", no);
ptoh.Status = '0';
ptoh.TrCode = trcode;
savefp = fopen("savefile", "w");
fseek(savefp, 0L, SEEK_SET);
fwrite(&ptoh, sizeof(ptoh), 1, savefp);
fclose(savefp);
return 0;
}
int GetCardno(char *track2, char *track3)
{
int i,j=0;
char card_no[17];
memset(cmdline, 0, sizeof(cmdline));
memset(result, 0, sizeof(result));
for (i=0; ; i++)
{
if (i == 3)
{
cmdline[0] = SC552_SF_RESET;
cmdline[1] = 0;
posio(cmdline, SC552_COM, SC552_Time_Short, SC552_Type_1);
return -1;
}
cmdline[0] = SC552_SF_RESET;
cmdline[1] = 0;
posio(cmdline, SC552_COM, SC552_Time_Short, SC552_Type_1);
cmdline[0] = SC552_GET_CARDNO;
cmdline[1] = 0;
strcpy(result, posio(cmdline, SC552_COM, SC552_Time_Long,
SC552_Type_2));
if (strlen(result) >= 104 && result[0] == SC552_GET_CARDNO)
break;
for(j=1;j<1000;j++) /*equal sleep(1)*/
{};
/* sleep(1);20000720 del*/
}
sscanf(result+1, "%s %s", track2, track3);
return 0;
}
/*******************************************************************
GetHolderpin() --
取持卡人密码
********************************************************************/
int GetHolderpin()
{
int i, j;
memset(cmdline, 0, sizeof(cmdline));
memset(result, 0, sizeof(result));
cmdline[0] = SC552_LOAD_KEY;
memcpy(cmdline + 1, "00", 2);
memcpy(cmdline + 3, PINKEY, 16);
cmdline[19] = 0;
strcpy( result, posio( cmdline, SC552_COM, SC552_Time_Long,
SC552_Type_2 ));
cmdline[ 0 ] = SC552_BEEP_NORMAL;
cmdline[ 1 ] = 0;
strcpy( result, posio( cmdline, SC552_COM, SC552_Time_Long,
SC552_Type_2 ));
for (i = 0; ; i++) {
if (i == 3)
return -1;
if (i == 0)
{}
else
{}
cmdline[0] = SC552_GET_X98_PIN;
memcpy(cmdline + 1, "00", 2);
memcpy(cmdline + 3, CardNo, 16);
cmdline[19] = 0;
strcpy(result, posio(cmdline, SC552_COM, SC552_Time_Long,
SC552_Type_2));
if (strlen(result) == 17 && result[0] == SC552_GET_X98_PIN)
break;
}
return 0;
}
static void HEX_2_DSP(char *hex, char *dsp, int count)
{
int i;
char ch;
for(i = 0; i < count; i++)
{
ch=(hex[i]&0xf0)>>4;
dsp[i*2]=(ch>9)?ch+0x41-10:ch+0x30;
ch=hex[i]&0xf;
dsp[i*2+1]=(ch>9)?ch+0x41-10:ch+0x30;
}
}
static void DO_xor(char *str1, char *str2)
{
int i;
for(i=0; i<8; i++)
str1[i] ^= str2[i];
}
int GetMAC()
{
int i, len;
#define BMJYM "1027LXCZ"
memset(cmdline, 0, sizeof(cmdline));
strcpy(cmdline, "00");
strcat(cmdline, ptoh.TrDate);
strcat(cmdline, ptoh.TrTime);
strcat(cmdline, ptoh.Amount);
strcat(cmdline, ptoh.Track2);
len = strlen(cmdline);
memcpy(result, cmdline, 8);
for(i = 1; i < (len -1)/8 + 1; i++ )
DO_xor(result, cmdline+i*8);
DO_xor(result, BMJYM);
HEX_2_DSP(result, ptoh.Mac, 8);
ptoh.Mac[16] = 0;
return 0;
}
/*******************************************************************
WritePface() -- 写交换接口文件
********************************************************************/
int WritePface(HTOP htop)
{
FILE *fp;
int i;
strcpy (CardRej.RejCode, htop.RejCode);
strcpy (CardRej.ErrDisp, htop.ErrDisp);
strcpy (CardRej.CardNo, htop.CardNo);
strcpy (CardRej.Expiry, htop.Expiry);
strcpy (CardRej.AuthNo, htop.AuthNo);
strcpy (CardRej.PosSer, htop.PosSer);
strcpy (CardRej.VposSer, htop.VposSer);
// strcpy (CardRej.HostSer, htop.HostSer);
CardRej.BankId = htop.BankId;
strcpy (CardRej.BankName, htop.BankName);
strcpy (CardRej.CardName, htop.CardName);
CardRej.PrtTimes = htop.PrtTimes;
CardRej.AwardClass = htop.AwardClass;
strcpy (CardRej.Amount, htop.Amount);
if ((fp = fopen("rej.dat", "w")) == NULL)
return -1;
fwrite(&CardRej, sizeof(CardRej), 1, fp);
fclose(fp);
return 0;
}
void PubProcess()
{
WritePface(htop);
return;
}
GetTrackInfo(char *track2, char *track3)
{
FILE *fp;
char buf[200];
if ((fp = fopen(TRACK_FILE, "r")) == NULL)
return -1;
memset(buf, 0, sizeof(buf));
fread(buf, 141, 1, fp);
memcpy(track2, buf, 37); track2[37] = 0;
memcpy(track3, buf+37, 104); track3[104] = 0;
fclose(fp);
return 0;
}
WriteTrackInfo(char *track2, char *track3)
{
FILE *fp;
char buf[200];
int i;
if ((fp = fopen(TRACK_FILE, "w")) == NULL)
return -1;
memset(buf, 0, sizeof(buf));
memcpy(buf, track2, 37);
memcpy(buf+37, track3, 104);
fwrite(buf, 141, 1, fp);
fclose(fp);
if ( memcmp(track2, "62", 2) == 0 )
{
memcpy(htop.CardNo, track2+2, 16);
memcpy(htop.Expiry, track2+22, 4);
}
else
if(strlen(track2))
{
for(i=0;i<50;i++)
if(track2[i]=='=') break;
memcpy(htop.CardNo,track2+i-16,16);
}
else
{
for(i=0;i<50;i++)
if(track3[i]=='=') break;
memcpy(htop.CardNo,track3+i-16,16);
}
return 0;
}
int GetTime()
{
struct tm *tm;
time_t t1;
t1 = time(NULL);
tm = localtime(&t1);
sprintf(ptoh.TrDate, "%04d%02d%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
sprintf(ptoh.TrTime, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
return 0;
}
int GetDateAndTime(char *str)
{
struct tm *tm;
time_t t1;
t1 = time(NULL);
tm = localtime(&t1);
sprintf(str, "%04d%02d%02d",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
sprintf(str+8, "%02d:%02d:%02d",
tm->tm_hour, tm->tm_min, tm->tm_sec);
str[16]=0;
return 0;
}
WriteRevFile()
{
FILE *fp;
if (ptoh.TrCode != POS_SALE && ptoh.TrCode != POS_VOID) return (0);
fp = fopen(REV_FILE, "w");
if (fp == NULL) return (-1);
fwrite(&ptoh, sizeof(ptoh), 1, fp);
fclose(fp);
return (0);
}
DelRevFile()
{
unlink(REV_FILE);
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -