WapController.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using NEducation.GenKeyWs;
  2. using Microsoft.Extensions.Configuration;
  3. using NEducation.Controllers;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Threading.Tasks;
  10. using System.ServiceModel;
  11. using NEducation;
  12. using NEducation.Models;
  13. using System.Web.Mvc;
  14. using System.Web;
  15. using System.Net;
  16. using NEducation.Code;
  17. using NEducation.Content.Texts;
  18. namespace NEducation.Controllers
  19. {
  20. public class Data
  21. {
  22. public String key { get; set; }
  23. public String value { get; set; }
  24. public Data(String key, String value)
  25. {
  26. this.key = key;
  27. this.value = value;
  28. }
  29. }
  30. public class WapController : BaseController
  31. {
  32. static public log4net.ILog log = log4net.LogManager.GetLogger("WapController");
  33. String wsUser;
  34. String wsPass;
  35. //static public String wsUrl = "http://paymentgateway.metfone.com.kh/MPS/";
  36. //static public String wsUrlRes = "http://happycall.fun/Wap/ResponseFromWap";
  37. ////static public String wsUrlRes = "https://localhost:44310/Wap/ResponseFromWap";
  38. static private String PRO = "GTS";
  39. static private String SER = "GTS_MEDU";
  40. //static private String SUB = "GTS_HappyCalling_Daily";
  41. static private String SUP_CP = "GTS";
  42. static private String CONT = null;
  43. static private String TYPE = null;
  44. GenKeyWs.WsGenKeyClient wsClient = new GenKeyWs.WsGenKeyClient();
  45. public async Task<ActionResult> ResponseFromWapAsync(string DATA, string SIG)
  46. {
  47. try
  48. {
  49. DATA = "al12Rg2wdrLixNin9i19FdMMqBF6Xe+dQzXarlvBMqGQeh/PRD5F9zBrZc0lGlN/fGoJ/NuuMh96ymeuOtyv6rlu9+KOBMGXiAH43J81zBLMJ9+aF3mQR7OT7mQnQkFe4i2gpMnYi2NRUSyJAZm1NnTeoDswqsYWVcke+jMtubSQdmBkSmm2pUTW+rLgHi4sMrPLyRlw0T5HsEuT6YUpqRKaweF97pLZDNUaJePRRPyJASHvT3yOy7VTfAIcVSC1DGrYOv3Kyj18pKhxjskCWdYUd3r4jngN/iMALnQvgAWtsBsfH50KS77rs0r6MIMpN6rv96BrtpHn0gmqqYTDGtykNa5FsRyclqyI3CLgn9hQi2TWUVMWHETqdR0E8VrdIXMNGhgaNclDgKFFyTl/V02vSH+RbeuPU05XLfUaBD/e8SKA0rTlQfGVAyJZtRvMreg76dQBZFBbda96M95Mdphhp/ZR5XrVRKt+dM1+8FxLpQefsBp21L3HFJrzgcPxkz5qQN1rdx2Umhzf0W4rQErsGk7xZoYlxVGo2/7Ouwi0BJU1ocK3/z5bSskAQTc3UWdzzm0q+KDs/gEd1ouqoA3R2n5PuMqMTZhztcpxIJODKfmSPS/pTZNPQrXSpbAMqN11FRTxzt7yjRdFrOd4zDVRPYXcQ7ExYvdY0JrXAZE=";
  50. SIG = "Bwdr1erav2UsFY7OpAGFNN%2BZPQ90cLDhqniE6Ta2kaD3kEtrKIprsfIz6BAL%2BIY8KhC1Xx0N7cSZyU1uAfEHRG%2BCJ5hbM5s%2B7kzMv4ZE7CFHqF9LuZrQQqz8IHzkPRHBgyWDy7RtESE1y%2BMRC1X9sAAdNFKxEL0NSCQigepfFD6YTnNFJix1OPAYwy1KfRTVAU10%2F%2BlkNuGegk651SH%2FgZ8yptwpnoIYdlUPQh3D404vm58EpFz7%2BtetzcolF6ajvJFGov%2BOvAOataD7%2BQDrJpqQ%2B7pf8A3fdAPAqRTmpUjCYtL0gRL8TILkPrF%2BmpaWCyWFzYug7CGvr%2F2qQForFhPH0xQj2SFI1tfBfYQOg4V7oCnOI9Jq0haiQ2mCCA5o1cQ0dEAK43NdRP2dfmwh%2BgVK%2BVeZpWxqOLhegeU%2B9bFA0iUmjXCxLNcSbsHqA7j7IvmPb4Ms2zhv4B9ALQ94YZft9Ut5ZRzvO8LYrmhmWqvK8mFQwIGwQNTKMd9UCCoSiTV8an7MFG2uzweS9Q65wwNbbaEstD8mOvD9Jkwv2x71WGI%2FFwkGPrbxzrfATD5DgQ7IFIpDD0Rr%2BPG4S5WlJlSYTPks%2FRgLNyHgQ%2BX3UGPINNKzj3xaC25RCN2iKoN7TY%2F4FEB8Day0gfd3aMfvFCpOXF5h6uGOpAp7ETsOgP0%3D";
  51. // Pre-process input data
  52. string dataGet = DATA.Replace(" ", "+");
  53. var sigGet = Uri.EscapeDataString(SIG).ToString();
  54. var data = "DATA=" + dataGet + "&SIG=" + sigGet;
  55. log.Debug("data " + data);
  56. // Load configuration settings
  57. String wsUrlRes = ConfigurationManager.AppSettings["domainRes"];
  58. String wsUrl = ConfigurationManager.AppSettings["paymentUrl"];
  59. String packageName = ConfigurationManager.AppSettings["packname"];
  60. String packNameCannel = ConfigurationManager.AppSettings["packNameCannel"];
  61. String packNameREGISTER_ToBe = ConfigurationManager.AppSettings["packNameREGISTER_ToBe"];
  62. String packNameCannel_ToBe = ConfigurationManager.AppSettings["packNameCannel_ToBe"];
  63. String DIR = ConfigurationManager.AppSettings["directoryPath"];
  64. log.Debug("DIR_Wap " + DIR);
  65. if (packageName == null)
  66. {
  67. Session["register-result-message"] = "Register unsuccessful";
  68. Session["register-result-status"] = "-1";
  69. return Redirect("/Home/RegisterResult");
  70. }
  71. // Decrypt the key
  72. GenKeyWs.wsDecryptKeyResponse decrypt = await wsClient.wsDecryptKeyAsync(null, null, DIR + packageName.ToUpper() + "\\", wsUrl, data);
  73. log.Debug("errorCode " + decrypt.@return.errorCode);
  74. log.Debug("decrypt " + decrypt.@return.descrypt);
  75. // Log details if available
  76. if (decrypt.@return.mpsResponse != null)
  77. {
  78. log.Debug("CMD " + decrypt.@return.mpsResponse.CMD);
  79. log.Debug("MOBILE " + decrypt.@return.mpsResponse.MOBILE);
  80. log.Debug("PRICE " + decrypt.@return.mpsResponse.PRICE);
  81. log.Debug("RES " + decrypt.@return.mpsResponse.RES);
  82. log.Debug("REQ " + decrypt.@return.mpsResponse.REQ);
  83. log.Debug("SOURCE " + decrypt.@return.mpsResponse.SOURCE);
  84. log.Debug("STATUS " + decrypt.@return.mpsResponse.STATUS);
  85. log.Debug("SUB_END_DATE " + decrypt.@return.mpsResponse.SUB_END_DATE);
  86. log.Debug("SUB_NEW " + decrypt.@return.mpsResponse.SUB_NEW);
  87. log.Debug("SUB_SERVICE " + decrypt.@return.mpsResponse.SUB_SERVICE);
  88. log.Debug("SUB_START_DATE " + decrypt.@return.mpsResponse.SUB_START_DATE);
  89. }
  90. else
  91. {
  92. log.Debug("mpsResponse null ");
  93. }
  94. if (decrypt.@return.errorCode == UtilsController.WapConstant.SUCCESS)
  95. {
  96. // Handle different command cases
  97. if (decrypt.@return.mpsResponse.CMD == UtilsController.WapConstant.REGISTER)
  98. {
  99. log.Info("REGISTER MOBILE " + decrypt.@return.mpsResponse.MOBILE);
  100. if (!string.IsNullOrEmpty(decrypt.@return.mpsResponse.MOBILE))
  101. {
  102. // Validate and process phone number
  103. String phoneNumber = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE);
  104. if (!string.IsNullOrEmpty(phoneNumber))
  105. {
  106. Session["msisdn-detecting"] = phoneNumber;
  107. // Perform registration request
  108. UserRequest userRequest = new UserRequest
  109. {
  110. users = decrypt.@return.mpsResponse.MOBILE,
  111. msisdn = decrypt.@return.mpsResponse.MOBILE,
  112. subServiceCode = packNameREGISTER_ToBe + "|" + decrypt.@return.mpsResponse.PRICE,
  113. requestId = "-1",
  114. otp = decrypt.@return.mpsResponse.RES,
  115. serviceId = "3"
  116. };
  117. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.MpsRequest);
  118. UserActionResult ress = new UserActionResult(rs);
  119. if (ress.responseCode == UtilsController.Constant.SUCCESS)
  120. {
  121. Charging charging = new Charging(rs);
  122. Session["charging"] = charging;
  123. Session["msisdn"] = decrypt.@return.mpsResponse.MOBILE;
  124. Session["package"] = packNameCannel;
  125. Session["CHARGE_ACTION"] = UtilsController.Constant.REGISTER;
  126. Session["CHARGE_DATA"] = userRequest;
  127. }
  128. else if (ress.status == "2")
  129. {
  130. return Json(new
  131. {
  132. error_code = ress.status,
  133. error_content = UtilsController.GetErrorCodeCharging(ress.status)
  134. });
  135. }
  136. return Json(new
  137. {
  138. error_code = ress.responseCode,
  139. error_content = UtilsController.GetErrorCodeCharging(ress.responseCode)
  140. });
  141. }
  142. else
  143. {
  144. log.Info("Cannel Can not detecting");
  145. }
  146. }
  147. Session["register-result-message"] = "Register successful";
  148. Session["register-result-status"] = "0";
  149. return Redirect("/Home/RegisterResult");
  150. }
  151. else if (decrypt.@return.mpsResponse.CMD == UtilsController.WapConstant.CANCEL)
  152. {
  153. String phoneNumber = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE);
  154. if (!string.IsNullOrEmpty(phoneNumber))
  155. {
  156. if (decrypt.@return.mpsResponse.RES == UtilsController.WapConstant.SUCCESS ||
  157. decrypt.@return.mpsResponse.RES == UtilsController.WapConstant.CANCEL_USER_IN_CHARGING_LIST)
  158. {
  159. if (!string.IsNullOrEmpty(decrypt.@return.mpsResponse.MOBILE))
  160. {
  161. String phoneNumbers = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE);
  162. if (!string.IsNullOrEmpty(phoneNumbers))
  163. {
  164. Session["msisdn-detecting"] = phoneNumber;
  165. var checkSub = UtilsController.checkAccountSub(phoneNumber, packNameCannel_ToBe);
  166. if (!checkSub)
  167. {
  168. UserRequest userRequest = new UserRequest
  169. {
  170. users = decrypt.@return.mpsResponse.MOBILE,
  171. msisdn = decrypt.@return.mpsResponse.MOBILE,
  172. subServiceCode = packNameCannel_ToBe + "|" + decrypt.@return.mpsResponse.PRICE,
  173. requestId = "-1",
  174. otp = decrypt.@return.mpsResponse.RES,
  175. serviceId = "3"
  176. };
  177. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.MpsRequest);
  178. UserActionResult ress = new UserActionResult(rs);
  179. if (ress.responseCode == UtilsController.Constant.SUCCESS)
  180. {
  181. Charging charging = new Charging(rs);
  182. Session["charging"] = charging;
  183. Session["msisdn"] = decrypt.@return.mpsResponse.MOBILE;
  184. Session["package"] = packageName;
  185. Session["CHARGE_ACTION"] = UtilsController.Constant.REGISTER;
  186. Session["CHARGE_DATA"] = userRequest;
  187. }
  188. else if (ress.status == "2")
  189. {
  190. return Json(new
  191. {
  192. error_code = ress.status,
  193. error_content = UtilsController.GetErrorCodeCharging(ress.status)
  194. });
  195. }
  196. return Json(new
  197. {
  198. error_code = ress.responseCode,
  199. error_content = UtilsController.GetErrorCodeCharging(ress.responseCode)
  200. });
  201. }
  202. else
  203. {
  204. return Json(new
  205. {
  206. error_code = UtilsController.Constant.USER_EXISTED,
  207. error_content = UtilsController.GetErrorCodeCharging(UtilsController.Constant.USER_EXISTED)
  208. });
  209. }
  210. }
  211. else
  212. {
  213. return Json(new
  214. {
  215. error_code = UtilsController.Constant.INVALID_MSISDN,
  216. error_content = @Lang.InvalidMsisdn,
  217. });
  218. }
  219. }
  220. }
  221. else
  222. {
  223. log.Error("CANCEL unsuccessful");
  224. Session["register-result-message"] = "Cancel unsuccessful";
  225. Session["register-result-status"] = "-1";
  226. return Redirect("/Home/RegisterResult");
  227. }
  228. Session["register-result-message"] = "Cancel successful";
  229. Session["register-result-status"] = "0";
  230. return Redirect("/Home/RegisterResult");
  231. }
  232. Session["register-result-message"] = "Cancel unsuccessful";
  233. Session["register-result-status"] = "-1";
  234. return Redirect("/Home/RegisterResult");
  235. }
  236. else if (decrypt.@return.mpsResponse.CMD == UtilsController.WapConstant.MSISDN)
  237. {
  238. if (!string.IsNullOrEmpty(decrypt.@return.mpsResponse.MOBILE) &&
  239. decrypt.@return.mpsResponse.MOBILE != "null")
  240. {
  241. // Validate and process phone number
  242. String phoneNumber = UtilsController.validateMsisdn(decrypt.@return.mpsResponse.MOBILE);
  243. if (!string.IsNullOrEmpty(phoneNumber))
  244. {
  245. // Auto login for this user
  246. Session["msisdn-detecting"] = phoneNumber;
  247. return Redirect("/Home?detecting=0");
  248. }
  249. }
  250. else
  251. {
  252. log.Info("Can not detecting");
  253. }
  254. }
  255. else
  256. {
  257. log.Info("Command: " + decrypt.@return.mpsResponse.CMD);
  258. }
  259. }
  260. else
  261. {
  262. // Handle unsuccessful registration
  263. log.Info("Error ");
  264. Session["register-result-message"] = "Register unsuccessful";
  265. Session["register-result-status"] = "-1";
  266. return Redirect("/Home/RegisterResult");
  267. }
  268. return Redirect("/Home?detecting=0");
  269. }
  270. catch (Exception ex)
  271. {
  272. log.Info("Exception: " + ex);
  273. return Redirect("/Home?detecting=0");
  274. }
  275. }
  276. // GET: Wap
  277. public static async Task<GenKeyWs.response> UserDetectingAsync(IConfiguration configuration, GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams)
  278. {
  279. GenKeyWs.response response = new GenKeyWs.response();
  280. response.errorCode = UtilsController.WapConstant.FAILURE;
  281. //String DIR = configuration.GetSection("directoryPath").Value;
  282. //String wsUrl = configuration.GetSection("paymentUrl").Value;
  283. String DIR = configuration.GetSection("directoryPath").Value;
  284. String wsUrl = configuration.GetSection("paymentUrl").Value;
  285. GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "mobile.html?", PRO, SER, mpsparams.SUB,
  286. mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE);
  287. log.Debug("UserDetecting build req: " + makeUrl.@return.enscrypt);
  288. //HttpContext.Session.SetComplexData("mps-params", mpsparams);
  289. return makeUrl.@return;
  290. }
  291. public static async Task<GenKeyWs.response> UserRegisteringAsync(GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams)
  292. {
  293. GenKeyWs.response response = new GenKeyWs.response();
  294. response.errorCode = UtilsController.WapConstant.FAILURE;
  295. String DIR = ConfigurationManager.AppSettings["directoryPath"] + mpsparams.SUB.ToUpper() + "/";
  296. log.Debug("UserRegistering build DIR: " + DIR);
  297. String wsUrl = ConfigurationManager.AppSettings["paymentUrl"];
  298. //String DIR = configuration.GetSection("directoryPath").Value + mpsparams.SUB.ToUpper() + "/";
  299. //String wsUrl = configuration.GetSection("paymentUrl").Value;
  300. log.Debug("UserRegistering makeUrl:");
  301. GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "charge.html?", PRO, SER, mpsparams.SUB,
  302. mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE);
  303. log.Debug("UserRegistering build req: " + makeUrl.@return.enscrypt);
  304. // save session
  305. //HttpContext.Session.SetComplexData("mps-params", mpsparams);
  306. return makeUrl.@return;
  307. }
  308. public static async Task<GenKeyWs.response> UserChargingAsync(GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams)
  309. {
  310. GenKeyWs.response response = new GenKeyWs.response();
  311. response.errorCode = UtilsController.WapConstant.FAILURE;
  312. response.errorCode = UtilsController.WapConstant.FAILURE;
  313. String DIR = ConfigurationManager.AppSettings["directoryPath"];
  314. String wsUrl = ConfigurationManager.AppSettings["paymentUrl"];
  315. //String DIR = configuration.GetSection("directoryPath").Value;
  316. //String wsUrl = configuration.GetSection("paymentUrl").Value;
  317. GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "charge.html?", PRO, SER, mpsparams.SUB,
  318. mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE);
  319. log.Debug("UserCharging build req: " + makeUrl.@return.enscrypt);
  320. //HttpContext.Session.SetComplexData("mps-params", mpsparams);
  321. return makeUrl.@return;
  322. }
  323. public static async Task<GenKeyWs.response> UserCancelingAsync(GenKeyWs.WsGenKeyClient wsClient, MpsParams mpsparams)
  324. {
  325. GenKeyWs.response response = new GenKeyWs.response();
  326. response.errorCode = UtilsController.WapConstant.FAILURE;
  327. response.errorCode = UtilsController.WapConstant.FAILURE;
  328. String DIR = ConfigurationManager.AppSettings["directoryPath"] + mpsparams.SUB.ToUpper() + "/";
  329. String wsUrl = ConfigurationManager.AppSettings["paymentUrl"];
  330. //String DIR = configuration.GetSection("directoryPath").Value + mpsparams.SUB.ToUpper() + "/";
  331. //String wsUrl = configuration.GetSection("paymentUrl").Value;
  332. GenKeyWs.wsEncryptKeyResponse makeUrl = await wsClient.wsEncryptKeyAsync(null, null, DIR, wsUrl + "charge.html?", PRO, SER, mpsparams.SUB,
  333. mpsparams.CMD, "WAP", mpsparams.CATE, mpsparams.ITEM, SUP_CP, CONT, mpsparams.PRICE, mpsparams.MOBILE, TYPE);
  334. log.Debug("UserCanceling build req: " + makeUrl.@return.enscrypt);
  335. // save session
  336. //HttpContext.Session.SetComplexData("mps-params", mpsparams);
  337. return makeUrl.@return;
  338. }
  339. static public String PostData(string mpsurl)
  340. {
  341. String url = mpsurl;
  342. log.Debug("Request: " + url);
  343. using (var client = new HttpClient())
  344. {
  345. var response = client.GetAsync(url).Result;
  346. if (response.IsSuccessStatusCode)
  347. {
  348. var responseContent = response.Content;
  349. // by calling .Result you are synchronously reading the result
  350. string responseString = responseContent.ReadAsStringAsync().Result;
  351. log.Debug("Response: " + responseString);
  352. return responseString;
  353. }
  354. else
  355. {
  356. log.Error("Response: " + response.StatusCode.ToString());
  357. return response.StatusCode.ToString();
  358. }
  359. }
  360. }
  361. // push to webservice
  362. public async Task<bool> RegisterOtp(String package, String msisdn, String code, String action)
  363. {
  364. try
  365. {
  366. MpsReq req = new MpsReq();
  367. req.msisdn = msisdn;
  368. req.subServiceCode = package;
  369. req.otp = code; // not send to mps
  370. //NcGamesWs.subRequestPGWResponse res = await wsGGameClient.subRequestPGWAsync(wsUser, wsPass,
  371. // msisdn, UtilsController.Constant.SERVICE_ID,
  372. // package, action);
  373. //MpsRes res = new MpsRes(rs);
  374. // check success
  375. if (
  376. //res.@return == "0" &&
  377. (code == UtilsController.WapConstant.SUCCESS ||
  378. code == UtilsController.WapConstant.CANCEL_USER_IN_CHARGING_LIST))
  379. {
  380. //HttpContext.Session.SetComplexData("msisdn", msisdn);
  381. //HttpContext.Session.SetComplexData("msisdn-detecting", msisdn);
  382. return true;
  383. }
  384. return false;
  385. }
  386. catch (Exception ex)
  387. {
  388. log.Error("Exception: " + ex);
  389. return false;
  390. }
  391. }
  392. }
  393. }