Commit 2ce7ed1f by LIDINGDA\ldd

1.修复自动冲正不追加parentorderid问题;2.添加模板自动补齐功能;3.添加数据库支付POS主动发起券冲正

parent 19bc2799
No preview for this file type
......@@ -63,7 +63,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\openssl\openssl-1.0.1s\out32dll;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>libeay32.lib;ssleay32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>libeay32.lib;ssleay32.lib;sqlite3.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
......@@ -96,6 +96,7 @@
<ClCompile Include="strconv.c" />
<ClCompile Include="tastdataprocess.cpp" />
<ClCompile Include="testlog.cpp" />
<ClCompile Include="testtool.cpp" />
<ClCompile Include="utf.c" />
<ClCompile Include="value.c" />
</ItemGroup>
......@@ -103,17 +104,21 @@
<ClInclude Include="dataprocess.h" />
<ClInclude Include="filesystem.h" />
<ClInclude Include="fmcrypt.h" />
<ClInclude Include="fmdatabase.h" />
<ClInclude Include="fmerror.h" />
<ClInclude Include="fmglobal.h" />
<ClInclude Include="fmlog.h" />
<ClInclude Include="fmtool.h" />
<ClInclude Include="hashtable.h" />
<ClInclude Include="jansson.h" />
<ClInclude Include="jansson_config.h" />
<ClInclude Include="jansson_private.h" />
<ClInclude Include="lookup3.h" />
<ClInclude Include="sqlite3.h" />
<ClInclude Include="strbuffer.h" />
<ClInclude Include="testdataprocess.h" />
<ClInclude Include="testlog.h" />
<ClInclude Include="testtool.h" />
<ClInclude Include="utf.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
......
......@@ -63,6 +63,9 @@
<ClCompile Include="value.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="testtool.cpp">
<Filter>Source Files\TestCase</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="fmcrypt.h">
......@@ -110,5 +113,17 @@
<ClInclude Include="strbuffer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="fmtool.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="testtool.h">
<Filter>Header Files\TestCase</Filter>
</ClInclude>
<ClInclude Include="sqlite3.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="fmdatabase.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#ifndef DATABASE_H_
#define DATABASE_H_
#include <iostream>
#include "sqlite3.h"
#include "filesystem.h"
#include "fmglobal.h"
#include "fmlog.h"
using std::string;
class DataBase
{
public:
static int update(const string& sql)
{
string path;
sqlite3 *dbhend = NULL;
char *err = NULL;
FileSys::GetProcPath(path);
path.append(DB_ORDER);
int rlt = sqlite3_open(path.c_str(), &dbhend);
if(rlt)
{
LOG() << "open " << path << " failed";
return 0;
}
int res = sqlite3_exec(dbhend, sql.c_str(), 0, 0, &err);
if(res != SQLITE_OK)
{
LOG() << "update table failed error : " << err;
sqlite3_close(dbhend);
return 0;
}
sqlite3_close(dbhend);
return 1;
}
static int creat(const string& sql)
{
string path;
sqlite3 *dbhend = NULL;
char *err = NULL;
FileSys::GetProcPath(path);
path.append(DB_ORDER);
int rlt = sqlite3_open(path.c_str(), &dbhend);
if(rlt)
{
LOG() << "open " << path << " failed";
return 0;
}
int res = sqlite3_exec(dbhend, sql.c_str(), 0, 0, &err);
if(res != SQLITE_OK)
{
LOG() << "creat table failed error : " << err;
sqlite3_close(dbhend);
return 0;
}
sqlite3_close(dbhend);
return 1;
}
static int insert(const string& sql)
{
string path;
sqlite3 *dbhend = NULL;
char *err = NULL;
FileSys::GetProcPath(path);
path.append(DB_ORDER);
int rlt = sqlite3_open(path.c_str(), &dbhend);
if(rlt)
{
LOG() << "open " << path << " failed";
return 0;
}
int res = sqlite3_exec(dbhend, sql.c_str(), 0, 0, &err);
if(res != SQLITE_OK)
{
LOG() << "creat table failed error : " << err;
sqlite3_close(dbhend);
return 0;
}
sqlite3_close(dbhend);
return 1;
}
static int callback(void* dst ,int nCount,char** pValue,char** pName)
{
strcpy_s((char *)dst, MAX_ORDERID_LEN - 1, pValue[0]);
return 0;
}
static int select(const string& sql, string &result)
{
string path;
sqlite3 *dbhend = NULL;
char *err = NULL;
FileSys::GetProcPath(path);
path.append(DB_ORDER);
int rlt = sqlite3_open(path.c_str(), &dbhend);
if(rlt)
{
LOG() << "open " << path << " failed";
return 0;
}
char parentOrederID[MAX_ORDERID_LEN] = { 0 };
int res = sqlite3_exec(dbhend, sql.c_str(), callback, parentOrederID, &err);
if(res != SQLITE_OK)
{
LOG() << "slelct table failed error : " << err;
sqlite3_close(dbhend);
return 0;
}
result = string(parentOrederID);
sqlite3_close(dbhend);
return 1;
}
static int delt(const string &sql)
{
string path;
sqlite3 *dbhend = NULL;
char *err = NULL;
FileSys::GetProcPath(path);
path.append(DB_ORDER);
int rlt = sqlite3_open(path.c_str(), &dbhend);
if(rlt)
{
LOG() << "open " << path << " failed";
return 0;
}
int res = sqlite3_exec(dbhend, sql.c_str(), 0, 0, &err);
if(res != SQLITE_OK)
{
LOG() << "delete table failed error : " << err;
sqlite3_close(dbhend);
return 0;
}
sqlite3_close(dbhend);
return 1;
}
};
#endif
#ifndef FM_GLOBAL_H
#define FM_GLOBAL_H
#define NUM_SIZE 60
#define KEY_SIZE 24
#define MAX_LOG_LINE_NUM 1024
#define MAX_RSA_KEY_LEN 2048
......@@ -14,4 +15,10 @@
#define CRET_FILE_NAME "client.p12"
#define JSON_KEY_ONLY "partnerOrderId"
#define CFG_KEY "template"
#define TMP_JSON "json.ini"
#define DB_ORDER "order.db"
#define DB_TABLE_NAME "orderlist"
#define MAX_ORDERID_LEN 200
#endif
\ No newline at end of file
#ifndef FM_TOOL_H_
#define FM_TOOL_H_
#include <jansson.h>
#include <jansson_private.h>
#include <Windows.h>
#include "fmglobal.h"
#include "fmlog.h"
#include "filesystem.h"
class Tool
{
public:
static int ReadTMPForType(char *tplt, int reqType)
{
char tmp[MAX_BUF_LEN] = { 0 };
std::string path;
char type[64] = { 0 };
itoa(reqType, type, 10);
FileSys::GetProcPath(path);
path.append(TMP_JSON);
LOG() << "template cfg file path :" << path;
GetPrivateProfileString(type, CFG_KEY,"",tmp, MAX_BUF_LEN, path.c_str());
strcpy(tplt, tmp);
//LOG() << "get template json : " << tplt;
return (tmp[0] == 0 ? 0 : 1);
}
static void CompareJson(json_t *tplt, json_t *tmpin)
{
if(json_is_object(tplt) && json_is_object(tmpin))
{
const char *key;
json_t *value;
json_object_foreach(tplt, key, value)
{
if(value->type == json_type::JSON_OBJECT)
{
json_t *tmps = json_object_get(tmpin, key);
if(tmps)
{
CompareJson(value, tmps);
}
else
{
json_object_set(tmpin, key, value);
}
}
else if(value->type == json_type::JSON_ARRAY)
{
json_t *tmps = json_object_get(tmpin, key);
if(tmps)
{
CompareJson(value, tmps);
}
else
{
json_object_set(tmpin, key, value);
}
}
else
{
json_t *tmps = json_object_get(tmpin, key);
if(tmps)
continue;
else
{
json_object_set(tmpin, key, value);
}
}
}
}
else if(json_is_array(tplt) && json_is_array(tmpin))
{
int index = 0;
json_t *value, *tmpvalue;
value = json_array_get(tplt, 0);
if(value)
{
json_array_foreach(tmpin, index, tmpvalue)
{
CompareJson(value, tmpvalue);
}
if(index == 0)
json_array_append(tmpin, value);
}
}
}
static int ComplementJson(const char *in, char *out, int reqType)
{
char templatestr[MAX_BUF_LEN] = { 0 };
if(ReadTMPForType(templatestr, reqType) == 0)
{
LOG() << "get temple failed";
return 0;
}
json_t *root, *value, *tmpin;
json_error_t error;
root = json_loads(templatestr, 0, &error);
tmpin = json_loads(in, 0, &error);
LOG() << "get temple success : " << json_dumps(root, JSON_COMPACT | JSON_SORT_KEYS);
if(!root)
{
LOG() << "error temple or injson json(reqType:"<< reqType << "):" << templatestr;
return 0;
}
if( !tmpin )
{
LOG() << "error temple or injson json(reqType:"<< reqType << "):" << in;
return 0;
}
CompareJson(root, tmpin);
char *tmpstr = json_dumps(tmpin, JSON_COMPACT | JSON_SORT_KEYS);
strcpy(out, tmpstr);
LOG() << "complement json success : " << out;
json_decref(root);
json_decref(tmpin);
return 1;
}
};
#endif
\ No newline at end of file
......@@ -14,12 +14,15 @@
#include "fmlog.h"
#include "curl.h"
#include "dataprocess.h"
#include "fmtool.h"
#include "fmdatabase.h"
//#define FM_TEST
//#define FM_TESTS
#include "testdataprocess.h"
#include "testlog.h"
#include "testtool.h"
using std::cout;
using std::endl;
......@@ -262,13 +265,44 @@ int InitClient()
return 1;
}
int ProcessPosReqData(const char *indata, string &in, int *needrbk)
time_t subDay(time_t time1,int days)
{
int type, rlt = 0;
return (time1-(days*60*60*24-28800));
}
void DeleteTableForDate()
{
time_t tt = time(NULL);
time_t tmptt = subDay(tt, 2);
tm *t = localtime(&tmptt);
std::stringstream stm;
stm << t->tm_year + 1900 << "-" << std::setw(2) << std::setfill('0') << t->tm_mon + 1 << "-";
stm << std::setw(2) << std::setfill('0') << t->tm_mday;
LOG() << "delete data" << stm.str().c_str();
std::stringstream deletesql;
deletesql << "delete from " << DB_TABLE_NAME << " where businessDate<datetime('" << stm.str().c_str() << "')" ;
std::stringstream tmps;
json_t *root, *reqtype, *partnerId, *storeId, *stationId;
LOG() << "delete sql : " << deletesql.str().c_str();
DataBase::delt(string(deletesql.str().c_str()));
}
int ProcessPosReqData(const char *indata, string &in, int *needrbk, int *reqType_i)
{
int type, rlt = 0;
*reqType_i = -1;
std::stringstream tmps, tmpkey;
json_t *root, *reqtype, *partnerId, *storeId, *stationId, *code, *transId, *businessDate;
json_error_t error;
string codestr;
string transIdstr;
string date;
*needrbk == 0;
......@@ -280,7 +314,53 @@ int ProcessPosReqData(const char *indata, string &in, int *needrbk)
return 0;
}
reqtype = json_object_get(root, "reqType");
if (!json_is_integer(reqtype))
{
LOG() << "error: reqtype is not a integer!";
rlt = 0;
goto freeJson;
}
type = (int)json_integer_value(reqtype);
*reqType_i = type;
if(type == 72 || type == 62 || type == 71)
*needrbk = 1;
if(type == 71 || type == 3)
{
code = json_object_get(root, "code");
if (!json_is_string(code))
{
LOG() << "error: code is not a string!";
rlt = 0;
goto freeJson;
}
codestr = string(json_string_value(code));
businessDate = json_object_get(root, "businessDate");
if (!json_is_string(businessDate))
{
LOG() << "error: businessDate is not a string!";
rlt = 0;
goto freeJson;
}
date = string(json_string_value(businessDate));
date.insert(4, "-");
date.insert(7, "-");
transId = json_object_get(root, "transId");
if (!json_is_string(transId))
{
LOG() << "error: transId is not a string!";
rlt = 0;
goto freeJson;
}
transIdstr = string(json_string_value(transId));
}
partnerId = json_object_get(root, "partnerId");
if (!json_is_integer(partnerId))
{
......@@ -318,28 +398,54 @@ int ProcessPosReqData(const char *indata, string &in, int *needrbk)
LOG() << "partnerOrderId code : " << tmps.str();
json_object_set(root, JSON_KEY_ONLY, json_string(tmps.str().c_str()));
in = string(json_dumps(root, JSON_COMPACT | JSON_SORT_KEYS));
if(type != 3)
json_object_set(root, JSON_KEY_ONLY, json_string(tmps.str().c_str()));
reqtype = json_object_get(root, "reqType");
if (!json_is_integer(reqtype))
if(type == 71)
{
LOG() << "error: reqtype is not a integer!";
rlt = 0;
goto freeJson;
tmpkey << codestr << transIdstr << json_integer_value(partnerId) << json_string_value(storeId) << json_string_value(stationId);
std::stringstream creatsql, insetsql, updatesql;
creatsql << "create table IF NOT EXISTS " << DB_TABLE_NAME << "(orderID varchar(200) NOT NULL,parentID varchar(60),businessDate date,PRIMARY KEY (orderID))";
insetsql << "insert into " << DB_TABLE_NAME << "(orderID,parentID,businessDate)values('" << tmpkey.str().c_str() << "','" << tmps.str().c_str() << "','" << date <<"')";
updatesql << "update " << DB_TABLE_NAME << " set parentID='" << tmps.str().c_str() << "',businessDate='" << date << "' where orderID='" << tmpkey.str().c_str() << "'" ;
LOG() << "creat table : " << creatsql.str().c_str();
LOG() << "insert table : " << insetsql.str().c_str();
LOG() << "update table : " << updatesql.str().c_str();
DataBase::creat(string(creatsql.str().c_str()));
DataBase::insert(string(insetsql.str().c_str()));
DataBase::update(string(updatesql.str().c_str()));
}
type = (int)json_integer_value(reqtype);
if(type == 72 || type == 62 || type == 71)
*needrbk = 1;
if(type == 3)
{
tmpkey << codestr << transIdstr << (int)json_integer_value(partnerId) << (char *)json_string_value(storeId) << (char *)json_string_value(stationId);
std::stringstream selectsql;
selectsql << "select parentID from " << DB_TABLE_NAME << " where orderID='" << tmpkey.str().c_str() << "'" ;
LOG() << "select table : " << selectsql.str().c_str();
string prid;
if(DataBase::select(string(selectsql.str().c_str()), prid) == 1)
{
LOG() << "get partnerOrderId : " << prid;
json_object_set(root, JSON_KEY_ONLY, json_string(prid.c_str()));
}else
{
goto freeJson;
}
}
in = string(json_dumps(root, JSON_COMPACT | JSON_SORT_KEYS));
rlt = 1;
freeJson:
json_decref(root);
return rlt;
}
......@@ -440,6 +546,10 @@ extern "C"
__declspec(dllexport) int _stdcall GetValue(char *indata, char *outdata)
{
int reqType_i = 0;
DeleteTableForDate();
//读取配置文件
if(InitClient() == 0)
{
......@@ -495,7 +605,7 @@ __declspec(dllexport) int _stdcall GetValue(char *indata, char *outdata)
int needrbk = 0;
if(ProcessPosReqData(tmpin.c_str(), in, &needrbk) != 1)
if(ProcessPosReqData(tmpin.c_str(), in, &needrbk, &reqType_i) != 1)
{
strcpy(outdata, ERROR_INDATA);
LOG() << "return error message to pos :" << outdata ;
......@@ -516,7 +626,7 @@ __declspec(dllexport) int _stdcall GetValue(char *indata, char *outdata)
if(ssign == -1 && needrbk == 1)
{
if(SaveRollBackFile(string(indata)) == 0)
if(SaveRollBackFile(in) == 0)
LOG() << "Save Roll Back file failed";
goto endflag;
}
......@@ -541,8 +651,16 @@ __declspec(dllexport) int _stdcall GetValue(char *indata, char *outdata)
//UTF8ToGBK(outtmp, outgbk, MAX_BUF_LEN - 1000);
//LOG() << "UTF-8 to GBK :" << outgbk;
strcpy(outdata, global_info.recvbuf.c_str());
char cmpstr[MAX_BUF_LEN] = {0};
if(Tool::ComplementJson(global_info.recvbuf.c_str(), cmpstr, reqType_i) == 0)
{
strcpy(outdata, global_info.recvbuf.c_str());
}
else
{
strcpy(outdata, cmpstr);
}
endflag:
//检查是否存在冲正文件
......@@ -957,9 +1075,9 @@ void testinitclient()
//char a[] = "{\"ver\":1,\"partnerId\":1234,\"reqType\": 62,\"coupon\": \"122222222222\",\"stationId\": \"1\",\"storeId\": \"999999\",\"operator_id\": \"00000002\" }";
//char a[] = "{\r\n \"pay_transId\":\"2017052600001162504297\",\r\n \"total_amount\":1,\r\n \"mcoupon_amount\":0,\r\n \"alipay_amount\":1,\r\n \"pay_ebcode\":\"10010\",\r\n \"ver\":1,\r\n \"fmId\":\"SXA1377O27012186844\",\r\n \"pay_date\":\"2017-05-26 17:01:16.004\",\r\n \"statusCode\":100\r\n}";
//char a[] = "{\"businessDate\":\"20170526\",\"code\":\"7310180009462033\",\"operatorId\":\"90001\",\"partnerId\":1443,\"products\":[{\"consume_num\":1,\"discount\":0,\"name\":\"Coffee\",\"number\":0,\"payment\":null,\"pid\":\"3488\",\"price\":3100,\"priceAct\":0,\"salesType\":\"Normal\",\"scope\":null}],\"promotionTag\":null,\"reqType\":72,\"stationId\":\"3\",\"storeId\":\"1713\",\"transAmount\":3100,\"transId\":6460,\"undiscountAmount\":0,\"ver\":1}";
//char a[] = "{\r\n \"ver\": 1,\r\n \"reqType\": 0,\r\n \"partnerId\": 1443,\r\n \"stationId\": \"123123\",\r\n \"storeId\": \"12344\",\r\n \"operatorId\": \"00000002\",\r\n \"code\": \"7320020400887150\"\r\n}";
char a[] = "{\r\n \"businessDate\": \"20170601\",\r\n \"code\": \"7310188850000277\",\r\n \"operatorId\": \"90001\",\r\n \"partnerId\": 1443,\r\n \"partnerOrderId\": \"1443171223323\",\r\n \"products\": [\r\n {\r\n \"consume_num\": 1,\r\n \"discount\": 0,\r\n \"name\": \"Coffee\",\r\n \"number\": 0,\r\n \"payment\": null,\r\n \"pid\": \"3488\",\r\n \"price\": 3100,\r\n \"priceAct\": 0,\r\n \"salesType\": \"Normal\",\r\n \"scope\": null\r\n }\r\n ],\r\n \"promotionTag\": null,\r\n \"reqType\": 72,\r\n \"stationId\": \"9\",\r\n \"storeId\": \"12344\",\r\n \"transAmount\": 29,\r\n \"transId\": 1,\r\n \"undiscountAmount\": 0,\r\n \"ver\": 1\r\n}";
char a[] = "{\"businessDate\":\"20170526\",\"code\":\"7310180009462033\",\"operatorId\":\"90001\",\"partnerId\":1443,\"products\":[{\"consume_num\":1,\"discount\":0,\"name\":\"Coffee\",\"number\":0,\"payment\":null,\"pid\":\"3488\",\"price\":3100,\"priceAct\":0,\"salesType\":\"Normal\",\"scope\":null}],\"promotionTag\":null,\"reqType\":71,\"stationId\":\"3\",\"storeId\":\"1713\",\"transAmount\":3100,\"transId\":\"6460\",\"undiscountAmount\":0,\"ver\":1}";
//char a[] = "{\r\n \"ver\": 1,\r\n \"reqType\": 71,\r\n \"partnerId\": 1443,\r\n \"stationId\": \"123123\",\r\n \"storeId\": \"12344\",\r\n \"operatorId\": \"00000002\",\r\n \"code\": \"7320020400887150\"\r\n}";
//char a[] = "{\r\n \"businessDate\": \"20170601\",\r\n \"code\": \"7310188850000277\",\r\n \"operatorId\": \"90001\",\r\n \"partnerId\": 1443,\r\n \"partnerOrderId\": \"1443171223323\",\r\n \"products\": [\r\n {\r\n \"consume_num\": 1,\r\n \"discount\": 0,\r\n \"name\": \"Coffee\",\r\n \"number\": 0,\r\n \"payment\": null,\r\n \"pid\": \"3488\",\r\n \"price\": 3100,\r\n \"priceAct\": 0,\r\n \"salesType\": \"Normal\",\r\n \"scope\": null\r\n }\r\n ],\r\n \"promotionTag\": null,\r\n \"reqType\": 72,\r\n \"stationId\": \"9\",\r\n \"storeId\": \"12344\",\r\n \"transAmount\": 29,\r\n \"transId\": 1,\r\n \"undiscountAmount\": 0,\r\n \"ver\": 1\r\n}";
void testCheckRollbackData()
{
InitClient();
......@@ -972,8 +1090,10 @@ int main()
char storeid[] = "12344";
char posno[] = "01";
Test_RSASign();
Test_RSAVerify();
TestComplementJson();
//Test_RSASign();
//Test_RSAVerify();
//Test_GetPEMKey();
//Test_3DES();
//Test_LOG();
......@@ -986,10 +1106,10 @@ int main()
//TestRETDES();
GetRSACret(1443, storeid, posno);
//GetRSACret(1443, storeid, posno);
//while(1)
//{
GetValue(a, b);
//GetValue(a, b);
// LOG() << b;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
#include "fmglobal.h"
#include "fmtool.h"
#include "fmlog.h"
void TestComplementJson()
{
//char in[] = "{\"ver\":1,\"statusCode\":0,\"message\":\"\",\"paymentMethod\":\"\",\"paymentMethodCode\":\"\",\"ext\":{\"print\":\"\",\"hint\":\"\"},\"info\":{\"status\":\"\",\"statusDesc\":\"\",\"actId\":\"\",\"actName\":\"\",\"vdata\":\"\",\"code\":\"\",\"authCode\":\"\",\"amount\":8,\"products\":[]}}";
char in[] = "{\"ver\":1,\"message\":\"\",\"products\":[{\"pid\":4,\"name\":\"lcd\"},{\"name\":\"lcd\"},{\"pid\":4},{}]}";
char out[MAX_BUF_LEN] = { 0 };
Tool::ComplementJson(in, out, 95);
LOG() << out ;
}
\ No newline at end of file
#ifdef FM_TEST
void TestComplementJson();
#endif
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