CommonFunction.cs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. using CommonObj.lotoModel;
  2. using CommonObj.model;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Net.Sockets;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Xml;
  13. namespace CommonObj.common
  14. {
  15. public class CommonFunction
  16. {
  17. public static string getGuiId()
  18. {
  19. string _key = Guid.NewGuid().ToString();
  20. return _key;
  21. }
  22. public static tokenObj createToken(redisConnection _redis, string user, string pass, string channel, string type, string msisdn, string role)
  23. {
  24. tokenObj _result = new tokenObj();
  25. string _key = Guid.NewGuid().ToString();
  26. string _token = CustomEncryption.Encrypt(_key, _key);
  27. _result.key = _key;
  28. _result.token = _token;
  29. _result.users = user;
  30. _result.password = pass;
  31. _result.channel = channel;
  32. _result.type = type;
  33. _result.msisdn = msisdn;
  34. _result.role = role;
  35. //ghi lai token vao DB(1)
  36. int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT; //10 phút
  37. try
  38. {
  39. int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
  40. if (timeout_temp > 0) _token_timeout = timeout_temp;
  41. }
  42. catch
  43. { }
  44. TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
  45. _redis.setByKey(_redis.DB_INDEX_TOKEN, _token, _result.ToString(), _timeSpam);
  46. return _result;
  47. }
  48. public static bool dellToken(redisConnection _redis, string _token)
  49. {
  50. _redis.delByKey(_redis.DB_INDEX_TOKEN, _token);
  51. return true;
  52. }
  53. public static bool setToken(redisConnection _redis, tokenObj _tokenObj)
  54. {
  55. //ghi lai token vao DB(1)
  56. int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
  57. TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
  58. _redis.setByKey(_redis.DB_INDEX_TOKEN, _tokenObj.token, _tokenObj.ToString(), _timeSpam);
  59. return true;
  60. }
  61. public static tokenObj createToken(redisConnection _redis, string user, string pass, string channel, string type, string msisdn, string role, object _obj)
  62. {
  63. tokenObj _result = new tokenObj();
  64. string _key = Guid.NewGuid().ToString();
  65. string _token = CustomEncryption.Encrypt(_key, _key);
  66. _result.key = _key;
  67. _result.token = _token;
  68. _result.users = user;
  69. _result.password = pass;
  70. _result.channel = channel;
  71. _result.type = type;
  72. _result.msisdn = msisdn;
  73. _result.role = role;
  74. _result.obj = _obj;
  75. //ghi lai token vao DB(1)
  76. int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT; //10 phút
  77. try
  78. {
  79. _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
  80. }
  81. catch
  82. { }
  83. TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
  84. _redis.setByKey(_redis.DB_INDEX_TOKEN, _token, _result.ToString(), _timeSpam);
  85. return _result;
  86. }
  87. //Ham nay check token xem co bi sua doi gi ko? true
  88. public static string decryptToken(string token, string key, log4net.ILog _log)
  89. {
  90. string result = "";
  91. try
  92. {
  93. if (string.IsNullOrEmpty(token))
  94. return result;
  95. result = CustomEncryption.Decrypt(token, key);
  96. }
  97. catch (Exception ex)
  98. {
  99. _log.Info("Loi khi check token: " + ex.Message, ex);
  100. }
  101. _log.Info("Decrypt toke: " + token + " result:" + result);
  102. return result;
  103. }
  104. //Kiem tra xem token nay co phai la mot partner hop phap hay ko?
  105. //token sau deco thanh cong se co dang
  106. //select a.PARTNER_CODE||'_'||a.CODE||'_'||a.USERS_NAME||'_'||a.PASSWORD from LOTO_CHANNEL a
  107. public static bool checkPartnerToken(string token, string channel, string clientIp, redisConnection _redis, log4net.ILog _log)
  108. {
  109. bool result = false; //check token ko thanh cong
  110. try
  111. {
  112. //Lay ve key Encrypt thong tin cua partner
  113. string _keyEncryptPartner = getParamObjFromRedis(_redis, "KEY_ENCRYPT", "KEY_ENCRYPT", "SYSTEM").values;
  114. string _plaintext = decryptToken(token, _keyEncryptPartner, _log);
  115. _log.Info("checkPartnerToken: _keyEncryptPartner: " + _keyEncryptPartner + " _plaintext:" + _plaintext);
  116. if (string.IsNullOrEmpty(_plaintext))
  117. {
  118. result = false;
  119. }
  120. else
  121. {
  122. //Neu deco thanh cong se tien hanh boc tach thong tin
  123. string partnerCode = _plaintext.Split('_')[0];
  124. string code = _plaintext.Split('_')[1];
  125. string user = _plaintext.Split('_')[2];
  126. string pass = _plaintext.Split('_')[3];
  127. _log.Info("checkPartnerToken: partnerCode: " + partnerCode + " code:" + code + " user:" + user + " pass:" + pass);
  128. //Lay ve channel cua partner tuong ung de so sanh ket qua
  129. channelObj _channelObj = getChannelObjFromRedis(_redis, code);
  130. if (_channelObj.partnerCode == partnerCode && _channelObj.usersName == user && _channelObj.password == pass && code == channel)
  131. {
  132. //tiep tuc check IP
  133. if (_channelObj.listIp == "*")
  134. result = true;
  135. else
  136. {
  137. if (_channelObj.listIp.IndexOf("," + clientIp + ",") > 00)
  138. result = true;
  139. else
  140. result = false;
  141. }
  142. }
  143. else
  144. {
  145. _log.Info("_channelObj.partnerCode== partnerCode && _channelObj.usersName==user && _channelObj.password==pass && code== channel bi sau. Hay kiem tra lai ");
  146. result = false;
  147. }
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. _log.Info("Loi khi check token partner: " + ex.Message, ex);
  153. result = false;
  154. }
  155. _log.Info("Ket qua check checkPartnerToken : " + result);
  156. return result;
  157. }
  158. //Ham nay check token xem co bi sua doi gi ko?
  159. public static bool firstCheckToken(tokenObj _tokenObj, string token, log4net.ILog _log)
  160. {
  161. bool result = false; //check token ko thanh cong
  162. //Lay ve channel tuong ung
  163. try
  164. {
  165. string giaima_key = CustomEncryption.Decrypt(token, _tokenObj.key);
  166. if (string.IsNullOrEmpty(giaima_key))
  167. {
  168. _log.Info("Token :" + token + "da bi sua doi roi nhe. Co hacker");
  169. result = false;
  170. }
  171. else
  172. {
  173. if (giaima_key == _tokenObj.key)
  174. result = true;
  175. else
  176. result = false;
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. _log.Info("Loi khi check token: " + ex.Message, ex);
  182. result = false;
  183. }
  184. return result;
  185. }
  186. public static bool firstCheckToken(tokenObj _tokenObj, string token)
  187. {
  188. bool result = false; //check token ko thanh cong
  189. //Lay ve channel tuong ung
  190. try
  191. {
  192. string giaima_key = CustomEncryption.Decrypt(token, _tokenObj.key);
  193. if (string.IsNullOrEmpty(giaima_key))
  194. {
  195. //_log.Info("Token :" + token + "da bi sua doi roi nhe. Co hacker");
  196. result = false;
  197. }
  198. else
  199. {
  200. if (giaima_key == _tokenObj.token)
  201. result = true;
  202. else
  203. result = false;
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. //_log.Info("Loi khi check token: "+ex.Message,ex);
  209. result = false;
  210. }
  211. return result;
  212. }
  213. //ham nay kiem tra token co hop le hay ko?
  214. public static bool checkToken(string clientIp, string token, string channel, redisConnection _redis, log4net.ILog _log)
  215. {
  216. bool result = false; //check token ko thanh cong
  217. try
  218. {
  219. //1.Kiem tra xem token nay co phai cua partner nao ko?
  220. //- Neu thuc su cua partner thi tien thanh kiem tra channel,partnercode, user, pass va clientIp
  221. //- Neu ko phai cua partner thi tiep tuc kiem tra token nay co phai la mot login nao ko?
  222. //
  223. bool isPartner = checkPartnerToken(token, channel, clientIp, _redis, _log);
  224. if (isPartner)
  225. {
  226. _log.Info("the token: " + token + "la mot partner va da xac thuc thong tin dung");
  227. result = true;
  228. return result;
  229. }
  230. else
  231. {
  232. //truong ho nay se check token nay co phai la mot login hop le ko?
  233. if (!_redis.existsByKey(_redis.DB_INDEX_TOKEN, token))
  234. {
  235. //Truong hop nay kenh channel nay ko authen
  236. result = false;
  237. _log.Info("The token: " + token + " is not exits in redis select 1");
  238. return result;
  239. }
  240. tokenObj _token = JsonConvert.DeserializeObject<tokenObj>(_redis.getByKey(_redis.DB_INDEX_TOKEN, token));
  241. string plaintext = decryptToken(token, _token.key, _log);
  242. if (string.IsNullOrEmpty(plaintext))
  243. {
  244. result = false;
  245. _log.Info("The token: " + token + " have change");
  246. _redis.delByKey(_redis.DB_INDEX_TOKEN, token);
  247. return result;
  248. }
  249. else
  250. {
  251. if (plaintext == _token.key && channel == _token.channel)
  252. {
  253. result = true;
  254. //truong hop xac thuc thanh cong thi se gia han them thoi gian
  255. //int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
  256. int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT;//10 phút
  257. try
  258. {
  259. int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
  260. if (timeout_temp > 0) _token_timeout = timeout_temp;
  261. }
  262. catch
  263. { }
  264. TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
  265. _redis.setKeyTimeOut(_redis.DB_INDEX_TOKEN, token, _timeSpam);
  266. return result;
  267. }
  268. else
  269. {
  270. result = false;
  271. _log.Info("Decode token success but content invalid");
  272. return result;
  273. }
  274. }
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. _log.Info("Lôi khi checkToken " + ex.Message, ex);
  280. }
  281. _log.Info("Ket qua Authen " + result);
  282. return result;
  283. }
  284. //ham nay kiem tra token co hop le hay ko?
  285. //Nhu vay la neu out _outTokenObj ma null thi do la check token cua mot Partner local
  286. //Neu out _outTokenObj !=null thi do la mot token xac thuc tu Internet
  287. public static bool checkToken(string clientIp, string token, string channel, redisConnection _redis, log4net.ILog _log, out tokenObj _outTokenObj)
  288. {
  289. bool result = false; //check token ko thanh cong
  290. _outTokenObj = null;
  291. try
  292. {
  293. //1.Kiem tra xem token nay co phai cua partner nao ko?
  294. //- Neu thuc su cua partner thi tien thanh kiem tra channel,partnercode, user, pass va clientIp
  295. //- Neu ko phai cua partner thi tiep tuc kiem tra token nay co phai la mot login nao ko?
  296. //
  297. bool isPartner = checkPartnerToken(token, channel, clientIp, _redis, _log);
  298. if (isPartner)
  299. {
  300. _log.Info("the token: " + token + "la mot partner va da xac thuc thong tin dung");
  301. result = true;
  302. return result;
  303. }
  304. else
  305. {
  306. //truong ho nay se check token nay co phai la mot login hop le ko?
  307. if (!_redis.existsByKey(_redis.DB_INDEX_TOKEN, token))
  308. {
  309. //Truong hop nay kenh channel nay ko authen
  310. result = false;
  311. _log.Info("The token: " + token + " is not exits in redis select 1");
  312. return result;
  313. }
  314. tokenObj _token = JsonConvert.DeserializeObject<tokenObj>(_redis.getByKey(_redis.DB_INDEX_TOKEN, token));
  315. string plaintext = decryptToken(token, _token.key, _log);
  316. if (string.IsNullOrEmpty(plaintext))
  317. {
  318. result = false;
  319. _log.Info("The token: " + token + " have change");
  320. _redis.delByKey(_redis.DB_INDEX_TOKEN, token);
  321. return result;
  322. }
  323. else
  324. {
  325. if (plaintext == _token.key && channel == _token.channel)
  326. {
  327. result = true;
  328. //truong hop xac thuc thanh cong thi se gia han them thoi gian
  329. //int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
  330. int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT;//10 phút
  331. try
  332. {
  333. int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
  334. if (timeout_temp > 0) _token_timeout = timeout_temp;
  335. }
  336. catch
  337. { }
  338. TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
  339. _redis.setKeyTimeOut(_redis.DB_INDEX_TOKEN, token, _timeSpam);
  340. _outTokenObj = _token;
  341. return result;
  342. }
  343. else
  344. {
  345. result = false;
  346. _log.Info("Decode token success but content invalid");
  347. return result;
  348. }
  349. }
  350. }
  351. }
  352. catch (Exception ex)
  353. {
  354. _log.Info("Lôi khi checkToken " + ex.Message, ex);
  355. }
  356. _log.Info("Ket qua Authen " + result);
  357. return result;
  358. }
  359. public static paramObj getParamObjFromRedis(redisConnection _redis, string code, string codeGroup, string channel)
  360. {
  361. paramObj result = new paramObj();
  362. try
  363. {
  364. string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "PARAM_" + code + "_" + codeGroup + "_" + channel);
  365. result = JsonConvert.DeserializeObject<paramObj>(_objSting);
  366. }
  367. catch (Exception ex)
  368. {
  369. }
  370. return result;
  371. }
  372. ////KEY="BLACK_"+TYPE+"_"+SV_ID+"_"+MSISDN
  373. //--1= Nhan vien,2=Blacklist all,3=blacklist theo dich vu,4=whitelist theo dich vu
  374. public static blacklistObj getBlackListFromRedis(redisConnection _redis, string type, string serviceId, string msisdn)
  375. {
  376. blacklistObj result = new blacklistObj();
  377. try
  378. {
  379. string _objSting = _redis.getByKey(_redis.DB_INDEX_BLACKLIST, "BLACK_" + type + "_" + serviceId + "_" + msisdn);
  380. if (string.IsNullOrEmpty(_objSting)) return null;
  381. result = JsonConvert.DeserializeObject<blacklistObj>(_objSting);
  382. }
  383. catch (Exception ex)
  384. {
  385. }
  386. return result;
  387. }
  388. public static channelObj getChannelObjFromRedis(redisConnection _redis, string channel)
  389. {
  390. channelObj result = new channelObj();
  391. try
  392. {
  393. string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "CHANNEL_" + channel);
  394. result = JsonConvert.DeserializeObject<channelObj>(_objSting);
  395. }
  396. catch (Exception ex)
  397. {
  398. }
  399. return result;
  400. }
  401. public static termObj getTermObjObjFromRedis(redisConnection _redis, string gameId)
  402. {
  403. termObj result = new termObj();
  404. try
  405. {
  406. string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "TERM_" + gameId);
  407. result = JsonConvert.DeserializeObject<termObj>(_objSting);
  408. }
  409. catch (Exception ex)
  410. {
  411. }
  412. return result;
  413. }
  414. public static string getErrCodeObjFromRedis(redisConnection _redis, string channel, string errCode, string langauge)
  415. {
  416. //string _channelTemp = channel;
  417. string _channelTemp = "WEB";
  418. string _language = "0";
  419. if (!string.IsNullOrEmpty(langauge))
  420. _language = langauge;
  421. string _result = "";
  422. errCodeObj _obj = new errCodeObj();
  423. try
  424. {
  425. string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "ERRCODE_" + _channelTemp + "_" + errCode);
  426. _obj = JsonConvert.DeserializeObject<errCodeObj>(_objSting);
  427. if (_language == "0")
  428. _result = _obj.msg0;
  429. else if (_language == "1")
  430. _result = _obj.msg1;
  431. else if (_language == "2")
  432. _result = _obj.msg2;
  433. else if (_language == "3")
  434. _result = _obj.msg3;
  435. else if (_language == "4")
  436. _result = _obj.msg4;
  437. else if (_language == "5")
  438. _result = _obj.msg5;
  439. else
  440. _result = _obj.msg0;
  441. }
  442. catch (Exception ex)
  443. {
  444. }
  445. return _result;
  446. }
  447. public static string getErrCodeObjFromRedis(redisConnection _redis, string channel, string errCode)
  448. {
  449. string _result = "";
  450. errCodeObj _obj = new errCodeObj();
  451. try
  452. {
  453. string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "ERRCODE_" + channel + "_" + errCode);
  454. _obj = JsonConvert.DeserializeObject<errCodeObj>(_objSting);
  455. _result = _obj.msg0;
  456. }
  457. catch (Exception ex)
  458. {
  459. }
  460. return _result;
  461. }
  462. public static bool checkTicket(buyTicketObj _obj, out long totalMoney)
  463. {
  464. bool _result = false;
  465. totalMoney = 0;
  466. try
  467. {
  468. string gameId = _obj.gameId;
  469. int gameIdTemp;
  470. if (!int.TryParse(gameId, out gameIdTemp))
  471. {
  472. _result = false;
  473. return _result;
  474. }
  475. if (gameIdTemp < 3 || gameIdTemp > 9)
  476. {
  477. _result = false;
  478. return _result;
  479. }
  480. foreach (ticketObj _o in _obj.ticket)
  481. {
  482. string code = _o.code;
  483. int _codeTem;
  484. if (!int.TryParse(code, out _codeTem))
  485. {
  486. _result = false;
  487. return _result;
  488. }
  489. if (gameId == "3" && (code.Length != 2|| !IsNumber(code)))
  490. {
  491. _result = false;
  492. return _result;
  493. }
  494. if ((gameId == "3"||gameId == "4" || gameId == "7") && code.Length != 2)
  495. {
  496. _result = false;
  497. return _result;
  498. }
  499. if ((gameId == "5" || gameId == "8") && code.Length != 4)
  500. {
  501. _result = false;
  502. return _result;
  503. }
  504. if ((gameId == "6" || gameId == "9") && code.Length != 3)
  505. {
  506. _result = false;
  507. return _result;
  508. }
  509. long _money = 0;
  510. if (long.TryParse(_o.money, out _money))
  511. {
  512. if (_money > 0)
  513. totalMoney = totalMoney + _money;
  514. else
  515. {
  516. _result = false;
  517. return _result;
  518. }
  519. }
  520. else
  521. {
  522. _result = false;
  523. return _result;
  524. }
  525. }
  526. }
  527. catch (Exception ex)
  528. {
  529. }
  530. _result = true;
  531. return _result;
  532. }
  533. //ngoai kiem tra format, con kiem tra xem trong block co ve trung hay ko?
  534. //kiem tra trong cache da mua ve nao chua? kiem trong trong DB xem co ai mua chua?
  535. //tra them ve list cac ticke da duoc buy
  536. public static bool checkTicket_THAILAN(buyTicketObj _obj, out long totalMoney)
  537. {
  538. bool _result = false;
  539. totalMoney = 0;
  540. try
  541. {
  542. string gameId = _obj.gameId;
  543. int gameIdTemp;
  544. if (!int.TryParse(gameId, out gameIdTemp))
  545. {
  546. _result = false;
  547. return _result;
  548. }
  549. if (gameIdTemp !=1)
  550. {
  551. _result = false;
  552. return _result;
  553. }
  554. foreach (ticketObj _o in _obj.ticket)
  555. {
  556. string code = _o.code;
  557. int _codeTem;
  558. if (!int.TryParse(code, out _codeTem))
  559. {
  560. _result = false;
  561. return _result;
  562. }
  563. if (gameId == "1" && (code.Length != 6 || !IsNumber(code)))
  564. {
  565. _result = false;
  566. return _result;
  567. }
  568. //Kiem tra xem cac ticket o dang sau co cai nao trung voi ticket code dang set ko?
  569. int count = 0;
  570. foreach (ticketObj _k in _obj.ticket)
  571. {
  572. if(code==_k.code)
  573. {
  574. count++;
  575. if(count==2)
  576. {
  577. _result = false;
  578. return _result;
  579. }
  580. }
  581. }
  582. //Khong can phai kiem tra money
  583. //se tu tinh money
  584. }
  585. }
  586. catch (Exception ex)
  587. {
  588. }
  589. _result = true;
  590. return _result;
  591. }
  592. //neu da mua la true, chua mua la false
  593. public static bool check_THAILAN_IS_BUY_CACHE(redisConnection _redis, string _code,string randomId)
  594. {
  595. bool _result = true;
  596. try
  597. {
  598. List<string> lst_key = _redis.getAllkeyByPattern(_redis.DB_INDEX_DATA_USER, "TRANS_");
  599. //Kiem tra lock tam thoi
  600. foreach (var _key in lst_key)
  601. {
  602. buyTicketObj data = JsonConvert.DeserializeObject<buyTicketObj>(_redis.getByKey(_redis.DB_INDEX_DATA_USER, _key));
  603. for (int i = 0; i < data.ticket.Length; i++)
  604. {
  605. if (_code == data.ticket[i].code)
  606. {
  607. return true;
  608. }
  609. }
  610. }
  611. //kiem tra cac ve da mua
  612. if (_redis.existsByKey(_redis.DB_INDEX_DATA_TICKET_UNIQUE_BUY, _code+"_"+randomId))
  613. {
  614. return true;
  615. }
  616. }
  617. catch (Exception ex)
  618. {
  619. return true;
  620. }
  621. return false;
  622. }
  623. /// <summary>
  624. /// true: la ticket co so cuoi trung voi ticket duoc fix giai
  625. /// false: khong lien qua gi den ticket duoc fix gi
  626. /// </summary>
  627. /// <param name="_ticket"></param>
  628. /// <param name="_random"></param>
  629. /// <returns></returns>
  630. public static bool IsFixTicket(redisConnection _redis, string _ticket)
  631. {
  632. bool _result = false;
  633. try
  634. {
  635. string _fix_ticket = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "FIX_TICKET");
  636. if (!string.IsNullOrEmpty(_fix_ticket))
  637. {
  638. List<string> _list = new List<string>(_fix_ticket.Split(','));
  639. foreach (string _item in _list)
  640. {
  641. if (_item.Length == 6 && _ticket == _item)
  642. return true; //Vi pham vao fix ticket
  643. else
  644. {
  645. if (_ticket.Substring(_ticket.Length - _item.Length, _item.Length) == _item)
  646. {
  647. return true; //Vi pham vao fix ticket
  648. }
  649. }
  650. }
  651. }
  652. else
  653. {
  654. return true;//Truong hop nay bat buoc phai vao DB check cho an toan
  655. }
  656. }
  657. catch (Exception)
  658. {
  659. return true; //Truong hop nay bat buoc phai vao DB check cho an toan
  660. }
  661. return _result;
  662. }
  663. public static string chargeMPS_THAILAN(redisConnection _redis, string reqeust, string msisdn, string money,string mps_ip,string mps_port, log4net.ILog _log)
  664. {
  665. string _result = "-2"; //He thong nang cap -2:nang cap,0=thanh cong,1=Het tien,21=Thue bao ko active,23=Khong ton tai thue bao
  666. mpsResponse response = new mpsResponse();
  667. try
  668. {
  669. string serviceid = string.Empty, subServiceCode = string.Empty;
  670. if (money == "199")
  671. {
  672. serviceid = "1";
  673. subServiceCode = "POUKP_PACKAGE1";
  674. }
  675. else if (money == "398")
  676. {
  677. serviceid = "1";
  678. subServiceCode = "POUKP_PACKAGE2";
  679. }
  680. else if (money == "597")
  681. {
  682. serviceid = "1";
  683. subServiceCode = "POUKP_PACKAGE3";
  684. }
  685. else if (money == "769")
  686. {
  687. serviceid = "1";
  688. subServiceCode = "POUKP_PACKAGE4";
  689. }
  690. else if (money == "955")
  691. {
  692. serviceid = "1";
  693. subServiceCode = "POUKP_PACKAGE5";
  694. }
  695. string data = "requestId=" + reqeust + "&msisdn=" + msisdn + "&otp=" + "&serviceid=" + serviceid + "&subServiceCode=" + subServiceCode;
  696. _log.Info("Send MPS ip :" + mps_ip + " port: " + mps_port + " data: " + data);
  697. string response111 = SocketUnSyn(data, mps_ip, Convert.ToInt32(mps_port), 600000);
  698. _log.Info("Socket respones: " + response111);
  699. response = JsonConvert.DeserializeObject<mpsResponse>(response111);
  700. _log.Info("After convert MPSRESPONSE : " + response.ToString());
  701. if (response.responseCode == "0")
  702. {
  703. return "0";
  704. }
  705. else if (response.responseCode == "404")
  706. {
  707. return "23";
  708. }
  709. else if (response.responseCode == "409")
  710. {
  711. return "21";
  712. }
  713. else if (response.responseCode == "401"|| response.responseCode == "402"|| response.responseCode == "403")
  714. {
  715. return "1";
  716. }
  717. else
  718. {
  719. return "-2";
  720. }
  721. }
  722. catch (Exception ex)
  723. {
  724. }
  725. return _result;
  726. }
  727. //Lay ve goi cuoc khach hang dang su dung, de biet thue bao tra truoc hay tra sau
  728. public static string chargeGWGetPackage(redisConnection _redis, string reqeust, string msisdn, log4net.ILog _log)
  729. {
  730. string _result = "unknow";
  731. try
  732. {
  733. string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
  734. " <soapenv:Header/>\n" +
  735. " <soapenv:Body>\n" +
  736. " <web:gwOperation>\n" +
  737. " <Input>\n" +
  738. " <username>#username#</username>\n" +
  739. " <password>#password#</password>\n" +
  740. " <wscode>view8585</wscode>\n" +
  741. " <param name=\"GWORDER\" value=\"1\"/>\n" +
  742. " <param name=\"isdn\" value=\"#msisdn#\"/>\n" +
  743. " <!--Optional:-->\n" +
  744. " <rawData>1</rawData>\n" +
  745. " </Input>\n" +
  746. " </web:gwOperation>\n" +
  747. " </soapenv:Body>\n" +
  748. "</soapenv:Envelope>";
  749. string _msisdnTemp = msisdn;
  750. if (msisdn.Length > 3)
  751. {
  752. if (msisdn.Substring(0, 3) == "509")
  753. {
  754. _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
  755. }
  756. }
  757. string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_WSCODE", "CHARGE_BASIC_WSCODE", "SYSTEM").values;
  758. string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
  759. string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_USER", "CHARGE_BASIC_USER", "SYSTEM").values;
  760. string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_PASS", "CHARGE_BASIC_PASS", "SYSTEM").values;
  761. string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_URL", "CHARGE_BASIC_URL", "SYSTEM").values;
  762. //string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "CHARGE_NATCOM_LOTTO|1|" + money + "|" + "NATCOMLOTTO" + reqeust;
  763. xml = xml.Replace("#username#", topupBasicUser);
  764. xml = xml.Replace("#password#", topupBasicPass);
  765. //xml = xml.Replace("#wscode#", topupBasicWscode);
  766. xml = xml.Replace("#msisdn#", _msisdnTemp);
  767. _log.Info("Request :" + reqeust + ", XML: " + xml);
  768. _log.Info("View of request :" + reqeust);
  769. //string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
  770. //Tra co dinh 1 ket qua cho viec test local
  771. string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
  772. " <S:Body>\n" +
  773. " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
  774. " <Result>\n" +
  775. " <error>0</error>\n" +
  776. " <description>success</description>\n" +
  777. " <return>0|The transaction was done successfully</return>\n" +
  778. " <original><![CDATA[<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:chargeResponse xmlns:ns2=\"http://ws.api.vasgw.viettel.com/\"><return>0|The transaction was done successfully</return></ns2:chargeResponse></S:Body></S:Envelope>]]></original>\n" +
  779. " </Result>\n" +
  780. " </ns2:gwOperationResponse>\n" +
  781. " </S:Body>\n" +
  782. "</S:Envelope>";
  783. _log.Info("Response :" + reqeust + ": " + resultPost);
  784. string err_code_cmd = "-2000";
  785. string err_msg_cmd = "cmd err unknow";
  786. if (!string.IsNullOrEmpty(resultPost))
  787. {
  788. XmlDocument xmlDoc = new XmlDocument();
  789. xmlDoc.LoadXml(resultPost);
  790. err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
  791. err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
  792. if (err_code_cmd == "0")
  793. {
  794. string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
  795. _log.Info("ressult view :" + ketqua);
  796. _result = ketqua.Split('|')[6];
  797. _log.Info("The packet of msisdn :" + _msisdnTemp + " is: " + _result);
  798. }
  799. }
  800. }
  801. catch (Exception ex)
  802. {
  803. }
  804. return _result.ToUpper();
  805. }
  806. //tru tien khac hang
  807. public static string chargeGW(redisConnection _redis, string reqeust, string msisdn, string money, log4net.ILog _log)
  808. {
  809. string _result = "-2"; //He thong nang cap -2:nang cap,0=thanh cong,1=Het tien,21=Thue bao ko active,23=Khong ton tai thue bao
  810. try
  811. {
  812. string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
  813. " <soapenv:Header/>\n" +
  814. " <soapenv:Body>\n" +
  815. " <web:gwOperation>\n" +
  816. " <Input>\n" +
  817. " <username>#username#</username>\n" +
  818. " <password>#password#</password>\n" +
  819. " <wscode>charge8585</wscode>\n" +
  820. " <param name=\"GWORDER\" value=\"1\"/>\n" +
  821. " <param name=\"input\" value=\"#input#\"/>\n" +
  822. " <!--Optional:-->\n" +
  823. " <rawData></rawData>\n" +
  824. " </Input>\n" +
  825. " </web:gwOperation>\n" +
  826. " </soapenv:Body>\n" +
  827. "</soapenv:Envelope>";
  828. string _msisdnTemp = msisdn;
  829. if (msisdn.Length > 3)
  830. {
  831. if (msisdn.Substring(0, 3) == "509")
  832. {
  833. _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
  834. }
  835. }
  836. string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_WSCODE", "CHARGE_BASIC_WSCODE", "SYSTEM").values;
  837. string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
  838. string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_USER", "CHARGE_BASIC_USER", "SYSTEM").values;
  839. string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_PASS", "CHARGE_BASIC_PASS", "SYSTEM").values;
  840. string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_URL", "CHARGE_BASIC_URL", "SYSTEM").values;
  841. string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "CHARGE_NATCOM_LOTTO|1|" + money + "|" + "NATCOMLOTTO" + reqeust;
  842. xml = xml.Replace("#username#", topupBasicUser);
  843. xml = xml.Replace("#password#", topupBasicPass);
  844. xml = xml.Replace("#wscode#", topupBasicWscode);
  845. xml = xml.Replace("#input#", input);
  846. _log.Info("Request :" + reqeust + ", XML: " + xml);
  847. //string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
  848. //Tra co dinh 1 ket qua cho viec test local
  849. string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
  850. " <S:Body>\n" +
  851. " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
  852. " <Result>\n" +
  853. " <error>0</error>\n" +
  854. " <description>success</description>\n" +
  855. " <return>0|The transaction was done successfully</return>\n" +
  856. " <original><![CDATA[<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:chargeResponse xmlns:ns2=\"http://ws.api.vasgw.viettel.com/\"><return>0|The transaction was done successfully</return></ns2:chargeResponse></S:Body></S:Envelope>]]></original>\n" +
  857. " </Result>\n" +
  858. " </ns2:gwOperationResponse>\n" +
  859. " </S:Body>\n" +
  860. "</S:Envelope>";
  861. _log.Info("Response :" + reqeust + ": " + resultPost);
  862. string err_code_cmd = "-2000";
  863. string err_msg_cmd = "cmd err unknow";
  864. if (!string.IsNullOrEmpty(resultPost))
  865. {
  866. XmlDocument xmlDoc = new XmlDocument();
  867. xmlDoc.LoadXml(resultPost);
  868. err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
  869. err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
  870. string errCode, errMsg;
  871. if (err_code_cmd == "0")
  872. {
  873. string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
  874. _log.Info("ressult :" + ketqua);
  875. //He thong nang cap -2:nang cap,0=thanh cong,1=Het tien,21=Thue bao ko active,23=Khong ton tai thue bao
  876. errCode = ketqua.Split('|')[0];
  877. errMsg = ketqua.Split('|')[1];
  878. if (errCode == "0")
  879. _result = "0";
  880. else if (errCode == "S-PRF-00025")
  881. _result = "21";
  882. else if (errCode == "55")
  883. _result = "1";
  884. else
  885. _result = "-2";
  886. }
  887. }
  888. }
  889. catch (Exception ex)
  890. {
  891. }
  892. return _result;
  893. }
  894. //Chu y ca 2 ham cong tien nay cho tra ve 2 trang thai 0= thanh cong, -1 = that bai do trycat (trang thai nay se pending)
  895. //cong tai khoan goc
  896. public static string topupBasicAcount(redisConnection _redis, string reqeust, string msisdn, string money,
  897. out string transId, out string errCode, out string errMsg, log4net.ILog _log)
  898. {
  899. transId = reqeust;
  900. errCode = "-1";
  901. errMsg = "Flase";
  902. string _result = "-1"; //He thong nang cap
  903. try
  904. {
  905. string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
  906. " <soapenv:Header/>\n" +
  907. " <soapenv:Body>\n" +
  908. " <web:gwOperation>\n" +
  909. " <Input>\n" +
  910. " <username>#username#</username>\n" +
  911. " <password>#password#</password>\n" +
  912. " <wscode>#wscode#</wscode>\n" +
  913. " <param name=\"GWORDER\" value=\"1\"/>\n" +
  914. " <param name=\"input\" value=\"#input#\"/>\n" +
  915. " <rawData>1</rawData>\n" +
  916. " </Input>\n" +
  917. " </web:gwOperation>\n" +
  918. " </soapenv:Body>\n" +
  919. "</soapenv:Envelope>";
  920. string _msisdnTemp = msisdn;
  921. if (msisdn.Length > 3)
  922. {
  923. if (msisdn.Substring(0, 3) == "509")
  924. {
  925. _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
  926. }
  927. }
  928. string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_WSCODE", "TOPUP_BASIC_WSCODE", "SYSTEM").values;
  929. string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
  930. string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_USER", "TOPUP_BASIC_USER", "SYSTEM").values;
  931. string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_PASS", "TOPUP_BASIC_PASS", "SYSTEM").values;
  932. string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_URL", "TOPUP_BASIC_URL", "SYSTEM").values;
  933. string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "ADD_NATCOM_LOTTO|1|" + money + ".0|0|" + "NATCOMLOTTO" + reqeust;
  934. xml = xml.Replace("#username#", topupBasicUser);
  935. xml = xml.Replace("#password#", topupBasicPass);
  936. xml = xml.Replace("#wscode#", topupBasicWscode);
  937. xml = xml.Replace("#input#", input);
  938. _log.Info("Request :" + reqeust + ", XML: " + xml);
  939. string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
  940. //Tra co dinh 1 ket qua cho viec test local
  941. //string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
  942. // " <S:Body>\n" +
  943. // " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
  944. // " <Result>\n" +
  945. // " <error>0</error>\n" +
  946. // " <description>success</description>\n" +
  947. // " <return>0|The transaction was done successfully</return>\n" +
  948. // " <original><![CDATA[<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:chargeResponse xmlns:ns2=\"http://ws.api.vasgw.viettel.com/\"><return>0|The transaction was done successfully</return></ns2:chargeResponse></S:Body></S:Envelope>]]></original>\n" +
  949. // " </Result>\n" +
  950. // " </ns2:gwOperationResponse>\n" +
  951. // " </S:Body>\n" +
  952. // "</S:Envelope>";
  953. _log.Info("Response :" + reqeust + ": " + resultPost);
  954. string err_code_cmd = "-2000";
  955. string err_msg_cmd = "cmd err unknow";
  956. if (!string.IsNullOrEmpty(resultPost))
  957. {
  958. XmlDocument xmlDoc = new XmlDocument();
  959. xmlDoc.LoadXml(resultPost);
  960. err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
  961. err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
  962. if (err_code_cmd == "0")
  963. {
  964. string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
  965. _log.Info("ressult :" + ketqua);
  966. errCode = ketqua.Split('|')[0];
  967. errMsg = ketqua.Split('|')[1];
  968. }
  969. }
  970. _result = "0";//neu da den day thi chac chan hoac success hoac revert. Khong bao gio pending
  971. }
  972. catch (Exception ex)
  973. {
  974. }
  975. return _result;
  976. }
  977. //Chu y ca 2 ham cong tien nay cho tra ve 2 trang thai 0= thanh cong, -1 = that bai do trycat (trang thai nay se pending)
  978. public static string topupToWallet(redisConnection _redis, string reqeust, string msisdn, string money,
  979. out string transId, out string errCode, out string errMsg, log4net.ILog _log)
  980. {
  981. transId = reqeust;
  982. errCode = "-1";
  983. errMsg = "false";
  984. string _result = "-1"; //truong hop nay la loi try cath nen phai pending
  985. try
  986. {
  987. string jsomPost = "{\n" +
  988. " \"partnerCode\": \"#partnerCode#\",\n" +
  989. " \"accessKey\": \"#accessKey#\",\n" +
  990. " \"requestId\": \"#requestId#\",\n" +
  991. " \"requestTime\": \"#requestTime#\",\n" +
  992. " \"requestInfo\": \"#requestInfo#\",\n" +
  993. " \"functionName\": \"#functionName#\",\n" +
  994. " \"functionParams\": \"#functionParams#\",\n" +
  995. " \"signature\": \"#signature#\"\n" +
  996. "}";
  997. string topupWalletKeyPrivate = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_KEY_PRIVATE", "TOPUP_WALLET_KEY_PRIVATE", "SYSTEM").values;
  998. string topupWalletPartnerCode = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_PARTNER_CODE", "TOPUP_WALLET_PARTNER_CODE", "SYSTEM").values;
  999. string topupWalletUrl = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_URL", "TOPUP_WALLET_URL", "SYSTEM").values;
  1000. string topupWalletContentPay = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CONTEN_PAY", "TOPUP_WALLET_CONTEN_PAY", "SYSTEM").values;
  1001. string accessKey = Sha256Hash(topupWalletKeyPrivate + reqeust);
  1002. string requestTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fff");
  1003. string functionName = "dc92836b9910aecce8f4b216eeeb5506fd8de7a12f224a8938ddd3b79c500a1e";
  1004. string _msisdnTemp = msisdn;
  1005. //if (msisdn.Length > 3)
  1006. //{
  1007. // if (msisdn.Substring(0, 3) == "509")
  1008. // {
  1009. // _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
  1010. // }
  1011. //}
  1012. if (msisdn.Length > 3)
  1013. {
  1014. if (msisdn.Substring(0, 3) != "509")
  1015. {
  1016. _msisdnTemp = "509" + msisdn;
  1017. }
  1018. }
  1019. _log.Info("topup for msisdn: " + _msisdnTemp);
  1020. _log.Info("topupToWallet :" + reqeust + ", _msisdnTemp: " + _msisdnTemp);
  1021. string param = "{\"msisdn\":" + _msisdnTemp + ",\"amount\":" + money + "}";
  1022. string functionParams = Base64Encode(param);
  1023. string signature = "{accessKey=$accessKey&functionName=$functionName&functionParams=$functionParams&partnerCode=$partnerCode&requestId=$requestId&requestInfo=$requestInfo&requestTime=$requestTime}";
  1024. signature = signature.Replace("$accessKey", accessKey);
  1025. signature = signature.Replace("$functionName", functionName);
  1026. signature = signature.Replace("$functionParams", functionParams);
  1027. signature = signature.Replace("$partnerCode", topupWalletPartnerCode);
  1028. signature = signature.Replace("$requestId", reqeust);
  1029. signature = signature.Replace("$requestInfo", topupWalletContentPay);
  1030. signature = signature.Replace("$requestTime", requestTime);
  1031. signature = HMACSHA256Encode(signature, topupWalletKeyPrivate);
  1032. jsomPost = jsomPost.Replace("#partnerCode#", topupWalletPartnerCode);
  1033. jsomPost = jsomPost.Replace("#accessKey#", accessKey);
  1034. jsomPost = jsomPost.Replace("#requestId#", reqeust);
  1035. jsomPost = jsomPost.Replace("#requestTime#", requestTime);
  1036. jsomPost = jsomPost.Replace("#requestInfo#", topupWalletContentPay);
  1037. jsomPost = jsomPost.Replace("#functionName#", functionName);
  1038. jsomPost = jsomPost.Replace("#functionParams#", functionParams);
  1039. jsomPost = jsomPost.Replace("#signature#", signature);
  1040. string strResponse = SendPost(topupWalletUrl, jsomPost, _log);
  1041. //Tra ve ket qua co dinh, danh cho test local
  1042. //string strResponse = "{\n" +
  1043. // " \"partnerCode\": \"natcom_loto\",\n" +
  1044. // " \"requestId\": \"123457430\",\n" +
  1045. // " \"requestTime\": \"2022-02-18T14:06:03.547\",\n" +
  1046. // " \"responseTime\": \"2022-02-18T14:06:04.678\",\n" +
  1047. // " \"status\": \"200\",\n" +
  1048. // " \"message\": \"OK\",\n" +
  1049. // " \"result\": {\n" +
  1050. // " \"phoneNumber\": \"50940825038\",\n" +
  1051. // " \"responseDescription\": \"Transaction successfully!\",\n" +
  1052. // " \"transactionId\": \"22021840220258\",\n" +
  1053. // " \"responseCode\": \"00000\"\n" +
  1054. // " }\n" +
  1055. // "}";
  1056. _log.Info("Response :" + reqeust + ": " + strResponse);
  1057. topupWalletResponseObj response = JsonConvert.DeserializeObject<topupWalletResponseObj>(strResponse);
  1058. if (response.Status == "200")
  1059. {
  1060. errCode = response.Result.ResponseCode; //den day thi chac chan se ko pending, hoac la thanh cong, hoac la revert
  1061. errMsg = response.Result.ResponseDescription;
  1062. }
  1063. _result = "0"; //khi chay den day la moi lenh da thanh cong roi.
  1064. }
  1065. catch (Exception ex)
  1066. {
  1067. _log.Info("topup exeption: " + ex.ToString(), ex);
  1068. }
  1069. return _result;
  1070. }
  1071. //-1: He thong nang cap,1= Vi ton tai va active,2=Khong ton tai vi,3= Vi khong active
  1072. public static string checkExistsWallet(redisConnection _redis, string reqeust, string msisdn, log4net.ILog _log)
  1073. {
  1074. string _result = "-1"; //he thong nang cap
  1075. try
  1076. {
  1077. string jsomPost =
  1078. "{\n" +
  1079. " \"username\": \"$user\",\n" +
  1080. " \"password\": \"$pass\",\n" +
  1081. " \"msisdn\": \"$msisdn\",\n" +
  1082. " \"requestId\": \"$reqeust\"\n" +
  1083. "}";
  1084. string TOPUP_WALLET_CHECK_USER = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_USER", "TOPUP_WALLET_CHECK_USER", "SYSTEM").values;
  1085. string TOPUP_WALLET_CHECK_PASS = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_PASS", "TOPUP_WALLET_CHECK_PASS", "SYSTEM").values;
  1086. string TOPUP_WALLET_CHECK_URL = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_URL", "TOPUP_WALLET_CHECK_URL", "SYSTEM").values;
  1087. string accessKey = Sha256Hash(TOPUP_WALLET_CHECK_PASS + reqeust);
  1088. _log.Info("accessKey: " + accessKey);
  1089. jsomPost = jsomPost.Replace("$user", TOPUP_WALLET_CHECK_USER);
  1090. jsomPost = jsomPost.Replace("$pass", accessKey);
  1091. jsomPost = jsomPost.Replace("$msisdn", msisdn);
  1092. jsomPost = jsomPost.Replace("$reqeust", reqeust);
  1093. _log.Info("Request :" + reqeust + ", jsomPost: " + jsomPost);
  1094. string strResponse = SendPost(TOPUP_WALLET_CHECK_URL, jsomPost, _log);
  1095. //Tra ve ket qua co dinh, danh cho test local
  1096. //string strResponse = "{\n" +
  1097. // " \"status\": \"200\",\n" +
  1098. // " \"timestamp\": \"2022-02-24T05:51:56.800\",\n" +
  1099. // " \"message\": \"OK\",\n" +
  1100. // " \"result\": {\n" +
  1101. // " \"accountState\": 1,\n" +
  1102. // " \"role\": \"EU\",\n" +
  1103. // " \"msisdn\": \"50940825038\",\n" +
  1104. // " \"registerDate\": \"2022-01-27 22:43:01\"\n" +
  1105. // " }\n" +
  1106. // "}";
  1107. _log.Info("Response :" + reqeust + ": " + strResponse);
  1108. //-1: He thong nang cap,1= Vi ton tai va active,2=Khong ton tai vi,3= Vi khong active
  1109. walletCheckExists response = JsonConvert.DeserializeObject<walletCheckExists>(strResponse);
  1110. if (response.Status == "200")
  1111. {
  1112. if (response.Result.AccountState == "1" && response.Result.Role == "EU")
  1113. _result = "1";
  1114. else if (response.Result.AccountState == "-1")
  1115. _result = "2";
  1116. else
  1117. _result = "3";
  1118. }
  1119. }
  1120. catch (Exception ex)
  1121. {
  1122. }
  1123. return _result;
  1124. }
  1125. public static String SendPost(string url, string str, log4net.ILog _log)
  1126. {
  1127. _log.Info("start port URL: " + url);
  1128. _log.Info("Reqeust: " + str);
  1129. var data = new StringContent(str, Encoding.UTF8, "application/json");
  1130. //StringBuilder sb = new StringBuilder();
  1131. //sb.Append("Request: " + data);
  1132. using (var client = new HttpClient())
  1133. {
  1134. TimeSpan timeSpam = new TimeSpan(0, 5, 0);
  1135. client.Timeout = timeSpam;
  1136. var response = client.PostAsync(url, data).Result;
  1137. if (response.IsSuccessStatusCode)
  1138. {
  1139. var responseContent = response.Content;
  1140. //sb.Append("\r\nResponse: " + responseContent);
  1141. //log.Debug(sb.ToString());
  1142. // by calling .Result you are synchronously reading the result
  1143. string responseString = responseContent.ReadAsStringAsync().Result;
  1144. _log.Info("Response: " + responseString);
  1145. return responseString;
  1146. }
  1147. else
  1148. {
  1149. _log.Info("Post not succssss: " + response.IsSuccessStatusCode.ToString());
  1150. }
  1151. //sb.Append("\r\nResponse: " + response);
  1152. //log.Debug(sb.ToString());
  1153. return response.StatusCode.ToString();
  1154. }
  1155. }
  1156. public static string sendPostXML(string api_path, string timeout, string xml)
  1157. {
  1158. string rp = "";
  1159. try
  1160. {
  1161. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api_path);
  1162. byte[] bytes;
  1163. bytes = System.Text.Encoding.UTF8.GetBytes(xml);
  1164. request.ContentType = "text/xml; encoding='utf-8'";
  1165. request.ContentLength = bytes.Length;
  1166. request.Method = "POST";
  1167. request.Timeout = Convert.ToInt32(timeout);
  1168. Stream requestStream = request.GetRequestStream();
  1169. requestStream.Write(bytes, 0, bytes.Length);
  1170. requestStream.Close();
  1171. HttpWebResponse response;
  1172. response = (HttpWebResponse)request.GetResponse();
  1173. Stream responseStream = response.GetResponseStream();
  1174. rp = new StreamReader(responseStream).ReadToEnd();
  1175. }
  1176. catch (Exception ex)
  1177. {
  1178. }
  1179. return rp;
  1180. }
  1181. public static bool WriteTextFile(String fileName, String t)
  1182. {
  1183. TextWriter textWriter;
  1184. try
  1185. {
  1186. textWriter = new StreamWriter(fileName);
  1187. }
  1188. catch (Exception)
  1189. {
  1190. return false;
  1191. }
  1192. try
  1193. {
  1194. textWriter.WriteLine(t);
  1195. }
  1196. catch (Exception)
  1197. {
  1198. return false;
  1199. }
  1200. textWriter.Close();
  1201. return true;
  1202. }
  1203. public static string subStrBetween(string STR, string FirstString, string LastString)
  1204. {
  1205. string FinalString;
  1206. int Pos1 = STR.IndexOf(FirstString) + FirstString.Length;
  1207. int Pos2 = STR.IndexOf(LastString);
  1208. FinalString = STR.Substring(Pos1, Pos2 - Pos1);
  1209. return FinalString;
  1210. }
  1211. public static string stringRemoveFrom0ToString(string STR, string Stringkey)
  1212. {
  1213. string FinalString;
  1214. int Pos1 = STR.IndexOf(Stringkey) + Stringkey.Length;
  1215. FinalString = STR.Remove(0, Pos1);
  1216. return FinalString;
  1217. }
  1218. public static string stringGet1CharFirstString(string STR, string Stringkey)
  1219. {
  1220. string FinalString;
  1221. int Pos1 = STR.IndexOf(Stringkey);
  1222. FinalString = STR.Substring(Pos1 - 1, 1);
  1223. return FinalString;
  1224. }
  1225. public static string Sha256Hash(string rawData)
  1226. {
  1227. // Create a SHA256
  1228. using (SHA256 sha256Hash = SHA256.Create())
  1229. {
  1230. // ComputeHash - returns byte array
  1231. byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
  1232. // Convert byte array to a string
  1233. StringBuilder builder = new StringBuilder();
  1234. for (int i = 0; i < bytes.Length; i++)
  1235. {
  1236. builder.Append(bytes[i].ToString("x2"));
  1237. }
  1238. return builder.ToString();
  1239. }
  1240. }
  1241. public static string Base64Encode(string plainText)
  1242. {
  1243. var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  1244. return System.Convert.ToBase64String(plainTextBytes);
  1245. }
  1246. public static string Base64Decode(string base64EncodedData)
  1247. {
  1248. var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
  1249. return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
  1250. }
  1251. public static string HMACSHA256Encode(string input, string key)
  1252. {
  1253. byte[] k = Encoding.ASCII.GetBytes(key);
  1254. HMACSHA256 myhmacsha256 = new HMACSHA256(k);
  1255. byte[] byteArray = Encoding.ASCII.GetBytes(input);
  1256. using (MemoryStream stream = new MemoryStream(byteArray))
  1257. {
  1258. return byteToHex(myhmacsha256.ComputeHash(stream));
  1259. }
  1260. }
  1261. private static string byteToHex(byte[] hash)
  1262. {
  1263. return BitConverter.ToString(hash).Replace("-", "").ToLower();
  1264. }
  1265. public static string ByteArrayToHexString(byte[] ba)
  1266. {
  1267. StringBuilder hex = new StringBuilder(ba.Length * 2);
  1268. foreach (byte b in ba)
  1269. {
  1270. hex.AppendFormat("{0:X2}", b);
  1271. }
  1272. return hex.ToString();
  1273. }
  1274. public static bool IsNumber(string aNumber)
  1275. {
  1276. int numericValue;
  1277. bool isNumber = int.TryParse(aNumber, out numericValue);
  1278. return isNumber;
  1279. }
  1280. public static string SocketUnSyn(string msg, string host, int port, int timeout)
  1281. {
  1282. //log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  1283. //log4net.Config.XmlConfigurator.Configure();
  1284. string result = "-1000";
  1285. TcpClient client = null;
  1286. NetworkStream stream = null;
  1287. try
  1288. {
  1289. byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
  1290. client = new TcpClient(host, port);
  1291. client.SendTimeout = timeout;
  1292. stream = client.GetStream();
  1293. BinaryWriter writer = new BinaryWriter(stream);
  1294. writer.Write(data);
  1295. String _readFromStream = String.Empty;
  1296. byte[] data_read = new byte[1024];
  1297. using (MemoryStream ms = new MemoryStream())
  1298. {
  1299. int numBytesRead;
  1300. while ((numBytesRead = stream.Read(data_read, 0, data_read.Length)) > 0)
  1301. {
  1302. ms.Write(data_read, 0, numBytesRead);
  1303. }
  1304. _readFromStream = System.Text.Encoding.ASCII.GetString(ms.ToArray(), 0, (int)ms.Length);
  1305. }
  1306. //string _readFromStream = GetResponse(stream);
  1307. /*
  1308. data = new Byte[1024];
  1309. String responseData = String.Empty;
  1310. Int32 bytes = stream.Read(data, 0, data.Length);
  1311. responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  1312. */
  1313. // Close everything.
  1314. stream.Close();
  1315. client.Close();
  1316. //Phan tich data tra ve:
  1317. result = _readFromStream;
  1318. }
  1319. catch (ArgumentNullException e)
  1320. {
  1321. stream.Close();
  1322. client.Close();
  1323. return "-1000";
  1324. }
  1325. catch (SocketException e)
  1326. {
  1327. return "-1000";
  1328. }
  1329. return result;
  1330. }
  1331. }
  1332. }