Exemple #1
0
/*****************************************************************************
 ** Descriptions:	加密发送包
 ** Parameters:    	SDK_8583_ST8583 *pstSendBag 发送包
 ** Returned value:
 ** Created By:		lilin2012.12.22
 ** Remarks:
 *****************************************************************************/
s32 EncryptSendData(struct _TradeData *pstTradeData,
		SDK_8583_ST8583 *pstSendBag) {
	s32 slt = SDK_OK;
	u8 ucMacBuf[32] = { 0 };
//lilin 20140319
//    //重新改写报文头
//    ReConfigBag8583(pstSendBag);
	//磁道信息加密
	if (pstSendBag == NULL || pstTradeData == NULL) {
		return 0;
	}
	slt = EncryptMagData(pstTradeData, pstSendBag);
	Log("wangb", "EncryptMagData is %d ", slt);
	if (slt != SDK_OK) {
		return SDK_ESC;
	}

	//获得MAC
	if (sdk8583IsDomainExist(pstSendBag, 64)) {
		slt = GetMacData(pstTradeData, pstSendBag, ucMacBuf);
		if (slt != SDK_OK) {
			return SDK_ESC;
		}
		slt = SetFieldData(pstSendBag, 64, ucMacBuf, 8);
		//   slt = SetFieldData(pstSendBag, 64, "\x02\x00\x30\x20\x04\xC0\x20\xC0", 8);
		//    slt = SetFieldData(pstSendBag, 64, "\x32\x42\x35\x39\x36\x33\x39\x37", 8);
		if (slt <= 0) {
			return SDK_ESC;
		}
	}
	return SDK_OK;
}
Exemple #2
0
int CPayment::BuildPackage(CIso8583Package *package,LONG Money)
{
	int money = Money * 100;//* 100 
	char szMoney[13] = {'\0'};
	_snprintf(szMoney,12,"%012d",money);

	TCHAR szCurPath[MAX_PATH];
	GetCurrentDirectory(sizeof(szCurPath),szCurPath);
	_tcscat(szCurPath,TEXT("\\Reversal.ini"));
	char cTemp[5];
	WritePrivateProfileString(TEXT("Reversal"),TEXT("Money"),ltoa(Money,cTemp,10),szCurPath);

	package->SetFieldData(3,6,(BYTE*)"000000");
	package->SetFieldData(4,12,(BYTE*)szMoney);
	package->SetFieldData(11,6,(BYTE*)szTraceCode.GetBuffer());
	WritePrivateProfileString(TEXT("Reversal"),TEXT("TraceCode"),szTraceCode.GetBuffer(),szCurPath);

	if( strcmp(szTraceCode.GetBuffer(),"999999") == 0)
		szTraceCode = "000001";
	package->SetFieldData(22,3,(BYTE*)"021");
	package->SetFieldData(25,2,(BYTE*)"00");
	package->SetFieldData(26,2,(BYTE*)"12");


	char *pAccount;
	if (strcmp(szTestMode.GetBuffer(),"1") == 0)
	{
		package->SetFieldData(2,19,(BYTE*)"9559480089076438611"); //主帐号
		pAccount = "9559480089076438611";
		package->SetFieldData(35,37,(BYTE*)"9559480089076438611=00004209600000000");//磁道
	}
	else
	{
		char cCard[40];
		int nLen = 0;
		if( GetCardNo(cCard,nLen) != 0) return -1;
		package->SetFieldData(35,37,(BYTE *)cCard);

		const char *split = "="; 
		pAccount = strtok (cCard,split); 
		if(pAccount == NULL) return -1;
		package->SetFieldData(2,strlen(pAccount),(BYTE *)pAccount);//19
		WritePrivateProfileString(TEXT("Reversal"),TEXT("Account"),pAccount,szCurPath);
	}

	package->SetFieldData(41,8,(BYTE*)szTermID.GetBuffer());	//终端号
	package->SetFieldData(42,15,(BYTE*)szMercCode.GetBuffer()); //商户号
	package->SetFieldData(49,3,(BYTE*)"156");
	package->SetFieldData(53,16,(BYTE*)"2600000000000000");
	char cReserved[9] = "22";
	strcat(cReserved,szTraceCode.GetBuffer());
	package->SetFieldData(60,8,(BYTE*)cReserved);//自定义域 "22002025"
	package->SetFieldData(63,3,(BYTE*)"001");//自定义域

	if (GetMacData(package, pAccount) != 0)
	{
		return -1;
	}

	return 0;
}
Exemple #3
0
/*****************************************************************************
 ** Descriptions:	效验接收包
 ** Parameters:          void
 ** Returned value:
 ** Created By:		lilin2012.08.06
 ** Remarks:
 ***************************************************+**************************/
