📄 card.c
字号:
/***************************************************
** PROGRAM: card.c **
** AUTHOR: kenny **
** WRITE DATE: 10/10/1997 **
** LAST MODIFY DATE: 11/13/2000 BY LiuSong **
** JOIN FILE: **
** COMMENT: 商场银行卡交易系统 前台主程序 **
** **
***************************************************/
#include "card.h"
#include "sqlfront.h"
#include "sqldb.h"
PTOH ptoh;
HTOP htop;
char result[256], cmdline[100];
char CardNo[17];
int _DebugFlag=0, _DemoFlag=0, tcp_sd;
int GetDateAndTime(char *str);
long recno = 0;
/****************************************************************************
* main() -- 主程序 *
* usage: card [flag] [posid] [operid] [trcode] [amount] [unitcardflag] *
****************************************************************************/
main(int argc, char **argv)
{
int i;
char trcode;
time_t beg;
FILE *nofp, *savefp, *fp;
char no[7];
char temp1[100];
CARDOUT CardRej;
memset(&ptoh, 0, sizeof(PTOH));
memset(&htop, 0, sizeof(HTOP));
memset(temp1, 0, sizeof(temp1) );
memset(&CardRej, 0, sizeof(CardRej));
sprintf(temp1,"readcard %s %s %s %s %0.0f %s",
argv[1],argv[2], argv[3], argv[4], atof(argv[5])*100, argv[6]);
system(temp1);
RevPro();
if((savefp = fopen("savefile", "r")) == NULL) {
strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
exit(0);
}
fread(&ptoh, 1, sizeof(ptoh), savefp);
fclose(savefp);
ptoh.OldCode = ' ';
ptoh.UnitCardFlag = '0';
memcpy(no, ptoh.Flag, 7);
/*
printf("\n\nTrCode = %c", ptoh.TrCode);
printf("\nOldCode = %c", ptoh.OldCode);
printf("\nPosId = %s", ptoh.PosId);
printf("\nOperId = %s", ptoh.OperId);
printf("\nTrDate = %s", ptoh.TrDate);
printf("\nTrTime = %s", ptoh.TrTime);
printf("\nAmount = %s", ptoh.Amount);
printf("\nTrack2 = %s", ptoh.Track2);
printf("\nTrack3 = %s", ptoh.Track3);
printf("\nPin = %s", ptoh.Pin);
printf("\nAuthNo = %s", ptoh.AuthNo);
printf("\nMac = %s", ptoh.Mac);
printf("\nUnitCardFlag = %c", ptoh.UnitCardFlag);
printf("\nStatus = %c", ptoh.Status);
printf("\nFlag = %s\n", ptoh.Flag);
*/
// if(ConnectDb("s001", "develop", "server_main", 0) <= 0)
if(ConnectDb("xyk", "xyk001", "server_main") <= 0)
{
printf("\nConnectDB err\n");
strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
return -1;
}
else {
if (SendData(ptoh) > 0 ) {
WriteRevFile();
beg = time(NULL);
while(1) {
if (time(NULL) - beg >= RESPONSE_TIMEOUT) {
strcpy(htop.RejCode, "A4");
strcpy(htop.ErrDisp, "接收银行超时, 请再试一次 ...");
PubProcess();
return -1;
}
if(RecvData(htop) <= 0) {
continue;
}
savefp=fopen("savefile", "r");
fread(&htop, 1, sizeof(htop), savefp);
fclose(savefp);
if(memcmp(htop.Flag, no, 6) == 0) {
UpData(htop);
DelRevFile();
break;
}
else
continue;
}
}
}
dbexit();
PubProcess();
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 1;
}
WritePface(HTOP *htop)
{
FILE *fp;
int i;
char response[280], File[30];
switch(strlen(ptoh.PosId)) {
case 1:
sprintf(File, "%s%01ld.txt", PFACE_FILE, atol(ptoh.PosId));
break;
case 3:
sprintf(File, "%s%03ld.txt", PFACE_FILE, atol(ptoh.PosId));
break;
default:
sprintf(File, "%s%02ld.txt", PFACE_FILE, atol(ptoh.PosId));
break;
}
if ((fp = fopen(File, "w")) == NULL)
return -1;
memset(response, 0, sizeof(response));
memcpy(response, htop->RejCode, 2);
strcpy(response+2, htop->CardNo);
memcpy(response+22, htop->Expiry, 4);
strcpy(response+26, htop->AuthNo);
strcpy(response+32, htop->PosSer);
strcpy(response+38, htop->VposSer);
response[44] = htop->BankId;
strcpy(response+45, htop->BankName);
strcpy(response+65, htop->CardName);
response[85] = htop->PrtTimes;
GetDateAndTime(response+86); //16
response[102] = '0'; /*** awardclass 改为两位 01,02...前补零 mzy***/
response[103] = htop->AwardClass;
strcpy(response+104, htop->Amount); //12
strcpy(response+116, htop->ErrDisp); //40
memset(response+156, ' ', 120);
if (htop->BankId == '1') {
sprintf(response+156, "余额: %.2lf", atof(htop->Note1)/100.0);
}
for (i=0; i<156+120; i++)
if (response[i] == 0)
response[i] = ' ';
fwrite(response, 156+120, 1, fp);
fclose(fp);
return 0;
}
void PubProcess()
{
char temp2[20];
/* 写接口文件 */
WritePface(&htop);
if (strcmp(htop.RejCode, "00") == 0)
{
system("beep");
if (ptoh.TrCode == POS_INQ)
{
memset(temp2,0,20);
sprintf(temp2,"disp %s",htop.Amount);
system(temp2); //DispMsg("查询余额成功, 请顾客看密码显示屏 ...");
}
}
return;
}
disp_arg_mess(char* fname)
{
printf("\r新利商场银行卡交易系统在线帮助\n" );
printf("功能 : 实现在商场进行银行卡交易\n" );
printf("Usage : %s flag posid operid trcode amount unitcardflag [/help] [/debug] [/demo]..\n", fname);
printf("\tflag -- 调用位; '0'所有功能, '1'只取卡号, '2'除取卡号外的所有功能\n");
printf("\tposid -- 收银台号; 不超过八位\n");
printf("\toperid -- 收银员号; 不超过六位\n");
printf("\ttrcode -- 交易类型; 'S'消费, 'V'冲正, 'Q'查询, 'B'查止付\n");
printf("\tamount -- 交易金额; 不超过十二位\n");
printf("\tunitcardflag -- 是否联名卡标志; '0'不是, '1'是\n");
printf("\t/?, /HELP -- 帮助\n" );
printf("\t/DEBUG -- 调试模式\n");
printf("\t/DEMO -- 演示模式\n\n");
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;
}
RevPro()
{
FILE *fp;
PTOH ptoh;
HTOP htop;
FILE *nofp;
time_t beg;
char no[7];
int i;
fp = fopen(REV_FILE, "r");
if (fp == NULL) return (0);
if (fread(&ptoh, 1, sizeof(ptoh), fp) != sizeof(ptoh))
return (-1);
fclose(fp);
ptoh.OldCode = ptoh.TrCode;
ptoh.TrCode = POS_REV;
nofp=fopen("nofile","r");
if(nofp==NULL)
{
strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
return -1;
}
if(fgets(no,6,nofp)==NULL)
{
strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
return -1;
}
fclose(nofp);
// if(ConnectDb("s001", "develop", "server_main", 1) <= 0)
if (ConnectDb("singlee","singlee","server_main") <= 0)
{ strcpy(htop.RejCode, "A3");
strcpy(htop.ErrDisp, "网络忙, 请稍候重试...");
PubProcess();
return -1;
}
else
{
if(SendData(ptoh) >0 )
{
WriteRevFile();
beg = time(NULL);
while(1)
{
if (time(NULL) - beg >= RESPONSE_TIMEOUT)
{
strcpy(htop.RejCode, "A4");
strcpy(htop.ErrDisp, "接收银行超时, 请再试一次 ...");
PubProcess();
return -1;
}
if(RecvData(htop) < 0)
continue;;
if(memcmp(htop.Flag,no,3)==0)
{
UpData(htop);
DelRevFile();
}
else
continue;
}
}
}
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 + -