BuyTicketController.cs 18 KB

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