s32 CheckRecBag(SDK_8583_ST8583 *pstSendBag, SDK_8583_ST8583 *pstRevBag) {
	//SDK_PED_DES_TYPE type;
	u8 ucMacBuf[32];
	u8 sendbuf[64] = { 0 };
	u8 proccode[6] = { 0 };
	s32 slen;
	s32 rlen;
	u8 revbuf[64] = { 0 };
	u8 res[3] = { 0 };
	s32 rlt;
	u8 i;
	u8 num = 0;
	struct _Field60 stfield60;

	struct _TradeData *stTradeData = &gstTradeData;

#ifdef ELECSIGN
	u8 maxCheckNum = 6;
#endif
	u8 he_checkdata[6] = {
	SDK_8583_FIELD_MSG,           //消息类型
			3,                              //交易处理码
			11,                             //流水号
			41,                             //终端号
			42,                             //商户号
			4,                              //交易金额
			};

	//获取处理吗
	Log("wangb", "CheckRecBag start");
	if (sdk8583IsDomainExist(pstSendBag, 3)) {
		GetFieldData(pstSendBag, 3, proccode, 6);
	}
	memset(sendbuf, 0, sizeof(sendbuf));
	rlt = GetFieldData(pstSendBag, SDK_8583_FIELD_MSG, sendbuf, 4);   //获取消息类型
	if (rlt < 0) {
		Log("CheckRecBag", "取RECV0错误");
		return ERR_RECZERO;
	} else {
		if (memcmp(sendbuf, "05", 2))                        //结算报文没有39域
				{
			memset(res, 0, sizeof(res));
			rlt = GetFieldData(pstRevBag, 39, res, 3);
			if (rlt < 0) {
				Log("CheckRecBag", "取RECV39错误\r\n");
				return ERR_RECZERO;
			} else {
				ProviderSetTagValue(TAG_8583_BKRESULT, res, rlt);
			}
		}
	}
	//是否缺失64域
	if (sdk8583IsDomainExist(pstSendBag, 64))          //先判断发送包是否有64
			{
		if (strcmp(res, "00") == 0)       //39域返回正确
				{
			if (!sdk8583IsDomainExist(pstRevBag, 64))         //判断接收64域
					{
				rlt = ERR_MISSMAC;
				return rlt;
			}
		}
	}

	//判断64域是否正确
	if (sdk8583IsDomainExist(pstRevBag, 64)) {
		rlt = GetMacData(stTradeData, pstRevBag, ucMacBuf);
		Log("wangb", "ucMacBuf64 %s", ucMacBuf);
		if (rlt != SDK_OK) {
			Log("CheckRecBag", "ECB算法错误");
			return ERR_CALCMAC;
		}
		memset(revbuf, 0, sizeof(revbuf));
		rlt = GetFieldData(pstRevBag, 64, revbuf, 8);
		Log("wangb", "revbuf64 %s", revbuf);
		if (rlt < 0) {
			Log("CheckRecBag", "缺少64域");
			return ERR_RECZERO;
		}
		// TraceHex("CheckRecBag", "cal=", ucMacBuf, 8);
		// TraceHex("CheckRecBag", "mac=", revbuf, 8);

		if (memcmp(ucMacBuf, revbuf, 8)) {
			Log("CheckRecBag", "MAC效验错误");
			rlt = ERR_CALCMAC;
			return rlt;
		}
	}

	u8 gElecSignIsSendingEsTrade = false; //本次交易为电子签名上送交易(用于解包8583不校验4域--打包8583)
#ifdef ELECSIGN
	if (gElecSignIsSendingEsTrade) {
		maxCheckNum = 5;
	} else {
		maxCheckNum = 6;
	}
	for (i = 0; i < maxCheckNum; i++)
#else
			for (i = 0; i < 6; i++)
#endif
			{
		num = he_checkdata[i];

		if (sdk8583IsDomainExist(pstRevBag, num)
				&& sdk8583IsDomainExist(pstSendBag, num)) {
			memset(revbuf, 0, sizeof(revbuf));
			rlen = GetFieldData(pstRevBag, num, revbuf, 64);

			if (rlen < 0) {
				Log("CheckRecBag", "取RECV%d错误\r\n", num);
				return ERR_RECZERO;
			}
			memset(sendbuf, 0, sizeof(sendbuf));
			slen = GetFieldData(pstSendBag, num, sendbuf, 64);

			if (slen < 0) {
				Log("CheckRecBag", "取SEND%d错误\r\n", num);
				return ERR_RECZERO;
			}
			Log("wangb", "sendbuf is %s", sendbuf);
			Log("wangb", "revbuf is %s", revbuf);
			switch (num) {
			case SDK_8583_FIELD_MSG:   //判断消息类型

				if ((memcmp(sendbuf, revbuf, 2))
						|| (revbuf[2] != (sendbuf[2] + 1))) {
					rlt = ERR_MSGTYPE;
					return rlt;
				}
				break;

			case 3:      //比较处理码(银行卡检测中心说一定要比较第1个字节交易类型)

				if (memcmp(sendbuf, revbuf, 2)) {
					rlt = ERR_PROCESSCODE;
					return rlt;
				}
				break;

			case 11:

				//比较POS流水号
				if (strcmp(sendbuf, revbuf)) {
					rlt = ERR_VOUCHERNO;
					return rlt;
				}
				break;

			case 41:

				//比较终端号
				if (strcmp(sendbuf, revbuf)) {
					rlt = ERR_TERMINALNO;
					return rlt;
				}
				break;

			case 42:

				//比较商户号
				if (strcmp(sendbuf, revbuf)) {
					rlt = ERR_MERCHANTNO;
					return rlt;
				}
				break;

			case 4:

				//比较交易金额
				if (strcmp(sendbuf, revbuf)) {
					Log("CheckRecBag", "%s  %s\r\n", sendbuf, revbuf);
					memset(&stfield60, 0, sizeof(stfield60));

					if (ParseField60(&stfield60)) {
						if (strcmp(res, "10") || stfield60.ucPortion != '1') //不为部分扣款
								{
							rlt = ERR_TRADEAMOUNT;
							return rlt;
						}
					}
				}
				break;

			default:
				break;
			}
		} else {
			if (num == 41) {
				rlt = ERR_TERMINALNO;
				return rlt;
			}

			if (num == 42) {
				rlt = ERR_MERCHANTNO;
				return rlt;
			}
		}
	}

	//响应码处理
	memset(sendbuf, 0, sizeof(sendbuf));
	rlt = GetFieldData(pstSendBag, SDK_8583_FIELD_MSG, sendbuf, 4); //获取消息类型码

	if (rlt < 0) {
		Log("CheckRecBag", "取RECV0错误");
		return ERR_RECZERO;
	}

	if (memcmp(sendbuf, "04", 2) == 0)                    //冲正包判断
			{
		if (memcmp(res, "00", 2) && memcmp(res, "12", 2)
				&& memcmp(res, "25", 2)) {
			return ERR_RESPONSE;
		}
	}
	//磁条卡现金充值确认包判断
	else if (memcmp(sendbuf, "0220", 4) == 0
			&& memcmp(proccode, "63", 2) == 0) {
		if (memcmp(res, "00", 2) && memcmp(res, "A6", 2)) {
			return ERR_RESPONSE;
		}
	} else {
		if (memcmp(sendbuf, "05", 2))                        //结算报文没有39域
				{
			if (!CheckResponseCode(res))    //响应码错误
					{
				Log("CheckRecBag", "响应码error  %s", res);
//			  Log("CheckRecBag", "FunCode", stfield60.asFunCode, 2);
				//磁条卡现金充值回98当做超时
				memset(&stfield60, 0, sizeof(struct _Field60));
				ParseField60(&stfield60);

				//磁条卡现金充值包判断
				if (memcmp(sendbuf, "0200", 4) == 0
						&& memcmp(proccode, "63", 2) == 0
						&& memcmp(stfield60.asFunCode, "48", 2) == 0) {
					if (memcmp(res, "98", 2) == 0) {
						//lilin 20140214磁条卡现金充值不显示交易失败要进行磁条卡现金充值确认
//                        GetStrResponseError(dispbuf);
//                        sdkDispPromptBox(false, 500, dispbuf);
						return SDK_TIME_OUT;
					}
				}
				/*=======BEGIN: lilin 2015.01.12  15:31 modify===========*/
				if ((memcmp(sendbuf, "0200", 4) == 0)
						&& (memcmp(proccode, "00", 2) == 0)
						&& (memcmp(stfield60.asFunCode, "22", 2) == 0))     //消费
						{
					if ((strcmp(res, "10") == 0)
							&& (stfield60.ucPortion != '1')) //如果不是部分扣费还返回10则需要冲正
							{
						return ERR_NEEDREVERSAL;
					}
					if ((strcmp(res, "10") == 0)
							&& (stfield60.ucPortion == '1'))           //部分扣款
							{
						return ERR_RESPONSE10;
					}
				}
				/*====================== END======================== */
				return ERR_RESPONSE;
			}
		}
	}

	//都正确处理更新时间和日期
	if (sdk8583IsDomainExist(pstRevBag, 12))                         //更新交易时间
			{
		memset(revbuf, 0, sizeof(revbuf));
		rlt = GetFieldData(pstRevBag, 12, revbuf, 64);
		/*
		 if (rlt)
		 {
		 sdkAscToBcd(&gstSys.ucHour, &revbuf[0], 2);
		 sdkAscToBcd(&gstSys.ucMinute, &revbuf[2], 2);
		 sdkAscToBcd(&gstSys.ucSecond, &revbuf[4], 2);
		 }
		 */
	}

	if (sdk8583IsDomainExist(pstRevBag, 13))                     //更新交易日期
			{
		memset(revbuf, 0, sizeof(revbuf));
		rlt = GetFieldData(pstRevBag, 13, revbuf, 64);
		/*
		 if (rlt)
		 {
		 sdkAscToBcd(&gstSys.ucMonth, &revbuf[0], 2);
		 sdkAscToBcd(&gstSys.ucDay, &revbuf[2], 2);
		 }
		 */
	}
	// Trace("CheckRecBag", "OK\r\n");
	Log("wangb", "CheckRecBag OK");
	return SDK_OK;
}