BuyTicketController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using LotteryWebApp.Common;
  2. using LotteryWebApp.Components;
  3. using LotteryWebApp.Extensions;
  4. using LotteryWebApp.Languages;
  5. using LotteryWebApp.Models;
  6. using LotteryWebApp.Service;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Configuration;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Globalization;
  13. namespace LotteryWebApp.Controllers
  14. {
  15. [AutoValidateAntiforgeryToken]
  16. public class BuyTicketController : BaseController
  17. {
  18. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
  19. IConfiguration configuration;
  20. private readonly IWebHostEnvironment webHostEnvironment;
  21. APIFunctions api = new APIFunctions();
  22. public BuyTicketController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment)
  23. {
  24. configuration = _configuration;
  25. webHostEnvironment = hostEnvironment;
  26. }
  27. public String GetParameter(String key)
  28. {
  29. return configuration.GetSection(key).Value;
  30. }
  31. public IActionResult Index(string termType)
  32. {
  33. if (!CheckAuthToken())
  34. {
  35. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  36. }
  37. HttpContext.Session.SetComplexData("navigator", Constants.BUY_TICKET_NAVIGATOR);
  38. BuyTicket_ViewModel model = new BuyTicket_ViewModel();
  39. model.termType = termType != null ? termType : Constants.FLORIDA_BOLET_CODE;
  40. HttpContext.Session.SetComplexData("termType", model.termType);
  41. return View(model);
  42. }
  43. [ValidateAntiForgeryToken]
  44. public IActionResult LastTermResult(string termType, String seqPage)
  45. {
  46. if (!CheckAuthToken())
  47. {
  48. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  49. }
  50. LastTermResult_ViewModel model = new LastTermResult_ViewModel();
  51. model.listTerm = new List<Term>();
  52. try
  53. {
  54. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  55. String token = HttpContext.Session.GetComplexData<String>("token");
  56. string fromDate = DateTime.Now.AddDays(-2).ToString("dd/MM/yyyy");
  57. string toDate = DateTime.Now.ToString("dd/MM/yyyy");
  58. if (seqPage == "1")
  59. {
  60. HttpContext.Session.DeleteComplexData("listTerms");
  61. }
  62. // get result
  63. ResultOfTermRequest resultOfTermRequest = new ResultOfTermRequest();
  64. resultOfTermRequest.gameId = termType;
  65. resultOfTermRequest.token = token;
  66. resultOfTermRequest.type = Constants.TERM_HAS_NOT_RESULT_TYPE;
  67. resultOfTermRequest.order = Constants.DECS;
  68. resultOfTermRequest.fromDate = fromDate;
  69. resultOfTermRequest.toDate = toDate;
  70. resultOfTermRequest.rowsOnPage = Constants.ROW_ON_PAGE;
  71. resultOfTermRequest.seqPage = seqPage;
  72. resultOfTermRequest.id = Constants.ALL_DATA;
  73. ResultOfTermResponse result = api.GetResultOfTermApi(configuration, resultOfTermRequest);
  74. if (result.responseCode == Code.SESSION_EXPIRED)
  75. {
  76. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  77. }
  78. else if (result.responseCode == Code.SUCCESS)
  79. {
  80. if (result.listTerm != null)
  81. model.listTermNotDrawn = result.listTerm;
  82. model.termType = termType;
  83. }
  84. resultOfTermRequest.type = Constants.TERM_HAS_RESULT_TYPE;
  85. result = api.GetResultOfTermApi(configuration, resultOfTermRequest);
  86. if (result.responseCode == Code.SESSION_EXPIRED)
  87. {
  88. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  89. }
  90. else if (result.responseCode == Code.SUCCESS)
  91. {
  92. if (result.listTerm != null)
  93. {
  94. model.listTerm = result.listTerm;
  95. if (termType == Constants.BOULCHANS_CODE)
  96. {
  97. List<Term> listTerms = HttpContext.Session.GetComplexData<List<Term>>("listTerms");
  98. if (listTerms != null && listTerms.Count > 0)
  99. {
  100. listTerms.AddRange(result.listTerm);
  101. }
  102. else
  103. {
  104. listTerms = result.listTerm;
  105. }
  106. HttpContext.Session.SetComplexData("listTerms", listTerms);
  107. }
  108. }
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. log.Error(ex);
  114. }
  115. return PartialView("LastTermResult", model);
  116. }
  117. public IActionResult Choose(string ticketType, string termType)
  118. {
  119. if (!CheckAuthToken())
  120. {
  121. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  122. }
  123. BuyTicketChoose_ViewModel model = new BuyTicketChoose_ViewModel();
  124. if (ticketType == null)
  125. {
  126. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Home");
  127. }
  128. if (termType != null)
  129. {
  130. HttpContext.Session.SetComplexData("termType", termType);
  131. }
  132. model.ticketType = ticketType;
  133. return View(model);
  134. }
  135. [ValidateAntiForgeryToken]
  136. public IActionResult AddTicket_Action(string ticketType)
  137. {
  138. ChooseFormModel model = new ChooseFormModel();
  139. model.tics = DateTime.Now.Ticks;
  140. model.ticketType = ticketType;
  141. return ViewComponent("ChooseForm", model);
  142. }
  143. [ValidateAntiForgeryToken]
  144. public JsonResult ConfirmTicket_Action(string ticketType, string tickets)
  145. {
  146. try
  147. {
  148. // check tickets
  149. int check = 1;
  150. string[] ticketsList = tickets.Split(',');
  151. for (int i = 0; i < ticketsList.Length; i++)
  152. {
  153. if (ticketType == Constants.BOLET_CODE && ticketsList[i].Length != 2)
  154. {
  155. check *= 0;
  156. }
  157. if (ticketType == Constants.MAYRAJ_CODE && ticketsList[i].Length != 4)
  158. {
  159. check *= 0;
  160. }
  161. if (ticketType == Constants.LOTTO3_CODE && ticketsList[i].Length != 3)
  162. {
  163. check *= 0;
  164. }
  165. if (ticketType == Constants.BOULCHANS_CODE && ticketsList[i].Length != 2)
  166. {
  167. check *= 0;
  168. }
  169. }
  170. if (check == 1)
  171. {
  172. HttpContext.Session.SetComplexData("ticketType", ticketType);
  173. HttpContext.Session.SetComplexData("tickets", tickets);
  174. return Json(new
  175. {
  176. code = Code.SUCCESS,
  177. });
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. log.Error(ex);
  183. }
  184. return Json(new
  185. {
  186. code = Code.FAILURE,
  187. message = Lang.ticket_invalid
  188. });
  189. }
  190. public IActionResult Payment(string step)
  191. {
  192. if (!CheckAuthToken())
  193. {
  194. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  195. }
  196. BuyTicketPayment_ViewModel model = new BuyTicketPayment_ViewModel();
  197. Profile profile = HttpContext.Session.GetComplexData<Profile>("profile");
  198. String tickets = HttpContext.Session.GetComplexData<String>("tickets");
  199. String ticketType = HttpContext.Session.GetComplexData<String>("ticketType");
  200. String termType = HttpContext.Session.GetComplexData<String>("termType");
  201. ConfirmTicketDataResponse confirmTicketDataResponse = HttpContext.Session.GetComplexData<ConfirmTicketDataResponse>("confirmTicketDataResponse");
  202. model.tickets = tickets;
  203. model.step = step != null ? step : Constants.PAYMENT_SHOW;
  204. model.ticketType = ticketType;
  205. model.confirmTicketDataResponse = confirmTicketDataResponse;
  206. model.profile = profile;
  207. model.termType = termType;
  208. if (step == Constants.PAYMENT_RESULT)
  209. {
  210. ConfirmBuyingTicketResponse confirmBuyingTicketResponse = HttpContext.Session.GetComplexData<ConfirmBuyingTicketResponse>("confirmBuyingTicketResponse");
  211. model.confirmBuyingTicketResponse = confirmBuyingTicketResponse;
  212. }
  213. return View(model);
  214. }
  215. [ValidateAntiForgeryToken]
  216. public JsonResult PaymentSendOTP_Action(string ticketMoney)
  217. {
  218. try
  219. {
  220. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  221. String token = HttpContext.Session.GetComplexData<String>("token");
  222. String termType = HttpContext.Session.GetComplexData<String>("termType");
  223. String tickets = HttpContext.Session.GetComplexData<String>("tickets");
  224. String ticketType = HttpContext.Session.GetComplexData<String>("ticketType");
  225. string[] ticketMoneyReal = ticketMoney.Split(',');
  226. string[] ticketNumberReal = tickets.Split(',');
  227. if (ticketMoneyReal.Length != ticketNumberReal.Length)
  228. {
  229. return Json(new
  230. {
  231. code = Code.FAILURE,
  232. message = Lang.error_happened
  233. });
  234. }
  235. int totalMoney = 0;
  236. List<TicketInfo> ticketInfos = new List<TicketInfo>();
  237. for (int i = 0; i < ticketMoneyReal.Length; i++)
  238. {
  239. TicketInfo ticketInfo = new TicketInfo();
  240. ticketInfo.money = ticketMoneyReal[i];
  241. ticketInfo.code = ticketNumberReal[i];
  242. ticketInfos.Add(ticketInfo);
  243. totalMoney += int.Parse(ticketInfo.money);
  244. }
  245. ConfirmTicketDataRequest confirmTicketDataRequest = new ConfirmTicketDataRequest();
  246. confirmTicketDataRequest.token = token;
  247. confirmTicketDataRequest.msisdn = msisdn;
  248. confirmTicketDataRequest.ticket = ticketInfos;
  249. confirmTicketDataRequest.gameId = ConvertToGameID(termType, ticketType);
  250. confirmTicketDataRequest.requestId = RandomString(100, true);
  251. ConfirmTicketDataResponse confirmTicketDataResponse = api.ConfirmTicketDataApi(configuration, confirmTicketDataRequest);
  252. if (confirmTicketDataResponse.responseCode == Code.SUCCESS)
  253. {
  254. //string drawnTime = DateTime.ParseExact(confirmTicketDataResponse.termObj.endDate, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture).ToString("HH:mm");
  255. string content = Lang.confirm_payment_1 + " " + totalMoney + " " + Lang.confirm_payment_2 + " " + ConvertGameIdToName(confirmTicketDataRequest.gameId)
  256. + " " + Lang.confirm_payment_3 + " 0 " + Lang.confirm_payment_4 + " " +
  257. ConvertDrawnTimeFromTerm(termType, confirmTicketDataResponse.termObj) + Lang.confirm_payment_5;
  258. HttpContext.Session.SetComplexData("confirmTicketDataResponse", confirmTicketDataResponse);
  259. String channel = GetParameter(Constants.CHANNEL);
  260. if (channel == ApiConstants.WALLET)
  261. {
  262. // ma hoa
  263. String param = "transactionId=" + confirmTicketDataResponse.transId + "&requestId=" + confirmTicketDataResponse.requestId + "&money=" + totalMoney;
  264. String url = CreatePrivateURL(configuration, param, "seconds", "0", "0", GetParameter("rsaPolicy"));
  265. // redirect
  266. return Json(new
  267. {
  268. code = "110",
  269. param = url,
  270. });
  271. }
  272. else
  273. {
  274. // send OTP
  275. SendOTPRequest sendOTPRequest = new SendOTPRequest();
  276. sendOTPRequest.msisdn = msisdn;
  277. sendOTPRequest.token = token;
  278. SendOTPResponse sendOTPResponse = api.SendOTPApi(configuration, sendOTPRequest);
  279. return Json(new
  280. {
  281. code = int.Parse(sendOTPResponse.responseCode),
  282. message = sendOTPResponse.responseMessage,
  283. content = content,
  284. });
  285. }
  286. }
  287. return Json(new
  288. {
  289. code = int.Parse(confirmTicketDataResponse.responseCode),
  290. message = GetLangFromCode(confirmTicketDataResponse.responseCode),
  291. });
  292. }
  293. catch (Exception ex)
  294. {
  295. log.Error(ex);
  296. }
  297. return Json(new
  298. {
  299. code = Code.FAILURE,
  300. message = Lang.error_happened
  301. });
  302. }
  303. [ValidateAntiForgeryToken]
  304. public JsonResult PaymentConfirmOTP_Action(string otp)
  305. {
  306. try
  307. {
  308. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  309. String token = HttpContext.Session.GetComplexData<String>("token");
  310. ConfirmTicketDataResponse confirmTicketDataResponse = HttpContext.Session.GetComplexData<ConfirmTicketDataResponse>("confirmTicketDataResponse");
  311. ConfirmOTPRequest confirmOTPRequest = new ConfirmOTPRequest();
  312. confirmOTPRequest.otp = otp;
  313. confirmOTPRequest.token = token;
  314. confirmOTPRequest.msisdn = msisdn;
  315. ConfirmOTPResponse confirmOTPResponse = api.ConfirmOTPApi(configuration, confirmOTPRequest);
  316. if (confirmOTPResponse.responseCode == Code.SUCCESS)
  317. {
  318. // confirm ticket
  319. ConfirmBuyingTicketRequest confirmBuyingTicketRequest = new ConfirmBuyingTicketRequest();
  320. confirmBuyingTicketRequest.requestId = confirmTicketDataResponse.requestId;
  321. confirmBuyingTicketRequest.transIdByTicket = confirmTicketDataResponse.transId;
  322. confirmBuyingTicketRequest.token = token;
  323. ConfirmBuyingTicketResponse confirmBuyingTicketResponse = api.ConfirmBuyingTicketApi(configuration, confirmBuyingTicketRequest);
  324. HttpContext.Session.SetComplexData("confirmBuyingTicketResponse", confirmBuyingTicketResponse);
  325. return Json(new
  326. {
  327. code = int.Parse(confirmBuyingTicketResponse.responseCode),
  328. message = confirmBuyingTicketResponse.responseMessage,
  329. });
  330. }
  331. return Json(new
  332. {
  333. code = int.Parse(confirmOTPResponse.responseCode),
  334. message = GetLangFromCode(confirmOTPResponse.responseCode),
  335. });
  336. }
  337. catch (Exception ex)
  338. {
  339. log.Error(ex);
  340. }
  341. return Json(new
  342. {
  343. code = Code.FAILURE,
  344. message = Lang.error_happened
  345. });
  346. }
  347. [AutoValidateAntiforgeryToken]
  348. public JsonResult PaymentResentOTP_Action()
  349. {
  350. try
  351. {
  352. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  353. String token = HttpContext.Session.GetComplexData<String>("token");
  354. // send OTP
  355. SendOTPRequest sendOTPRequest = new SendOTPRequest();
  356. sendOTPRequest.msisdn = msisdn;
  357. sendOTPRequest.token = token;
  358. SendOTPResponse sendOTPResponse = api.SendOTPApi(configuration, sendOTPRequest);
  359. return Json(new
  360. {
  361. code = int.Parse(sendOTPResponse.responseCode),
  362. message = GetLangFromCode(sendOTPResponse.responseCode),
  363. });
  364. }
  365. catch (Exception ex)
  366. {
  367. log.Error(ex);
  368. }
  369. return Json(new
  370. {
  371. code = Code.FAILURE,
  372. message = Lang.error_happened
  373. });
  374. }
  375. public IActionResult BackToApp()
  376. {
  377. return View();
  378. }
  379. }
  380. }