Commit 81dd0ace by 刘帅

1. 设置门店信息、非码支付、支付查询、支付退款接口测试ok

parent 34dfc7cf
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <QFile> #include <QFile>
#include <Qdebug> #include <Qdebug>
#include <QJsonArray> #include <QJsonArray>
#include <QtAlgorithms>
#include <QDir>
#ifdef WIN32 #ifdef WIN32
#include <direct.h> #include <direct.h>
...@@ -77,6 +79,30 @@ bool QFmClient::Init(QString proxy_IP, unsigned short proxy_port, unsigned short ...@@ -77,6 +79,30 @@ bool QFmClient::Init(QString proxy_IP, unsigned short proxy_port, unsigned short
return true; return true;
} }
/*初始化门店信息
* 启动时,如果存在门店信息文件,读取
* 如果不存在,新建
* 每次更新门店信息时,写入
*/
bool QFmClient::InitStoreInfo()
{
QString fileName = qApp->applicationDirPath();
fileName.append(QDir::separator());
fileName.append("store_info.ini");
if(QFile::exists(fileName))
{
//read store info
}
else
{
QFile storeInfoFileName(fileName);
if(!storeInfoFileName.open(QIODevice::ReadWrite | QIODevice::Text))
qDebug()<<"open file failed!\n"<<fileName;
}
return true;
}
int QFmClient::RecvSockData(int sock, char *buffer, int length) int QFmClient::RecvSockData(int sock, char *buffer, int length)
{ {
#ifdef WIN32 #ifdef WIN32
...@@ -96,17 +122,17 @@ void QFmClient::WaitAndSleep(int elapse) ...@@ -96,17 +122,17 @@ void QFmClient::WaitAndSleep(int elapse)
} }
bool QFmClient::WaitForConnectReqFromPos() bool QFmClient::WaitForConnectReqFromPos()
{ {
int sin_size = sizeof(struct sockaddr_in); int sin_size = sizeof(struct sockaddr_in);
struct sockaddr_in c_add; struct sockaddr_in c_add;
if(0 < _acceptedSock) if(0 < _acceptedSock)
{ {
RecvSockData(_acceptedSock, _recvbuf, (MAX_BUF_LEN - 1)); RecvSockData(_acceptedSock, _recvbuf, (MAX_BUF_LEN - 1));
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
} }
qDebug()<<"wait for connection from pos";
_acceptedSock = accept(_listenSock, (struct sockaddr *)(&c_add), &sin_size); _acceptedSock = accept(_listenSock, (struct sockaddr *)(&c_add), &sin_size);
if(-1 == _acceptedSock) if(-1 == _acceptedSock)
{ {
...@@ -122,9 +148,9 @@ bool QFmClient::WaitForConnectReqFromPos() ...@@ -122,9 +148,9 @@ bool QFmClient::WaitForConnectReqFromPos()
int QFmClient::CheckIsCompleteJsonData(int * count, char * data) int QFmClient::CheckIsCompleteJsonData(int * count, char * data)
{ {
int loop = 0; int loop = 0;
while(data[loop]) while(data[loop])
{ {
if ('{' == data[loop]) if ('{' == data[loop])
{ {
...@@ -240,12 +266,12 @@ char * QFmClient::DecodeRecvData(char * buf, int length, int * dLength) ...@@ -240,12 +266,12 @@ char * QFmClient::DecodeRecvData(char * buf, int length, int * dLength)
int QFmClient::WaitAndReadDataFromPos(int type, int flag) int QFmClient::WaitAndReadDataFromPos(int type, int flag)
{ {
qDebug() << "wait and receive data from POS...\r\n"; qDebug() << "wait to receive data from POS...\r\n";
_rcvLength = 0; _rcvLength = 0;
int recbytes, finLen, curPos = 0, prosLen = 0, checkCount = 0; int recbytes, finLen, curPos = 0, prosLen = 0, checkCount = 0;
int i = 0; memset(_tempbuf,0,MAX_BUF_LEN);
do do
{ {
char * precvBuf; char * precvBuf;
...@@ -259,8 +285,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag) ...@@ -259,8 +285,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
return -1; return -1;
} }
#ifdef WIN32 #ifdef WIN32
qDebug() << "received data " << _acceptedSock << " : " << _tempbuf;
if(type) if(type)
{ {
...@@ -269,6 +294,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag) ...@@ -269,6 +294,7 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
//recbytes = GBKToUTF8( (unsigned char *)_sendbuf, recbytes, (unsigned char *)_tempbuf, MAX_BUF_LEN - 100); //recbytes = GBKToUTF8( (unsigned char *)_sendbuf, recbytes, (unsigned char *)_tempbuf, MAX_BUF_LEN - 100);
} }
#endif #endif
qDebug() << "received data from pos(socket " << _acceptedSock << ") : \n" << _tempbuf;
precvBuf = _tempbuf; precvBuf = _tempbuf;
_tempbuf[recbytes] = '\0'; _tempbuf[recbytes] = '\0';
...@@ -276,8 +302,6 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag) ...@@ -276,8 +302,6 @@ int QFmClient::WaitAndReadDataFromPos(int type, int flag)
if(1 == flag) if(1 == flag)
precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes); precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes);
qDebug() << "received data from socket " << _acceptedSock << " : " << precvBuf;
while(0 < recbytes) while(0 < recbytes)
{ {
finLen = CheckIsCompleteJsonData(&checkCount, precvBuf + prosLen); finLen = CheckIsCompleteJsonData(&checkCount, precvBuf + prosLen);
...@@ -326,9 +350,18 @@ int QFmClient::CheckReqFromPos(char * pJsonData, int dLength, int chkValue) ...@@ -326,9 +350,18 @@ int QFmClient::CheckReqFromPos(char * pJsonData, int dLength, int chkValue)
return ZH_checkChkValue(pJsonData, dLength, chkValue); return ZH_checkChkValue(pJsonData, dLength, chkValue);
} }
/*
* 返回值
* -1 失败
* 0 成功
* SET_STORE_INFO
* FREEMUD_PAY
* QUERY_PAY
* REFUND_PAY
*/
int QFmClient::ProcessPosReqData(QJsonObject& object) int QFmClient::ProcessPosReqData(QJsonObject& object)
{ {
int flag = 0; int nResult = 0;
QByteArray recvdata(_recvbuf); QByteArray recvdata(_recvbuf);
QJsonParseError json_error; QJsonParseError json_error;
...@@ -343,72 +376,54 @@ int QFmClient::ProcessPosReqData(QJsonObject& object) ...@@ -343,72 +376,54 @@ int QFmClient::ProcessPosReqData(QJsonObject& object)
//! TODO: Json 转换 //! TODO: Json 转换
object = doc.object(); object = doc.object();
int fm_cmd = 10031; int fm_cmd = FREEMUD_PAY;
if(object.contains("fm_cmd")) if(object.contains("fm_cmd"))
{ {
fm_cmd = object.take("fm_cmd").toInt(); fm_cmd = object.value("fm_cmd").toInt();
switch(fm_cmd) switch(fm_cmd)
{ {
case 10031: case FREEMUD_PAY:
nResult = FREEMUD_PAY;
if(object.find("store_id") != object.end()) if(object.find("store_id") != object.end())
{ {
GetStoreInfo(object); SetStoreInfo(object);
} }
qDebug()<<"convert json format"; qDebug()<<"convert json format";
ConvertJsonFormat(object); ConvertRequestJsonFormat(object);
break;
case SET_STORE_INFO:
nResult = SET_STORE_INFO;
SetStoreInfo(object);
break;
case QUERY_PAY:
nResult = QUERY_PAY;
QueryPay(object);
break; break;
case 1000: case REFUND_PAY:
GetStoreInfo(object); nResult = REFUND_PAY;
sendResponseToPos(1000); RefundPay(object);
return 1;
break; break;
default: default:
nResult = -1;
qDebug()<<"unknown command: "<<fm_cmd;
break; break;
} }
qDebug()<<"request json:\n"<<object;
} }
do else
{ {
_sockTimeOut = _longTimeOut; qDebug()<<"unknown command(can not find fm_cmd section) ";
if(object.contains("reqtype")) nResult = -1;
{ }
if(object["reqtype"] != 72) return nResult;
{
_sockTimeOut = _shortTimeOut;
}
if(object["reqtype"] == 72 || object["reqtype"] == 71)
{
flag = 1;
}
}else
{
qDebug() << "not found reqtype";
flag = -1;
break;
}
// if(!object.contains("checkValue") || !object["checkValue"].isDouble())
// {
// qDebug() << "checkValue failed!\r\n";
// flag = -1;
// break;
// }
AddReqCount(object);
}while(0);
return flag;
} }
//convert pos requst json format from //convert pos requst json format from
// 非码支付插件 API 对接说明.pdf // 非码支付插件 API 对接说明.pdf
//to //to
// 非码支付FMClient商户版_20170921.pdf // 非码支付FMClient商户版_20170921.pdf
bool QFmClient::ConvertJsonFormat(QJsonObject &object) bool QFmClient::ConvertRequestJsonFormat(QJsonObject &object)
{ {
QJsonObject newObject; QJsonObject newObject;
QJsonArray transactionArray; QJsonArray transactionArray;
...@@ -425,85 +440,409 @@ bool QFmClient::ConvertJsonFormat(QJsonObject &object) ...@@ -425,85 +440,409 @@ bool QFmClient::ConvertJsonFormat(QJsonObject &object)
if(object.contains("trans_id")) if(object.contains("trans_id"))
{ {
QString trans_id = object.take("trans_id").toString(); QString trans_id = object.value("trans_id").toString();
newObject.insert("trans_id",trans_id); newObject.insert("trans_id",trans_id);
} }
if(object.contains("order_amount")) if(object.contains("order_amount"))
{ {
int order_amount = object.take("order_amount").toInt(); int order_amount = object.value("order_amount").toInt();
transactionObject.insert("amount",order_amount); transactionObject.insert("amount",order_amount);
} }
if(object.contains("undis_amount")) if(object.contains("undis_amount"))
{ {
int undis_amount = object.take("undis_amount").toInt(); int undis_amount = object.value("undis_amount").toInt();
transactionObject.insert("undis_amount",undis_amount); transactionObject.insert("undis_amount",undis_amount);
} }
if(object.contains("barcode")) if(object.contains("barcode"))
{ {
QString barcode = object.take("barcode").toString(); QString barcode = object.value("barcode").toString();
transactionObject.insert("code",barcode); transactionObject.insert("code",barcode);
} }
if(object.contains("products")) if(object.contains("products"))
{ {
QJsonValue products = object.take("products"); QJsonValue products = object.value("products");
if(products.isArray()) if(products.isArray())
transactionObject.insert("products",products); transactionObject.insert("products",products);
} }
transactionArray.append(transactionObject); transactionArray.append(transactionObject);
newObject.insert("transactions",transactionArray); newObject.insert("transactions",transactionArray);
object = newObject; qSwap(object, newObject);
qDebug()<<object; qDebug()<<object;
return true; return true;
} }
//获取门店信息 /* 转换回复json格式
void QFmClient::GetStoreInfo(QJsonObject &object) * object
* nType 回复类型
*/
bool QFmClient::ConvertResponseJsonFormat(QJsonObject &object, int nType)
{
bool bRtValue = true;
qDebug()<<"response json:\n"<<object;
switch(nType)
{
case SET_STORE_INFO:
break;
case FREEMUD_PAY:
FreemudPayResponse(object);
break;
case QUERY_PAY:
QueryPayResponse(object);
break;
case REFUND_PAY:
RefundPayResponse(object);
break;
default:
bRtValue = false;
break;
}
return bRtValue;
}
//设置门店信息
void QFmClient::SetStoreInfo(QJsonObject &object)
{ {
if(object.contains("partner_id")) if(object.contains("partner_id"))
{ {
QString partner_id = object.take("partner_id").toString(); QString partner_id = object.value("partner_id").toString();
store_info.partner_id = partner_id; store_info.partner_id = partner_id;
} }
if(object.contains("store_id")) if(object.contains("store_id"))
{ {
QString store_id = object.take("store_id").toString(); QString store_id = object.value("store_id").toString();
store_info.store_id = store_id; store_info.store_id = store_id;
} }
if(object.contains("pos_id")) if(object.contains("pos_id"))
{ {
QString pos_id = object.take("pos_id").toString(); QString pos_id = object.value("pos_id").toString();
store_info.pos_id = pos_id; store_info.pos_id = pos_id;
} }
if(object.contains("business_date")) if(object.contains("business_date"))
{ {
QString business_date = object.take("business_date").toString(); QString business_date = object.value("business_date").toString();
store_info.business_date = business_date; store_info.business_date = business_date;
} }
if(object.contains("operator_id")) if(object.contains("operator_id"))
{ {
QString operator_id = object.take("operator_id").toString(); QString operator_id = object.value("operator_id").toString();
store_info.operator_id = operator_id; store_info.operator_id = operator_id;
} }
} }
//nflag: 1000 response for set store infomation void QFmClient::FreemudPay(QJsonObject &object)
void QFmClient::sendResponseToPos(int nflag)
{ {
if(nflag == 1000) do
{ {
QJsonObject responseObj; _sockTimeOut = _longTimeOut;
responseObj.insert("status_code",100); if(object.contains("reqtype"))
responseObj.insert("msg",QString("set store infomation success")); {
QString str = QJsonDocument(responseObj).toJson(); if(object["reqtype"] != 72)
char* pResponse = new char[str.size()+1]; {
strcpy(pResponse,str.toStdString().c_str()); _sockTimeOut = _shortTimeOut;
}
int nRt = SendSocketData(_acceptedSock,pResponse,str.size(),0); if(object["reqtype"] == 72 || object["reqtype"] == 71)
delete []pResponse; {
if(nRt == 0) //flag = 1;
qDebug()<<"send response for set store info failed!"; }
}else
{
qDebug() << "not found reqtype";
//flag = -1;
break;
}
// if(!object.contains("checkValue") || !object["checkValue"].isDouble())
// {
// qDebug() << "checkValue failed!\r\n";
// flag = -1;
// break;
// }
AddReqCount(object);
}while(0);
}
//支付查询
bool QFmClient::QueryPay(QJsonObject &object)
{
QJsonObject queryObject;
QString fm_order_id;
QString trans_id;
if(object.contains("trans_id"))
{
trans_id = object.value("trans_id").toString();
queryObject.insert("trans_id",trans_id.toInt());
} }
else
{
qDebug()<<"can not find trans_id in json request!";
return false;
}
if(object.contains("fm_order_id"))
{
fm_order_id = object.value("fm_order_id").toString();
}
queryObject.insert("ver", 4);
queryObject.insert("reqtype", 52);
queryObject.insert("partnerId",store_info.partner_id.toInt());
queryObject.insert("store_id",store_info.store_id);
queryObject.insert("station_id",store_info.pos_id);
queryObject.insert("operator_id",store_info.operator_id);
queryObject.insert("fmid",fm_order_id);
queryObject.insert("sign",QString());
qSwap(object,queryObject);
return true;
}
//支付退款
bool QFmClient::RefundPay(QJsonObject &object)
{
QJsonObject refundObject;
QJsonArray transactionArray;
QJsonObject transactionObject;
QString trans_id;
QString fm_trans_id;
int refund_amount = 0;
if(object.contains("trans_id"))
trans_id = object.value("trans_id").toString();
if(object.contains("fm_trans_id"))
fm_trans_id = object.value("fm_trans_id").toString();
if(object.contains("refund_amount"))
refund_amount = object.value("refund_amount").toInt();
refundObject.insert("ver", 2);
refundObject.insert("reqtype", 62);
refundObject.insert("partnerId",store_info.partner_id.toInt());
refundObject.insert("store_id",store_info.store_id);
refundObject.insert("station_id",store_info.pos_id);
refundObject.insert("operator_id",store_info.operator_id);
refundObject.insert("business_date",store_info.business_date);
refundObject.insert("trans_id", trans_id.toInt());
transactionObject.insert("fmid",fm_trans_id);
transactionObject.insert("refund_count",refund_amount);
transactionArray.append(transactionObject);
refundObject.insert("transactions",transactionArray);
qSwap(object,refundObject);
return true;
}
//非码支付回复处理
bool QFmClient::FreemudPayResponse(QJsonObject &object)
{
QJsonObject payResponseObject;
QJsonArray pay_idsArray;
QJsonObject pay_idObject;
QJsonValue ext;
int statusCode = 100;
if(object.contains("statusCode"))
{
statusCode = object.value("statusCode").toInt();
payResponseObject.insert("status_code",statusCode);
}
if(object.contains("msg"))
{
QJsonValue msg = object.value("msg");
payResponseObject.insert("msg",msg);
}
if(statusCode != 100)
{
qSwap(object,payResponseObject);
return true;
}
if(object.contains("pay_transId"))
{
QJsonValue fm_trans_id = object.value("pay_transId");
payResponseObject.insert("fm_trans_id",fm_trans_id);
}
if(object.contains("fmId"))
{
QJsonValue fm_order_id = object.value("fmId");
payResponseObject.insert("fm_order_id",fm_order_id);
}
if(object.contains("total_amount"))
{
QJsonValue paid_total_amount = object.value("total_amount");
payResponseObject.insert("paid_total_amount",paid_total_amount);
pay_idObject.insert("pay_amount",paid_total_amount);
}
if(object.contains("invoice_amount"))
{
QJsonValue invoice_amount = object.value("invoice_amount");
payResponseObject.insert("invoice_amount",invoice_amount);
}
if(object.contains("mcoupon_amount") && object.contains("pcoupon_amount"))
{
int platform_discount = object.value("mcoupon_amount").toInt();
int merchant_discount = object.value("pcoupon_amount").toInt();
payResponseObject.insert("discount_amount",platform_discount + merchant_discount);
pay_idObject.insert("platform_discount",platform_discount);
pay_idObject.insert("merchant_discount",merchant_discount);
}
if(object.contains("pay_ebcode"))
{
QJsonValue pay_id = object.value("pay_ebcode");
pay_idObject.insert("pay_id",pay_id);
}
if(object.contains("pay_id"))
{
QJsonValue pay_str = object.value("pay_id");
pay_idObject.insert("pay_str",pay_str);
}
if(object.contains("pay_acount"))
{
QJsonValue pay_account = object.value("pay_acount");
pay_idObject.insert("pay_account",pay_account);
}
if(object.contains("ext"))
{
ext = object.value("ext");
pay_idObject.insert("ext", ext);
}
pay_idsArray.append(pay_idObject);
payResponseObject.insert("pay_ids",pay_idsArray);
qSwap(object,payResponseObject);
return true;
}
//支付查询回复处理
bool QFmClient::QueryPayResponse(QJsonObject &object)
{
QJsonObject queryPayResponseObject;
QJsonArray pay_idsArray;
QJsonObject pay_idObject;
QJsonValue ext;
int statusCode = 100;
if(object.contains("statusCode"))
{
statusCode = object.value("statusCode").toInt();
queryPayResponseObject.insert("status_code",QString::number(statusCode));
}
if(statusCode != 100)
return true;
queryPayResponseObject.insert("msg",QString::fromLocal8Bit("成功"));
if(object.contains("pay_transId"))
{
QJsonValue fm_trans_id = object.value("pay_transId");
queryPayResponseObject.insert("fm_trans_id",fm_trans_id);
}
if(object.contains("fmId"))
{
QJsonValue fm_order_id = object.value("fmId");
queryPayResponseObject.insert("fm_order_id",fm_order_id);
}
if(object.contains("total_amount"))
{
QJsonValue paid_total_amount = object.value("total_amount");
queryPayResponseObject.insert("paid_total_amount",paid_total_amount);
pay_idObject.insert("pay_amount",paid_total_amount);
}
if(object.contains("invoice_amount"))
{
QJsonValue invoice_amount = object.value("invoice_amount");
queryPayResponseObject.insert("invoice_amount",invoice_amount);
}
if(object.contains("mcoupon_amount") && object.contains("pcoupon_amount"))
{
int platform_discount = object.value("mcoupon_amount").toInt();
int merchant_discount = object.value("pcoupon_amount").toInt();
queryPayResponseObject.insert("discount_amount",platform_discount + merchant_discount);
pay_idObject.insert("platform_discount",platform_discount);
pay_idObject.insert("merchant_discount",merchant_discount);
}
if(object.contains("pay_ebcode"))
{
QJsonValue pay_id = object.value("pay_ebcode");
pay_idObject.insert("pay_id",pay_id);
}
if(object.contains("pay_id"))
{
QJsonValue pay_str = object.value("pay_id");
pay_idObject.insert("pay_str",pay_str);
}
if(object.contains("pay_acount"))
{
QJsonValue pay_account = object.value("pay_acount");
pay_idObject.insert("pay_account",pay_account);
}
if(object.contains("ext"))
{
ext = object.value("ext");
pay_idObject.insert("ext", ext);
}
pay_idsArray.append(pay_idObject);
queryPayResponseObject.insert("pay_ids",pay_idsArray);
qSwap(object,queryPayResponseObject);
return true;
}
//支付退款回复处理
bool QFmClient::RefundPayResponse(QJsonObject &object)
{
QJsonObject refundPayResponseObject;
QJsonValue ext;
int statusCode = 100;
if(object.contains("statusCode"))
{
statusCode = object.value("statusCode").toInt();
refundPayResponseObject.insert("status_code",QString::number(statusCode));
}
if(statusCode != 100)
{
refundPayResponseObject.insert("msg",QString::fromLocal8Bit("失败"));
qSwap(object,refundPayResponseObject);
return true;
}
refundPayResponseObject.insert("msg",QString::fromLocal8Bit("成功"));
if(object.contains("ext"))
{
ext = object.value("ext");
refundPayResponseObject.insert("ext", ext);
}
qSwap(object,refundPayResponseObject);
return true;
}
/*发送设置门店信息响应
* bFlag true client处理成功
* false client处理失败
*/
void QFmClient::sendSetResponseToPos(bool bFlag)
{
QJsonObject responseObj;
int status_code = bFlag?100:0;
QString msg = bFlag?QString::fromLocal8Bit("成功"):QString::fromLocal8Bit("失败");
responseObj.insert("status_code",status_code);
responseObj.insert("msg",msg);
QString str = QString(QJsonDocument(responseObj).toJson(QJsonDocument::Compact));
QByteArray byteArray = str.toLocal8Bit();
const char* temp = byteArray.data();
size_t st = strlen(temp);
char* pResponse = new char[st + 1];
strcpy(pResponse,temp);
int nRt = SendSocketData(_acceptedSock,pResponse,st,0);
delete []pResponse;
if(nRt == 0)
qDebug()<<"send response for set store info failed!";
} }
int QFmClient::Try2ConnectZhProxy() int QFmClient::Try2ConnectZhProxy()
...@@ -512,7 +851,7 @@ int QFmClient::Try2ConnectZhProxy() ...@@ -512,7 +851,7 @@ int QFmClient::Try2ConnectZhProxy()
int rlt = 0; int rlt = 0;
unsigned long ul = 1; unsigned long ul = 1;
unsigned long sendsize = 32; //unsigned long sendsize = 32;
BOOL bNoDelay = TRUE; BOOL bNoDelay = TRUE;
fd_set fs; fd_set fs;
...@@ -619,7 +958,7 @@ int QFmClient::SendSocketData(int sock, char * buf, int length, int flag) ...@@ -619,7 +958,7 @@ int QFmClient::SendSocketData(int sock, char * buf, int length, int flag)
} }
return tmplen; return tmplen;
#else #else
return write(sock, psendBuf, length); return write(sock, psendBuf, length);
#endif #endif
} }
...@@ -646,14 +985,12 @@ int QFmClient::SendCtrlInfo2Proxy() ...@@ -646,14 +985,12 @@ int QFmClient::SendCtrlInfo2Proxy()
#endif #endif
return -1; return -1;
} }
return 0; return 0;
} }
int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type = 0 proxy re, = 1 pos re int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type = 0 proxy re, = 1 pos re
{ {
int recbytes, finLen, curPos = 0, prosLen = 0, checkCount = 0; int recbytes, finLen, curPos = 0, prosLen = 0, checkCount = 0;
int i = 0;
do do
{ {
char * precvBuf; char * precvBuf;
...@@ -685,7 +1022,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type = ...@@ -685,7 +1022,7 @@ int QFmClient::RecvAndCheckDataFromSock(int sock, int flag, int type) //type =
precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes); precvBuf = DecodeRecvData(_tempbuf, recbytes, &recbytes);
} }
qDebug() << "received data from socket " << sock << " : " << precvBuf; qDebug() << "received data from proxy " << sock << " :\n " << precvBuf;
while(0 < recbytes) while(0 < recbytes)
{ {
...@@ -742,12 +1079,12 @@ int QFmClient::SendData2ZhProxyAndWaitRspData() ...@@ -742,12 +1079,12 @@ int QFmClient::SendData2ZhProxyAndWaitRspData()
return -1; return -1;
} }
qDebug() << "wait and receive data from ZH Proxy...\r\n"; qDebug() << "wait and receive data from ZH Proxy...\r\n";
if (0 != RecvAndCheckDataFromSock(_zhProxySock, _codeFlag,0)) if (0 != RecvAndCheckDataFromSock(_zhProxySock, _codeFlag,0))
{ {
qDebug() << "receive data or check data failed\r\n"; qDebug() << "receive data or check data failed\r\n";
return -1; return -1;
} }
return 0; return 0;
...@@ -768,7 +1105,7 @@ int QFmClient::CheckRecvedData() ...@@ -768,7 +1105,7 @@ int QFmClient::CheckRecvedData()
return 0; return 0;
} }
int QFmClient::ProcessZhProxyRspDataAndSend2Pos() int QFmClient::ProcessZhProxyRspDataAndSend2Pos(int nType)
{ {
int rlt, length; int rlt, length;
...@@ -784,7 +1121,24 @@ int QFmClient::ProcessZhProxyRspDataAndSend2Pos() ...@@ -784,7 +1121,24 @@ int QFmClient::ProcessZhProxyRspDataAndSend2Pos()
//! TODO: 转换JSON //! TODO: 转换JSON
rlt = SendSocketData(_acceptedSock, _recvbuf, length, 0); QByteArray recvdata(_recvbuf);
QJsonParseError json_error;
QJsonDocument doc = QJsonDocument::fromJson(recvdata, &json_error);
QJsonObject object = doc.object();
ConvertResponseJsonFormat(object,nType);
QString str = QString(QJsonDocument(object).toJson(QJsonDocument::Compact));
QByteArray byteArray = str.toLocal8Bit();
const char* temp = byteArray.data();
size_t st = strlen(temp);
char* pResponse = new char[st + 1];
strcpy(pResponse,temp);
length = st;
rlt = SendSocketData(_acceptedSock, pResponse, length, 0);
delete pResponse;
if (rlt != length) if (rlt != length)
{ {
...@@ -818,7 +1172,7 @@ void QFmClient::run() ...@@ -818,7 +1172,7 @@ void QFmClient::run()
emit Error("Init QFmClient failed"); emit Error("Init QFmClient failed");
} }
qDebug()<< "Init success"; qDebug()<< "Init QFmClient success";
do do
{ {
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
...@@ -827,16 +1181,14 @@ void QFmClient::run() ...@@ -827,16 +1181,14 @@ void QFmClient::run()
if(!StartListenport()) if(!StartListenport())
{ {
qDebug() << "Start Listen port : " << _clientPort <<" failed"; qDebug() << "Start Listen port : " << _clientPort <<" failed";
WaitAndSleep(3000); WaitAndSleep(3000);
continue ; continue ;
} }
do do
{ {
int rlt, needBackup; int rlt, needBackup = 1;
int nResult = 0;
if (!WaitForConnectReqFromPos()) if (!WaitForConnectReqFromPos())
{ {
break; break;
...@@ -845,29 +1197,32 @@ void QFmClient::run() ...@@ -845,29 +1197,32 @@ void QFmClient::run()
if (0 != WaitAndReadDataFromPos(1, 0)) if (0 != WaitAndReadDataFromPos(1, 0))
{ {
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
continue; continue;
} }
QJsonObject object; QJsonObject object;
char temp_recv[MAX_BUF_LEN] = {0}; char temp_recv[MAX_BUF_LEN] = {0};
strcpy(temp_recv, _recvbuf); strcpy(temp_recv, _recvbuf);
needBackup = ProcessPosReqData(object); nResult = ProcessPosReqData(object);
if (0 > needBackup) if (0 > nResult)
{ {
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
qDebug()<<"ProcessPosReqData error";
continue;
}
if(nResult == SET_STORE_INFO)
{
sendSetResponseToPos(true);
CloseSocket(&_acceptedSock);
continue; continue;
} }
if (0 != Try2ConnectZhProxy()) if (0 != Try2ConnectZhProxy())
{ {
qDebug() << "connect to proxy failed"; qDebug() << "connect to proxy failed";
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
continue; continue;
} }
if (0 != SendCtrlInfo2Proxy()) if (0 != SendCtrlInfo2Proxy())
{ {
CloseSocket(&_acceptedSock); CloseSocket(&_acceptedSock);
...@@ -882,7 +1237,7 @@ void QFmClient::run() ...@@ -882,7 +1237,7 @@ void QFmClient::run()
if (0 == rlt) if (0 == rlt)
{ {
rlt = ProcessZhProxyRspDataAndSend2Pos(); rlt = ProcessZhProxyRspDataAndSend2Pos(nResult);
} }
CloseSocket(&_zhProxySock); CloseSocket(&_zhProxySock);
...@@ -891,17 +1246,13 @@ void QFmClient::run() ...@@ -891,17 +1246,13 @@ void QFmClient::run()
{ {
if (1 == needBackup) if (1 == needBackup)
{//is write off data, need to check roll back {//is write off data, need to check roll back
BackupPosReq(temp_recv); //BackupPosReq(temp_recv);
break; break;
} }
CloseSocket(&_acceptedSock);
} }
CloseSocket(&_acceptedSock);
}while(1); }while(1);
WaitAndSleep(3000); WaitAndSleep(3000);
}while(!_endflag); }while(!_endflag);
qDebug() << "Exit ZH Client!\r\n"; qDebug() << "Exit ZH Client!\r\n";
...@@ -909,7 +1260,6 @@ void QFmClient::run() ...@@ -909,7 +1260,6 @@ void QFmClient::run()
#ifdef WIN32 #ifdef WIN32
WSACleanup(); WSACleanup();
#endif #endif
} }
void QFmClient::CloseSocket(int *sock) void QFmClient::CloseSocket(int *sock)
...@@ -917,11 +1267,11 @@ void QFmClient::CloseSocket(int *sock) ...@@ -917,11 +1267,11 @@ void QFmClient::CloseSocket(int *sock)
if(0 < *sock) if(0 < *sock)
{ {
#ifdef WIN32 #ifdef WIN32
closesocket(*sock); closesocket(*sock);
#else #else
close(*sock); close(*sock);
#endif #endif
*sock = 0; *sock = 0;
} }
} }
......
...@@ -12,18 +12,20 @@ ...@@ -12,18 +12,20 @@
#define MAX_REQ_COUNT 25600 #define MAX_REQ_COUNT 25600
#define ROLL_BACK_FILE_NAME "fmclient.rbk" #define ROLL_BACK_FILE_NAME "fmclient.rbk"
#define SET_STORE_INFO 1000 //设置门店信息
#define FREEMUD_PAY 10031 //非码支付
#define QUERY_PAY 10030 //支付查询
#define REFUND_PAY 10041 //支付退款
class QFmClient : public QThread class QFmClient : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
QFmClient(QObject *parent = NULL); QFmClient(QObject *parent = NULL);
void run(); void run();
bool Init(QString proxy_IP, unsigned short proxy_port, unsigned short listen_prot, unsigned short long_timeout, unsigned short short_timeout); bool Init(QString proxy_IP, unsigned short proxy_port, unsigned short listen_prot, unsigned short long_timeout, unsigned short short_timeout);
bool InitStoreInfo();
signals: signals:
void Error(QString error); void Error(QString error);
private: private:
...@@ -36,9 +38,17 @@ private: ...@@ -36,9 +38,17 @@ private:
void WaitAndSleep(int elapse); void WaitAndSleep(int elapse);
bool WaitForConnectReqFromPos(); bool WaitForConnectReqFromPos();
int ProcessPosReqData(QJsonObject& object); int ProcessPosReqData(QJsonObject& object);
bool ConvertJsonFormat(QJsonObject& object); //convert pos requst json format from 非码支付插件 API 对接说明.pdf to 非码支付FMClient商户版_20170921.pdf bool ConvertRequestJsonFormat(QJsonObject& object); //convert pos request json format from 非码支付插件 API 对接说明.pdf to 非码支付FMClient商户版_20170921.pdf
void GetStoreInfo(QJsonObject& object); //获取门店信息 bool ConvertResponseJsonFormat(QJsonObject& object,int nType);
void sendResponseToPos(int nflag); void SetStoreInfo(QJsonObject& object); //设置门店信息
void FreemudPay(QJsonObject& object); //非码支付
bool QueryPay(QJsonObject& object); //支付查询
bool RefundPay(QJsonObject& object); //支付退款
bool FreemudPayResponse(QJsonObject& object); //非码支付回复处理
bool QueryPayResponse(QJsonObject& object); //支付查询回复处理
bool RefundPayResponse(QJsonObject& object); //支付退款回复处理
void sendSetResponseToPos(bool bFlag);
void AddReqCount(QJsonObject &object); void AddReqCount(QJsonObject &object);
int CheckIsCompleteJsonData(int *count, char *data); int CheckIsCompleteJsonData(int *count, char *data);
int RecvSockData(int sock, char *buffer, int length); int RecvSockData(int sock, char *buffer, int length);
...@@ -51,17 +61,15 @@ private: ...@@ -51,17 +61,15 @@ private:
char *EncodeSendData(char *buf, int length, int *eLength); char *EncodeSendData(char *buf, int length, int *eLength);
int SendData2ZhProxyAndWaitRspData(); int SendData2ZhProxyAndWaitRspData();
int RecvAndCheckDataFromSock(int sock, int flag, int type); int RecvAndCheckDataFromSock(int sock, int flag, int type);
int ProcessZhProxyRspDataAndSend2Pos(); int ProcessZhProxyRspDataAndSend2Pos(int nType);
int CheckRecvedData(); int CheckRecvedData();
void BackupPosReq(char *req); void BackupPosReq(char *req);
private:
private:
bool _endflag; bool _endflag;
char _recvbuf[MAX_BUF_LEN]; char _recvbuf[MAX_BUF_LEN];
char _tempbuf[MAX_BUF_LEN]; char _tempbuf[MAX_BUF_LEN];
char _sendbuf[MAX_BUF_LEN]; char _sendbuf[MAX_BUF_LEN];
char _codeBuf[MAX_BUF_LEN]; char _codeBuf[MAX_BUF_LEN];
unsigned char _codeKey[MAX_CODE_KEY]; unsigned char _codeKey[MAX_CODE_KEY];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment