CommonFunction_test.cs 55 KB

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