using LotteryWebApp.Common; using LotteryWebApp.Components; using LotteryWebApp.Extensions; using LotteryWebApp.Languages; using LotteryWebApp.Models; using LotteryWebApp.Service; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Globalization; using System.Net.Sockets; using static LotteryWebApp.Common.Constants; using static Microsoft.AspNetCore.Razor.Language.TagHelperMetadata; namespace LotteryWebApp.Controllers { [AutoValidateAntiforgeryToken] public class BuyTicketController : BaseController { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); IConfiguration configuration; private readonly IWebHostEnvironment webHostEnvironment; APIFunctions api = new APIFunctions(); public BuyTicketController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) { configuration = _configuration; webHostEnvironment = hostEnvironment; } public String GetParameter(String key) { return configuration.GetSection(key).Value; } public IActionResult Index(string termType) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } HttpContext.Session.SetComplexData("navigator", Constants.BUY_TICKET_NAVIGATOR); BuyTicket_ViewModel model = new BuyTicket_ViewModel(); model.termType = termType != null ? termType : Constants.GameId.Direct4D; HttpContext.Session.SetComplexData("termType", model.termType); return View(model); } [ValidateAntiForgeryToken] public IActionResult LastTermResult(string termType, String seqPage) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } LastTermResult_ViewModel model = new LastTermResult_ViewModel(); model.listTerm = new List(); try { String msisdn = HttpContext.Session.GetComplexData("msisdn"); String token = HttpContext.Session.GetComplexData("token"); string fromDate = DateTime.Now.ToString("dd/MM/yyyy"); string toDate = DateTime.Now.AddDays(7).ToString("dd/MM/yyyy"); if (seqPage == "1") { HttpContext.Session.DeleteComplexData("listTerms"); } // get result ResultOfTermRequest resultOfTermRequest = new ResultOfTermRequest(); resultOfTermRequest.gameId = termType; resultOfTermRequest.token = token; resultOfTermRequest.type = Constants.TERM_HAS_NOT_RESULT_TYPE; resultOfTermRequest.order = Constants.DECS; resultOfTermRequest.fromDate = fromDate; resultOfTermRequest.toDate = toDate; resultOfTermRequest.rowsOnPage = Constants.ROW_ON_PAGE; resultOfTermRequest.seqPage = seqPage; resultOfTermRequest.id = Constants.ALL_DATA; ResultOfTermResponse result = api.GetResultOfTermApi(configuration, resultOfTermRequest); if (result.responseCode == Code.SESSION_EXPIRED) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } else if (result.responseCode == Code.SUCCESS) { //if (result.listTerm != null) model.listTermNotDrawn = result.listTerm; model.termType = termType; } //resultOfTermRequest.type = Constants.TERM_HAS_RESULT_TYPE; //result = api.GetResultOfTermApi(configuration, resultOfTermRequest); //if (result.responseCode == Code.SESSION_EXPIRED) //{ // return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); //} //else if (result.responseCode == Code.SUCCESS) //{ // if (result.listTerm != null) // { // model.listTerm = result.listTerm; // //if (termType == Constants.BOULCHANS_CODE) // //{ // // List listTerms = HttpContext.Session.GetComplexData>("listTerms"); // // if (listTerms != null && listTerms.Count > 0) // // { // // listTerms.AddRange(result.listTerm); // // } // // else // // { // // listTerms = result.listTerm; // // } // // HttpContext.Session.SetComplexData("listTerms", listTerms); // //} // } //} } catch (Exception ex) { log.Error(ex); } return PartialView("LastTermResult", model); } public IActionResult Choose(string ticketType, string termType) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } BuyTicketChoose_ViewModel model = new BuyTicketChoose_ViewModel(); if (ticketType == null) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Home"); } if (termType != null) { HttpContext.Session.SetComplexData("termType", termType); } model.ticketType = ticketType; return View(model); } [ValidateAntiForgeryToken] public IActionResult AddTicket_Action(string ticketType) { ChooseFormModel model = new ChooseFormModel(); model.tics = DateTime.Now.Ticks; model.ticketType = ticketType; return ViewComponent("ChooseForm", model); } [ValidateAntiForgeryToken] public JsonResult ConfirmTicket_Action(string ticketType, string tickets) { try { // check tickets int check = 1; string[] ticketsList = tickets.Split(','); for (int i = 0; i < ticketsList.Length; i++) { if (ticketType == Constants.TicketType._2D && ticketsList[i].Length != 2) { check *= 0; } if (ticketType == Constants.TicketType._3D && ticketsList[i].Length != 3) { check *= 0; } if (ticketType == Constants.TicketType._4D && ticketsList[i].Length != 4) { check *= 0; } } if (check == 1) { HttpContext.Session.SetComplexData("ticketType", ticketType); HttpContext.Session.SetComplexData("tickets", tickets); return Json(new { code = Code.SUCCESS, }); } } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.ticket_invalid }); } public IActionResult Payment(string step) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } BuyTicketPayment_ViewModel model = new BuyTicketPayment_ViewModel(); Profile profile = HttpContext.Session.GetComplexData("profile"); String tickets = HttpContext.Session.GetComplexData("tickets"); String ticketType = HttpContext.Session.GetComplexData("ticketType"); String termType = HttpContext.Session.GetComplexData("termType"); ConfirmTicketDataResponse confirmTicketDataResponse = HttpContext.Session.GetComplexData("confirmTicketDataResponse"); model.tickets = tickets; model.step = step != null ? step : Constants.PAYMENT_SHOW; model.ticketType = ticketType; model.confirmTicketDataResponse = confirmTicketDataResponse; model.profile = profile; model.termType = termType; if (step == Constants.PAYMENT_RESULT) { ConfirmBuyingTicketResponse confirmBuyingTicketResponse = HttpContext.Session.GetComplexData("confirmBuyingTicketResponse"); model.confirmBuyingTicketResponse = confirmBuyingTicketResponse; } return View(model); } [ValidateAntiForgeryToken] public JsonResult PaymentSendOTP_Action(string ticketMoney) { try { String msisdn = HttpContext.Session.GetComplexData("msisdn"); String token = HttpContext.Session.GetComplexData("token"); String termType = HttpContext.Session.GetComplexData("termType"); String tickets = HttpContext.Session.GetComplexData("tickets"); String ticketType = HttpContext.Session.GetComplexData("ticketType"); string[] ticketMoneyReal = ticketMoney.Split(','); string[] ticketNumberReal = tickets.Split(','); if (ticketMoneyReal.Length != ticketNumberReal.Length) { return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } int totalMoney = 0; List ticketInfos = new List(); for (int i = 0; i < ticketMoneyReal.Length; i++) { TicketInfo ticketInfo = new TicketInfo(); ticketInfo.money = ticketMoneyReal[i]; ticketInfo.code = ticketNumberReal[i]; ticketInfos.Add(ticketInfo); totalMoney += int.Parse(ticketInfo.money); } ConfirmTicketDataRequest confirmTicketDataRequest = new ConfirmTicketDataRequest(); confirmTicketDataRequest.token = token; confirmTicketDataRequest.msisdn = msisdn; confirmTicketDataRequest.ticket = ticketInfos; confirmTicketDataRequest.gameId = ConvertToGameID(termType, ticketType); confirmTicketDataRequest.requestId = RandomString(100, true); ConfirmTicketDataResponse confirmTicketDataResponse = api.ConfirmTicketDataApi(configuration, confirmTicketDataRequest); if (confirmTicketDataResponse.responseCode == Code.SUCCESS) { //string drawnTime = DateTime.ParseExact(confirmTicketDataResponse.termObj.endDate, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture).ToString("HH:mm"); //string content = Lang.confirm_payment_1 + " " + totalMoney + " " + Lang.confirm_payment_2 + " " + ConvertGameIdToName(confirmTicketDataRequest.gameId) // + " " + Lang.confirm_payment_3 + " 0 " + Lang.confirm_payment_4 + " " + // ConvertDrawnTimeFromTerm(termType, confirmTicketDataResponse.termObj) + Lang.confirm_payment_5; //Confirm payment %totalPayment% cent (%totalMoney% cents to buy ticket, fee %totalFee% cents), date time %buyTime%. Please fill OTP: string content = Lang.confirm_payment; content = content.Replace("%totalPayment%", Functions.FormatNumber(confirmTicketDataResponse.totalMoneyPayment)); content = content.Replace("%totalMoney%", Functions.FormatNumber(confirmTicketDataResponse.totalMoney)); content = content.Replace("%totalFee%", Functions.FormatNumber(confirmTicketDataResponse.totalMoneyFee)); content = content.Replace("%buyTime%", DateTime.Now.ToString("HH:mm dd/MM/yyyy")); content = content.Replace("%drawTime%", DateTime.ParseExact(confirmTicketDataResponse.termObj.randomDate, "dd/MM/yyyy HH:mm:ss", null).ToString("HH:mm dd/MM/yyyy")); // HttpContext.Session.SetComplexData("confirmTicketDataResponse", confirmTicketDataResponse); String channel = GetParameter(Constants.CHANNEL); if (channel == ApiConstants.WALLET) { // ma hoa String param = "transactionId=" + confirmTicketDataResponse.transId + "&requestId=" + confirmTicketDataResponse.requestId + "&money=" + totalMoney; String url = CreatePrivateURL(configuration, param, "seconds", "0", "0", GetParameter("rsaPolicy")); // redirect return Json(new { code = "110", param = url, }); } else { // send OTP SendOTPRequest sendOTPRequest = new SendOTPRequest(); sendOTPRequest.msisdn = msisdn; sendOTPRequest.token = token; SendOTPResponse sendOTPResponse = api.SendOTPApi(configuration, sendOTPRequest); return Json(new { code = int.Parse(sendOTPResponse.responseCode), message = sendOTPResponse.responseMessage, content = content, }); } } return Json(new { code = int.Parse(confirmTicketDataResponse.responseCode), message = GetLangFromCode(confirmTicketDataResponse.responseCode), }); } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } [ValidateAntiForgeryToken] public JsonResult PaymentConfirmOTP_Action(string otp) { try { String msisdn = HttpContext.Session.GetComplexData("msisdn"); String token = HttpContext.Session.GetComplexData("token"); ConfirmTicketDataResponse confirmTicketDataResponse = HttpContext.Session.GetComplexData("confirmTicketDataResponse"); ConfirmOTPRequest confirmOTPRequest = new ConfirmOTPRequest(); confirmOTPRequest.otp = otp; confirmOTPRequest.token = token; confirmOTPRequest.msisdn = msisdn; ConfirmOTPResponse confirmOTPResponse = api.ConfirmOTPApi(configuration, confirmOTPRequest); if (confirmOTPResponse.responseCode == Code.SUCCESS) { // confirm ticket ConfirmBuyingTicketRequest confirmBuyingTicketRequest = new ConfirmBuyingTicketRequest(); confirmBuyingTicketRequest.requestId = confirmTicketDataResponse.requestId; confirmBuyingTicketRequest.transIdByTicket = confirmTicketDataResponse.transId; confirmBuyingTicketRequest.token = token; ConfirmBuyingTicketResponse confirmBuyingTicketResponse = api.ConfirmBuyingTicketApi(configuration, confirmBuyingTicketRequest); HttpContext.Session.SetComplexData("confirmBuyingTicketResponse", confirmBuyingTicketResponse); return Json(new { code = int.Parse(confirmBuyingTicketResponse.responseCode), message = confirmBuyingTicketResponse.responseMessage, }); } return Json(new { code = int.Parse(confirmOTPResponse.responseCode), message = GetLangFromCode(confirmOTPResponse.responseCode), }); } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } [AutoValidateAntiforgeryToken] public JsonResult PaymentResentOTP_Action() { try { String msisdn = HttpContext.Session.GetComplexData("msisdn"); String token = HttpContext.Session.GetComplexData("token"); // send OTP SendOTPRequest sendOTPRequest = new SendOTPRequest(); sendOTPRequest.msisdn = msisdn; sendOTPRequest.token = token; SendOTPResponse sendOTPResponse = api.SendOTPApi(configuration, sendOTPRequest); return Json(new { code = int.Parse(sendOTPResponse.responseCode), message = GetLangFromCode(sendOTPResponse.responseCode), }); } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } public IActionResult BackToApp() { return View(); } } }