| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695 |
- using CommonObj.lotoModel;
- using CommonObj.model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Sockets;
- using System.Security.Cryptography;
- using System.Text;
- using System.Xml;
- namespace CommonObj.common
- {
- public class CommonFunction
- {
- public static string getGuiId()
- {
- string _key = Guid.NewGuid().ToString();
- return _key;
- }
- public static tokenObj createToken(redisConnection _redis, string user, string pass, string channel, string type, string msisdn, string role)
- {
- tokenObj _result = new tokenObj();
- string _key = Guid.NewGuid().ToString();
- string _token = CustomEncryption.Encrypt(_key, _key);
- _result.key = _key;
- _result.token = _token;
- _result.users = user;
- _result.password = pass;
- _result.channel = channel;
- _result.type = type;
- _result.msisdn = msisdn;
- _result.role = role;
- //ghi lai token vao DB(1)
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT; //10 phút
- try
- {
- int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- if (timeout_temp > 0) _token_timeout = timeout_temp;
- }
- catch
- { }
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setByKey(_redis.DB_INDEX_TOKEN, _token, _result.ToString(), _timeSpam);
- return _result;
- }
- public static bool dellToken(redisConnection _redis, string _token)
- {
- _redis.delByKey(_redis.DB_INDEX_TOKEN, _token);
- return true;
- }
- public static bool setToken(redisConnection _redis, tokenObj _tokenObj)
- {
- //ghi lai token vao DB(1)
- int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setByKey(_redis.DB_INDEX_TOKEN, _tokenObj.token, _tokenObj.ToString(), _timeSpam);
- return true;
- }
- public static tokenObj createToken(redisConnection _redis, string user, string pass, string channel, string type, string msisdn, string role, object _obj)
- {
- tokenObj _result = new tokenObj();
- string _key = Guid.NewGuid().ToString();
- string _token = CustomEncryption.Encrypt(_key, _key);
- _result.key = _key;
- _result.token = _token;
- _result.users = user;
- _result.password = pass;
- _result.channel = channel;
- _result.type = type;
- _result.msisdn = msisdn;
- _result.role = role;
- _result.obj = _obj;
- //ghi lai token vao DB(1)
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT; //10 phút
- try
- {
- _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- }
- catch
- { }
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setByKey(_redis.DB_INDEX_TOKEN, _token, _result.ToString(), _timeSpam);
- return _result;
- }
- //Ham nay check token xem co bi sua doi gi ko? true
- public static string decryptToken(string token, string key, log4net.ILog _log)
- {
- string result = "";
- try
- {
- if (string.IsNullOrEmpty(token))
- return result;
- result = CustomEncryption.Decrypt(token, key);
- }
- catch (Exception ex)
- {
- _log.Info("Loi khi check token: " + ex.Message, ex);
- }
- _log.Info("Decrypt toke: " + token + " result:" + result);
- return result;
- }
- //Kiem tra xem token nay co phai la mot partner hop phap hay ko?
- //token sau deco thanh cong se co dang
- //select a.PARTNER_CODE||'_'||a.CODE||'_'||a.USERS_NAME||'_'||a.PASSWORD from LOTO_CHANNEL a
- public static bool checkPartnerToken(string token, string channel, string clientIp, redisConnection _redis, log4net.ILog _log)
- {
- bool result = false; //check token ko thanh cong
- try
- {
- //Lay ve key Encrypt thong tin cua partner
- string _keyEncryptPartner = getParamObjFromRedis(_redis, "KEY_ENCRYPT", "KEY_ENCRYPT", "SYSTEM").values;
- string _plaintext = decryptToken(token, _keyEncryptPartner, _log);
- _log.Info("checkPartnerToken: _keyEncryptPartner: " + _keyEncryptPartner + " _plaintext:" + _plaintext);
- if (string.IsNullOrEmpty(_plaintext))
- {
- result = false;
- }
- else
- {
- //Neu deco thanh cong se tien hanh boc tach thong tin
- string partnerCode = _plaintext.Split('_')[0];
- string code = _plaintext.Split('_')[1];
- string user = _plaintext.Split('_')[2];
- string pass = _plaintext.Split('_')[3];
- _log.Info("checkPartnerToken: partnerCode: " + partnerCode + " code:" + code + " user:" + user + " pass:" + pass);
- //Lay ve channel cua partner tuong ung de so sanh ket qua
- channelObj _channelObj = getChannelObjFromRedis(_redis, code);
- if (_channelObj.partnerCode == partnerCode && _channelObj.usersName == user && _channelObj.password == pass && code == channel)
- {
- //tiep tuc check IP
- if (_channelObj.listIp == "*")
- result = true;
- else
- {
- if (_channelObj.listIp.IndexOf("," + clientIp + ",") > 00)
- result = true;
- else
- result = false;
- }
- }
- else
- {
- _log.Info("_channelObj.partnerCode== partnerCode && _channelObj.usersName==user && _channelObj.password==pass && code== channel bi sau. Hay kiem tra lai ");
- result = false;
- }
- }
- }
- catch (Exception ex)
- {
- _log.Info("Loi khi check token partner: " + ex.Message, ex);
- result = false;
- }
- _log.Info("Ket qua check checkPartnerToken : " + result);
- return result;
- }
- //Ham nay check token xem co bi sua doi gi ko?
- public static bool firstCheckToken(tokenObj _tokenObj, string token, log4net.ILog _log)
- {
- bool result = false; //check token ko thanh cong
- //Lay ve channel tuong ung
- try
- {
- string giaima_key = CustomEncryption.Decrypt(token, _tokenObj.key);
- if (string.IsNullOrEmpty(giaima_key))
- {
- _log.Info("Token :" + token + "da bi sua doi roi nhe. Co hacker");
- result = false;
- }
- else
- {
- if (giaima_key == _tokenObj.key)
- result = true;
- else
- result = false;
- }
- }
- catch (Exception ex)
- {
- _log.Info("Loi khi check token: " + ex.Message, ex);
- result = false;
- }
- return result;
- }
- public static bool firstCheckToken(tokenObj _tokenObj, string token)
- {
- bool result = false; //check token ko thanh cong
- //Lay ve channel tuong ung
- try
- {
- string giaima_key = CustomEncryption.Decrypt(token, _tokenObj.key);
- if (string.IsNullOrEmpty(giaima_key))
- {
- //_log.Info("Token :" + token + "da bi sua doi roi nhe. Co hacker");
- result = false;
- }
- else
- {
- if (giaima_key == _tokenObj.token)
- result = true;
- else
- result = false;
- }
- }
- catch (Exception ex)
- {
- //_log.Info("Loi khi check token: "+ex.Message,ex);
- result = false;
- }
- return result;
- }
- //ham nay kiem tra token co hop le hay ko?
- public static bool checkToken(string clientIp, string token, string channel, redisConnection _redis, log4net.ILog _log)
- {
- bool result = false; //check token ko thanh cong
- try
- {
- //1.Kiem tra xem token nay co phai cua partner nao ko?
- //- Neu thuc su cua partner thi tien thanh kiem tra channel,partnercode, user, pass va clientIp
- //- Neu ko phai cua partner thi tiep tuc kiem tra token nay co phai la mot login nao ko?
- //
- bool isPartner = checkPartnerToken(token, channel, clientIp, _redis, _log);
- if (isPartner)
- {
- _log.Info("the token: " + token + "la mot partner va da xac thuc thong tin dung");
- result = true;
- return result;
- }
- else
- {
- //truong ho nay se check token nay co phai la mot login hop le ko?
- if (!_redis.existsByKey(_redis.DB_INDEX_TOKEN, token))
- {
- //Truong hop nay kenh channel nay ko authen
- result = false;
- _log.Info("The token: " + token + " is not exits in redis select 1");
- return result;
- }
- tokenObj _token = JsonConvert.DeserializeObject<tokenObj>(_redis.getByKey(_redis.DB_INDEX_TOKEN, token));
- string plaintext = decryptToken(token, _token.key, _log);
- if (string.IsNullOrEmpty(plaintext))
- {
- result = false;
- _log.Info("The token: " + token + " have change");
- _redis.delByKey(_redis.DB_INDEX_TOKEN, token);
- return result;
- }
- else
- {
- if (plaintext == _token.key && channel == _token.channel)
- {
- result = true;
- //truong hop xac thuc thanh cong thi se gia han them thoi gian
- //int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT;//10 phút
- try
- {
- int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- if (timeout_temp > 0) _token_timeout = timeout_temp;
- }
- catch
- { }
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setKeyTimeOut(_redis.DB_INDEX_TOKEN, token, _timeSpam);
- return result;
- }
- else
- {
- result = false;
- _log.Info("Decode token success but content invalid");
- return result;
- }
- }
- }
- }
- catch (Exception ex)
- {
- _log.Info("Lôi khi checkToken " + ex.Message, ex);
- }
- _log.Info("Ket qua Authen " + result);
- return result;
- }
- //ham nay kiem tra token co hop le hay ko?
- //Nhu vay la neu out _outTokenObj ma null thi do la check token cua mot Partner local
- //Neu out _outTokenObj !=null thi do la mot token xac thuc tu Internet
- public static bool checkToken(string clientIp, string token, string channel, redisConnection _redis, log4net.ILog _log, out tokenObj _outTokenObj)
- {
- bool result = false; //check token ko thanh cong
- _outTokenObj = null;
- try
- {
- //1.Kiem tra xem token nay co phai cua partner nao ko?
- //- Neu thuc su cua partner thi tien thanh kiem tra channel,partnercode, user, pass va clientIp
- //- Neu ko phai cua partner thi tiep tuc kiem tra token nay co phai la mot login nao ko?
- //
- bool isPartner = checkPartnerToken(token, channel, clientIp, _redis, _log);
- if (isPartner)
- {
- _log.Info("the token: " + token + "la mot partner va da xac thuc thong tin dung");
- result = true;
- return result;
- }
- else
- {
- //truong ho nay se check token nay co phai la mot login hop le ko?
- if (!_redis.existsByKey(_redis.DB_INDEX_TOKEN, token))
- {
- //Truong hop nay kenh channel nay ko authen
- result = false;
- _log.Info("The token: " + token + " is not exits in redis select 1");
- return result;
- }
- tokenObj _token = JsonConvert.DeserializeObject<tokenObj>(_redis.getByKey(_redis.DB_INDEX_TOKEN, token));
- string plaintext = decryptToken(token, _token.key, _log);
- if (string.IsNullOrEmpty(plaintext))
- {
- result = false;
- _log.Info("The token: " + token + " have change");
- _redis.delByKey(_redis.DB_INDEX_TOKEN, token);
- return result;
- }
- else
- {
- if (plaintext == _token.key && channel == _token.channel)
- {
- result = true;
- //truong hop xac thuc thanh cong thi se gia han them thoi gian
- //int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT;//10 phút
- try
- {
- int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- if (timeout_temp > 0) _token_timeout = timeout_temp;
- }
- catch
- { }
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setKeyTimeOut(_redis.DB_INDEX_TOKEN, token, _timeSpam);
- _outTokenObj = _token;
- return result;
- }
- else
- {
- result = false;
- _log.Info("Decode token success but content invalid");
- return result;
- }
- }
- }
- }
- catch (Exception ex)
- {
- _log.Info("Lôi khi checkToken " + ex.Message, ex);
- }
- _log.Info("Ket qua Authen " + result);
- return result;
- }
- public static paramObj getParamObjFromRedis(redisConnection _redis, string code, string codeGroup, string channel)
- {
- paramObj result = new paramObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "PARAM_" + code + "_" + codeGroup + "_" + channel);
- result = JsonConvert.DeserializeObject<paramObj>(_objSting);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- ////KEY="BLACK_"+TYPE+"_"+SV_ID+"_"+MSISDN
- //--1= Nhan vien,2=Blacklist all,3=blacklist theo dich vu,4=whitelist theo dich vu
- public static blacklistObj getBlackListFromRedis(redisConnection _redis, string type, string serviceId, string msisdn)
- {
- blacklistObj result = new blacklistObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_BLACKLIST, "BLACK_" + type + "_" + serviceId + "_" + msisdn);
- if (string.IsNullOrEmpty(_objSting)) return null;
- result = JsonConvert.DeserializeObject<blacklistObj>(_objSting);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- public static channelObj getChannelObjFromRedis(redisConnection _redis, string channel)
- {
- channelObj result = new channelObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "CHANNEL_" + channel);
- result = JsonConvert.DeserializeObject<channelObj>(_objSting);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- public static termObj getTermObjObjFromRedis(redisConnection _redis, string gameId)
- {
- termObj result = new termObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "TERM_" + gameId);
- result = JsonConvert.DeserializeObject<termObj>(_objSting);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- public static string getErrCodeObjFromRedis(redisConnection _redis, string channel, string errCode, string langauge)
- {
- //string _channelTemp = channel;
- string _channelTemp = "WEB";
- string _language = "0";
- if (!string.IsNullOrEmpty(langauge))
- _language = langauge;
- string _result = "";
- errCodeObj _obj = new errCodeObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "ERRCODE_" + _channelTemp + "_" + errCode);
- _obj = JsonConvert.DeserializeObject<errCodeObj>(_objSting);
- if (_language == "0")
- _result = _obj.msg0;
- else if (_language == "1")
- _result = _obj.msg1;
- else if (_language == "2")
- _result = _obj.msg2;
- else if (_language == "3")
- _result = _obj.msg3;
- else if (_language == "4")
- _result = _obj.msg4;
- else if (_language == "5")
- _result = _obj.msg5;
- else
- _result = _obj.msg0;
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- public static string getErrCodeObjFromRedis(redisConnection _redis, string channel, string errCode)
- {
- string _result = "";
- errCodeObj _obj = new errCodeObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "ERRCODE_" + channel + "_" + errCode);
- _obj = JsonConvert.DeserializeObject<errCodeObj>(_objSting);
- _result = _obj.msg0;
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- public static bool checkTicket(buyTicketObj _obj, out long totalMoney)
- {
- bool _result = false;
- totalMoney = 0;
- try
- {
- string gameId = _obj.gameId;
- int gameIdTemp;
- if (!int.TryParse(gameId, out gameIdTemp))
- {
- _result = false;
- return _result;
- }
- if (gameIdTemp < 3 || gameIdTemp > 9)
- {
- _result = false;
- return _result;
- }
- foreach (ticketObj _o in _obj.ticket)
- {
- string code = _o.code;
- int _codeTem;
- if (!int.TryParse(code, out _codeTem))
- {
- _result = false;
- return _result;
- }
- if (gameId == "3" && (code.Length != 2|| !IsNumber(code)))
- {
- _result = false;
- return _result;
- }
- if ((gameId == "3"||gameId == "4" || gameId == "7") && code.Length != 2)
- {
- _result = false;
- return _result;
- }
- if ((gameId == "5" || gameId == "8") && code.Length != 4)
- {
- _result = false;
- return _result;
- }
- if ((gameId == "6" || gameId == "9") && code.Length != 3)
- {
- _result = false;
- return _result;
- }
- long _money = 0;
- if (long.TryParse(_o.money, out _money))
- {
- if (_money > 0)
- totalMoney = totalMoney + _money;
- else
- {
- _result = false;
- return _result;
- }
- }
- else
- {
- _result = false;
- return _result;
- }
- }
- }
- catch (Exception ex)
- {
- }
- _result = true;
- return _result;
- }
- //ngoai kiem tra format, con kiem tra xem trong block co ve trung hay ko?
- //kiem tra trong cache da mua ve nao chua? kiem trong trong DB xem co ai mua chua?
- //tra them ve list cac ticke da duoc buy
- public static bool checkTicket_THAILAN(buyTicketObj _obj, out long totalMoney)
- {
- bool _result = false;
- totalMoney = 0;
-
- try
- {
- string gameId = _obj.gameId;
- int gameIdTemp;
- if (!int.TryParse(gameId, out gameIdTemp))
- {
- _result = false;
- return _result;
- }
- if (gameIdTemp !=1)
- {
- _result = false;
- return _result;
- }
- foreach (ticketObj _o in _obj.ticket)
- {
- string code = _o.code;
- int _codeTem;
- if (!int.TryParse(code, out _codeTem))
- {
- _result = false;
- return _result;
- }
- if (gameId == "1" && (code.Length != 6 || !IsNumber(code)))
- {
- _result = false;
- return _result;
- }
- //Kiem tra xem cac ticket o dang sau co cai nao trung voi ticket code dang set ko?
- int count = 0;
- foreach (ticketObj _k in _obj.ticket)
- {
- if(code==_k.code)
- {
- count++;
- if(count==2)
- {
- _result = false;
- return _result;
- }
-
- }
- }
- //Khong can phai kiem tra money
- //se tu tinh money
- }
-
- }
- catch (Exception ex)
- {
- }
- _result = true;
- return _result;
- }
- //neu da mua la true, chua mua la false
- public static bool check_THAILAN_IS_BUY_CACHE(redisConnection _redis, string _code,string randomId)
- {
- bool _result = true;
-
-
- try
- {
-
- List<string> lst_key = _redis.getAllkeyByPattern(_redis.DB_INDEX_DATA_USER, "TRANS_");
- //Kiem tra lock tam thoi
- foreach (var _key in lst_key)
- {
- buyTicketObj data = JsonConvert.DeserializeObject<buyTicketObj>(_redis.getByKey(_redis.DB_INDEX_DATA_USER, _key));
- for (int i = 0; i < data.ticket.Length; i++)
- {
- if (_code == data.ticket[i].code)
- {
- return true;
- }
- }
- }
- //kiem tra cac ve da mua
- if (_redis.existsByKey(_redis.DB_INDEX_DATA_TICKET_UNIQUE_BUY, _code+"_"+randomId))
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// true: la ticket co so cuoi trung voi ticket duoc fix giai
- /// false: khong lien qua gi den ticket duoc fix gi
- /// </summary>
- /// <param name="_ticket"></param>
- /// <param name="_random"></param>
- /// <returns></returns>
- public static bool IsFixTicket(redisConnection _redis, string _ticket)
- {
- bool _result = false;
- try
- {
- string _fix_ticket = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "FIX_TICKET");
- if (!string.IsNullOrEmpty(_fix_ticket))
- {
- List<string> _list = new List<string>(_fix_ticket.Split(','));
- foreach (string _item in _list)
- {
- if (_item.Length == 6 && _ticket == _item)
- return true; //Vi pham vao fix ticket
- else
- {
- if (_ticket.Substring(_ticket.Length - _item.Length, _item.Length) == _item)
- {
- return true; //Vi pham vao fix ticket
- }
- }
- }
- }
- else
- {
- return true;//Truong hop nay bat buoc phai vao DB check cho an toan
- }
- }
- catch (Exception)
- {
- return true; //Truong hop nay bat buoc phai vao DB check cho an toan
- }
- return _result;
- }
- public static string chargeMPS_THAILAN(redisConnection _redis, string reqeust, string msisdn, string money,string mps_ip,string mps_port, log4net.ILog _log)
- {
- 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
- mpsResponse response = new mpsResponse();
- try
- {
- string serviceid = string.Empty, subServiceCode = string.Empty;
- if (money == "199")
- {
- serviceid = "1";
- subServiceCode = "POUKP_PACKAGE1";
- }
- else if (money == "398")
- {
- serviceid = "1";
- subServiceCode = "POUKP_PACKAGE2";
- }
- else if (money == "597")
- {
- serviceid = "1";
- subServiceCode = "POUKP_PACKAGE3";
- }
- else if (money == "769")
- {
- serviceid = "1";
- subServiceCode = "POUKP_PACKAGE4";
- }
- else if (money == "955")
- {
- serviceid = "1";
- subServiceCode = "POUKP_PACKAGE5";
- }
- string data = "requestId=" + reqeust + "&msisdn=" + msisdn + "&otp=" + "&serviceid=" + serviceid + "&subServiceCode=" + subServiceCode;
- _log.Info("Send MPS ip :" + mps_ip + " port: " + mps_port + " data: " + data);
- string response111 = SocketUnSyn(data, mps_ip, Convert.ToInt32(mps_port), 600000);
- _log.Info("Socket respones: " + response111);
- response = JsonConvert.DeserializeObject<mpsResponse>(response111);
- _log.Info("After convert MPSRESPONSE : " + response.ToString());
- if (response.responseCode == "0")
- {
- return "0";
- }
- else if (response.responseCode == "404")
- {
- return "23";
- }
- else if (response.responseCode == "409")
- {
- return "21";
- }
- else if (response.responseCode == "401"|| response.responseCode == "402"|| response.responseCode == "403")
- {
- return "1";
- }
- else
- {
- return "-2";
- }
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- //Lay ve goi cuoc khach hang dang su dung, de biet thue bao tra truoc hay tra sau
- public static string chargeGWGetPackage(redisConnection _redis, string reqeust, string msisdn, log4net.ILog _log)
- {
- string _result = "unknow";
- try
- {
- string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <soapenv:Header/>\n" +
- " <soapenv:Body>\n" +
- " <web:gwOperation>\n" +
- " <Input>\n" +
- " <username>#username#</username>\n" +
- " <password>#password#</password>\n" +
- " <wscode>view8585</wscode>\n" +
- " <param name=\"GWORDER\" value=\"1\"/>\n" +
- " <param name=\"isdn\" value=\"#msisdn#\"/>\n" +
- " <!--Optional:-->\n" +
- " <rawData>1</rawData>\n" +
- " </Input>\n" +
- " </web:gwOperation>\n" +
- " </soapenv:Body>\n" +
- "</soapenv:Envelope>";
- string _msisdnTemp = msisdn;
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) == "509")
- {
- _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- }
- }
- string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_WSCODE", "CHARGE_BASIC_WSCODE", "SYSTEM").values;
- string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
- string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_USER", "CHARGE_BASIC_USER", "SYSTEM").values;
- string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_PASS", "CHARGE_BASIC_PASS", "SYSTEM").values;
- string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_URL", "CHARGE_BASIC_URL", "SYSTEM").values;
- //string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "CHARGE_NATCOM_LOTTO|1|" + money + "|" + "NATCOMLOTTO" + reqeust;
- xml = xml.Replace("#username#", topupBasicUser);
- xml = xml.Replace("#password#", topupBasicPass);
- //xml = xml.Replace("#wscode#", topupBasicWscode);
- xml = xml.Replace("#msisdn#", _msisdnTemp);
- _log.Info("Request :" + reqeust + ", XML: " + xml);
- _log.Info("View of request :" + reqeust);
- //string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
- //Tra co dinh 1 ket qua cho viec test local
-
- string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
- " <S:Body>\n" +
- " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <Result>\n" +
- " <error>0</error>\n" +
- " <description>success</description>\n" +
- " <return>0|The transaction was done successfully</return>\n" +
- " <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" +
- " </Result>\n" +
- " </ns2:gwOperationResponse>\n" +
- " </S:Body>\n" +
- "</S:Envelope>";
-
- _log.Info("Response :" + reqeust + ": " + resultPost);
- string err_code_cmd = "-2000";
- string err_msg_cmd = "cmd err unknow";
- if (!string.IsNullOrEmpty(resultPost))
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(resultPost);
- err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
- err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
- if (err_code_cmd == "0")
- {
- string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
- _log.Info("ressult view :" + ketqua);
- _result = ketqua.Split('|')[6];
- _log.Info("The packet of msisdn :" + _msisdnTemp + " is: " + _result);
- }
- }
- }
- catch (Exception ex)
- {
- }
- return _result.ToUpper();
- }
-
- //tru tien khac hang
- public static string chargeGW(redisConnection _redis, string reqeust, string msisdn, string money, log4net.ILog _log)
- {
- 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
- try
- {
- string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <soapenv:Header/>\n" +
- " <soapenv:Body>\n" +
- " <web:gwOperation>\n" +
- " <Input>\n" +
- " <username>#username#</username>\n" +
- " <password>#password#</password>\n" +
- " <wscode>charge8585</wscode>\n" +
- " <param name=\"GWORDER\" value=\"1\"/>\n" +
- " <param name=\"input\" value=\"#input#\"/>\n" +
- " <!--Optional:-->\n" +
- " <rawData></rawData>\n" +
- " </Input>\n" +
- " </web:gwOperation>\n" +
- " </soapenv:Body>\n" +
- "</soapenv:Envelope>";
- string _msisdnTemp = msisdn;
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) == "509")
- {
- _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- }
- }
- string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_WSCODE", "CHARGE_BASIC_WSCODE", "SYSTEM").values;
- string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
- string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_USER", "CHARGE_BASIC_USER", "SYSTEM").values;
- string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_PASS", "CHARGE_BASIC_PASS", "SYSTEM").values;
- string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_URL", "CHARGE_BASIC_URL", "SYSTEM").values;
- string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "CHARGE_NATCOM_LOTTO|1|" + money + "|" + "NATCOMLOTTO" + reqeust;
- xml = xml.Replace("#username#", topupBasicUser);
- xml = xml.Replace("#password#", topupBasicPass);
- xml = xml.Replace("#wscode#", topupBasicWscode);
- xml = xml.Replace("#input#", input);
- _log.Info("Request :" + reqeust + ", XML: " + xml);
- //string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
- //Tra co dinh 1 ket qua cho viec test local
-
- string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
- " <S:Body>\n" +
- " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <Result>\n" +
- " <error>0</error>\n" +
- " <description>success</description>\n" +
- " <return>0|The transaction was done successfully</return>\n" +
- " <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" +
- " </Result>\n" +
- " </ns2:gwOperationResponse>\n" +
- " </S:Body>\n" +
- "</S:Envelope>";
-
- _log.Info("Response :" + reqeust + ": " + resultPost);
- string err_code_cmd = "-2000";
- string err_msg_cmd = "cmd err unknow";
- if (!string.IsNullOrEmpty(resultPost))
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(resultPost);
- err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
- err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
- string errCode, errMsg;
- if (err_code_cmd == "0")
- {
- string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
- _log.Info("ressult :" + ketqua);
- //He thong nang cap -2:nang cap,0=thanh cong,1=Het tien,21=Thue bao ko active,23=Khong ton tai thue bao
- errCode = ketqua.Split('|')[0];
- errMsg = ketqua.Split('|')[1];
- if (errCode == "0")
- _result = "0";
- else if (errCode == "S-PRF-00025")
- _result = "21";
- else if (errCode == "55")
- _result = "1";
- else
- _result = "-2";
- }
- }
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- //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)
- //cong tai khoan goc
- public static string topupBasicAcount(redisConnection _redis, string reqeust, string msisdn, string money,
- out string transId, out string errCode, out string errMsg, log4net.ILog _log)
- {
- transId = reqeust;
- errCode = "-1";
- errMsg = "Flase";
- string _result = "-1"; //He thong nang cap
- try
- {
- string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <soapenv:Header/>\n" +
- " <soapenv:Body>\n" +
- " <web:gwOperation>\n" +
- " <Input>\n" +
- " <username>#username#</username>\n" +
- " <password>#password#</password>\n" +
- " <wscode>#wscode#</wscode>\n" +
- " <param name=\"GWORDER\" value=\"1\"/>\n" +
- " <param name=\"input\" value=\"#input#\"/>\n" +
- " <rawData>1</rawData>\n" +
- " </Input>\n" +
- " </web:gwOperation>\n" +
- " </soapenv:Body>\n" +
- "</soapenv:Envelope>";
- string _msisdnTemp = msisdn;
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) == "509")
- {
- _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- }
- }
- string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_WSCODE", "TOPUP_BASIC_WSCODE", "SYSTEM").values;
- string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
- string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_USER", "TOPUP_BASIC_USER", "SYSTEM").values;
- string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_PASS", "TOPUP_BASIC_PASS", "SYSTEM").values;
- string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_URL", "TOPUP_BASIC_URL", "SYSTEM").values;
- string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "ADD_NATCOM_LOTTO|1|" + money + ".0|0|" + "NATCOMLOTTO" + reqeust;
- xml = xml.Replace("#username#", topupBasicUser);
- xml = xml.Replace("#password#", topupBasicPass);
- xml = xml.Replace("#wscode#", topupBasicWscode);
- xml = xml.Replace("#input#", input);
- _log.Info("Request :" + reqeust + ", XML: " + xml);
- string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
- //Tra co dinh 1 ket qua cho viec test local
- //string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
- // " <S:Body>\n" +
- // " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
- // " <Result>\n" +
- // " <error>0</error>\n" +
- // " <description>success</description>\n" +
- // " <return>0|The transaction was done successfully</return>\n" +
- // " <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" +
- // " </Result>\n" +
- // " </ns2:gwOperationResponse>\n" +
- // " </S:Body>\n" +
- // "</S:Envelope>";
- _log.Info("Response :" + reqeust + ": " + resultPost);
- string err_code_cmd = "-2000";
- string err_msg_cmd = "cmd err unknow";
- if (!string.IsNullOrEmpty(resultPost))
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(resultPost);
- err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
- err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
- if (err_code_cmd == "0")
- {
- string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
- _log.Info("ressult :" + ketqua);
- errCode = ketqua.Split('|')[0];
- errMsg = ketqua.Split('|')[1];
- }
- }
- _result = "0";//neu da den day thi chac chan hoac success hoac revert. Khong bao gio pending
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- //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)
- public static string topupToWallet(redisConnection _redis, string reqeust, string msisdn, string money,
- out string transId, out string errCode, out string errMsg, log4net.ILog _log)
- {
- transId = reqeust;
- errCode = "-1";
- errMsg = "false";
- string _result = "-1"; //truong hop nay la loi try cath nen phai pending
- try
- {
- string jsomPost = "{\n" +
- " \"partnerCode\": \"#partnerCode#\",\n" +
- " \"accessKey\": \"#accessKey#\",\n" +
- " \"requestId\": \"#requestId#\",\n" +
- " \"requestTime\": \"#requestTime#\",\n" +
- " \"requestInfo\": \"#requestInfo#\",\n" +
- " \"functionName\": \"#functionName#\",\n" +
- " \"functionParams\": \"#functionParams#\",\n" +
- " \"signature\": \"#signature#\"\n" +
- "}";
- string topupWalletKeyPrivate = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_KEY_PRIVATE", "TOPUP_WALLET_KEY_PRIVATE", "SYSTEM").values;
- string topupWalletPartnerCode = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_PARTNER_CODE", "TOPUP_WALLET_PARTNER_CODE", "SYSTEM").values;
- string topupWalletUrl = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_URL", "TOPUP_WALLET_URL", "SYSTEM").values;
- string topupWalletContentPay = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CONTEN_PAY", "TOPUP_WALLET_CONTEN_PAY", "SYSTEM").values;
- string accessKey = Sha256Hash(topupWalletKeyPrivate + reqeust);
- string requestTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fff");
- string functionName = "dc92836b9910aecce8f4b216eeeb5506fd8de7a12f224a8938ddd3b79c500a1e";
- string _msisdnTemp = msisdn;
- //if (msisdn.Length > 3)
- //{
- // if (msisdn.Substring(0, 3) == "509")
- // {
- // _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- // }
- //}
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) != "509")
- {
- _msisdnTemp = "509" + msisdn;
- }
- }
- _log.Info("topup for msisdn: " + _msisdnTemp);
- _log.Info("topupToWallet :" + reqeust + ", _msisdnTemp: " + _msisdnTemp);
- string param = "{\"msisdn\":" + _msisdnTemp + ",\"amount\":" + money + "}";
- string functionParams = Base64Encode(param);
- string signature = "{accessKey=$accessKey&functionName=$functionName&functionParams=$functionParams&partnerCode=$partnerCode&requestId=$requestId&requestInfo=$requestInfo&requestTime=$requestTime}";
- signature = signature.Replace("$accessKey", accessKey);
- signature = signature.Replace("$functionName", functionName);
- signature = signature.Replace("$functionParams", functionParams);
- signature = signature.Replace("$partnerCode", topupWalletPartnerCode);
- signature = signature.Replace("$requestId", reqeust);
- signature = signature.Replace("$requestInfo", topupWalletContentPay);
- signature = signature.Replace("$requestTime", requestTime);
- signature = HMACSHA256Encode(signature, topupWalletKeyPrivate);
- jsomPost = jsomPost.Replace("#partnerCode#", topupWalletPartnerCode);
- jsomPost = jsomPost.Replace("#accessKey#", accessKey);
- jsomPost = jsomPost.Replace("#requestId#", reqeust);
- jsomPost = jsomPost.Replace("#requestTime#", requestTime);
- jsomPost = jsomPost.Replace("#requestInfo#", topupWalletContentPay);
- jsomPost = jsomPost.Replace("#functionName#", functionName);
- jsomPost = jsomPost.Replace("#functionParams#", functionParams);
- jsomPost = jsomPost.Replace("#signature#", signature);
- string strResponse = SendPost(topupWalletUrl, jsomPost, _log);
- //Tra ve ket qua co dinh, danh cho test local
- //string strResponse = "{\n" +
- // " \"partnerCode\": \"natcom_loto\",\n" +
- // " \"requestId\": \"123457430\",\n" +
- // " \"requestTime\": \"2022-02-18T14:06:03.547\",\n" +
- // " \"responseTime\": \"2022-02-18T14:06:04.678\",\n" +
- // " \"status\": \"200\",\n" +
- // " \"message\": \"OK\",\n" +
- // " \"result\": {\n" +
- // " \"phoneNumber\": \"50940825038\",\n" +
- // " \"responseDescription\": \"Transaction successfully!\",\n" +
- // " \"transactionId\": \"22021840220258\",\n" +
- // " \"responseCode\": \"00000\"\n" +
- // " }\n" +
- // "}";
- _log.Info("Response :" + reqeust + ": " + strResponse);
- topupWalletResponseObj response = JsonConvert.DeserializeObject<topupWalletResponseObj>(strResponse);
- if (response.Status == "200")
- {
- errCode = response.Result.ResponseCode; //den day thi chac chan se ko pending, hoac la thanh cong, hoac la revert
- errMsg = response.Result.ResponseDescription;
- }
- _result = "0"; //khi chay den day la moi lenh da thanh cong roi.
- }
- catch (Exception ex)
- {
- _log.Info("topup exeption: " + ex.ToString(), ex);
- }
- return _result;
- }
- //-1: He thong nang cap,1= Vi ton tai va active,2=Khong ton tai vi,3= Vi khong active
- public static string checkExistsWallet(redisConnection _redis, string reqeust, string msisdn, log4net.ILog _log)
- {
- string _result = "-1"; //he thong nang cap
- try
- {
- string jsomPost =
- "{\n" +
- " \"username\": \"$user\",\n" +
- " \"password\": \"$pass\",\n" +
- " \"msisdn\": \"$msisdn\",\n" +
- " \"requestId\": \"$reqeust\"\n" +
- "}";
- string TOPUP_WALLET_CHECK_USER = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_USER", "TOPUP_WALLET_CHECK_USER", "SYSTEM").values;
- string TOPUP_WALLET_CHECK_PASS = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_PASS", "TOPUP_WALLET_CHECK_PASS", "SYSTEM").values;
- string TOPUP_WALLET_CHECK_URL = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_URL", "TOPUP_WALLET_CHECK_URL", "SYSTEM").values;
- string accessKey = Sha256Hash(TOPUP_WALLET_CHECK_PASS + reqeust);
- _log.Info("accessKey: " + accessKey);
- jsomPost = jsomPost.Replace("$user", TOPUP_WALLET_CHECK_USER);
- jsomPost = jsomPost.Replace("$pass", accessKey);
- jsomPost = jsomPost.Replace("$msisdn", msisdn);
- jsomPost = jsomPost.Replace("$reqeust", reqeust);
- _log.Info("Request :" + reqeust + ", jsomPost: " + jsomPost);
- string strResponse = SendPost(TOPUP_WALLET_CHECK_URL, jsomPost, _log);
-
- //Tra ve ket qua co dinh, danh cho test local
- //string strResponse = "{\n" +
- // " \"status\": \"200\",\n" +
- // " \"timestamp\": \"2022-02-24T05:51:56.800\",\n" +
- // " \"message\": \"OK\",\n" +
- // " \"result\": {\n" +
- // " \"accountState\": 1,\n" +
- // " \"role\": \"EU\",\n" +
- // " \"msisdn\": \"50940825038\",\n" +
- // " \"registerDate\": \"2022-01-27 22:43:01\"\n" +
- // " }\n" +
- // "}";
- _log.Info("Response :" + reqeust + ": " + strResponse);
- //-1: He thong nang cap,1= Vi ton tai va active,2=Khong ton tai vi,3= Vi khong active
- walletCheckExists response = JsonConvert.DeserializeObject<walletCheckExists>(strResponse);
- if (response.Status == "200")
- {
- if (response.Result.AccountState == "1" && response.Result.Role == "EU")
- _result = "1";
- else if (response.Result.AccountState == "-1")
- _result = "2";
- else
- _result = "3";
- }
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- public static String SendPost(string url, string str, log4net.ILog _log)
- {
- _log.Info("start port URL: " + url);
- _log.Info("Reqeust: " + str);
- var data = new StringContent(str, Encoding.UTF8, "application/json");
- //StringBuilder sb = new StringBuilder();
- //sb.Append("Request: " + data);
- using (var client = new HttpClient())
- {
- TimeSpan timeSpam = new TimeSpan(0, 5, 0);
- client.Timeout = timeSpam;
- var response = client.PostAsync(url, data).Result;
- if (response.IsSuccessStatusCode)
- {
- var responseContent = response.Content;
- //sb.Append("\r\nResponse: " + responseContent);
- //log.Debug(sb.ToString());
- // by calling .Result you are synchronously reading the result
- string responseString = responseContent.ReadAsStringAsync().Result;
- _log.Info("Response: " + responseString);
- return responseString;
- }
- else
- {
- _log.Info("Post not succssss: " + response.IsSuccessStatusCode.ToString());
- }
- //sb.Append("\r\nResponse: " + response);
- //log.Debug(sb.ToString());
- return response.StatusCode.ToString();
- }
- }
- public static string sendPostXML(string api_path, string timeout, string xml)
- {
- string rp = "";
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api_path);
- byte[] bytes;
- bytes = System.Text.Encoding.UTF8.GetBytes(xml);
- request.ContentType = "text/xml; encoding='utf-8'";
- request.ContentLength = bytes.Length;
- request.Method = "POST";
- request.Timeout = Convert.ToInt32(timeout);
- Stream requestStream = request.GetRequestStream();
- requestStream.Write(bytes, 0, bytes.Length);
- requestStream.Close();
- HttpWebResponse response;
- response = (HttpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- rp = new StreamReader(responseStream).ReadToEnd();
- }
- catch (Exception ex)
- {
- }
- return rp;
- }
- public static bool WriteTextFile(String fileName, String t)
- {
- TextWriter textWriter;
- try
- {
- textWriter = new StreamWriter(fileName);
- }
- catch (Exception)
- {
- return false;
- }
- try
- {
- textWriter.WriteLine(t);
- }
- catch (Exception)
- {
- return false;
- }
- textWriter.Close();
- return true;
- }
- public static string subStrBetween(string STR, string FirstString, string LastString)
- {
- string FinalString;
- int Pos1 = STR.IndexOf(FirstString) + FirstString.Length;
- int Pos2 = STR.IndexOf(LastString);
- FinalString = STR.Substring(Pos1, Pos2 - Pos1);
- return FinalString;
- }
- public static string stringRemoveFrom0ToString(string STR, string Stringkey)
- {
- string FinalString;
- int Pos1 = STR.IndexOf(Stringkey) + Stringkey.Length;
- FinalString = STR.Remove(0, Pos1);
- return FinalString;
- }
- public static string stringGet1CharFirstString(string STR, string Stringkey)
- {
- string FinalString;
- int Pos1 = STR.IndexOf(Stringkey);
- FinalString = STR.Substring(Pos1 - 1, 1);
- return FinalString;
- }
- public static string Sha256Hash(string rawData)
- {
- // Create a SHA256
- using (SHA256 sha256Hash = SHA256.Create())
- {
- // ComputeHash - returns byte array
- byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
- // Convert byte array to a string
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- builder.Append(bytes[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public static string Base64Encode(string plainText)
- {
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
- return System.Convert.ToBase64String(plainTextBytes);
- }
- public static string Base64Decode(string base64EncodedData)
- {
- var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
- return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
- }
- public static string HMACSHA256Encode(string input, string key)
- {
- byte[] k = Encoding.ASCII.GetBytes(key);
- HMACSHA256 myhmacsha256 = new HMACSHA256(k);
- byte[] byteArray = Encoding.ASCII.GetBytes(input);
- using (MemoryStream stream = new MemoryStream(byteArray))
- {
- return byteToHex(myhmacsha256.ComputeHash(stream));
- }
- }
- private static string byteToHex(byte[] hash)
- {
- return BitConverter.ToString(hash).Replace("-", "").ToLower();
- }
- public static string ByteArrayToHexString(byte[] ba)
- {
- StringBuilder hex = new StringBuilder(ba.Length * 2);
- foreach (byte b in ba)
- {
- hex.AppendFormat("{0:X2}", b);
- }
- return hex.ToString();
- }
- public static bool IsNumber(string aNumber)
- {
- int numericValue;
- bool isNumber = int.TryParse(aNumber, out numericValue);
- return isNumber;
- }
- public static string SocketUnSyn(string msg, string host, int port, int timeout)
- {
- //log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
- //log4net.Config.XmlConfigurator.Configure();
- string result = "-1000";
- TcpClient client = null;
- NetworkStream stream = null;
- try
- {
- byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);
- client = new TcpClient(host, port);
- client.SendTimeout = timeout;
- stream = client.GetStream();
- BinaryWriter writer = new BinaryWriter(stream);
- writer.Write(data);
- String _readFromStream = String.Empty;
- byte[] data_read = new byte[1024];
- using (MemoryStream ms = new MemoryStream())
- {
- int numBytesRead;
- while ((numBytesRead = stream.Read(data_read, 0, data_read.Length)) > 0)
- {
- ms.Write(data_read, 0, numBytesRead);
- }
- _readFromStream = System.Text.Encoding.ASCII.GetString(ms.ToArray(), 0, (int)ms.Length);
- }
- //string _readFromStream = GetResponse(stream);
- /*
- data = new Byte[1024];
- String responseData = String.Empty;
- Int32 bytes = stream.Read(data, 0, data.Length);
- responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
- */
-
- // Close everything.
- stream.Close();
- client.Close();
- //Phan tich data tra ve:
- result = _readFromStream;
- }
- catch (ArgumentNullException e)
- {
-
- stream.Close();
- client.Close();
- return "-1000";
- }
- catch (SocketException e)
- {
- return "-1000";
- }
- return result;
- }
- }
- }
|